2e572a3d0bd1283f8e3d184dcfb7c67a1fd4f683
[vfc/nfvo/lcm.git] / lcm / ns / biz / ns_get.py
1 # Copyright 2016 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 import json
15 import logging
16
17 from lcm.ns.const import OWNER_TYPE
18 from lcm.pub.database.models import NSInstModel, NfInstModel, VLInstModel, CPInstModel, VNFFGInstModel
19
20 logger = logging.getLogger(__name__)
21
22
23 class GetNSInfoService(object):
24     def __init__(self, ns_filter=None):
25         self.ns_filter = ns_filter
26
27     def get_ns_info(self):
28         ns_insts = None
29         if self.ns_filter and "ns_inst_id" in self.ns_filter:
30             ns_inst_id = self.ns_filter["ns_inst_id"]
31             ns_insts = NSInstModel.objects.filter(id=ns_inst_id)
32         else:
33             ns_insts = NSInstModel.objects.all()
34
35         return [self.get_single_ns_info(ns_inst) for ns_inst in ns_insts]
36
37     def get_single_ns_info(self, ns_inst):
38         return {
39             'nsInstanceId': ns_inst.id,
40             'nsName': ns_inst.name,
41             'description': ns_inst.description,
42             'nsdId': ns_inst.nsd_id,
43             'nsdInvariantId': ns_inst.nsd_invariant_id,
44             'vnfInfo': self.get_vnf_infos(ns_inst.id),
45             'vlInfo': self.get_vl_infos(ns_inst.id),
46             'vnffgInfo': self.get_vnffg_infos(ns_inst.id, ns_inst.nsd_model),
47             'nsState': ns_inst.status}
48
49     @staticmethod
50     def get_vnf_infos(ns_inst_id):
51         vnfs = NfInstModel.objects.filter(ns_inst_id=ns_inst_id)
52         return [{
53             'vnfInstanceId': vnf.nfinstid,
54             'vnfInstanceName': vnf.nf_name,
55             'vnfProfileId': vnf.vnf_id} for vnf in vnfs]
56
57     def get_vl_infos(self, ns_inst_id):
58         vls = VLInstModel.objects.filter(ownertype=OWNER_TYPE.NS, ownerid=ns_inst_id)
59         return [{
60             'vlInstanceId': vl.vlinstanceid,
61             'vlInstanceName': vl.vlinstancename,
62             'vldId': vl.vldid,
63             'relatedCpInstanceId': self.get_cp_infos(vl.vlinstanceid)} for vl in vls]
64
65     @staticmethod
66     def get_cp_infos(vl_inst_id):
67         cps = CPInstModel.objects.filter(relatedvl__icontains=vl_inst_id)
68         return [{
69             'cpInstanceId': cp.cpinstanceid,
70             'cpInstanceName': cp.cpname,
71             'cpdId': cp.cpdid} for cp in cps]
72
73     def get_vnffg_infos(self, ns_inst_id, nsd_model):
74         vnffgs = VNFFGInstModel.objects.filter(nsinstid=ns_inst_id)
75         return [{
76             'vnffgInstanceId': vnffg.vnffginstid,
77             'vnfId': self.convert_string_to_list(vnffg.vnflist),
78             'pnfId': self.get_pnf_infos(nsd_model),
79             'virtualLinkId': self.convert_string_to_list(vnffg.vllist),
80             'cpId': self.convert_string_to_list(vnffg.cplist),
81             'nfp': self.convert_string_to_list(vnffg.fplist)} for vnffg in vnffgs]
82
83     @staticmethod
84     def get_pnf_infos(nsd_model):
85         context = json.loads(nsd_model)
86         pnfs = context['pnfs']
87         return [pnf['pnf_id'] for pnf in pnfs]
88
89     @staticmethod
90     def convert_string_to_list(detail_id_string):
91         if not detail_id_string:
92             return None
93         return detail_id_string.split(',')