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.
18 from lcm.ns.const import OWNER_TYPE
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_inst_id=None):
26 self.ns_inst_id = ns_inst_id
28 def get_ns_info(self):
31 return self.get_single_ns_info(self.ns_inst_id)
33 return self.get_total_ns_info()
35 logger.error(traceback.format_exc())
36 return None if self.ns_inst_id else []
38 def get_total_ns_info(self):
39 ns_inst_infos = NSInstModel.objects.all()
41 for info in ns_inst_infos:
42 ret = self.get_single_ns_info(info.id)
45 ns_info_list.append(ret)
48 def get_single_ns_info(self, ns_inst_id):
49 ns_insts = NSInstModel.objects.filter(id=ns_inst_id)
52 ns_inst_info = ns_insts[0]
54 'nsInstanceId': ns_inst_info.id,
55 'nsName': ns_inst_info.name,
56 'description': ns_inst_info.description,
57 'nsdId': ns_inst_info.nsd_id,
58 'vnfInfoId': self.get_vnf_infos(ns_inst_id),
59 'vlInfo': self.get_vl_infos(ns_inst_id),
60 'vnffgInfo': self.get_vnffg_infos(ns_inst_id, ns_inst_info.nsd_model),
61 'nsState': ns_inst_info.status}
65 def get_vnf_infos(ns_inst_id):
66 ns_inst_infos = NfInstModel.objects.filter(ns_inst_id=ns_inst_id)
68 for info in ns_inst_infos:
70 'vnfInstanceId': info.nfinstid,
71 'vnfInstanceName': info.nf_name,
72 'vnfProfileId': info.vnf_id}
73 vnf_info_list.append(vnf_info)
76 def get_vl_infos(self, ns_inst_id):
77 vl_inst_infos = VLInstModel.objects.filter(ownertype=OWNER_TYPE.NS, ownerid=ns_inst_id)
79 for info in vl_inst_infos:
81 'vlInstanceId': info.vlinstanceid,
82 'vlInstanceName': info.vlinstancename,
84 'relatedCpInstanceId': self.get_cp_infos(info.vlinstanceid)}
85 vl_info_list.append(vl_info)
89 def get_cp_infos(vl_inst_id):
90 cp_inst_infos = CPInstModel.objects.filter(relatedvl__icontains=vl_inst_id)
92 for info in cp_inst_infos:
94 'cpInstanceId': info.cpinstanceid,
95 'cpInstanceName': info.cpname,
97 cp_info_list.append(cp_info)
100 def get_vnffg_infos(self, ns_inst_id, nsd_model):
101 vnffg_inst_infos = VNFFGInstModel.objects.filter(nsinstid=ns_inst_id)
103 for info in vnffg_inst_infos:
105 'vnffgInstanceId': info.vnffginstid,
106 'vnfId': self.convert_string_to_list(info.vnflist),
107 'pnfId': self.get_pnf_infos(nsd_model),
108 'virtualLinkId': self.convert_string_to_list(info.vllist),
109 'cpId': self.convert_string_to_list(info.cplist),
110 'nfp': self.convert_string_to_list(info.fplist)}
111 vnffg_info_list.append(vnffg_info)
112 return vnffg_info_list
115 def get_pnf_infos(nsd_model):
116 context = json.loads(nsd_model)
117 pnfs = context['pnfs']
120 pnf_list.append(pnf['pnf_id'])
124 def convert_string_to_list(detail_id_string):
125 if not detail_id_string:
127 return detail_id_string.split(',')