enhace the ns instance query to support pnfinfo
[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.utils import restcall
19 from lcm.pub.database.models import NSInstModel, NfInstModel, VLInstModel, CPInstModel, VNFFGInstModel
20
21 logger = logging.getLogger(__name__)
22
23
24 class GetNSInfoService(object):
25     def __init__(self, ns_filter=None):
26         self.ns_filter = ns_filter
27
28     def get_ns_info(self):
29         ns_insts = None
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)
33         else:
34             ns_insts = NSInstModel.objects.all()
35
36         return [self.get_single_ns_info(ns_inst) for ns_inst in ns_insts]
37
38     def get_single_ns_info(self, ns_inst):
39         return {
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}
50
51     @staticmethod
52     def get_vnf_infos(ns_inst_id):
53         vnfs = NfInstModel.objects.filter(ns_inst_id=ns_inst_id)
54         return [{
55             'vnfInstanceId': vnf.nfinstid,
56             'vnfInstanceName': vnf.nf_name,
57             'vnfProfileId': vnf.vnf_id} for vnf in vnfs]
58
59     def get_vl_infos(self, ns_inst_id):
60         vls = VLInstModel.objects.filter(ownertype=OWNER_TYPE.NS, ownerid=ns_inst_id)
61         return [{
62             'vlInstanceId': vl.vlinstanceid,
63             'vlInstanceName': vl.vlinstancename,
64             'vldId': vl.vldid,
65             'relatedCpInstanceId': self.get_cp_infos(vl.vlinstanceid)} for vl in vls]
66
67     @staticmethod
68     def get_cp_infos(vl_inst_id):
69         cps = CPInstModel.objects.filter(relatedvl__icontains=vl_inst_id)
70         return [{
71             'cpInstanceId': cp.cpinstanceid,
72             'cpInstanceName': cp.cpname,
73             'cpdId': cp.cpdid} for cp in cps]
74
75     def get_vnffg_infos(self, ns_inst_id, nsd_model):
76         vnffgs = VNFFGInstModel.objects.filter(nsinstid=ns_inst_id)
77         return [{
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]
84
85     @staticmethod
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]
90
91     @staticmethod
92     def convert_string_to_list(detail_id_string):
93         if not detail_id_string:
94             return None
95         return detail_id_string.split(',')
96
97     @staticmethod
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")
101         if ret[0] == 0:
102             return json.loads(ret[1])
103         else:
104             return []