2 # ============LICENSE_START=======================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2017 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 .network_roles import get_network_role_from_port
41 from .vm_types import get_vm_type_for_nova_server
45 def is_valid_ip_address(
46 ip_address, vm_type, network_role, port_property, parameter_type
49 Check the ip_address to make sure it is properly formatted and
50 also contains {vm_type} and {network_role}
55 "allowed_address_pairs",
58 re.compile(r"(.+?)_int_(.+?)_floating_v6_ip"),
61 "allowed_address_pairs",
64 re.compile(r"(.+?)_int_(.+?)_floating_ip"),
67 "allowed_address_pairs",
70 re.compile(r"(.+?)_floating_v6_ip"),
73 "allowed_address_pairs",
76 re.compile(r"(.+?)_floating_ip"),
79 "allowed_address_pairs",
82 re.compile(r"(.+?)_int_(.+?)_v6_ip_\d+"),
85 "allowed_address_pairs",
88 re.compile(r"(.+?)_int_(.+?)_ip_\d+"),
90 ["allowed_address_pairs", "string", "external", re.compile(r"(.+?)_v6_ip_\d+")],
91 ["allowed_address_pairs", "string", "external", re.compile(r"(.+?)_ip_\d+")],
93 "allowed_address_pairs",
94 "comma_delimited_list",
96 re.compile(r"(.+?)_int_(.+?)_v6_ips"),
99 "allowed_address_pairs",
100 "comma_delimited_list",
102 re.compile(r"(.+?)_int_(.+?)_ips"),
105 "allowed_address_pairs",
106 "comma_delimited_list",
108 re.compile(r"(.+?)_v6_ips"),
111 "allowed_address_pairs",
112 "comma_delimited_list",
114 re.compile(r"(.+?)_ips"),
116 ["fixed_ips", "string", "internal", re.compile(r"(.+?)_int_(.+?)_v6_ip_\d+")],
117 ["fixed_ips", "string", "internal", re.compile(r"(.+?)_int_(.+?)_ip_\d+")],
118 ["fixed_ips", "string", "external", re.compile(r"(.+?)_v6_ip_\d+")],
119 ["fixed_ips", "string", "external", re.compile(r"(.+?)_ip_\d+")],
122 "comma_delimited_list",
124 re.compile(r"(.+?)_int_(.+?)_v6_ips"),
128 "comma_delimited_list",
130 re.compile(r"(.+?)_int_(.+?)_ips"),
132 ["fixed_ips", "comma_delimited_list", "external", re.compile(r"(.+?)_v6_ips")],
133 ["fixed_ips", "comma_delimited_list", "external", re.compile(r"(.+?)_ips")],
136 for v3 in allowed_formats:
137 if v3[1] != parameter_type:
139 if v3[0] != port_property:
141 # check if pattern matches
142 m = v3[3].match(ip_address)
144 if v3[2] == "internal" and len(m.groups()) > 1:
145 return m.group(1) == vm_type and m.group(2) == network_role
146 elif v3[2] == "external" and len(m.groups()) > 0:
147 return m.group(1) == vm_type + "_" + network_role
152 def get_invalid_ip_addresses(resources, port_property, parameters):
154 Get a list of valid ip addresses for a heat resources section
156 invalid_ip_addresses = []
158 for k, v in resources.items():
159 if not isinstance(v, dict):
163 if v["type"] not in "OS::Nova::Server":
165 if "properties" not in v:
167 if "networks" not in v["properties"]:
172 vm_type = get_vm_type_for_nova_server(v)
176 # get all ports associated with the nova server
177 properties = v["properties"]
178 for network in properties["networks"]:
179 for k3, v3 in network.items():
182 if not isinstance(v3, dict):
185 if "get_resource" in v3:
186 port_id = v3["get_resource"]
187 if not resources[port_id]:
189 port_resource = resources[port_id]
193 network_role = get_network_role_from_port(port_resource)
197 for k1, v1 in port_resource["properties"].items():
198 if k1 != port_property:
201 if "ip_address" not in v2:
203 if "get_param" not in v2["ip_address"]:
205 ip_address = v2["ip_address"]["get_param"]
207 if isinstance(ip_address, list):
208 ip_address = ip_address[0]
210 if ip_address not in parameters:
213 parameter_type = parameters[ip_address].get("type")
214 if not parameter_type:
217 valid_ip_address = is_valid_ip_address(
225 if not valid_ip_address:
226 invalid_ip_addresses.append(ip_address)
228 return invalid_ip_addresses
231 def get_list_of_ports_attached_to_nova_server(nova_server):
232 networks_list = nova_server.get("properties", {}).get("networks")
236 for network in networks_list:
237 network_prop = network.get("port")
239 pid = network_prop.get("get_param")
241 pid = network_prop.get("get_resource")