Fix/add use cases under SMO package
[it/dep.git] / smo-install / test / pythonsdk / src / orantests / network_slicing / preparation / so_preparation.py
1 #!/usr/bin/env python3
2 ###
3 # ============LICENSE_START===================================================
4 # ORAN SMO PACKAGE - PYTHONSDK TESTS
5 # ================================================================================
6 #  Copyright (C) 2022 AT&T Intellectual Property. All rights
7 #                             reserved.
8 # ============================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #      http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 #
21 # SPDX-License-Identifier: Apache-2.0
22 # ============LICENSE_END=====================================================
23 #
24 ###
25 """Prepare SO for Network Slicing option2 test."""
26 import logging
27 import logging.config
28 import os
29 import subprocess
30 from subprocess import check_output
31 from onapsdk.configuration import settings
32
33 # Set working dir as python script location
34 abspath = os.path.abspath(__file__)
35 dname = os.path.dirname(abspath)
36 os.chdir(dname)
37
38 logging.config.dictConfig(settings.LOG_CONFIG)
39 logger = logging.getLogger("####################### Start SO Preparation")
40
41 class SoPreparation():
42     """Can be used to prepare SO for Network Slicing usecase option2."""
43
44     @classmethod
45     def prepare_so(cls, cst_id, sp_id):
46         """Update So catalog db.
47
48         Args:
49             cst_id (str): The CST uuid of from the SDC Template creation step.
50             sp_id (str): The ServiceProfile uuid from the SDC Template creation step.
51         """
52         logger.info("####################### Start to update SO catalog DB")
53         cmd = "kubectl get secret/onap-mariadb-galera-db-root-password -n onap -o jsonpath={.data.password} | base64 --decode"
54         pw = check_output(cmd, shell=True).decode('utf-8')
55         #logger.info("####################### pass is:%s", pw)
56
57         # populate communication service actions
58         sql = f"INSERT INTO  \
59               catalogdb.service_recipe(ACTION, VERSION_STR, DESCRIPTION, ORCHESTRATION_URI, SERVICE_PARAM_XSD, \
60               RECIPE_TIMEOUT, SERVICE_TIMEOUT_INTERIM, SERVICE_MODEL_UUID) \
61               VALUES ('createInstance', '1', 'Custom recipe to create communication service-instance if no custom BPMN flow is found', \
62               '/mso/async/services/CreateCommunicationService', NULL, 180, NULL, '{cst_id}');"
63         cmd = f"kubectl -n onap exec onap-mariadb-galera-0 -- mysql -uroot -p{pw} -D catalogdb -e \"{sql}\""
64         check_output(cmd, shell=True).decode('utf-8')
65
66         sql = f"INSERT INTO  \
67               catalogdb.service_recipe(ACTION, VERSION_STR, DESCRIPTION, ORCHESTRATION_URI, SERVICE_PARAM_XSD, \
68               RECIPE_TIMEOUT, SERVICE_TIMEOUT_INTERIM, SERVICE_MODEL_UUID) \
69               VALUES ('deleteInstance', '1', 'Custom recipe to delete communication service if no custom BPMN flow is found', \
70               '/mso/async/services/DeleteCommunicationService', NULL, 180, NULL, '{cst_id}');"
71         cmd = f"kubectl -n onap exec onap-mariadb-galera-0 -- mysql -uroot -p{pw} -D catalogdb -e \"{sql}\""
72         check_output(cmd, shell=True).decode('utf-8')
73
74         sql = f"INSERT INTO  \
75               catalogdb.service_recipe(ACTION, VERSION_STR, DESCRIPTION, ORCHESTRATION_URI, SERVICE_PARAM_XSD, \
76               RECIPE_TIMEOUT, SERVICE_TIMEOUT_INTERIM, SERVICE_MODEL_UUID) \
77               VALUES ('activateInstance', '1.0', 'activate communication service', '/mso/async/services/ActivateCommunicationService', \
78               NULL, 180, NULL, '{cst_id}');"
79         cmd = f"kubectl -n onap exec onap-mariadb-galera-0 -- mysql -uroot -p{pw} -D catalogdb -e \"{sql}\""
80         check_output(cmd, shell=True).decode('utf-8')
81
82         # populate slice service actions
83         sql = f"INSERT INTO  \
84               catalogdb.service_recipe(ACTION, VERSION_STR, DESCRIPTION, ORCHESTRATION_URI, SERVICE_PARAM_XSD, \
85               RECIPE_TIMEOUT, SERVICE_TIMEOUT_INTERIM, SERVICE_MODEL_UUID) \
86               VALUES ('createInstance', '1', 'Custom recipe to create slice service-instance if no custom BPMN flow is found', \
87               '/mso/async/services/CreateSliceService', NULL, 180, NULL, '{sp_id}');"
88         cmd = f"kubectl -n onap exec onap-mariadb-galera-0 -- mysql -uroot -p{pw} -D catalogdb -e \"{sql}\""
89         check_output(cmd, shell=True).decode('utf-8')
90
91         sql = f"INSERT INTO  \
92               catalogdb.service_recipe(ACTION, VERSION_STR, DESCRIPTION, ORCHESTRATION_URI, SERVICE_PARAM_XSD, \
93               RECIPE_TIMEOUT, SERVICE_TIMEOUT_INTERIM, SERVICE_MODEL_UUID) \
94               VALUES ('deleteInstance', '1', 'Custom recipe to create slice service-instance if no custom BPMN flow is found', \
95               '/mso/async/services/DeleteSliceService', NULL, 180, NULL, '{sp_id}');"
96         cmd = f"kubectl -n onap exec onap-mariadb-galera-0 -- mysql -uroot -p{pw} -D catalogdb -e \"{sql}\""
97         check_output(cmd, shell=True).decode('utf-8')
98
99         sql = f"INSERT INTO  \
100               catalogdb.service_recipe(ACTION, VERSION_STR, DESCRIPTION, ORCHESTRATION_URI, SERVICE_PARAM_XSD, \
101               RECIPE_TIMEOUT, SERVICE_TIMEOUT_INTERIM, SERVICE_MODEL_UUID) \
102               VALUES ('activateInstance', '1.0', 'Gr api recipe to activate service-instance', \
103               '/mso/async/services/ActivateSliceService', NULL, 180, NULL, '{sp_id}');"
104         cmd = f"kubectl -n onap exec onap-mariadb-galera-0 -- mysql -uroot -p{pw} -D catalogdb -e \"{sql}\""
105         check_output(cmd, shell=True).decode('utf-8')
106
107         logger.info("####################### Start to copy subnetCapability.json to SO main pod")
108         so_pod = subprocess.run("kubectl get pods -n onap | awk '{print $1}' | grep  onap-so-[a-z0-9]*-[a-z0-9]*$", shell=True, check=True, stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
109         cmd = f"kubectl cp ../resources/subnetCapability.json -n onap {so_pod}:/app"
110         check_output(cmd, shell=True).decode('utf-8')
111
112     @classmethod
113     def cleanup_so(cls, cst_id, sp_id):
114         """Clean up So configuration.
115
116         Args:
117             cst_id (str): The CST uuid of from the SDC Template creation step.
118             sp_id (str): The ServiceProfile uuid from the SDC Template creation step.
119         """
120         logger.info("####################### Start to clean up SO catalog DB")
121         cmd = "kubectl get secret/onap-mariadb-galera-db-root-password -n onap -o jsonpath={.data.password} | base64 --decode"
122         pw = check_output(cmd, shell=True).decode('utf-8')
123
124         # remove communication service actions
125         sql = f"Delete from  service_recipe where SERVICE_MODEL_UUID=\"{cst_id}\";"
126         cmd = f"kubectl -n onap exec onap-mariadb-galera-0 -- mysql -uroot -p{pw} -D catalogdb -e \"{sql}\""
127         check_output(cmd, shell=True).decode('utf-8')
128
129         # remove slice service actions
130         sql = f"Delete from  service_recipe where SERVICE_MODEL_UUID=\"{sp_id}\";"
131         cmd = f"kubectl -n onap exec onap-mariadb-galera-0 -- mysql -uroot -p{pw} -D catalogdb -e \"{sql}\""
132         check_output(cmd, shell=True).decode('utf-8')
133
134         logger.info("####################### Start to remove subnetCapability.json to SO main pod")
135         so_pod = subprocess.run("kubectl get pods -n onap | awk '{print $1}' | grep  onap-so-[a-z0-9]*-[a-z0-9]*$", shell=True, check=True, stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
136         cmd = f"kubectl -n onap exec {so_pod} -- rm -f /app/subnetCapability.json -n onap"
137         check_output(cmd, shell=True).decode('utf-8')