genericparser seed code
[modeling/etsicatalog.git] / genericparser / pub / utils / toscaparsers / servicemodel.py
1 # Copyright 2018 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
15 import functools
16 import logging
17 from genericparser.pub.utils.toscaparsers.const import NS_METADATA_SECTIONS, PNF_METADATA_SECTIONS, VNF_SECTIONS, PNF_SECTIONS, VL_SECTIONS
18 from genericparser.pub.utils.toscaparsers.basemodel import BaseInfoModel
19
20 logger = logging.getLogger(__name__)
21
22 SDC_SERVICE_SECTIONS = (SERVICE_TYPE, SRV_DESCRIPTION) = (
23     'org.openecomp.resource.abstract.nodes.service', 'description')
24
25 SDC_SERVICE_METADATA_SECTIONS = (SRV_UUID, SRV_INVARIANTUUID, SRV_NAME) = (
26     'UUID', 'invariantUUID', 'name')
27
28 SDC_VL = (VL_TYPE) = ('tosca.nodes.nfv.ext.zte.VL')
29 SDC_VL_SECTIONS = (VL_ID, VL_METADATA, VL_PROPERTIES, VL_DESCRIPTION) = \
30     ("name", "metadata", "properties", "description")
31
32 SDC_VF = (VF_TYPE, VF_UUID) = \
33     ('org.openecomp.resource.abstract.nodes.VF', 'UUID')
34 SDC_VF_SECTIONS = (VF_ID, VF_METADATA, VF_PROPERTIES, VF_DESCRIPTION) = \
35     ("name", "metadata", "properties", "description")
36
37 SDC_PNF = (PNF_TYPE) = \
38     ('org.openecomp.resource.abstract.nodes.PNF')
39 SDC_PNF_METADATA_SECTIONS = (SDC_PNF_UUID, SDC_PNF_INVARIANTUUID, SDC_PNF_NAME, SDC_PNF_METADATA_DESCRIPTION, SDC_PNF_VERSION) = \
40     ("UUID", "invariantUUID", "name", "description", "version")
41 SDC_PNF_SECTIONS = (SDC_PNF_ID, SDC_PNF_METADATA, SDC_PNF_PROPERTIES, SDC_PNF_DESCRIPTION) = \
42     ("name", "metadata", "properties", "description")
43
44 SERVICE_RELATIONSHIPS = [["tosca.relationships.network.LinksTo", "tosca.relationships.nfv.VirtualLinksTo", "tosca.capabilities.nfv.VirtualLinkable", "tosca.relationships.DependsOn"], []]
45
46
47 class SdcServiceModel(BaseInfoModel):
48
49     def __init__(self, tosca):
50         super(SdcServiceModel, self).__init__(tosca=tosca)
51
52     def parseModel(self, tosca):
53         self.metadata = self._buildServiceMetadata(tosca)
54         self.ns = self._build_ns(tosca)
55         self.inputs = self.buildInputs(tosca)
56         if hasattr(tosca, 'nodetemplates'):
57             nodeTemplates = map(functools.partial(self.buildNode, tosca=tosca), tosca.nodetemplates)
58             types = tosca.topology_template.custom_defs
59             self.basepath = self.get_base_path(tosca)
60             self.vnfs = self._get_all_vnf(nodeTemplates, types)
61             self.pnfs = self._get_all_pnf(nodeTemplates, types)
62             self.vls = self._get_all_vl(nodeTemplates, types)
63             self.graph = self.get_deploy_graph(tosca, SERVICE_RELATIONSHIPS)
64
65     def _buildServiceMetadata(self, tosca):
66         """ SDC service Meta Format
67          invariantUUID: e2618ee1 - a29a - 44c4 - a52a - b718fe1269f4
68          UUID: 2362d14a - 115f - 4a2b - b449 - e2f93c0b7c89
69          name: demoVLB
70          description: catalogservicedescription
71          type: Service
72          category: NetworkL1 - 3
73          serviceType: ''
74          serviceRole: ''
75          serviceEcompNaming: true
76          ecompGeneratedNaming: true
77          namingPolicy: ''
78         """
79         metadata_temp = self.buildMetadata(tosca)
80         metadata = {}
81         return self.setTargetValues(metadata, NS_METADATA_SECTIONS, metadata_temp, SDC_SERVICE_METADATA_SECTIONS)
82
83     def _get_all_vnf(self, nodeTemplates, node_types):
84         """  SDC Resource Metadata
85         invariantUUID: 9ed46ddc-8eb7-4cb0-a1b6-04136c921af4
86         UUID: b56ba35d-45fb-41e3-b6b8-b4f66917baa1
87         customizationUUID: af0a6e64-967b-476b-87bc-959dcf59c305
88         version: '1.0'
89         name: b7d2fceb-dd11-43cd-a3fa
90         description: vendor software product
91         type: VF
92         category: Generic
93         subcategory: Abstract
94         resourceVendor: b9d9f9f7-7994-4f0d-8104
95         resourceVendorRelease: '1.0'
96         resourceVendorModelNumber: ''
97         """
98         vnfs = []
99         for node in nodeTemplates:
100             if self.isNodeTypeX(node, node_types, VF_TYPE):
101                 vnf = {}
102                 self.setTargetValues(vnf, VNF_SECTIONS, node, SDC_VF_SECTIONS)
103                 if not vnf['properties'].get('id', None) and node['metadata']:
104                     vnf['properties']['id'] = node['metadata'].get('UUID', None)
105                 vnf['properties']['vnfm_info'] = vnf['properties'].get('nf_type', None)
106                 vnf['dependencies'] = self._get_networks(node, node_types)
107                 vnf['networks'] = self._get_networks(node, node_types)
108                 vnfs.append(vnf)
109         return vnfs
110
111     def _get_all_pnf(self, nodeTemplates, node_types):
112         pnfs = []
113         for node in nodeTemplates:
114             if self.isNodeTypeX(node, node_types, PNF_TYPE):
115                 pnf = {}
116                 self.setTargetValues(pnf, PNF_SECTIONS, node, SDC_PNF_SECTIONS)
117                 self.setTargetValues(pnf['properties'], PNF_METADATA_SECTIONS, node['metadata'], SDC_PNF_METADATA_SECTIONS)
118                 pnf['networks'] = self._get_networks(node, node_types)
119                 pnfs.append(pnf)
120         return pnfs
121
122     def _get_all_vl(self, nodeTemplates, node_types):
123         vls = []
124         for node in nodeTemplates:
125             if self.isNodeTypeX(node, node_types, VL_TYPE):
126                 vl = {}
127                 self.setTargetValues(vl, VL_SECTIONS, node, SDC_VL_SECTIONS)
128                 vl_profile = {}
129                 if 'segmentation_id' in vl['properties']:
130                     vl_profile['segmentationId'] = vl['properties'].get('segmentation_id')
131                 if 'network_name' in vl['properties']:
132                     vl_profile['networkName'] = vl['properties'].get('network_name')
133                 if 'cidr' in vl['properties']:
134                     vl_profile['cidr'] = vl['properties'].get('cidr')
135                 if 'network_name' in vl['properties']:
136                     vl_profile['networkName'] = vl['properties'].get('network_name')
137                 if 'start_ip' in vl['properties']:
138                     vl_profile['startIp'] = vl['properties'].get('start_ip', '')
139                 if 'end_ip' in vl['properties']:
140                     vl_profile['endIp'] = vl['properties'].get('end_ip', '')
141                 if 'gateway_ip' in vl['properties']:
142                     vl_profile['gatewayIp'] = vl['properties'].get('gateway_ip', '')
143                 if 'physical_network' in vl['properties']:
144                     vl_profile['physicalNetwork'] = vl['properties'].get('physical_network', '')
145                 if 'network_type' in vl['properties']:
146                     vl_profile['networkType'] = vl['properties'].get('network_type', '')
147                 if 'dhcp_enabled' in vl['properties']:
148                     vl_profile['dhcpEnabled'] = vl['properties'].get('dhcp_enabled', '')
149                 if 'vlan_transparent' in vl['properties']:
150                     vl_profile['vlanTransparent'] = vl['properties'].get('vlan_transparent', '')
151                 if 'mtu' in vl['properties']:
152                     vl_profile['mtu'] = vl['properties'].get('mtu', '')
153                 if 'ip_version' in vl['properties']:
154                     vl_profile['ip_version'] = vl['properties'].get('ip_version', '')
155                 if 'dns_nameservers' in vl['properties']:
156                     vl_profile['dns_nameservers'] = vl['properties'].get('dns_nameservers', [])
157                 if 'host_routes' in vl['properties']:
158                     vl_profile['host_routes'] = vl['properties'].get('host_routes', [])
159                 if 'network_id' in vl['properties']:
160                     vl_profile['network_id'] = vl['properties'].get('network_id', '')
161                 vl['properties']['vl_profile'] = vl_profile
162                 vls.append(vl)
163         return vls
164
165     def _get_networks(self, node, node_types):
166         rets = []
167         if 'requirements' in node and self.isNodeTypeX(node, node_types, VF_TYPE):
168             for item in node['requirements']:
169                 for key, value in item.items():
170                     rets.append({"key_name": key, "vl_id": self.get_requirement_node_name(value)})
171         return rets
172
173     def _build_ns(self, tosca):
174         ns = self.get_substitution_mappings(tosca)
175         properties = ns.get("properties", {})
176         metadata = ns.get("metadata", {})
177         if properties.get("descriptor_id", "") == "":
178             descriptor_id = metadata.get(SRV_UUID, "")
179             properties["descriptor_id"] = descriptor_id
180         properties["verison"] = ""
181         properties["designer"] = ""
182         if properties.get("name", "") == "":
183             template_name = metadata.get(SRV_NAME, "")
184             properties["name"] = template_name
185         if properties.get("invariant_id", "") == "":
186             nsd_invariant_id = metadata.get(SRV_INVARIANTUUID, "")
187             properties["invariant_id"] = nsd_invariant_id
188         return ns