[VVP] Generated completed preload from env files
[vvp/validation-scripts.git] / ice_validator / preload_grapi / grapi_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 import json
38 import os
39
40 from preload.generator import (
41     get_json_template,
42     get_or_create_template,
43     AbstractPreloadGenerator,
44 )
45
46 THIS_DIR = os.path.dirname(os.path.abspath(__file__))
47 DATA_DIR = os.path.join(THIS_DIR, "grapi_data")
48
49
50 def get_or_create_network_template(network, vm_networks):
51     """
52     If the network role already exists in vm_networks, then
53     return that otherwise create a blank template and return that
54     """
55     return get_or_create_template(
56         DATA_DIR, "network-role", network, vm_networks, "vm-network"
57     )
58
59
60 class GrApiPreloadGenerator(AbstractPreloadGenerator):
61     @classmethod
62     def supports_output_passing(cls):
63         return True
64
65     @classmethod
66     def format_name(cls):
67         return "GR-API"
68
69     @classmethod
70     def output_sub_dir(cls):
71         return "grapi"
72
73     def generate_module(self, vnf_module, output_dir):
74         template = get_json_template(DATA_DIR, "preload_template")
75         self._populate(template, vnf_module)
76         vnf_name = vnf_module.vnf_name
77         outfile = "{}/{}.json".format(output_dir, vnf_name)
78         with open(outfile, "w") as f:
79             json.dump(template, f, indent=4)
80
81     def add_floating_ips(self, network_template, floating_ips):
82         for ip in floating_ips:
83             key = "floating-ip-v4" if ip.ip_version == 4 else "floating-ip-v6"
84             ips = network_template["floating-ips"][key]
85             value = self.replace(ip.param, single=True)
86             if value not in ips:
87                 ips.append(value)
88
89     def add_fixed_ips(self, network_template, fixed_ips, uses_dhcp):
90         items = network_template["network-information-items"][
91             "network-information-item"
92         ]
93         ipv4s = next(item for item in items if item["ip-version"] == "4")
94         ipv6s = next(item for item in items if item["ip-version"] == "6")
95         if uses_dhcp:
96             ipv4s["use-dhcp"] = "Y"
97             ipv6s["use-dhcp"] = "Y"
98         for ip in fixed_ips:
99             target = ipv4s if ip.ip_version == 4 else ipv6s
100             ips = target["network-ips"]["network-ip"]
101             if ip.param not in ips:
102                 ips.append(self.replace(ip.param, single=True))
103             target["ip-count"] += 1
104
105     def _populate(self, preload, vnf_module):
106         self._add_vnf_metadata(preload)
107         self._add_vms(preload, vnf_module)
108         self._add_availability_zones(preload, vnf_module)
109         self._add_parameters(preload, vnf_module)
110         self._add_vnf_networks(preload, vnf_module)
111
112     def _add_vms(self, preload, vnf_module):
113         vms = preload["input"]["preload-vf-module-topology-information"][
114             "vf-module-topology"
115         ]["vf-module-assignments"]["vms"]["vm"]
116         for vm in vnf_module.virtual_machine_types:
117             vm_template = get_json_template(DATA_DIR, "vm")
118             vms.append(vm_template)
119             vm_template["vm-type"] = vm.vm_type
120             for name in vm.names:
121                 value = self.replace(name, single=True)
122                 vm_template["vm-names"]["vm-name"].append(value)
123             vm_template["vm-count"] = vm.vm_count
124             vm_networks = vm_template["vm-networks"]["vm-network"]
125             for port in vm.ports:
126                 role = port.network.network_role
127                 network_template = get_or_create_network_template(role, vm_networks)
128                 network_template["network-role"] = role
129                 self.add_fixed_ips(network_template, port.fixed_ips, port.uses_dhcp)
130                 self.add_floating_ips(network_template, port.floating_ips)
131
132     def _add_availability_zones(self, preload, vnf_module):
133         zones = preload["input"]["preload-vf-module-topology-information"][
134             "vnf-resource-assignments"
135         ]["availability-zones"]["availability-zone"]
136         for zone in vnf_module.availability_zones:
137             value = self.replace(zone, single=True)
138             zones.append(value)
139
140     def _add_parameters(self, preload, vnf_module):
141         params = [
142             {"name": key, "value": self.replace(key, value)}
143             for key, value in vnf_module.preload_parameters.items()
144         ]
145         preload["input"]["preload-vf-module-topology-information"][
146             "vf-module-topology"
147         ]["vf-module-parameters"]["param"].extend(params)
148
149     def _add_vnf_networks(self, preload, vnf_module):
150         networks = preload["input"]["preload-vf-module-topology-information"][
151             "vnf-resource-assignments"
152         ]["vnf-networks"]["vnf-network"]
153         for network in vnf_module.networks:
154             network_data = {
155                 "network-role": network.network_role,
156                 "network-name": self.replace(
157                     network.name_param,
158                     "VALUE FOR: network name of {}".format(network.name_param),
159                 ),
160             }
161             if network.subnet_params:
162                 network_data["subnets-data"] = {"subnet-data": []}
163                 subnet_data = network_data["subnets-data"]["subnet-data"]
164                 for subnet_param in network.subnet_params:
165                     subnet_data.append(
166                         {"subnet-id": self.replace(subnet_param, single=True)}
167                     )
168             networks.append(network_data)
169
170     def _add_vnf_metadata(self, preload):
171         topology = preload["input"]["preload-vf-module-topology-information"]
172         vnf_meta = topology["vnf-topology-identifier-structure"]
173         vnf_meta["vnf-name"] = self.replace("vnf_name")
174         vnf_meta["vnf-type"] = self.replace(
175             "vnf-type",
176             "VALUE FOR: Concatenation of <Service Name>/"
177             "<VF Instance Name> MUST MATCH SDC",
178         )
179         module_meta = topology["vf-module-topology"]["vf-module-topology-identifier"]
180         module_meta["vf-module-name"] = self.replace("vf_module_name")
181         module_meta["vf-module-type"] = self.replace(
182             "vf-module-model-name", "VALUE FOR: <vfModuleModelName> from CSAR or SDC"
183         )