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")
51 if state
== "executed":
52 return self
.__execute
_task
(t_id
)
53 elif state
== "cancelled":
54 return self
.__cancel
_task
(t_id
)
56 def __actionable(self
, state
):
57 return state
in ["Pending"]
59 def __terminal(self
, state
):
60 return state
in ["Completed", "Cancelled"]
62 def __state_is_different(self
, task
, target
):
63 return self
.__get
_state
(task
) != target
65 def update_all_tasks(self
, data
):
67 for task_id
in data
.keys():
68 res
= self
.cvpClientApi
.get_task_by_id(task_id
)
69 new_data
[task_id
] = res
72 def get_pending_tasks(self
):
73 return self
.cvpClientApi
.get_tasks_by_status('Pending')
75 def get_pending_tasks_old(self
):
77 tasksField
= {'workOrderId': 'workOrderId',
78 'workOrderState': 'workOrderState',
79 'currentTaskName': 'currentTaskName',
80 'description': 'description',
81 'workOrderUserDefinedStatus':
82 'workOrderUserDefinedStatus',
84 'taskStatus': 'taskStatus',
85 'workOrderDetails': 'workOrderDetails'}
86 tasks
= self
.cvpClientApi
.get_tasks_by_status('Pending')
87 # Reduce task data to required fields
90 for field
in task
.keys():
91 if field
in tasksField
:
92 taskFacts
[tasksField
[field
]] = task
[field
]
93 taskList
.append(taskFacts
)
96 def task_action(self
, tasks
, wait
, state
):
101 at
= [t
for t
in tasks
if self
.__actionable
(self
.__get
_state
(t
))]
102 actionable_tasks
= at
104 if len(actionable_tasks
) == 0:
105 warnings
.append("No actionable tasks found on CVP")
106 return changed
, data
, warnings
108 for task
in actionable_tasks
:
109 if self
.__state
_is
_different
(task
, state
):
110 self
.__apply
_state
(task
, state
)
112 data
[self
.__get
_id
(task
)] = task
115 return changed
, data
, warnings
119 while (now
- start
) < wait
:
120 data
= self
.update_all_tasks(data
)
121 if all([self
.__terminal
(self
.__get
_state
(t
)) for t
in data
.values()]):
127 for i
, task
in data
.items():
128 if not self
.__terminal
(self
.__get
_state
(task
)):
129 warnings
.append("Task {} has not completed in {} seconds".
132 return changed
, data
, warnings