1 # -*- coding: utf-8 -*-
3 # Copyright 2019 Atos - CoE Telco NFV Team
6 # Contributors: Oscar Luis Peral, Atos
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: <oscarluis.peral@atos.net>
23 # Neither the name of Atos nor the names of its
24 # contributors may be used to endorse or promote products derived from
25 # this software without specific prior written permission.
27 # This work has been performed in the context of Arista Telefonica OSM PoC.
33 def __init__(self
, cvpClientApi
):
34 self
.cvpClientApi
= cvpClientApi
36 def __get_id(self
, task
):
37 return task
.get("workOrderId")
39 def __get_state(self
, task
):
40 return task
.get("workOrderUserDefinedStatus")
42 def __execute_task(self
, task_id
):
43 return self
.cvpClientApi
.execute_task(task_id
)
45 def __cancel_task(self
, task_id
):
46 return self
.cvpClientApi
.cancel_task(task_id
)
48 def __apply_state(self
, task
, state
):
49 t_id
= self
.__get
_id
(task
)
50 self
.cvpClientApi
.add_note_to_task(t_id
, "Executed by OSM")
52 if state
== "executed":
53 return self
.__execute
_task
(t_id
)
54 elif state
== "cancelled":
55 return self
.__cancel
_task
(t_id
)
57 def __actionable(self
, state
):
58 return state
in ["Pending"]
60 def __terminal(self
, state
):
61 return state
in ["Completed", "Cancelled"]
63 def __state_is_different(self
, task
, target
):
64 return self
.__get
_state
(task
) != target
66 def update_all_tasks(self
, data
):
69 for task_id
in data
.keys():
70 res
= self
.cvpClientApi
.get_task_by_id(task_id
)
71 new_data
[task_id
] = res
75 def get_pending_tasks(self
):
76 return self
.cvpClientApi
.get_tasks_by_status("Pending")
78 def get_pending_tasks_old(self
):
81 "workOrderId": "workOrderId",
82 "workOrderState": "workOrderState",
83 "currentTaskName": "currentTaskName",
84 "description": "description",
85 "workOrderUserDefinedStatus": "workOrderUserDefinedStatus",
87 "taskStatus": "taskStatus",
88 "workOrderDetails": "workOrderDetails",
90 tasks
= self
.cvpClientApi
.get_tasks_by_status("Pending")
92 # Reduce task data to required fields
95 for field
in task
.keys():
96 if field
in tasksField
:
97 taskFacts
[tasksField
[field
]] = task
[field
]
99 taskList
.append(taskFacts
)
103 def task_action(self
, tasks
, wait
, state
):
108 at
= [t
for t
in tasks
if self
.__actionable
(self
.__get
_state
(t
))]
109 actionable_tasks
= at
111 if len(actionable_tasks
) == 0:
112 warnings
.append("No actionable tasks found on CVP")
113 return changed
, data
, warnings
115 for task
in actionable_tasks
:
116 if self
.__state
_is
_different
(task
, state
):
117 self
.__apply
_state
(task
, state
)
119 data
[self
.__get
_id
(task
)] = task
122 return changed
, data
, warnings
126 while (now
- start
) < wait
:
127 data
= self
.update_all_tasks(data
)
129 if all([self
.__terminal
(self
.__get
_state
(t
)) for t
in data
.values()]):
136 for i
, task
in data
.items():
137 if not self
.__terminal
(self
.__get
_state
(task
)):
139 "Task {} has not completed in {} seconds".format(i
, wait
)
142 return changed
, data
, warnings