diff options
Diffstat (limited to 'heat/vFW_CNF_CDS/automation/delete.py')
-rwxr-xr-x | heat/vFW_CNF_CDS/automation/delete.py | 123 |
1 files changed, 81 insertions, 42 deletions
diff --git a/heat/vFW_CNF_CDS/automation/delete.py b/heat/vFW_CNF_CDS/automation/delete.py index c5ac334a..09f2856d 100755 --- a/heat/vFW_CNF_CDS/automation/delete.py +++ b/heat/vFW_CNF_CDS/automation/delete.py @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (C) 2020 Orange +# Copyright (C) 2021 Orange # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,13 +16,11 @@ # ============LICENSE_END========================================================= import logging -from time import sleep - -from onapsdk.aai.business import Customer -from onapsdk.so.so_element import OrchestrationRequest from config import Config +from instantiate import get_customer, check_orchestration_status, get_service_model + logger = logging.getLogger("") logger.setLevel(logging.DEBUG) fh = logging.StreamHandler() @@ -30,40 +28,81 @@ fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(lineno)d:%(filenam fh.setFormatter(fh_formatter) logger.addHandler(fh) -logger.info("******** Get Customer *******") -customer = None -try: - customer = Customer.get_by_global_customer_id(Config.GLOBAL_CUSTOMER_ID) -except: - logger.error("Customer not found") - exit(1) - -logger.info("******** Check Service Subscription *******") -service_subscription = None -for service_sub in customer.service_subscriptions: - if service_sub.service_type == Config.SERVICENAME: - logger.info("Service %s subscribed", Config.SERVICENAME) - service_subscription = service_sub - break -if not service_subscription: - logger.error("Service Subscription not found") - exit(1) - -logger.info("******** Get Service Instance details *******") -service_instance = None -for service in service_subscription.service_instances: - if service.instance_name == Config.SERVICE_INSTANCE_NAME: - service_instance = service - break -if not service_instance: - logger.error("Service Instance not found") - exit(1) - -logger.info("******** Delete Service %s *******", service_instance.instance_name) -service_deletion = service_instance.delete() -status = None -while not (status == OrchestrationRequest.StatusEnum.COMPLETED - or status == OrchestrationRequest.StatusEnum.FAILED): - sleep(10) - status = service_deletion.status - logger.info(f"Orchestration status is: {status.value}") + +def get_service_subscription(customer, service_name): + try: + service_subscription = next( + service_sub for service_sub in customer.service_subscriptions if service_sub.service_type == service_name) + except StopIteration: + logger.error("Service Subscription not found") + exit(1) + return service_subscription + + +def get_service_instance(service_subscription, service_instance_name): + try: + service_instance = next(instance for instance in service_subscription.service_instances + if instance.instance_name == service_instance_name) + except StopIteration: + logger.error("Service Instance not found") + exit(1) + return service_instance + + +def delete_service_macro(service_instance): + service_deletion = service_instance.delete(a_la_carte=False) + check_orchestration_status(service_deletion) + + +def delete_service_alacarte(service_instance): + for vnf in service_instance.vnf_instances: + for vf_module in vnf.vf_modules: + vf_module_deletion = vf_module.delete() + check_orchestration_status(vf_module_deletion) + vnf_deletion = vnf.delete() + check_orchestration_status(vnf_deletion) + service_deletion = service_instance.delete() + check_orchestration_status(service_deletion) + + +def delete_service_alacarte2(service_instance, service_model): + for vnf in service_instance.vnf_instances: + for label in sorted(Config.VF_MODULE_PARAM_LIST, reverse=True): + vf_module_model = next(vfmodule for vfmodule in service_model.vf_modules + if vfmodule.properties["vf_module_label"] == label) + vfModuleModelInvariantUUID = vf_module_model.metadata["vfModuleModelInvariantUUID"] + vf_module = next(vfmodule for vfmodule in vnf.vf_modules if vfmodule.model_invariant_id == vfModuleModelInvariantUUID) + vf_module_deletion = vf_module.delete() + check_orchestration_status(vf_module_deletion) + + vnf_deletion = vnf.delete() + check_orchestration_status(vnf_deletion) + service_deletion = service_instance.delete() + check_orchestration_status(service_deletion) + + +def main(): + logger.info("*******************************") + logger.info("**** SERVICE DELETION ****") + logger.info("*******************************") + + logger.info("******** GET Customer *******") + customer = get_customer(Config.GLOBAL_CUSTOMER_ID) + + logger.info("******** Check Service Subscription *******") + service_subscription = get_service_subscription(customer, Config.SERVICENAME) + + logger.info("******** Get Service Instance details *******") + service_instance = get_service_instance(service_subscription, Config.SERVICE_INSTANCE_NAME) + + service_model = get_service_model(Config.SERVICENAME) + + logger.info("******** Delete Service %s *******", service_instance.instance_name) + if Config.MACRO_INSTANTIATION: + delete_service_macro(service_instance) + else: + delete_service_alacarte2(service_instance, service_model) + + +if __name__ == "__main__": + main() |