From 4ea43d0b7c6260b3195be15225d4e5250ce8f3f2 Mon Sep 17 00:00:00 2001 From: dyh Date: Thu, 23 Dec 2021 14:45:56 +0800 Subject: [PATCH] add comments Change-Id: I80b7c195a4d3017cf3ad6d33346e92f701c32e2e Issue-ID: VFC-1917 Signed-off-by: dyh --- lcm/ns/biz/create_subscription.py | 34 +++++++++++++++++++++++---- lcm/ns/biz/ns_create.py | 24 +++++++++++++++++++ lcm/ns/biz/ns_delete.py | 12 ++++++++++ lcm/ns/biz/ns_get.py | 49 +++++++++++++++++++++++++++++++++++++++ lcm/ns/biz/ns_heal.py | 4 ++++ lcm/ns/biz/ns_instant.py | 28 +++++++++++++++++++++- lcm/ns/biz/ns_manual_scale.py | 34 +++++++++++++++++++++++++-- lcm/ns/biz/ns_terminate.py | 31 ++++++++++++++++++++++++- lcm/ns/biz/ns_update.py | 4 ++++ lcm/ns/biz/query_subscription.py | 7 ++++++ lcm/ns/views/sol/heal_ns_view.py | 7 +++--- 11 files changed, 222 insertions(+), 12 deletions(-) diff --git a/lcm/ns/biz/create_subscription.py b/lcm/ns/biz/create_subscription.py index e1a44eee..d38b2ac1 100644 --- a/lcm/ns/biz/create_subscription.py +++ b/lcm/ns/biz/create_subscription.py @@ -53,6 +53,9 @@ NS_FILTER_TYPE = [ class CreateSubscription: + """ + Subscription Create process + """ def __init__(self, data): self.data = data @@ -80,6 +83,10 @@ class CreateSubscription: ) def check_callback_uri(self): + """ + Check if the callback Uri can access + :return: + """ logger.debug("SubscribeNotification-post::> Sending GET request to %s" % self.callback_uri) try: response = requests.get(self.callback_uri, timeout=2) @@ -102,6 +109,10 @@ class CreateSubscription: return subscription def check_filter_types(self): + """ + Validate operationTypes and operationStates if exists + :return: + """ logger.debug("SubscribeNotification--post::> Validating operationTypes and operationStates if exists") occ_notification = NOTIFICATION_TYPE.NSLCM_OPERATION_OCCURRENCE_NOTIFICATION if self.operation_types and occ_notification not in self.notification_types: @@ -112,6 +123,10 @@ class CreateSubscription: raise NSLCMException(except_message % occ_notification) def check_valid_auth_info(self): + """ + Validate Auth details if provided + :return: + """ logger.debug("SubscribeNotification--post::> Validating Auth details if provided") auth_type = self.authentication.get("authType") params_basic = self.authentication.get("paramsBasic") @@ -125,21 +140,25 @@ class CreateSubscription: # Check the notificationTypes, operationTypes, operationStates for filter_type in FILTER_TYPE: if not is_filter_type_equal( - getattr(self, filter_type), - ast.literal_eval(getattr(sub, filter_type)) + getattr(self, filter_type), + ast.literal_eval(getattr(sub, filter_type)) ): return False # If all the above types are same then check ns instance filters ns_filter = json.loads(sub.ns_instance_filter) for ns_filter_type in NS_FILTER_TYPE: if not is_filter_type_equal( - self.ns_filter.get(ns_filter_type, []), - ns_filter.get(ns_filter_type, []) + self.ns_filter.get(ns_filter_type, []), + ns_filter.get(ns_filter_type, []) ): return False return True def check_valid(self): + """ + Check DB if callbackUri already exists + :return: + """ logger.debug("SubscribeNotification--post::> Checking DB if callbackUri already exists") subscriptions = SubscriptionModel.objects.filter(callback_uri=self.callback_uri) if not subscriptions.exists(): @@ -150,7 +169,12 @@ class CreateSubscription: return False def save_db(self): - logger.debug("SubscribeNotification--post::> Saving the subscription(%s) to the database" % self.subscription_id) + """ + Save the subscription(%s) to the database + :return: + """ + logger.debug( + "SubscribeNotification--post::> Saving the subscription(%s) to the database" % self.subscription_id) links = { "self": { "href": const.SUBSCRIPTION_ROOT_URI % self.subscription_id diff --git a/lcm/ns/biz/ns_create.py b/lcm/ns/biz/ns_create.py index 2b85d041..59783a12 100644 --- a/lcm/ns/biz/ns_create.py +++ b/lcm/ns/biz/ns_create.py @@ -28,6 +28,10 @@ logger = logging.getLogger(__name__) class CreateNSService(object): + """ + This class for NS instance Model create + """ + def __init__(self, csar_id, ns_name, description, context): self.csar_id = csar_id self.ns_name = ns_name @@ -38,6 +42,10 @@ class CreateNSService(object): self.ns_package_id = '' def do_biz(self): + """ + Create NS instance model + :return: + """ self.check_nsd_valid() self.check_ns_inst_name_exist() self.create_ns_inst() @@ -47,6 +55,10 @@ class CreateNSService(object): return self.ns_inst_id def check_nsd_valid(self): + """ + Check the validation of NSD + :return: + """ logger.debug("CreateNSService::check_nsd_valid::csar_id=%s" % self.csar_id) ns_package_info = query_nspackage_by_id(self.csar_id) if not ns_package_info: @@ -58,12 +70,20 @@ class CreateNSService(object): logger.debug("CreateNSService::check_nsd_valid::ns_package_id=%s,nsd_id=%s", self.ns_package_id, self.nsd_id) def check_ns_inst_name_exist(self): + """ + Check if the ns instance with same name exists + :return: + """ is_exist = NSInstModel.objects.filter(name=self.ns_name).exclude(status='null').exists() logger.debug("CreateNSService::check_ns_inst_name_exist::is_exist=%s" % is_exist) if is_exist: raise NSLCMException("ns(%s) already existed." % self.ns_name) def create_ns_inst(self): + """ + Create NS instance Model + :return: + """ self.ns_inst_id = str(uuid.uuid4()) logger.debug("CreateNSService::create_ns_inst::ns_inst_id=%s" % self.ns_inst_id) NSInstModel(id=self.ns_inst_id, @@ -78,6 +98,10 @@ class CreateNSService(object): service_type=self.service_type).save() def create_ns_in_aai(self): + """ + Create NS instance record in AAI + :return: + """ logger.debug("CreateNSService::create_ns_in_aai::report ns instance[%s] to aai." % self.ns_inst_id) try: data = { diff --git a/lcm/ns/biz/ns_delete.py b/lcm/ns/biz/ns_delete.py index 9bfad7b8..10a1e657 100644 --- a/lcm/ns/biz/ns_delete.py +++ b/lcm/ns/biz/ns_delete.py @@ -24,6 +24,10 @@ logger = logging.getLogger(__name__) class DeleteNsService(object): + """ + Class to delete NS instance + """ + def __init__(self, ns_inst_id): self.ns_inst_id = ns_inst_id @@ -36,6 +40,10 @@ class DeleteNsService(object): logger.error(traceback.format_exc()) def delete_ns(self): + """ + Delete NS instance model + :return: + """ logger.debug("delele NSInstModel(%s)", self.ns_inst_id) NSInstModel.objects.filter(id=self.ns_inst_id).delete() @@ -49,6 +57,10 @@ class DeleteNsService(object): ServiceBaseInfoModel.objects.filter(service_id=self.ns_inst_id).delete() def delete_ns_in_aai(self): + """ + Delete NS instance record from AAI + :return: + """ logger.debug("DeleteNsService::delete_ns_in_aai::delete ns instance[%s] in aai." % self.ns_inst_id) try: ns_insts = NSInstModel.objects.filter(id=self.ns_inst_id) diff --git a/lcm/ns/biz/ns_get.py b/lcm/ns/biz/ns_get.py index 472195d7..87e425c1 100644 --- a/lcm/ns/biz/ns_get.py +++ b/lcm/ns/biz/ns_get.py @@ -28,10 +28,19 @@ logger = logging.getLogger(__name__) class GetNSInfoService(object): + """ + Get NS instance Model + """ + def __init__(self, ns_filter=None): self.ns_filter = ns_filter def get_ns_info(self, is_sol=False): + """ + Get all the ns instance models + :param is_sol: + :return: + """ if self.ns_filter and "ns_inst_id" in self.ns_filter: ns_inst_id = self.ns_filter["ns_inst_id"] ns_insts = NSInstModel.objects.filter(id=ns_inst_id) @@ -44,6 +53,12 @@ class GetNSInfoService(object): return result def get_single_ns_info(self, ns_inst, is_sol=False): + """ + Get a ns instance model + :param ns_inst: + :param is_sol: + :return: + """ if is_sol: nsInstance = {} nsInstance['id'] = ns_inst.id @@ -94,6 +109,12 @@ class GetNSInfoService(object): @staticmethod def get_vnf_infos(ns_inst_id, is_sol): + """ + VNF info + :param ns_inst_id: + :param is_sol: + :return: + """ vnfs = NfInstModel.objects.filter(ns_inst_id=ns_inst_id) if is_sol: return [{ @@ -113,6 +134,12 @@ class GetNSInfoService(object): 'vnfProfileId': vnf.vnf_id} for vnf in vnfs] def get_vl_infos(self, ns_inst_id, is_sol): + """ + VL info + :param ns_inst_id: + :param is_sol: + :return: + """ vls = VLInstModel.objects.filter(ownertype=OWNER_TYPE.NS, ownerid=ns_inst_id) if is_sol: return [ @@ -139,6 +166,11 @@ class GetNSInfoService(object): @staticmethod def get_cp_infos(vl_inst_id): + """ + CP info + :param vl_inst_id: + :return: + """ cps = CPInstModel.objects.filter(relatedvl__icontains=vl_inst_id) return [{ 'cpInstanceId': cp.cpinstanceid, @@ -146,6 +178,13 @@ class GetNSInfoService(object): 'cpdId': cp.cpdid} for cp in cps] def get_vnffg_infos(self, ns_inst_id, nsd_model, is_sol): + """ + VNFFG info + :param ns_inst_id: + :param nsd_model: + :param is_sol: + :return: + """ vnffgs = VNFFGInstModel.objects.filter(nsinstid=ns_inst_id) return [{ 'vnffgInstanceId': vnffg.vnffginstid, @@ -157,6 +196,11 @@ class GetNSInfoService(object): @staticmethod def get_pnf_ids(nsd_model): + """ + PNF ids + :param nsd_model: + :return: + """ context = json.loads(nsd_model) pnfs = context['pnfs'] return [pnf['pnf_id'] for pnf in pnfs] @@ -169,5 +213,10 @@ class GetNSInfoService(object): @staticmethod def get_pnf_infos(ns_instance_id): + """ + PNF infos + :param ns_instance_id: + :return: + """ pnfs = PNFInstModel.objects.filter(nsInstances__contains=ns_instance_id) return [pnf.__dict__ for pnf in pnfs] diff --git a/lcm/ns/biz/ns_heal.py b/lcm/ns/biz/ns_heal.py index 316df7ae..171a30e2 100644 --- a/lcm/ns/biz/ns_heal.py +++ b/lcm/ns/biz/ns_heal.py @@ -31,6 +31,10 @@ logger = logging.getLogger(__name__) class NSHealService(threading.Thread): + """ + Heal the NS instance + """ + def __init__(self, ns_instance_id, request_data, job_id): super(NSHealService, self).__init__() self.ns_instance_id = ns_instance_id diff --git a/lcm/ns/biz/ns_instant.py b/lcm/ns/biz/ns_instant.py index 5cd33783..19594711 100644 --- a/lcm/ns/biz/ns_instant.py +++ b/lcm/ns/biz/ns_instant.py @@ -51,6 +51,10 @@ class BuildInWorkflowThread(Thread): class InstantNSService(object): + """ + Instantiate NS + """ + def __init__(self, ns_inst_id, plan_content): self.ns_inst_id = ns_inst_id self.req_data = plan_content @@ -79,7 +83,7 @@ class InstantNSService(object): else: params_json = json.JSONEncoder().encode({}) - location_constraints = [] if not self.req_data.get('locationConstraints')\ + location_constraints = [] if not self.req_data.get('locationConstraints') \ else self.req_data['locationConstraints'] vnf_vim = self.get_vnf_vim_info(location_constraints) @@ -183,6 +187,14 @@ class InstantNSService(object): return dict(data={'error': e.args[0]}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) def start_wso2_workflow(self, job_id, ns_inst, plan_input, occ_id): + """ + Start WSO2 workflow + :param job_id: + :param ns_inst: + :param plan_input: + :param occ_id: + :return: + """ # todo occ_id servicetemplate_id = get_servicetemplate_id(ns_inst.nsd_id) process_id = get_process_id('init', servicetemplate_id) @@ -198,6 +210,13 @@ class InstantNSService(object): return dict(data={'error': ret['message']}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) def start_activiti_workflow(self, job_id, plan_input, occ_id): + """ + Start activiti workflow + :param job_id: + :param plan_input: + :param occ_id: + :return: + """ # todo occ_id plans = WFPlanModel.objects.filter() if not plans: @@ -215,6 +234,13 @@ class InstantNSService(object): return dict(data={'error': ret['message']}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) def start_buildin_workflow(self, job_id, plan_input, occ_id): + """ + Start buildin workflow + :param job_id: + :param plan_input: + :param occ_id: + :return: + """ JobUtil.add_job_status(job_id, 10, 'NS inst(%s) buildin workflow started.' % self.ns_inst_id) BuildInWorkflowThread(plan_input, occ_id).start() return dict(data={'jobId': job_id}, status=status.HTTP_200_OK, occ_id=occ_id) diff --git a/lcm/ns/biz/ns_manual_scale.py b/lcm/ns/biz/ns_manual_scale.py index 6d4e9386..d3f5eebe 100644 --- a/lcm/ns/biz/ns_manual_scale.py +++ b/lcm/ns/biz/ns_manual_scale.py @@ -33,6 +33,10 @@ logger = logging.getLogger(__name__) class NSManualScaleService(threading.Thread): + """ + Scale the NS instance + """ + def __init__(self, ns_instance_id, request_data, job_id): super(NSManualScaleService, self).__init__() self.ns_instance_id = ns_instance_id @@ -76,6 +80,10 @@ class NSManualScaleService(threading.Thread): raise NSLCMException('Failed to get scaleVnfData parameter') def do_vnfs_scale(self): + """ + Scale VNF instance + :return: + """ for i in range(len(self.scale_vnf_data)): vnf_scale_params = self.prepare_vnf_scale_params( self.scale_vnf_data[i]) @@ -89,11 +97,16 @@ class NSManualScaleService(threading.Thread): self.update_job( progress_range[1], desc='nf[%s] scale handle end' % - vnf_scale_params.get('vnfInstanceId')) + vnf_scale_params.get('vnfInstanceId')) else: raise NSLCMException('VNF scale failed') def prepare_vnf_scale_params(self, vnf_data): + """ + Prepare parameters for VNF instance scale + :param vnf_data: + :return: + """ return { "vnfInstanceId": ignore_case_get(vnf_data, 'vnfInstanceId'), "scaleVnfData": ignore_case_get(vnf_data, 'scaleByStepData'), @@ -101,13 +114,19 @@ class NSManualScaleService(threading.Thread): } def do_vnf_scale(self, vnf_scale_params, progress_range): + """ + Scale VNF instance + :param vnf_scale_params: + :param progress_range: + :return: + """ nf_inst_id = vnf_scale_params.get('vnfInstanceId') nf_service = NFManualScaleService(nf_inst_id, vnf_scale_params) nf_service.start() self.update_job( progress_range[0], desc='nf[%s] scale handle start' % - nf_inst_id) + nf_inst_id) status = self.wait_job_finish(nf_service.job_id) return status @@ -126,9 +145,20 @@ class NSManualScaleService(threading.Thread): return JOB_MODEL_STATUS.TIMEOUT def update_job(self, progress, desc=''): + """ + Update the information of job + :param progress: + :param desc: + :return: + """ JobUtil.add_job_status(self.job_id, progress, desc) def update_ns_status(self, status): + """ + Update NS instance status + :param status: + :return: + """ NSInstModel.objects.filter( id=self.ns_instance_id).update( status=status) diff --git a/lcm/ns/biz/ns_terminate.py b/lcm/ns/biz/ns_terminate.py index a4ae4df5..3f3a50be 100644 --- a/lcm/ns/biz/ns_terminate.py +++ b/lcm/ns/biz/ns_terminate.py @@ -36,6 +36,10 @@ logger = logging.getLogger(__name__) class TerminateNsService(threading.Thread): + """ + Terminate the NS instance + """ + def __init__(self, ns_inst_id, job_id, request_data): threading.Thread.__init__(self) self.terminate_type = request_data.get('terminationType', 'GRACEFUL') @@ -74,12 +78,20 @@ class TerminateNsService(threading.Thread): build_in.post_deal(self.ns_inst_id, "false") def modify_package_state(self): + """ + Update the state of NS instance model + :return: + """ ns_inst = NSInstModel.objects.filter(id=self.ns_inst_id) ns_insts = NSInstModel.objects.filter(nspackage_id=ns_inst[0].nspackage_id) if len(ns_insts) == 1: sdc_run_catalog.modify_nsd_state(ns_inst[0].nspackage_id, 0) def cancel_vl_list(self): + """ + Delete list of VL related + :return: + """ array_vlinst = VLInstModel.objects.filter(ownertype=OWNER_TYPE.NS, ownerid=self.ns_inst_id) if not array_vlinst: logger.info("[cancel_vl_list] no vlinst attatch to ns_inst_id: %s" % self.ns_inst_id) @@ -102,6 +114,10 @@ class TerminateNsService(threading.Thread): JobUtil.add_job_status(self.job_id, cur_progress, job_msg) def cancel_sfc_list(self): + """ + Delete SFC list + :return: + """ array_sfcinst = FPInstModel.objects.filter(nsinstid=self.ns_inst_id) if not array_sfcinst: logger.info("[cancel_sfc_list] no sfcinst attatch to ns_inst_id: %s" % self.ns_inst_id) @@ -124,6 +140,10 @@ class TerminateNsService(threading.Thread): JobUtil.add_job_status(self.job_id, cur_progress, job_msg) def cancel_vnf_list(self): + """ + Delete VNF instance list + :return: + """ array_vnfinst = NfInstModel.objects.filter(ns_inst_id=self.ns_inst_id) if not array_vnfinst: logger.info("[cancel_vnf_list] no vnfinst attatch to ns_inst_id: %s" % self.ns_inst_id) @@ -154,7 +174,7 @@ class TerminateNsService(threading.Thread): if not vnfjobid: continue is_job_ok = self.wait_delete_vnf_job_finish(vnfjobid) - msg = "%s to delete VNF(%s)" %\ + msg = "%s to delete VNF(%s)" % \ ("Succeed" if is_job_ok else "Failed", vnfinstid) logger.debug(msg) JobUtil.add_job_status(self.job_id, cur_progress, msg) @@ -164,6 +184,11 @@ class TerminateNsService(threading.Thread): JobUtil.add_job_status(self.job_id, cur_progress, msg) def delete_vnf(self, nf_instid): + """ + Delete VNF instance + :param nf_instid: + :return: + """ term_param = { "terminationType": self.terminate_type } @@ -215,6 +240,10 @@ class TerminateNsService(threading.Thread): return job_end_normal def cancel_pnf_list(self): + """ + Delete PNF list + :return: + """ pnfinst_list = PNFInstModel.objects.filter(nsInstances__contains=self.ns_inst_id) if len(pnfinst_list) > 0: cur_progress = 90 diff --git a/lcm/ns/biz/ns_update.py b/lcm/ns/biz/ns_update.py index 6d996d29..90eb75af 100644 --- a/lcm/ns/biz/ns_update.py +++ b/lcm/ns/biz/ns_update.py @@ -30,6 +30,10 @@ logger = logging.getLogger(__name__) class NSUpdateService(threading.Thread): + """ + Update NS instance model + """ + def __init__(self, ns_instance_id, request_data, job_id): super(NSUpdateService, self).__init__() self.ns_instance_id = ns_instance_id diff --git a/lcm/ns/biz/query_subscription.py b/lcm/ns/biz/query_subscription.py index 0b3cbc9d..226931aa 100644 --- a/lcm/ns/biz/query_subscription.py +++ b/lcm/ns/biz/query_subscription.py @@ -33,12 +33,19 @@ NS_INSTANCE_FILTERS = { class QuerySubscription: + """ + Search subscription record + """ def __init__(self, data, subscription_id=''): self.subscription_id = subscription_id self.params = data def query_multi_subscriptions(self): + """ + Get multiple subscriptions + :return: + """ query_data = {} logger.debug( "QueryMultiSubscriptions--get--biz::> Check for filters in query params" % self.params) diff --git a/lcm/ns/views/sol/heal_ns_view.py b/lcm/ns/views/sol/heal_ns_view.py index f37d13dd..e6e0ed57 100644 --- a/lcm/ns/views/sol/heal_ns_view.py +++ b/lcm/ns/views/sol/heal_ns_view.py @@ -34,6 +34,7 @@ class HealNSView(APIView): This task resource represents the "Heal NS" operation. The client can use this resource to request healing a NS instance. """ + @swagger_auto_schema( request_body=HealNsReqSerializer(), responses={ @@ -45,9 +46,9 @@ class HealNSView(APIView): def post(self, request, ns_instance_id): """ The POST method requests to heal a NS instance resource. - :param request: - :param ns_instance_id: - :return: + :param request: + :param ns_instance_id: + :return: """ logger.debug("Enter HealNSView::post nsInstanceId:%s, request.data:%s" % (ns_instance_id, request.data)) req_serializer = HealNsReqSerializer(data=request.data) -- 2.16.6