X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=osm_lcm%2Fng_ro.py;fp=osm_lcm%2Fng_ro.py;h=6e9f6831466dbb87ec6e387a258353811a3fe913;hb=6500339f6d90776325acc9973f0c8243b62b9439;hp=0000000000000000000000000000000000000000;hpb=acc9045429bde37434966e93c0f8f525b1c06cb2;p=osm%2FLCM.git diff --git a/osm_lcm/ng_ro.py b/osm_lcm/ng_ro.py new file mode 100644 index 0000000..6e9f683 --- /dev/null +++ b/osm_lcm/ng_ro.py @@ -0,0 +1,189 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +## +# Copyright 2020 Telefónica Investigación y Desarrollo, S.A.U. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +## + +""" +asyncio RO python client to interact with New Generation RO server +""" + +import asyncio +import aiohttp +import yaml +import logging + +__author__ = "Alfonso Tierno = 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 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) + 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])) + 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 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: + self.logger.debug("DELETE %s", url) + # timeout = aiohttp.ClientTimeout(total=self.timeout_short) + 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) + return + + except (aiohttp.ClientOSError, aiohttp.ClientError) as e: + raise NgRoException(e, http_code=504) + except asyncio.TimeoutError: + raise NgRoException("Timeout", http_code=504) + + async def get_version(self): + """ + Obtain RO server version. + :return: a list with integers ["major", "minor", "release"]. Raises NgRoException on Error, + """ + try: + response_text = "" + async with aiohttp.ClientSession(loop=self.loop) as session: + url = "{}/version".format(self.endpoint_url) + self.logger.debug("RO 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])) + if response.status >= 300: + raise NgRoException(response_text, http_code=response.status) + + for word in str(response_text).split(" "): + if "." in word: + version_text, _, _ = word.partition("-") + return version_text + 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) + + @staticmethod + def _parse_yaml(descriptor, response=False): + try: + return yaml.safe_load(descriptor) + except yaml.YAMLError as exc: + error_pos = "" + if hasattr(exc, 'problem_mark'): + mark = exc.problem_mark + 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) + raise NgRoException(error_text)