aee101ddbd904661e473f17994b3b75e95433ae0
[demo.git] / tutorials / ApacheCNF / automation / healthcheck.py
1 # ============LICENSE_START=======================================================
2 # Copyright (C) 2021 Samsung
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 #todo update when onapsdk > 9.3.0
18 import logging
19 import zipfile
20
21 from onapsdk.aai.business import Customer
22 from onapsdk.cds.blueprint import Workflow, Blueprint
23
24 from config import Config
25
26 #FIXME remove from global scope
27 logger = logging.getLogger("")
28 logger.setLevel(logging.INFO)
29 fh = logging.StreamHandler()
30 fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(lineno)d:%(filename)s(%(process)d) - %(message)s')
31 fh.setFormatter(fh_formatter)
32 logger.addHandler(fh)
33
34
35 def resolve_hc_inputs():
36     logger.info("******** Check Customer *******")
37     customer = None
38     for found_customer in list(Customer.get_all()):
39         logger.debug("Customer %s found", found_customer.subscriber_name)
40         if found_customer.subscriber_name == Config.GLOBAL_CUSTOMER_ID:
41             logger.info("Customer %s found", found_customer.subscriber_name)
42             customer = found_customer
43             break
44     if customer is None:
45         raise Exception("Customer %s wasn't found in ONAP" % Config.GLOBAL_CUSTOMER_ID)
46     logger.info("******** Check Service Subscription *******")
47     service_subscription = None
48     for service_sub in customer.service_subscriptions:
49         logger.debug("Service subscription %s is found", service_sub.service_type)
50         if service_sub.service_type == Config.SERVICENAME:
51             logger.info("Service %s subscribed", Config.SERVICENAME)
52             service_subscription = service_sub
53             break
54     logger.info("******** Retrieve Service Metadata *******")
55     service_instance = None
56     for single_service in service_subscription.service_instances:
57         if single_service.instance_name == Config.SERVICE_INSTANCE_NAME:
58             service_instance = single_service
59             break
60     service_id = service_instance.instance_id
61     vnfs = list(service_instance.vnf_instances)
62     if len(vnfs) > 1:
63         raise NotImplementedError("Service %s is composed of more than one vnf!" % service_id)
64     if not vnfs:
65         raise Exception("Service %s doesn't contain any vnfs" % service_id)
66     vnf_id = vnfs[0].vnf_id
67     return service_id, vnf_id
68
69 def main():
70     blueprint = None
71     with zipfile.ZipFile(Config.VSPFILE, 'r') as package:
72         with package.open("CBA.zip", 'r') as cba:
73             blueprint = Blueprint(cba.read())
74
75     healthcheck = Workflow('health-check', None, blueprint)
76     serv_id, vnf_id = resolve_hc_inputs()
77     cds_input = {"health-check-properties":
78         {
79             "service-instance-id": serv_id,
80             "vnf-id": vnf_id
81         }
82     }
83     logger.info("Requesting Healthcheck for CBA %s:%s with inputs:\n%s",
84             blueprint.metadata.template_name,
85             blueprint.metadata.template_version,
86             cds_input)
87     result = healthcheck.execute(cds_input)
88     logger.info("Healthcheck process completed with result: %s", result)
89     logger.info("Please check cds-blueprints-processor logs to see exact status")
90
91 if __name__ == "__main__":
92     main()