update version of lcm
[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             'vnfInfoId': self.get_vnf_infos(ns_inst.id),
44             'vlInfo': self.get_vl_infos(ns_inst.id),
45             'vnffgInfo': self.get_vnffg_infos(ns_inst.id, ns_inst.nsd_model),
46             'nsState': ns_inst.status}
47
48     @staticmethod
49     def get_vnf_infos(ns_inst_id):
50         vnfs = NfInstModel.objects.filter(ns_inst_id=ns_inst_id)
51         return [{
52             'vnfInstanceId': vnf.nfinstid,
53             'vnfInstanceName': vnf.nf_name,
54             'vnfProfileId': vnf.vnf_id} for vnf in vnfs]
55
56     def get_vl_infos(self, ns_inst_id):
57         vls = VLInstModel.objects.filter(ownertype=OWNER_TYPE.NS, ownerid=ns_inst_id)
58         return [{
59             'vlInstanceId': vl.vlinstanceid,
60             'vlInstanceName': vl.vlinstancename,
61             'vldId': vl.vldid,
62             'relatedCpInstanceId': self.get_cp_infos(vl.vlinstanceid)} for vl in vls]
63
64     @staticmethod
65     def get_cp_infos(vl_inst_id):
66         cps = CPInstModel.objects.filter(relatedvl__icontains=vl_inst_id)
67         return [{
68             'cpInstanceId': cp.cpinstanceid,
69             'cpInstanceName': cp.cpname,
70             'cpdId': cp.cpdid} for cp in cps]
71
72     def get_vnffg_infos(self, ns_inst_id, nsd_model):
73         vnffgs = VNFFGInstModel.objects.filter(nsinstid=ns_inst_id)
74         return [{
75             'vnffgInstanceId': vnffg.vnffginstid,
76             'vnfId': self.convert_string_to_list(vnffg.vnflist),
77             'pnfId': self.get_pnf_infos(nsd_model),
78             'virtualLinkId': self.convert_string_to_list(vnffg.vllist),
79             'cpId': self.convert_string_to_list(vnffg.cplist),
80             'nfp': self.convert_string_to_list(vnffg.fplist)} for vnffg in vnffgs]
81
82     @staticmethod
83     def get_pnf_infos(nsd_model):
84         context = json.loads(nsd_model)
85         pnfs = context['pnfs']
86         return [pnf['pnf_id'] for pnf in pnfs]
87
88     @staticmethod
89     def convert_string_to_list(detail_id_string):
90         if not detail_id_string:
91             return None
92         return detail_id_string.split(',')