| lloretgalleg | 1d2ff51 | 2020-08-01 06:05:58 +0000 | [diff] [blame] | 1 | ## |
| 2 | # Copyright 2019 Telefonica Investigacion y Desarrollo, S.A.U. |
| 3 | # This file is part of OSM |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 15 | # implied. |
| 16 | # See the License for the specific language governing permissions and |
| 17 | # limitations under the License. |
| 18 | # |
| 19 | # For those usages not covered by the Apache License, Version 2.0 please |
| 20 | # contact with: nfvlabs@tid.es |
| 21 | ## |
| 22 | |
| 23 | import asyncio |
| 24 | import logging |
| 25 | import yaml |
| 26 | import os |
| 27 | |
| 28 | from osm_ee.vnf.vnf_ee import VnfEE |
| 29 | |
| 30 | |
| 31 | class BaseEE: |
| 32 | |
| 33 | RETURN_STATUS_LIST = ["OK", "PROCESSING", "ERROR"] |
| 34 | CONFIG_FILE = "/app/storage/config.yaml" |
| 35 | SSH_KEY_FILE = "~/.ssh/id_rsa.pub" |
| 36 | HEALTH_CHECK_ACTION = "health-check" |
| 37 | |
| 38 | def __init__(self): |
| 39 | self.logger = logging.getLogger('osm_ee.base') |
| 40 | |
| 41 | # Check if configuration is stored and load it |
| 42 | if os.path.exists(self.CONFIG_FILE): |
| 43 | with open(self.CONFIG_FILE, 'r') as file: |
| 44 | self.config_params = yaml.load(file, Loader=yaml.FullLoader) |
| 45 | self.logger.debug("Load existing config from file: {}".format(self.config_params)) |
| 46 | else: |
| 47 | self.config_params = {} |
| 48 | |
| 49 | self.vnf_ee = VnfEE(self.config_params) |
| 50 | |
| 51 | async def get_ssh_key(self): |
| 52 | self.logger.debug("Obtain ssh key") |
| 53 | filename = os.path.expanduser(self.SSH_KEY_FILE) |
| 54 | with open(filename) as reader: |
| 55 | ssh_key = reader.read() |
| 56 | return ssh_key |
| 57 | |
| 58 | async def run_action(self, id, name, params): |
| 59 | self.logger.debug("Execute action id: {}, name: {}, params: {}".format(id, name, params)) |
| 60 | |
| 61 | try: |
| 62 | # Health-check |
| 63 | if name == self.HEALTH_CHECK_ACTION: |
| 64 | yield "OK", "Health-check ok" |
| 65 | else: |
| 66 | |
| 67 | # Obtain dynamically code to be executed |
| 68 | method = getattr(self.vnf_ee, name) |
| 69 | |
| 70 | # Convert params from yaml format |
| 71 | action_params = yaml.safe_load(params) |
| 72 | |
| 73 | if name == "config": |
| 74 | self.logger.debug("Store config info in file: {}".format(self.CONFIG_FILE)) |
| 75 | self.config_params.update(action_params) |
| 76 | with open(self.CONFIG_FILE, 'w') as file: |
| 77 | config = yaml.dump(self.config_params, file) |
| 78 | |
| 79 | async for return_status, detailed_message in method(id, action_params): |
| 80 | if return_status not in self.RETURN_STATUS_LIST: |
| 81 | yield "ERROR", "Invalid return status" |
| 82 | else: |
| 83 | yield return_status, str(detailed_message) |
| 84 | except AttributeError as e: |
| 85 | error_msg = "Action name: {} not implemented".format(name) |
| 86 | self.logger.error(error_msg) |
| 87 | yield "ERROR", error_msg |
| 88 | except Exception as e: |
| 89 | self.logger.error("Error executing action id, name: {},{}: {}".format(id, name, str(e)), exc_info=True) |
| 90 | yield "ERROR", str(e) |
| 91 | |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | logging.basicConfig(level=logging.DEBUG) |
| 95 | |
| 96 | loop = asyncio.get_event_loop() |
| 97 | try: |
| 98 | ee = BaseEE() |
| 99 | id = "test1" |
| 100 | name = "touch2" |
| 101 | params = {"file_path": "/var/tmp/testfile1.txt"} |
| 102 | action = asyncio.ensure_future(ee.run_action(id, name, params)) |
| 103 | loop.run_until_complete(action) |
| 104 | finally: |
| 105 | loop.close() |