Reformatting RO
[osm/RO.git] / RO-SDN-arista_cloudvision / osm_rosdn_arista_cloudvision / aristaTask.py
1 # -*- coding: utf-8 -*-
2 ##
3 # Copyright 2019 Atos - CoE Telco NFV Team
4 # All Rights Reserved.
5 #
6 # Contributors: Oscar Luis Peral, Atos
7 #
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
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
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
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: <oscarluis.peral@atos.net>
22 #
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.
26 #
27 # This work has been performed in the context of Arista Telefonica OSM PoC.
28 ##
29 import time
30
31
32 class AristaCVPTask:
33 def __init__(self, cvpClientApi):
34 self.cvpClientApi = cvpClientApi
35
36 def __get_id(self, task):
37 return task.get("workOrderId")
38
39 def __get_state(self, task):
40 return task.get("workOrderUserDefinedStatus")
41
42 def __execute_task(self, task_id):
43 return self.cvpClientApi.execute_task(task_id)
44
45 def __cancel_task(self, task_id):
46 return self.cvpClientApi.cancel_task(task_id)
47
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
52 if state == "executed":
53 return self.__execute_task(t_id)
54 elif state == "cancelled":
55 return self.__cancel_task(t_id)
56
57 def __actionable(self, state):
58 return state in ["Pending"]
59
60 def __terminal(self, state):
61 return state in ["Completed", "Cancelled"]
62
63 def __state_is_different(self, task, target):
64 return self.__get_state(task) != target
65
66 def update_all_tasks(self, data):
67 new_data = dict()
68
69 for task_id in data.keys():
70 res = self.cvpClientApi.get_task_by_id(task_id)
71 new_data[task_id] = res
72
73 return new_data
74
75 def get_pending_tasks(self):
76 return self.cvpClientApi.get_tasks_by_status("Pending")
77
78 def get_pending_tasks_old(self):
79 taskList = []
80 tasksField = {
81 "workOrderId": "workOrderId",
82 "workOrderState": "workOrderState",
83 "currentTaskName": "currentTaskName",
84 "description": "description",
85 "workOrderUserDefinedStatus": "workOrderUserDefinedStatus",
86 "note": "note",
87 "taskStatus": "taskStatus",
88 "workOrderDetails": "workOrderDetails",
89 }
90 tasks = self.cvpClientApi.get_tasks_by_status("Pending")
91
92 # Reduce task data to required fields
93 for task in tasks:
94 taskFacts = {}
95 for field in task.keys():
96 if field in tasksField:
97 taskFacts[tasksField[field]] = task[field]
98
99 taskList.append(taskFacts)
100
101 return taskList
102
103 def task_action(self, tasks, wait, state):
104 changed = False
105 data = dict()
106 warnings = list()
107
108 at = [t for t in tasks if self.__actionable(self.__get_state(t))]
109 actionable_tasks = at
110
111 if len(actionable_tasks) == 0:
112 warnings.append("No actionable tasks found on CVP")
113 return changed, data, warnings
114
115 for task in actionable_tasks:
116 if self.__state_is_different(task, state):
117 self.__apply_state(task, state)
118 changed = True
119 data[self.__get_id(task)] = task
120
121 if wait == 0:
122 return changed, data, warnings
123
124 start = time.time()
125 now = time.time()
126 while (now - start) < wait:
127 data = self.update_all_tasks(data)
128
129 if all([self.__terminal(self.__get_state(t)) for t in data.values()]):
130 break
131
132 time.sleep(1)
133 now = time.time()
134
135 if wait:
136 for i, task in data.items():
137 if not self.__terminal(self.__get_state(task)):
138 warnings.append(
139 "Task {} has not completed in {} seconds".format(i, wait)
140 )
141
142 return changed, data, warnings