[APACHECNF] Update healthcheck and add scale workflow
[demo.git] / tutorials / ApacheCNF / automation / scale.py
1 # ============LICENSE_START=======================================================
2 # Copyright (C) 2022 Deutsche Telekom AG
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 import logging
18 import os
19 import sys
20 import zipfile
21 from io import BytesIO
22
23 from onapsdk.aai.business import Customer
24 from onapsdk.cds.blueprint import Workflow, Blueprint
25 from config import Config, VariablesDict
26
27 #FIXME remove from global scope
28 logger = logging.getLogger("")
29 logger.setLevel(logging.INFO)
30 fh = logging.StreamHandler()
31 fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(lineno)d:%(filename)s(%(process)d) - %(message)s')
32 fh.setFormatter(fh_formatter)
33 logger.addHandler(fh)
34
35
36 def resolve_inputs(config: Config):
37     logger.info("******** Check Customer *******")
38     customer_id = config.service_instance["customer_id"]
39     customer = Customer.get_by_global_customer_id(customer_id)
40     if customer is None:
41         raise Exception("Customer %s wasn't found in ONAP" % customer_id)
42     logger.info("******** Check Service Subscription *******")
43     service_subscription = None
44     for service_sub in customer.service_subscriptions:
45         logger.debug("Service subscription %s is found", service_sub.service_type)
46         if service_sub.service_type == config.service_model["model_name"]:
47             logger.info("Service %s subscribed", config.service_model["model_name"])
48             service_subscription = service_sub
49             break
50     logger.info("******** Retrieve Service Metadata *******")
51     service_instance = None
52     for single_service in service_subscription.service_instances:
53         if single_service.instance_name == config.service_instance["instance_name"]:
54             service_instance = single_service
55             break
56     service_id = service_instance.instance_id
57     vnfs = list(service_instance.vnf_instances)
58     if len(vnfs) > 1:
59         raise NotImplementedError("Service %s is composed of more than one vnf!" % service_id)
60     if not vnfs:
61         raise Exception("Service %s doesn't contain any vnfs" % service_id)
62     vnf_id = vnfs[0].vnf_id
63     return service_id, vnf_id
64
65 def main(replica_count):
66     mypath = os.path.dirname(os.path.realpath(__file__))
67     config = Config(env_dict=VariablesDict.env_variable)
68     for vnf in config.service_model["vnfs"]:
69         file = vnf["vsp"]["vsp_file"]
70         file_path = os.path.join(mypath, file)
71         with zipfile.ZipFile(file_path, 'r') as package:
72             cba_io = BytesIO(package.read("CBA.zip"))
73             cba_io.seek(0)
74             blueprint = Blueprint(cba_io.read())
75
76             healthcheck: Workflow = blueprint.get_workflow_by_name('scale')
77             serv_id, vnf_id = resolve_inputs(config)
78             cds_input = {"scale-properties":
79                 {
80                     "service-instance-id": serv_id,
81                     "vnf-id": vnf_id,
82                     "status-check-max-count": 20,
83                     "replica-count": int(replica_count)
84                 }
85             }
86
87             logger.info("Requesting Scale for CBA %s:%s with inputs:\n%s",
88                     blueprint.metadata.template_name,
89                     blueprint.metadata.template_version,
90                     cds_input)
91             result = healthcheck.execute(cds_input)
92             logger.info("Scale process completed with result: %s", result)
93             logger.info("Please check cds-blueprints-processor logs to see exact status")
94
95 if __name__ == "__main__":
96     replica_count = 1
97     if len(sys.argv) > 1:
98         replica_count = sys.argv[1]
99     print(f"Replica Count: %s" % replica_count)
100     main(replica_count)