c97b3510ccbba37d7ef8c8a354b2cf6ded704418
[demo.git] / heat / vFW_CNF_CDS / automation / onboard.py
1 # ============LICENSE_START=======================================================
2 # Copyright (C) 2020 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 from onapsdk.sdc.properties import Property
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, properties=[
71     Property(name="sdnc_model_name", property_type="string", value=SDNC_MODEL_NAME),
72     Property(name="sdnc_model_version", property_type="string", value=SDNC_MODEL_VERSION),
73     Property(name="sdnc_artifact_name", property_type="string", value=Config.SDNC_ARTIFACT_NAME)
74 ]
75         )
76 vf.vsp = vsp
77 vf.onboard()
78
79 logger.info("******** Onboard Service *******")
80 svc = Service(name=Config.SERVICENAME, resources=[vf], instantiation_type=ServiceInstantiationType.MACRO)
81 svc.onboard()
82
83 logger.info("******** Check Service Distribution *******")
84 distribution_completed = False
85 nb_try = 0
86 nb_try_max = 10
87 while distribution_completed is False and nb_try < nb_try_max:
88     distribution_completed = svc.distributed
89     if distribution_completed is True:
90         logger.info("Service Distribution for %s is successfully finished", svc.name)
91         break
92     logger.info("Service Distribution for %s ongoing, Wait for 60 s", svc.name)
93     time.sleep(60)
94     nb_try += 1
95
96 if distribution_completed is False:
97     logger.error("Service Distribution for %s failed !!", svc.name)
98     exit(1)