1 # Copyright 2016 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from lcm.ns.const import OWNER_TYPE
18 from lcm.pub.utils import restcall
19 from lcm.pub.database.models import NSInstModel, NfInstModel, VLInstModel, CPInstModel, VNFFGInstModel
21 logger = logging.getLogger(__name__)
24 class GetNSInfoService(object):
25 def __init__(self, ns_filter=None):
26 self.ns_filter = ns_filter
28 def get_ns_info(self):
30 if self.ns_filter and "ns_inst_id" in self.ns_filter:
31 ns_inst_id = self.ns_filter["ns_inst_id"]
32 ns_insts = NSInstModel.objects.filter(id=ns_inst_id)
34 ns_insts = NSInstModel.objects.all()
36 return [self.get_single_ns_info(ns_inst) for ns_inst in ns_insts]
38 def get_single_ns_info(self, ns_inst):
40 'nsInstanceId': ns_inst.id,
41 'nsName': ns_inst.name,
42 'description': ns_inst.description,
43 'nsdId': ns_inst.nsd_id,
44 'nsdInvariantId': ns_inst.nsd_invariant_id,
45 'vnfInfo': self.get_vnf_infos(ns_inst.id),
46 'pnfInfo': self.get_pnf_infos(ns_inst.id),
47 'vlInfo': self.get_vl_infos(ns_inst.id),
48 'vnffgInfo': self.get_vnffg_infos(ns_inst.id, ns_inst.nsd_model),
49 'nsState': ns_inst.status}
52 def get_vnf_infos(ns_inst_id):
53 vnfs = NfInstModel.objects.filter(ns_inst_id=ns_inst_id)
55 'vnfInstanceId': vnf.nfinstid,
56 'vnfInstanceName': vnf.nf_name,
57 'vnfProfileId': vnf.vnf_id} for vnf in vnfs]
59 def get_vl_infos(self, ns_inst_id):
60 vls = VLInstModel.objects.filter(ownertype=OWNER_TYPE.NS, ownerid=ns_inst_id)
62 'vlInstanceId': vl.vlinstanceid,
63 'vlInstanceName': vl.vlinstancename,
65 'relatedCpInstanceId': self.get_cp_infos(vl.vlinstanceid)} for vl in vls]
68 def get_cp_infos(vl_inst_id):
69 cps = CPInstModel.objects.filter(relatedvl__icontains=vl_inst_id)
71 'cpInstanceId': cp.cpinstanceid,
72 'cpInstanceName': cp.cpname,
73 'cpdId': cp.cpdid} for cp in cps]
75 def get_vnffg_infos(self, ns_inst_id, nsd_model):
76 vnffgs = VNFFGInstModel.objects.filter(nsinstid=ns_inst_id)
78 'vnffgInstanceId': vnffg.vnffginstid,
79 'vnfId': self.convert_string_to_list(vnffg.vnflist),
80 'pnfId': self.get_pnf_ids(nsd_model),
81 'virtualLinkId': self.convert_string_to_list(vnffg.vllist),
82 'cpId': self.convert_string_to_list(vnffg.cplist),
83 'nfp': self.convert_string_to_list(vnffg.fplist)} for vnffg in vnffgs]
86 def get_pnf_ids(nsd_model):
87 context = json.loads(nsd_model)
88 pnfs = context['pnfs']
89 return [pnf['pnf_id'] for pnf in pnfs]
92 def convert_string_to_list(detail_id_string):
93 if not detail_id_string:
95 return detail_id_string.split(',')
98 def get_pnf_infos(ns_instance_id):
99 uri = "api/nslcm/v1/pnfs?nsInstanceId=%s" % ns_instance_id
100 ret = restcall.req_by_msb(uri, "GET")
102 return json.loads(ret[1])