blob: c7b1bed130317eb918f9405d056a32ef7b187a61 [file] [log] [blame]
lloretgalleg1d2ff512020-08-01 06:05:58 +00001##
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
23import asyncio
24import logging
25import yaml
26import os
27
28from osm_ee.vnf.vnf_ee import VnfEE
29
30
31class 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):
garciadeblas43fc9352024-07-09 14:30:44 +020039 self.logger = logging.getLogger("osm_ee.base")
lloretgalleg1d2ff512020-08-01 06:05:58 +000040
garciadeblas43fc9352024-07-09 14:30:44 +020041 self.config_params = {}
lloretgalleg1d2ff512020-08-01 06:05:58 +000042 # Check if configuration is stored and load it
43 if os.path.exists(self.CONFIG_FILE):
garciadeblas43fc9352024-07-09 14:30:44 +020044 with open(self.CONFIG_FILE, "r") as file:
lloretgalleg1d2ff512020-08-01 06:05:58 +000045 self.config_params = yaml.load(file, Loader=yaml.FullLoader)
garciadeblas43fc9352024-07-09 14:30:44 +020046 self.logger.debug(
47 "Load existing config from file: {}".format(self.config_params)
48 )
lloretgalleg1d2ff512020-08-01 06:05:58 +000049
50 self.vnf_ee = VnfEE(self.config_params)
51
52 async def get_ssh_key(self):
53 self.logger.debug("Obtain ssh key")
54 filename = os.path.expanduser(self.SSH_KEY_FILE)
55 with open(filename) as reader:
56 ssh_key = reader.read()
57 return ssh_key
58
59 async def run_action(self, id, name, params):
garciadeblas43fc9352024-07-09 14:30:44 +020060 self.logger.debug(
61 "Execute action id: {}, name: {}, params: {}".format(id, name, params)
62 )
lloretgalleg1d2ff512020-08-01 06:05:58 +000063
64 try:
65 # Health-check
66 if name == self.HEALTH_CHECK_ACTION:
67 yield "OK", "Health-check ok"
68 else:
69
70 # Obtain dynamically code to be executed
71 method = getattr(self.vnf_ee, name)
72
73 # Convert params from yaml format
74 action_params = yaml.safe_load(params)
75
76 if name == "config":
garciadeblas43fc9352024-07-09 14:30:44 +020077 self.logger.debug(
78 "Store config info in file: {}".format(self.CONFIG_FILE)
79 )
lloretgalleg1d2ff512020-08-01 06:05:58 +000080 self.config_params.update(action_params)
garciadeblas43fc9352024-07-09 14:30:44 +020081 with open(self.CONFIG_FILE, "w") as file:
lloretgalleg1d2ff512020-08-01 06:05:58 +000082 config = yaml.dump(self.config_params, file)
garciadeblas43fc9352024-07-09 14:30:44 +020083 self.logger.debug("Config info: {}".format(config))
lloretgalleg1d2ff512020-08-01 06:05:58 +000084
85 async for return_status, detailed_message in method(id, action_params):
86 if return_status not in self.RETURN_STATUS_LIST:
87 yield "ERROR", "Invalid return status"
88 else:
89 yield return_status, str(detailed_message)
90 except AttributeError as e:
garciadeblas43fc9352024-07-09 14:30:44 +020091 error_msg = "Action name: {} not implemented. Exception: {}".format(
92 name, str(e)
93 )
lloretgalleg1d2ff512020-08-01 06:05:58 +000094 self.logger.error(error_msg)
95 yield "ERROR", error_msg
96 except Exception as e:
garciadeblas43fc9352024-07-09 14:30:44 +020097 self.logger.error(
98 "Error executing action id, name: {},{}: {}".format(id, name, str(e)),
99 exc_info=True,
100 )
lloretgalleg1d2ff512020-08-01 06:05:58 +0000101 yield "ERROR", str(e)
102
103
garciadeblas43fc9352024-07-09 14:30:44 +0200104if __name__ == "__main__":
lloretgalleg1d2ff512020-08-01 06:05:58 +0000105 logging.basicConfig(level=logging.DEBUG)
106
107 loop = asyncio.get_event_loop()
108 try:
109 ee = BaseEE()
110 id = "test1"
111 name = "touch2"
112 params = {"file_path": "/var/tmp/testfile1.txt"}
113 action = asyncio.ensure_future(ee.run_action(id, name, params))
114 loop.run_until_complete(action)
115 finally:
116 loop.close()