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============================================
38 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
41 from .network_roles import get_network_role_from_port
42 from .vm_types import get_vm_type_for_nova_server
46 def is_valid_ip_address(
47 ip_address, vm_type, network_role, port_property, parameter_type
50 Check the ip_address to make sure it is properly formatted and
51 also contains {vm_type} and {network_role}
56 "allowed_address_pairs",
59 re.compile(r"(.+?)_int_(.+?)_floating_v6_ip"),
62 "allowed_address_pairs",
65 re.compile(r"(.+?)_int_(.+?)_floating_ip"),
68 "allowed_address_pairs",
71 re.compile(r"(.+?)_floating_v6_ip"),
74 "allowed_address_pairs",
77 re.compile(r"(.+?)_floating_ip"),
80 "allowed_address_pairs",
83 re.compile(r"(.+?)_int_(.+?)_v6_ip_\d+"),
86 "allowed_address_pairs",
89 re.compile(r"(.+?)_int_(.+?)_ip_\d+"),
91 ["allowed_address_pairs", "string", "external", re.compile(r"(.+?)_v6_ip_\d+")],
92 ["allowed_address_pairs", "string", "external", re.compile(r"(.+?)_ip_\d+")],
94 "allowed_address_pairs",
95 "comma_delimited_list",
97 re.compile(r"(.+?)_int_(.+?)_v6_ips"),
100 "allowed_address_pairs",
101 "comma_delimited_list",
103 re.compile(r"(.+?)_int_(.+?)_ips"),
106 "allowed_address_pairs",
107 "comma_delimited_list",
109 re.compile(r"(.+?)_v6_ips"),
112 "allowed_address_pairs",
113 "comma_delimited_list",
115 re.compile(r"(.+?)_ips"),
117 ["fixed_ips", "string", "internal", re.compile(r"(.+?)_int_(.+?)_v6_ip_\d+")],
118 ["fixed_ips", "string", "internal", re.compile(r"(.+?)_int_(.+?)_ip_\d+")],
119 ["fixed_ips", "string", "external", re.compile(r"(.+?)_v6_ip_\d+")],
120 ["fixed_ips", "string", "external", re.compile(r"(.+?)_ip_\d+")],
123 "comma_delimited_list",
125 re.compile(r"(.+?)_int_(.+?)_v6_ips"),
129 "comma_delimited_list",
131 re.compile(r"(.+?)_int_(.+?)_ips"),
133 ["fixed_ips", "comma_delimited_list", "external", re.compile(r"(.+?)_v6_ips")],
134 ["fixed_ips", "comma_delimited_list", "external", re.compile(r"(.+?)_ips")],
137 for v3 in allowed_formats:
138 if v3[1] != parameter_type:
140 if v3[0] != port_property:
142 # check if pattern matches
143 m = v3[3].match(ip_address)
145 if v3[2] == "internal" and len(m.groups()) > 1:
146 return m.group(1) == vm_type and m.group(2) == network_role
147 elif v3[2] == "external" and len(m.groups()) > 0:
148 return m.group(1) == vm_type + "_" + network_role
153 def get_invalid_ip_addresses(resources, port_property, parameters):
155 Get a list of valid ip addresses for a heat resources section
157 invalid_ip_addresses = []
159 for k, v in resources.items():
160 if not isinstance(v, dict):
164 if v["type"] not in "OS::Nova::Server":
166 if "properties" not in v:
168 if "networks" not in v["properties"]:
173 vm_type = get_vm_type_for_nova_server(v)
177 # get all ports associated with the nova server
178 properties = v["properties"]
179 for network in properties["networks"]:
180 for k3, v3 in network.items():
183 if not isinstance(v3, dict):
186 if "get_resource" in v3:
187 port_id = v3["get_resource"]
188 if not resources[port_id]:
190 port_resource = resources[port_id]
194 network_role = get_network_role_from_port(port_resource)
198 for k1, v1 in port_resource["properties"].items():
199 if k1 != port_property:
202 if "ip_address" not in v2:
204 if "get_param" not in v2["ip_address"]:
206 ip_address = v2["ip_address"]["get_param"]
208 if isinstance(ip_address, list):
209 ip_address = ip_address[0]
211 if ip_address not in parameters:
214 parameter_type = parameters[ip_address].get("type")
215 if not parameter_type:
218 valid_ip_address = is_valid_ip_address(
226 if not valid_ip_address:
227 invalid_ip_addresses.append(ip_address)
229 return invalid_ip_addresses
232 def get_list_of_ports_attached_to_nova_server(nova_server):
233 networks_list = nova_server.get("properties", {}).get("networks")
237 for network in networks_list:
238 network_prop = network.get("port")
240 pid = network_prop.get("get_param")
242 pid = network_prop.get("get_resource")