Updated vimconn_fos fix bug 1089
[osm/RO.git] / RO-SDN-arista / osm_rosdn_arista / 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 if state == "executed":
52 return self.__execute_task(t_id)
53 elif state == "cancelled":
54 return self.__cancel_task(t_id)
55
56 def __actionable(self, state):
57 return state in ["Pending"]
58
59 def __terminal(self, state):
60 return state in ["Completed", "Cancelled"]
61
62 def __state_is_different(self, task, target):
63 return self.__get_state(task) != target
64
65 def update_all_tasks(self, data):
66 new_data = dict()
67 for task_id in data.keys():
68 res = self.cvpClientApi.get_task_by_id(task_id)
69 new_data[task_id] = res
70 return new_data
71
72 def get_pending_tasks(self):
73 return self.cvpClientApi.get_tasks_by_status('Pending')
74
75 def get_pending_tasks_old(self):
76 taskList = []
77 tasksField = {'workOrderId': 'workOrderId',
78 'workOrderState': 'workOrderState',
79 'currentTaskName': 'currentTaskName',
80 'description': 'description',
81 'workOrderUserDefinedStatus':
82 'workOrderUserDefinedStatus',
83 'note': 'note',
84 'taskStatus': 'taskStatus',
85 'workOrderDetails': 'workOrderDetails'}
86 tasks = self.cvpClientApi.get_tasks_by_status('Pending')
87 # Reduce task data to required fields
88 for task in tasks:
89 taskFacts = {}
90 for field in task.keys():
91 if field in tasksField:
92 taskFacts[tasksField[field]] = task[field]
93 taskList.append(taskFacts)
94 return taskList
95
96 def task_action(self, tasks, wait, state):
97 changed = False
98 data = dict()
99 warnings = list()
100
101 at = [t for t in tasks if self.__actionable(self.__get_state(t))]
102 actionable_tasks = at
103
104 if len(actionable_tasks) == 0:
105 warnings.append("No actionable tasks found on CVP")
106 return changed, data, warnings
107
108 for task in actionable_tasks:
109 if self.__state_is_different(task, state):
110 self.__apply_state(task, state)
111 changed = True
112 data[self.__get_id(task)] = task
113
114 if wait == 0:
115 return changed, data, warnings
116
117 start = time.time()
118 now = time.time()
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()]):
122 break
123 time.sleep(1)
124 now = time.time()
125
126 if wait:
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".
130 format(i, wait))
131
132 return changed, data, warnings