get ns instance
[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, is_sol=False):
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         return [self.get_single_ns_info(ns_inst, is_sol) for ns_inst in ns_insts]
36
37     def get_single_ns_info(self, ns_inst, is_sol=False):
38         if is_sol:
39             return {
40                 'id': ns_inst.id,
41                 'nsInstanceName': ns_inst.name,
42                 'nsInstanceDescription': ns_inst.description,
43                 'nsdId': ns_inst.nsd_id,
44                 'nsdInvariantId': ns_inst.nsd_invariant_id,
45                 'nsdInfoId': ns_inst.nspackage_id,
46                 'flavourId': ns_inst.flavour_id,
47                 'nsState': ns_inst.status,
48                 # todo 'nsScaleStatus':{}
49                 # todo  'additionalAffinityOrAntiAffinityRule':{}
50                 'vnfInstance': self.get_vnf_infos(ns_inst.id, is_sol),
51                 # todo 'pnfInfo': self.get_pnf_infos(ns_inst.id,is_sol),
52                 'virtualLinkInfo': self.get_vl_infos(ns_inst.id, is_sol),
53                 # todo 'vnffgInfo': self.get_vnffg_infos(ns_inst.id, ns_inst.nsd_model),
54                 # todo  'sapInfo':{},
55                 # todo  nestedNsInstanceId
56             }
57         return {
58             'nsInstanceId': ns_inst.id,
59             'nsName': ns_inst.name,
60             'description': ns_inst.description,
61             'nsdId': ns_inst.nsd_id,
62             'nsdInvariantId': ns_inst.nsd_invariant_id,
63             'vnfInfo': self.get_vnf_infos(ns_inst.id, is_sol),
64             'pnfInfo': self.get_pnf_infos(ns_inst.id),
65             'vlInfo': self.get_vl_infos(ns_inst.id, is_sol),
66             'vnffgInfo': self.get_vnffg_infos(ns_inst.id, ns_inst.nsd_model, is_sol),
67             'nsState': ns_inst.status}
68
69     @staticmethod
70     def get_vnf_infos(ns_inst_id, is_sol):
71         vnfs = NfInstModel.objects.filter(ns_inst_id=ns_inst_id)
72         if is_sol:
73             return [{
74                 'id': vnf.nfinstid,
75                 'vnfInstanceName': vnf.nf_name,
76                 'vnfdId': vnf.template_id,
77                 'vnfProvider': vnf.vendor,
78                 'vnfSoftwareVersion': vnf.version,
79                 'vnfProductName': vnf.nf_name,  # todo
80                 'vnfdVersion': vnf.version,  # todo
81                 'vnfPkgId': vnf.package_id,
82                 'instantiationState': vnf.status
83             } for vnf in vnfs]
84         return [{
85             'vnfInstanceId': vnf.nfinstid,
86             'vnfInstanceName': vnf.nf_name,
87             'vnfProfileId': vnf.vnf_id} for vnf in vnfs]
88
89     def get_vl_infos(self, ns_inst_id, is_sol):
90         vls = VLInstModel.objects.filter(ownertype=OWNER_TYPE.NS, ownerid=ns_inst_id)
91         if is_sol:
92             return [
93                 {
94                     'id': vl.vlinstanceid,
95                     'nsVirtualLinkDescId': vl.vldid,
96                     'nsVirtualLinkProfileId': vl.vldid,
97                     'vlInstanceName': vl.vlinstancename,
98                     'resourceHandle': {
99                         'vimId': vl.vimId,
100                         'resourceId': vl.relatednetworkid,
101                         'vimLevelResourceType': vl.vltype
102                     },
103                     # todo 'linkPort': self.get_cp_infos(vl.vlinstanceid,is_sol),
104                     'networkId': vl.relatednetworkid,
105                     'subNetworkid': vl.relatedsubnetworkid
106                 } for vl in vls]
107
108         return [{
109             'vlInstanceId': vl.vlinstanceid,
110             'vlInstanceName': vl.vlinstancename,
111             'vldId': vl.vldid,
112             'relatedCpInstanceId': self.get_cp_infos(vl.vlinstanceid)} for vl in vls]
113
114     @staticmethod
115     def get_cp_infos(vl_inst_id):
116         cps = CPInstModel.objects.filter(relatedvl__icontains=vl_inst_id)
117         return [{
118             'cpInstanceId': cp.cpinstanceid,
119             'cpInstanceName': cp.cpname,
120             'cpdId': cp.cpdid} for cp in cps]
121
122     def get_vnffg_infos(self, ns_inst_id, nsd_model, is_sol):
123         vnffgs = VNFFGInstModel.objects.filter(nsinstid=ns_inst_id)
124         return [{
125             'vnffgInstanceId': vnffg.vnffginstid,
126             'vnfId': self.convert_string_to_list(vnffg.vnflist),
127             'pnfId': self.get_pnf_ids(nsd_model),
128             'virtualLinkId': self.convert_string_to_list(vnffg.vllist),
129             'cpId': self.convert_string_to_list(vnffg.cplist),
130             'nfp': self.convert_string_to_list(vnffg.fplist)} for vnffg in vnffgs]
131
132     @staticmethod
133     def get_pnf_ids(nsd_model):
134         context = json.loads(nsd_model)
135         pnfs = context['pnfs']
136         return [pnf['pnf_id'] for pnf in pnfs]
137
138     @staticmethod
139     def convert_string_to_list(detail_id_string):
140         if not detail_id_string:
141             return None
142         return detail_id_string.split(',')
143
144     @staticmethod
145     def get_pnf_infos(ns_instance_id):
146         uri = "api/nslcm/v1/pnfs?nsInstanceId=%s" % ns_instance_id
147         ret = restcall.req_by_msb(uri, "GET")
148         if ret[0] == 0:
149             return json.loads(ret[1])
150         else:
151             return []