Reformat LCM to standardized format
[osm/LCM.git] / osm_lcm / ng_ro.py
index 6e9f683..b8893e8 100644 (file)
@@ -41,20 +41,28 @@ class NgRoException(Exception):
 
 
 class NgRoClient:
-    headers_req = {'Accept': 'application/yaml', 'content-type': 'application/yaml'}
-    client_to_RO = {'tenant': 'tenants', 'vim': 'datacenters', 'vim_account': 'datacenters', 'sdn': 'sdn_controllers',
-                    'vnfd': 'vnfs', 'nsd': 'scenarios', 'wim': 'wims', 'wim_account': 'wims',
-                    'ns': 'instances'}
+    headers_req = {"Accept": "application/yaml", "content-type": "application/yaml"}
+    client_to_RO = {
+        "tenant": "tenants",
+        "vim": "datacenters",
+        "vim_account": "datacenters",
+        "sdn": "sdn_controllers",
+        "vnfd": "vnfs",
+        "nsd": "scenarios",
+        "wim": "wims",
+        "wim_account": "wims",
+        "ns": "instances",
+    }
     mandatory_for_create = {
-        'tenant': ("name", ),
-        'vnfd': ("name", "id"),
-        'nsd': ("name", "id"),
-        'ns': ("name", "scenario", "datacenter"),
-        'vim': ("name", "vim_url"),
-        'wim': ("name", "wim_url"),
-        'vim_account': (),
-        'wim_account': (),
-        'sdn': ("name", 'type'),
+        "tenant": ("name",),
+        "vnfd": ("name", "id"),
+        "nsd": ("name", "id"),
+        "ns": ("name", "scenario", "datacenter"),
+        "vim": ("name", "vim_url"),
+        "wim": ("name", "wim_url"),
+        "vim_account": (),
+        "wim_account": (),
+        "sdn": ("name", "type"),
     }
     timeout_large = 120
     timeout_short = 30
@@ -73,7 +81,7 @@ class NgRoClient:
         self.tenant = None
         self.datacenter_id_name = kwargs.get("datacenter")
         self.datacenter = None
-        logger_name = kwargs.get('logger_name', 'lcm.ro')
+        logger_name = kwargs.get("logger_name", "lcm.ro")
         self.logger = logging.getLogger(logger_name)
         if kwargs.get("loglevel"):
             self.logger.setLevel(kwargs["loglevel"])
@@ -98,9 +106,15 @@ class NgRoClient:
             async with aiohttp.ClientSession(loop=self.loop) 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:
+                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]))
+                    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)
@@ -111,13 +125,19 @@ class NgRoClient:
 
     async def status(self, nsr_id, action_id):
         try:
-            url = "{}/ns/v1/deploy/{nsr_id}/{action_id}".format(self.endpoint_url, nsr_id=nsr_id, action_id=action_id)
+            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:
                 self.logger.debug("GET %s", url)
                 # timeout = aiohttp.ClientTimeout(total=self.timeout_short)
                 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]))
+                    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)
@@ -136,7 +156,9 @@ class NgRoClient:
                 async with session.delete(url, headers=self.headers_req) as response:
                     self.logger.debug("DELETE {} [{}]".format(url, response.status))
                     if response.status >= 300:
-                        raise NgRoException("Delete {}".format(nsr_id), http_code=response.status)
+                        raise NgRoException(
+                            "Delete {}".format(nsr_id), http_code=response.status
+                        )
                     return
 
         except (aiohttp.ClientOSError, aiohttp.ClientError) as e:
@@ -157,7 +179,11 @@ class NgRoClient:
                 # timeout = aiohttp.ClientTimeout(total=self.timeout_short)
                 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]))
+                    self.logger.debug(
+                        "GET {} [{}] {}".format(
+                            url, response.status, response_text[:100]
+                        )
+                    )
                     if response.status >= 300:
                         raise NgRoException(response_text, http_code=response.status)
 
@@ -165,14 +191,21 @@ class NgRoClient:
                     if "." in word:
                         version_text, _, _ = word.partition("-")
                         return version_text
-                raise NgRoException("Got invalid version text: '{}'".format(response_text), http_code=500)
+                raise NgRoException(
+                    "Got invalid version text: '{}'".format(response_text),
+                    http_code=500,
+                )
         except (aiohttp.ClientOSError, aiohttp.ClientError) as e:
             raise NgRoException(e, http_code=504)
         except asyncio.TimeoutError:
             raise NgRoException("Timeout", http_code=504)
         except Exception as e:
-            raise NgRoException("Got invalid version text: '{}'; causing exception {}".format(response_text, e),
-                                http_code=500)
+            raise NgRoException(
+                "Got invalid version text: '{}'; causing exception {}".format(
+                    response_text, e
+                ),
+                http_code=500,
+            )
 
     @staticmethod
     def _parse_yaml(descriptor, response=False):
@@ -180,9 +213,11 @@ class NgRoClient:
             return yaml.safe_load(descriptor)
         except yaml.YAMLError as exc:
             error_pos = ""
-            if hasattr(exc, 'problem_mark'):
+            if hasattr(exc, "problem_mark"):
                 mark = exc.problem_mark
-                error_pos = " at line:{} column:{}s".format(mark.line + 1, mark.column + 1)
+                error_pos = " at line:{} column:{}s".format(
+                    mark.line + 1, mark.column + 1
+                )
             error_text = "yaml format error" + error_pos
             if response:
                 raise NgRoException("reponse with " + error_text)