Add cofig-assign and config-deploy to cds workflow
[demo.git] / heat / vFW_CNF_CDS / automation / onboard.py
1 # ============LICENSE_START=======================================================
2 # Copyright (C) 2021 Orange
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # ============LICENSE_END=========================================================
17
18 import logging
19
20 import time
21 import zipfile
22 from io import BytesIO
23
24 import oyaml as yaml
25
26 from config import Config
27 import onapsdk.constants as const
28
29 from onapsdk.sdc.vendor import Vendor
30 from onapsdk.sdc.vsp import Vsp
31 from onapsdk.sdc.vf import Vf
32 from onapsdk.sdc.service import Service, ServiceInstantiationType
33
34 import os
35
36 logger = logging.getLogger("")
37 logger.setLevel(logging.DEBUG)
38 fh = logging.StreamHandler()
39 fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(lineno)d:%(filename)s(%(process)d) - %(message)s')
40 fh.setFormatter(fh_formatter)
41 logger.addHandler(fh)
42
43 # Read SDNC MODEL NAME and VERSION from CBA.zip
44 logger.info("*******************************")
45 logger.info("Retrieving SDNC MODEL NAME and VERSION")
46 logger.info("*******************************")
47 with zipfile.ZipFile(Config.VSPFILE, 'r') as package:
48     cba_io = BytesIO(package.read("CBA.zip"))
49     with zipfile.ZipFile(cba_io) as cba:
50         with cba.open('TOSCA-Metadata/TOSCA.meta') as meta_file:
51             tosca_meta = yaml.load(meta_file, Loader=yaml.FullLoader)
52             SDNC_MODEL_NAME = tosca_meta.get("Template-Name")
53             SDNC_MODEL_VERSION = tosca_meta.get("Template-Version")
54
55 logger.info("*******************************")
56 logger.info("******** SERVICE DESIGN *******")
57 logger.info("*******************************")
58
59 logger.info("******** Onboard Vendor *******")
60 vendor = Vendor(name=Config.VENDOR)
61 vendor.onboard()
62
63 logger.info("******** Onboard VSP *******")
64 mypath = os.path.dirname(os.path.realpath(__file__))
65 myvspfile = os.path.join(mypath, Config.VSPFILE)
66 vsp = Vsp(name=Config.VSPNAME, vendor=vendor, package=open(myvspfile, 'rb'))
67 vsp.onboard()
68
69 logger.info("******** Onboard VF *******")
70 vf = Vf(name=Config.VFNAME)
71 vf.vsp = vsp
72 vf.create()
73 vf.onboard()
74
75 logger.info("******** Onboard Service *******")
76 svc = Service(name=Config.SERVICENAME,
77               instantiation_type=ServiceInstantiationType.MACRO)
78 svc.create()
79
80 if svc.status == const.DRAFT:
81     svc.add_resource(vf)
82
83     logger.info("******** Set SDNC properties for VF ********")
84     component = svc.get_component(vf)
85     prop = component.get_property("sdnc_model_version")
86     prop.value = SDNC_MODEL_VERSION
87     prop = component.get_property("sdnc_artifact_name")
88     prop.value = Config.SDNC_ARTIFACT_NAME
89     prop = component.get_property("sdnc_model_name")
90     prop.value = SDNC_MODEL_NAME
91     prop = component.get_property("controller_actor")
92     prop.value = "CDS"
93     prop = component.get_property("skip_post_instantiation_configuration")
94     prop.value = Config.SKIP_POST_INSTANTIATION
95
96     logger.info("******** Onboard Service *******")
97     svc.checkin()
98     svc.onboard()
99
100 logger.info("******** Check Service Distribution *******")
101 distribution_completed = False
102 nb_try = 0
103 nb_try_max = 10
104 while distribution_completed is False and nb_try < nb_try_max:
105     distribution_completed = svc.distributed
106     if distribution_completed is True:
107         logger.info("Service Distribution for %s is successfully finished", svc.name)
108         break
109     logger.info("Service Distribution for %s ongoing, Wait for 60 s", svc.name)
110     time.sleep(60)
111     nb_try += 1
112
113 if distribution_completed is False:
114     logger.error("Service Distribution for %s failed !!", svc.name)
115     exit(1)