summaryrefslogtreecommitdiffstats
path: root/lcm/ns/biz/ns_get.py
blob: 2e572a3d0bd1283f8e3d184dcfb7c67a1fd4f683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Copyright 2016 ZTE Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import logging

from lcm.ns.const import OWNER_TYPE
from lcm.pub.database.models import NSInstModel, NfInstModel, VLInstModel, CPInstModel, VNFFGInstModel

logger = logging.getLogger(__name__)


class GetNSInfoService(object):
    def __init__(self, ns_filter=None):
        self.ns_filter = ns_filter

    def get_ns_info(self):
        ns_insts = None
        if self.ns_filter and "ns_inst_id" in self.ns_filter:
            ns_inst_id = self.ns_filter["ns_inst_id"]
            ns_insts = NSInstModel.objects.filter(id=ns_inst_id)
        else:
            ns_insts = NSInstModel.objects.all()

        return [self.get_single_ns_info(ns_inst) for ns_inst in ns_insts]

    def get_single_ns_info(self, ns_inst):
        return {
            'nsInstanceId': ns_inst.id,
            'nsName': ns_inst.name,
            'description': ns_inst.description,
            'nsdId': ns_inst.nsd_id,
            'nsdInvariantId': ns_inst.nsd_invariant_id,
            'vnfInfo': self.get_vnf_infos(ns_inst.id),
            'vlInfo': self.get_vl_infos(ns_inst.id),
            'vnffgInfo': self.get_vnffg_infos(ns_inst.id, ns_inst.nsd_model),
            'nsState': ns_inst.status}

    @staticmethod
    def get_vnf_infos(ns_inst_id):
        vnfs = NfInstModel.objects.filter(ns_inst_id=ns_inst_id)
        return [{
            'vnfInstanceId': vnf.nfinstid,
            'vnfInstanceName': vnf.nf_name,
            'vnfProfileId': vnf.vnf_id} for vnf in vnfs]

    def get_vl_infos(self, ns_inst_id):
        vls = VLInstModel.objects.filter(ownertype=OWNER_TYPE.NS, ownerid=ns_inst_id)
        return [{
            'vlInstanceId': vl.vlinstanceid,
            'vlInstanceName': vl.vlinstancename,
            'vldId': vl.vldid,
            'relatedCpInstanceId': self.get_cp_infos(vl.vlinstanceid)} for vl in vls]

    @staticmethod
    def get_cp_infos(vl_inst_id):
        cps = CPInstModel.objects.filter(relatedvl__icontains=vl_inst_id)
        return [{
            'cpInstanceId': cp.cpinstanceid,
            'cpInstanceName': cp.cpname,
            'cpdId': cp.cpdid} for cp in cps]

    def get_vnffg_infos(self, ns_inst_id, nsd_model):
        vnffgs = VNFFGInstModel.objects.filter(nsinstid=ns_inst_id)
        return [{
            'vnffgInstanceId': vnffg.vnffginstid,
            'vnfId': self.convert_string_to_list(vnffg.vnflist),
            'pnfId': self.get_pnf_infos(nsd_model),
            'virtualLinkId': self.convert_string_to_list(vnffg.vllist),
            'cpId': self.convert_string_to_list(vnffg.cplist),
            'nfp': self.convert_string_to_list(vnffg.fplist)} for vnffg in vnffgs]

    @staticmethod
    def get_pnf_infos(nsd_model):
        context = json.loads(nsd_model)
        pnfs = context['pnfs']
        return [pnf['pnf_id'] for pnf in pnfs]

    @staticmethod
    def convert_string_to_list(detail_id_string):
        if not detail_id_string:
            return None
        return detail_id_string.split(',')