+# -*- coding: utf-8 -*-\r
+##\r
+# Copyright 2019 Atos - CoE Telco NFV Team\r
+# All Rights Reserved.\r
+#\r
+# Contributors: Oscar Luis Peral, Atos\r
+#\r
+# Licensed under the Apache License, Version 2.0 (the "License"); you may\r
+# not use this file except in compliance with the License. You may obtain\r
+# a copy of the License at\r
+#\r
+# http://www.apache.org/licenses/LICENSE-2.0\r
+#\r
+# Unless required by applicable law or agreed to in writing, software\r
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\r
+# License for the specific language governing permissions and limitations\r
+# under the License.\r
+#\r
+# For those usages not covered by the Apache License, Version 2.0 please\r
+# contact with: <oscarluis.peral@atos.net>\r
+#\r
+# Neither the name of Atos nor the names of its\r
+# contributors may be used to endorse or promote products derived from\r
+# this software without specific prior written permission.\r
+#\r
+# This work has been performed in the context of Arista Telefonica OSM PoC.\r
+##\r
+import time\r
+\r
+\r
+class AristaCVPTask:\r
+ def __init__(self, cvpClientApi):\r
+ self.cvpClientApi = cvpClientApi\r
+\r
+ def __get_id(self, task):\r
+ return task.get("workOrderId")\r
+\r
+ def __get_state(self, task):\r
+ return task.get("workOrderUserDefinedStatus")\r
+\r
+ def __execute_task(self, task_id):\r
+ return self.cvpClientApi.execute_task(task_id)\r
+\r
+ def __cancel_task(self, task_id):\r
+ return self.cvpClientApi.cancel_task(task_id)\r
+\r
+ def __apply_state(self, task, state):\r
+ t_id = self.__get_id(task)\r
+ self.cvpClientApi.add_note_to_task(t_id, "Executed by OSM")\r
+ if state == "executed":\r
+ return self.__execute_task(t_id)\r
+ elif state == "cancelled":\r
+ return self.__cancel_task(t_id)\r
+\r
+ def __actionable(self, state):\r
+ return state in ["Pending"]\r
+\r
+ def __terminal(self, state):\r
+ return state in ["Completed", "Cancelled"]\r
+\r
+ def __state_is_different(self, task, target):\r
+ return self.__get_state(task) != target\r
+\r
+ def update_all_tasks(self, data):\r
+ new_data = dict()\r
+ for task_id in data.keys():\r
+ res = self.cvpClientApi.get_task_by_id(task_id)\r
+ new_data[task_id] = res\r
+ return new_data\r
+\r
+ def get_pending_tasks(self):\r
+ return self.cvpClientApi.get_tasks_by_status('Pending')\r
+\r
+ def get_pending_tasks_old(self):\r
+ taskList = []\r
+ tasksField = {'workOrderId': 'workOrderId',\r
+ 'workOrderState': 'workOrderState',\r
+ 'currentTaskName': 'currentTaskName',\r
+ 'description': 'description',\r
+ 'workOrderUserDefinedStatus':\r
+ 'workOrderUserDefinedStatus',\r
+ 'note': 'note',\r
+ 'taskStatus': 'taskStatus',\r
+ 'workOrderDetails': 'workOrderDetails'}\r
+ tasks = self.cvpClientApi.get_tasks_by_status('Pending')\r
+ # Reduce task data to required fields\r
+ for task in tasks:\r
+ taskFacts = {}\r
+ for field in task.keys():\r
+ if field in tasksField:\r
+ taskFacts[tasksField[field]] = task[field]\r
+ taskList.append(taskFacts)\r
+ return taskList\r
+\r
+ def task_action(self, tasks, wait, state):\r
+ changed = False\r
+ data = dict()\r
+ warnings = list()\r
+\r
+ at = [t for t in tasks if self.__actionable(self.__get_state(t))]\r
+ actionable_tasks = at\r
+\r
+ if len(actionable_tasks) == 0:\r
+ warnings.append("No actionable tasks found on CVP")\r
+ return changed, data, warnings\r
+\r
+ for task in actionable_tasks:\r
+ if self.__state_is_different(task, state):\r
+ self.__apply_state(task, state)\r
+ changed = True\r
+ data[self.__get_id(task)] = task\r
+\r
+ if wait == 0:\r
+ return changed, data, warnings\r
+\r
+ start = time.time()\r
+ now = time.time()\r
+ while (now - start) < wait:\r
+ data = self.update_all_tasks(data)\r
+ if all([self.__terminal(self.__get_state(t)) for t in data.values()]):\r
+ break\r
+ time.sleep(1)\r
+ now = time.time()\r
+\r
+ if wait:\r
+ for i, task in data.items():\r
+ if not self.__terminal(self.__get_state(task)):\r
+ warnings.append("Task {} has not completed in {} seconds".\r
+ format(i, wait))\r
+\r
+ return changed, data, warnings\r