[vFW_CNF_CDS] Add Healthcheck automation
[demo.git] / heat / vFW_CNF_CDS / 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
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 def resolve_hc_inputs():
35     logger.info("******** Check Customer *******")
36     customer = None
37     for found_customer in list(Customer.get_all()):
38         logger.debug("Customer %s found", found_customer.subscriber_name)
39         if found_customer.subscriber_name == Config.GLOBAL_CUSTOMER_ID:
40             logger.info("Customer %s found", found_customer.subscriber_name)
41             customer = found_customer
42             break
43     if customer is None:
44         raise Exception("Customer %s wasn't found in ONAP" % Config.GLOBAL_CUSTOMER_ID)
45     logger.info("******** Check Service Subscription *******")
46     service_subscription = None
47     for service_sub in customer.service_subscriptions:
48         logger.debug("Service subscription %s is found", service_sub.service_type)
49         if service_sub.service_type == Config.SERVICENAME:
50             logger.info("Service %s subscribed", Config.SERVICENAME)
51             service_subscription = service_sub
52             break
53     logger.info("******** Retrieve Service Metadata *******")
54     service_instance = None
55     for single_service in service_subscription.service_instances:
56         if single_service.instance_name == Config.SERVICE_INSTANCE_NAME:
57             service_instance = single_service
58             break
59     service_id = service_instance.instance_id
60     vnfs = list(service_instance.vnf_instances)
61     if len(vnfs) > 1:
62         raise NotImplementedError("Service %s is composed of more than one vnf!" % service_id)
63     if not vnfs:
64         raise Exception("Service %s doesn't contain any vnfs" % service_id)
65     vnf_id = vnfs[0].vnf_id
66     return service_id, vnf_id
67
68 def main():
69     blueprint = None
70     with zipfile.ZipFile(Config.VSPFILE, 'r') as package:
71         with package.open("CBA.zip", 'r') as cba:
72             blueprint = Blueprint(cba.read())
73
74     healthcheck = Workflow('health-check', None, blueprint)
75     serv_id, vnf_id = resolve_hc_inputs()
76     cds_input = {"health-check-properties":
77         {
78             "service-instance-id": serv_id,
79             "vnf-id": vnf_id
80         }
81     }
82     logger.info("Requesting Healthcheck for CBA %s:%s with inputs:\n%s",
83             blueprint.metadata.template_name,
84             blueprint.metadata.template_version,
85             cds_input)
86     result = healthcheck.execute(cds_input)
87     logger.info("Healthcheck process completed with result: %s", result)
88     logger.info("Please check cds-blueprints-processor logs to see exact status")
89
90 if __name__ == "__main__":
91     main()