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============================================
44 AbstractPreloadGenerator,
46 get_or_create_template,
50 THIS_DIR = os.path.dirname(os.path.abspath(__file__))
51 DATA_DIR = os.path.join(THIS_DIR, "vnfapi_data")
54 def add_fixed_ips(network_template, port):
55 for ip in port.fixed_ips:
56 if ip.ip_version == 4:
57 network_template["network-ips"].append({"ip-address": replace(ip.param)})
58 network_template["ip-count"] += 1
60 network_template["network-ips-v6"].append({"ip-address": replace(ip.param)})
61 network_template["ip-count-ipv6"] += 1
64 def add_floating_ips(network_template, network):
65 # only one floating IP is really supported, in the preload model
66 # so for now we'll just use the last one. We might revisit this
67 # and if multiple floating params exist, then come up with an
68 # approach to pick just one
69 for ip in network.floating_ips:
70 key = "floating-ip" if ip.ip_version == 4 else "floating-ip-v6"
71 network_template[key] = replace(ip.param)
74 def get_or_create_network_template(network_role, vm_networks):
76 If the network role already exists in vm_networks, then
77 return that otherwise create a blank template and return that
79 return get_or_create_template(
80 DATA_DIR, "network-role", network_role, vm_networks, "vm-network"
84 class VnfApiPreloadGenerator(AbstractPreloadGenerator):
86 def supports_output_passing(cls):
94 def output_sub_dir(cls):
97 def generate_module(self, vnf_module):
98 preload = get_json_template(DATA_DIR, "preload_template")
99 self._populate(preload, vnf_module)
100 outfile = "{}/{}.json".format(self.output_dir, vnf_module.vnf_name)
101 with open(outfile, "w") as f:
102 json.dump(preload, f, indent=4)
104 def _populate(self, preload, vnf_module):
105 self._add_availability_zones(preload, vnf_module)
106 self._add_vnf_networks(preload, vnf_module)
107 self._add_vms(preload, vnf_module)
108 self._add_parameters(preload, vnf_module)
111 def _add_availability_zones(preload, vnf_module):
112 zones = preload["input"]["vnf-topology-information"]["vnf-assignments"][
115 for zone in vnf_module.availability_zones:
116 zones.append({"availability-zone": replace(zone)})
119 def _add_vnf_networks(preload, vnf_module):
120 networks = preload["input"]["vnf-topology-information"]["vnf-assignments"][
123 for network in vnf_module.networks:
125 "network-role": network.network_role,
126 "network-name": replace(
127 "network name for {}".format(network.name_param)
130 for subnet in network.subnet_params:
131 key = "ipv6-subnet-id" if "_v6_" in subnet else "subnet-id"
132 network_data[key] = subnet
133 networks.append(network_data)
136 def _add_vms(preload, vnf_module):
137 vm_list = preload["input"]["vnf-topology-information"]["vnf-assignments"][
140 for vm in vnf_module.virtual_machine_types:
141 vm_template = get_json_template(DATA_DIR, "vm")
142 vm_template["vm-type"] = vm.vm_type
143 vm_template["vm-count"] = vm.vm_count
144 vm_template["vm-names"]["vm-name"].extend(map(replace, vm.names))
145 vm_list.append(vm_template)
146 vm_networks = vm_template["vm-networks"]
147 for port in vm.ports:
148 role = port.network.network_role
149 network_template = get_or_create_network_template(role, vm_networks)
150 network_template["network-role"] = role
151 network_template["network-role-tag"] = role
152 network_template["use-dhcp"] = "Y" if port.uses_dhcp else "N"
153 add_fixed_ips(network_template, port)
154 add_floating_ips(network_template, port)
157 def _add_parameters(preload, vnf_module):
158 params = preload["input"]["vnf-topology-information"]["vnf-parameters"]
159 for key, value in vnf_module.preload_parameters.items():
160 params.append({"vnf-parameter-name": key, "vnf-parameter-value": value})