2 # ============LICENSE_START====================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 # https://creativecommons.org/licenses/by/4.0/
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.
36 # ============LICENSE_END============================================
40 from preload.generator import (
42 get_or_create_template,
43 AbstractPreloadGenerator,
46 THIS_DIR = os.path.dirname(os.path.abspath(__file__))
47 DATA_DIR = os.path.join(THIS_DIR, "grapi_data")
50 def get_or_create_network_template(network, vm_networks):
52 If the network role already exists in vm_networks, then
53 return that otherwise create a blank template and return that
55 return get_or_create_template(
56 DATA_DIR, "network-role", network, vm_networks, "vm-network"
60 class GrApiPreloadGenerator(AbstractPreloadGenerator):
62 def supports_output_passing(cls):
70 def output_sub_dir(cls):
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 incomplete = "_incomplete" if self.module_incomplete else ""
78 outfile = "{}/{}{}.json".format(output_dir, vnf_name, incomplete)
79 with open(outfile, "w") as f:
80 json.dump(template, f, indent=4)
82 def add_floating_ips(self, network_template, floating_ips):
83 for ip in floating_ips:
84 key = "floating-ip-v4" if ip.ip_version == 4 else "floating-ip-v6"
85 ips = network_template["floating-ips"][key]
86 value = self.replace(ip.param, single=True)
90 def add_fixed_ips(self, network_template, fixed_ips, uses_dhcp):
91 items = network_template["network-information-items"][
92 "network-information-item"
94 ipv4s = next(item for item in items if item["ip-version"] == "4")
95 ipv6s = next(item for item in items if item["ip-version"] == "6")
97 ipv4s["use-dhcp"] = "Y"
98 ipv6s["use-dhcp"] = "Y"
100 target = ipv4s if ip.ip_version == 4 else ipv6s
101 ips = target["network-ips"]["network-ip"]
102 if ip.param not in ips:
103 ips.append(self.replace(ip.param, single=True))
104 target["ip-count"] += 1
106 def _populate(self, preload, vnf_module):
107 self._add_vnf_metadata(preload)
108 self._add_vms(preload, vnf_module)
109 self._add_availability_zones(preload, vnf_module)
110 self._add_parameters(preload, vnf_module)
111 self._add_vnf_networks(preload, vnf_module)
113 def _add_vms(self, preload, vnf_module):
114 vms = preload["input"]["preload-vf-module-topology-information"][
116 ]["vf-module-assignments"]["vms"]["vm"]
117 for vm in vnf_module.virtual_machine_types:
118 vm_template = get_json_template(DATA_DIR, "vm")
119 vms.append(vm_template)
120 vm_template["vm-type"] = vm.vm_type
121 for name in vm.names:
122 value = self.replace(name, single=True)
123 vm_template["vm-names"]["vm-name"].append(value)
124 vm_template["vm-count"] = vm.vm_count
125 vm_networks = vm_template["vm-networks"]["vm-network"]
126 for port in vm.ports:
127 role = port.network.network_role
128 network_template = get_or_create_network_template(role, vm_networks)
129 network_template["network-role"] = role
130 self.add_fixed_ips(network_template, port.fixed_ips, port.uses_dhcp)
131 self.add_floating_ips(network_template, port.floating_ips)
133 def _add_availability_zones(self, preload, vnf_module):
134 zones = preload["input"]["preload-vf-module-topology-information"][
135 "vnf-resource-assignments"
136 ]["availability-zones"]["availability-zone"]
137 for zone in vnf_module.availability_zones:
138 value = self.replace(zone, single=True)
141 def _add_parameters(self, preload, vnf_module):
143 {"name": key, "value": self.replace(key, value)}
144 for key, value in vnf_module.preload_parameters.items()
146 preload["input"]["preload-vf-module-topology-information"][
148 ]["vf-module-parameters"]["param"].extend(params)
150 def _add_vnf_networks(self, preload, vnf_module):
151 networks = preload["input"]["preload-vf-module-topology-information"][
152 "vnf-resource-assignments"
153 ]["vnf-networks"]["vnf-network"]
154 for network in vnf_module.networks:
156 "network-role": network.network_role,
157 "network-name": self.replace(
159 "VALUE FOR: network name of {}".format(network.name_param),
162 if network.subnet_params:
163 network_data["subnets-data"] = {"subnet-data": []}
164 subnet_data = network_data["subnets-data"]["subnet-data"]
165 for subnet_param in network.subnet_params:
167 {"subnet-id": self.replace(subnet_param, single=True)}
169 networks.append(network_data)
171 def _add_vnf_metadata(self, preload):
172 topology = preload["input"]["preload-vf-module-topology-information"]
173 vnf_meta = topology["vnf-topology-identifier-structure"]
174 vnf_meta["vnf-name"] = self.replace("vnf_name")
175 vnf_meta["vnf-type"] = self.replace(
177 "VALUE FOR: Concatenation of <Service Name>/"
178 "<VF Instance Name> MUST MATCH SDC",
180 module_meta = topology["vf-module-topology"]["vf-module-topology-identifier"]
181 module_meta["vf-module-name"] = self.replace("vf_module_name")
182 module_meta["vf-module-type"] = self.replace(
183 "vf-module-model-name", "VALUE FOR: <vfModuleModelName> from CSAR or SDC"