Pin pylint version to 3.1.1 in tox.ini
[osm/LCM.git] / osm_lcm / ng_ro.py
index edbaf88..9426488 100644 (file)
@@ -67,8 +67,7 @@ class NgRoClient:
     timeout_large = 120
     timeout_short = 30
 
-    def __init__(self, loop, uri, **kwargs):
-        self.loop = loop
+    def __init__(self, uri, **kwargs):
         self.endpoint_url = uri
         if not self.endpoint_url.endswith("/"):
             self.endpoint_url += "/"
@@ -103,7 +102,7 @@ class NgRoClient:
             payload_req = yaml.safe_dump(target)
 
             url = "{}/ns/v1/deploy/{nsr_id}".format(self.endpoint_url, nsr_id=nsr_id)
-            async with aiohttp.ClientSession(loop=self.loop) as session:
+            async with aiohttp.ClientSession() as session:
                 self.logger.debug("NG-RO POST %s %s", url, payload_req)
                 # timeout = aiohttp.ClientTimeout(total=self.timeout_large)
                 async with session.post(
@@ -136,7 +135,43 @@ class NgRoClient:
             payload_req = yaml.safe_dump(target)
 
             url = "{}/ns/v1/migrate/{nsr_id}".format(self.endpoint_url, nsr_id=nsr_id)
-            async with aiohttp.ClientSession(loop=self.loop) as session:
+            async with aiohttp.ClientSession() as session:
+                self.logger.debug("NG-RO POST %s %s", url, payload_req)
+                # timeout = aiohttp.ClientTimeout(total=self.timeout_large)
+                async with session.post(
+                    url, headers=self.headers_req, data=payload_req
+                ) as response:
+                    response_text = await response.read()
+                    self.logger.debug(
+                        "POST {} [{}] {}".format(
+                            url, response.status, response_text[:100]
+                        )
+                    )
+                    if response.status >= 300:
+                        raise NgRoException(response_text, http_code=response.status)
+                    return self._parse_yaml(response_text, response=True)
+        except (aiohttp.ClientOSError, aiohttp.ClientError) as e:
+            raise NgRoException(e, http_code=504)
+        except asyncio.TimeoutError:
+            raise NgRoException("Timeout", http_code=504)
+
+    async def operate(self, nsr_id, target, operation_type):
+        """
+        Performs start/stop/rebuil of VNFs
+        :param nsr_id: NS Instance Id
+        :param target: payload data for migrate operation
+        :param operation_type: start/stop/rebuil of VNFs
+        :return: dictionary with the information or raises NgRoException on Error
+        """
+        try:
+            if isinstance(target, str):
+                target = self._parse_yaml(target)
+            payload_req = yaml.safe_dump(target)
+
+            url = "{}/ns/v1/{operation_type}/{nsr_id}".format(
+                self.endpoint_url, operation_type=operation_type, nsr_id=nsr_id
+            )
+            async with aiohttp.ClientSession() as session:
                 self.logger.debug("NG-RO POST %s %s", url, payload_req)
                 # timeout = aiohttp.ClientTimeout(total=self.timeout_large)
                 async with session.post(
@@ -161,7 +196,7 @@ class NgRoClient:
             url = "{}/ns/v1/deploy/{nsr_id}/{action_id}".format(
                 self.endpoint_url, nsr_id=nsr_id, action_id=action_id
             )
-            async with aiohttp.ClientSession(loop=self.loop) as session:
+            async with aiohttp.ClientSession() as session:
                 self.logger.debug("GET %s", url)
                 # timeout = aiohttp.ClientTimeout(total=self.timeout_short)
                 async with session.get(url, headers=self.headers_req) as response:
@@ -183,7 +218,7 @@ class NgRoClient:
     async def delete(self, nsr_id):
         try:
             url = "{}/ns/v1/deploy/{nsr_id}".format(self.endpoint_url, nsr_id=nsr_id)
-            async with aiohttp.ClientSession(loop=self.loop) as session:
+            async with aiohttp.ClientSession() as session:
                 self.logger.debug("DELETE %s", url)
                 # timeout = aiohttp.ClientTimeout(total=self.timeout_short)
                 async with session.delete(url, headers=self.headers_req) as response:
@@ -206,7 +241,7 @@ class NgRoClient:
         """
         try:
             response_text = ""
-            async with aiohttp.ClientSession(loop=self.loop) as session:
+            async with aiohttp.ClientSession() as session:
                 url = "{}/version".format(self.endpoint_url)
                 self.logger.debug("RO GET %s", url)
                 # timeout = aiohttp.ClientTimeout(total=self.timeout_short)
@@ -240,6 +275,99 @@ class NgRoClient:
                 http_code=500,
             )
 
+    async def recreate(self, nsr_id, target):
+        """
+        Performs an action over an item
+        :param item: can be 'tenant', 'vnfd', 'nsd', 'ns', 'vim', 'vim_account', 'sdn'
+        :param item_id_name: RO id or name of the item. Raise and exception if more than one found
+        :param descriptor: can be a dict, or a yaml/json text. Autodetect unless descriptor_format is provided
+        :param descriptor_format: Can be 'json' or 'yaml'
+        :param kwargs: Overrides descriptor with values as name, description, vim_url, vim_url_admin, vim_type
+               keys can be a dot separated list to specify elements inside dict
+        :return: dictionary with the information or raises NgRoException on Error
+        """
+        try:
+            if isinstance(target, str):
+                target = self._parse_yaml(target)
+            payload_req = yaml.safe_dump(target)
+
+            url = "{}/ns/v1/recreate/{nsr_id}".format(self.endpoint_url, nsr_id=nsr_id)
+            async with aiohttp.ClientSession() as session:
+                self.logger.debug("NG-RO POST %s %s", url, payload_req)
+                async with session.post(
+                    url, headers=self.headers_req, data=payload_req
+                ) as response:
+                    response_text = await response.read()
+                    self.logger.debug(
+                        "POST {} [{}] {}".format(
+                            url, response.status, response_text[:100]
+                        )
+                    )
+                    if response.status >= 300:
+                        raise NgRoException(response_text, http_code=response.status)
+                    return self._parse_yaml(response_text, response=True)
+        except (aiohttp.ClientOSError, aiohttp.ClientError) as e:
+            raise NgRoException(e, http_code=504)
+        except asyncio.TimeoutError:
+            raise NgRoException("Timeout", http_code=504)
+
+    async def recreate_status(self, nsr_id, action_id):
+        try:
+            url = "{}/ns/v1/recreate/{nsr_id}/{action_id}".format(
+                self.endpoint_url, nsr_id=nsr_id, action_id=action_id
+            )
+            async with aiohttp.ClientSession() as session:
+                self.logger.debug("GET %s", url)
+                async with session.get(url, headers=self.headers_req) as response:
+                    response_text = await response.read()
+                    self.logger.debug(
+                        "GET {} [{}] {}".format(
+                            url, response.status, response_text[:100]
+                        )
+                    )
+                    if response.status >= 300:
+                        raise NgRoException(response_text, http_code=response.status)
+                    return self._parse_yaml(response_text, response=True)
+
+        except (aiohttp.ClientOSError, aiohttp.ClientError) as e:
+            raise NgRoException(e, http_code=504)
+        except asyncio.TimeoutError:
+            raise NgRoException("Timeout", http_code=504)
+
+    async def vertical_scale(self, nsr_id, target):
+        """
+        Performs migration of VNFs
+        :param nsr_id: NS Instance Id
+        :param target: payload data for migrate operation
+        :return: dictionary with the information or raises NgRoException on Error
+        """
+        try:
+            if isinstance(target, str):
+                target = self._parse_yaml(target)
+            payload_req = yaml.safe_dump(target)
+
+            url = "{}/ns/v1/verticalscale/{nsr_id}".format(
+                self.endpoint_url, nsr_id=nsr_id
+            )
+            async with aiohttp.ClientSession() as session:
+                self.logger.debug("NG-RO POST %s %s", url, payload_req)
+                async with session.post(
+                    url, headers=self.headers_req, data=payload_req
+                ) as response:
+                    response_text = await response.read()
+                    self.logger.debug(
+                        "POST {} [{}] {}".format(
+                            url, response.status, response_text[:100]
+                        )
+                    )
+                    if response.status >= 300:
+                        raise NgRoException(response_text, http_code=response.status)
+                    return self._parse_yaml(response_text, response=True)
+        except (aiohttp.ClientOSError, aiohttp.ClientError) as e:
+            raise NgRoException(e, http_code=504)
+        except asyncio.TimeoutError:
+            raise NgRoException("Timeout", http_code=504)
+
     @staticmethod
     def _parse_yaml(descriptor, response=False):
         try: