87a8408a3fe3946cbb45d134d34827a97732d104
[vvp/validation-scripts.git] / ice_validator / preload_vnfapi / vnfapi_generator.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
7 #
8 # Unless otherwise specified, all software contained herein is licensed
9 # under the Apache License, Version 2.0 (the "License");
10 # you may not use this software except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 #             http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 #
21 #
22 #
23 # Unless otherwise specified, all documentation contained herein is licensed
24 # under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 # you may not use this documentation except in compliance with the License.
26 # You may obtain a copy of the License at
27 #
28 #             https://creativecommons.org/licenses/by/4.0/
29 #
30 # Unless required by applicable law or agreed to in writing, documentation
31 # distributed under the License is distributed on an "AS IS" BASIS,
32 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 # See the License for the specific language governing permissions and
34 # limitations under the License.
35 #
36 # ============LICENSE_END============================================
37 #
38 #
39
40 import json
41 import os
42
43 from preload.generator import (
44     get_json_template,
45     get_or_create_template,
46     AbstractPreloadGenerator,
47 )
48
49 THIS_DIR = os.path.dirname(os.path.abspath(__file__))
50 DATA_DIR = os.path.join(THIS_DIR, "vnfapi_data")
51
52
53 def get_or_create_network_template(network_role, vm_networks):
54     """
55     If the network role already exists in vm_networks, then
56     return that otherwise create a blank template and return that
57     """
58     return get_or_create_template(
59         DATA_DIR, "network-role", network_role, vm_networks, "vm-network"
60     )
61
62
63 class VnfApiPreloadGenerator(AbstractPreloadGenerator):
64     @classmethod
65     def supports_output_passing(cls):
66         return False
67
68     @classmethod
69     def format_name(cls):
70         return "VNF-API"
71
72     @classmethod
73     def output_sub_dir(cls):
74         return "vnfapi"
75
76     def generate_module(self, vnf_module, output_dir):
77         preload = get_json_template(DATA_DIR, "preload_template")
78         self._populate(preload, vnf_module)
79         incomplete = "_incomplete" if self.module_incomplete else ""
80         outfile = "{}/{}{}.json".format(output_dir, vnf_module.vnf_name, incomplete)
81         with open(outfile, "w") as f:
82             json.dump(preload, f, indent=4)
83
84     def _populate(self, preload, vnf_module):
85         self._add_vnf_metadata(preload)
86         self._add_availability_zones(preload, vnf_module)
87         self._add_vnf_networks(preload, vnf_module)
88         self._add_vms(preload, vnf_module)
89         self._add_parameters(preload, vnf_module)
90
91     def _add_vnf_metadata(self, preload):
92         vnf_meta = preload["input"]["vnf-topology-information"][
93             "vnf-topology-identifier"
94         ]
95         vnf_meta["vnf-name"] = self.replace("vnf_name")
96         vnf_meta["generic-vnf-type"] = self.replace(
97             "vnf-type",
98             "VALUE FOR: Concatenation of <Service Name>/"
99             "<VF Instance Name> MUST MATCH SDC",
100         )
101         vnf_meta["vnf-type"] = self.replace(
102             "vf-module-model-name", "VALUE FOR: <vfModuleModelName> from CSAR or SDC"
103         )
104
105     def add_floating_ips(self, network_template, network):
106         # only one floating IP is really supported, in the preload model
107         # so for now we'll just use the last one.  We might revisit this
108         # and if multiple floating params exist, then come up with an
109         # approach to pick just one
110         for ip in network.floating_ips:
111             key = "floating-ip" if ip.ip_version == 4 else "floating-ip-v6"
112             network_template[key] = self.replace(ip.param, single=True)
113
114     def add_fixed_ips(self, network_template, port):
115         for ip in port.fixed_ips:
116             if ip.ip_version == 4:
117                 network_template["network-ips"].append(
118                     {"ip-address": self.replace(ip.param, single=True)}
119                 )
120                 network_template["ip-count"] += 1
121             else:
122                 network_template["network-ips-v6"].append(
123                     {"ip-address": self.replace(ip.param, single=True)}
124                 )
125                 network_template["ip-count-ipv6"] += 1
126
127     def _add_availability_zones(self, preload, vnf_module):
128         zones = preload["input"]["vnf-topology-information"]["vnf-assignments"][
129             "availability-zones"
130         ]
131         for zone in vnf_module.availability_zones:
132             zones.append({"availability-zone": self.replace(zone, single=True)})
133
134     def _add_vnf_networks(self, preload, vnf_module):
135         networks = preload["input"]["vnf-topology-information"]["vnf-assignments"][
136             "vnf-networks"
137         ]
138         for network in vnf_module.networks:
139             network_data = {
140                 "network-role": network.network_role,
141                 "network-name": self.replace(
142                     network.name_param,
143                     "VALUE FOR: network name for {}".format(network.name_param),
144                 ),
145             }
146             for subnet in network.subnet_params:
147                 key = "ipv6-subnet-id" if "_v6_" in subnet else "subnet-id"
148                 network_data[key] = subnet
149             networks.append(network_data)
150
151     def _add_vms(self, preload, vnf_module):
152         vm_list = preload["input"]["vnf-topology-information"]["vnf-assignments"][
153             "vnf-vms"
154         ]
155         for vm in vnf_module.virtual_machine_types:
156             vm_template = get_json_template(DATA_DIR, "vm")
157             vm_template["vm-type"] = vm.vm_type
158             vm_template["vm-count"] = vm.vm_count
159             for name in vm.names:
160                 value = self.replace(name, single=True)
161                 vm_template["vm-names"]["vm-name"].append(value)
162             vm_list.append(vm_template)
163             vm_networks = vm_template["vm-networks"]
164             for port in vm.ports:
165                 role = port.network.network_role
166                 network_template = get_or_create_network_template(role, vm_networks)
167                 network_template["network-role"] = role
168                 network_template["network-role-tag"] = role
169                 network_template["use-dhcp"] = "Y" if port.uses_dhcp else "N"
170                 self.add_fixed_ips(network_template, port)
171                 self.add_floating_ips(network_template, port)
172
173     def _add_parameters(self, preload, vnf_module):
174         params = preload["input"]["vnf-topology-information"]["vnf-parameters"]
175         for key, value in vnf_module.preload_parameters.items():
176             params.append(
177                 {
178                     "vnf-parameter-name": key,
179                     "vnf-parameter-value": self.replace(key, value),
180                 }
181             )