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============================================
45 from tests import cached_yaml as yaml
46 from tests.helpers import load_yaml, get_param
47 from .utils import nested_dict
51 # key = pattern, value = regex compiled from pattern
55 def _get_regex(pattern):
56 """Return a compiled version of pattern.
57 Keep result in _REGEX_CACHE to avoid re-compiling.
59 regex = _REGEX_CACHE.get(pattern, None)
61 regex = re.compile(pattern)
62 _REGEX_CACHE[pattern] = regex
66 class Hashabledict(dict):
68 dicts with the same keys and whose keys have the same values
69 are assigned the same hash.
73 return hash((frozenset(self), frozenset(self.values())))
76 class HeatProcessor(object):
77 """base class for xxxx::xxxx::xxxx processors
80 resource_type = None # string 'xxxx::xxxx::xxxx'
81 re_rids = collections.OrderedDict() # OrderedDict of name: regex
82 # name is a string to name the regex.
83 # regex parses the proper resource id format.
86 def get_param_value(value, withIndex=False):
87 """Return get_param value of `value`
89 if isinstance(value, dict) and len(value) == 1:
90 v = value.get("get_param")
91 if isinstance(v, list) and v:
92 if withIndex and len(v) > 1:
94 if isinstance(idx, dict):
95 idx = idx.get("get_param", idx)
96 v = "{}{}".format(v[0], idx)
104 def get_resource_or_param_value(cls, value):
105 """Return the get_resource or get_param value of `value`
107 if isinstance(value, dict) and len(value) == 1:
108 v = value.get("get_resource") or cls.get_param_value(value)
114 def get_rid_match_tuple(cls, rid):
115 """find the first regex matching `rid` and return the tuple
116 (name, match object) or ('', None) if no match.
118 rid = "" if rid is None else rid
119 for name, regex in cls.re_rids.items():
120 match = regex.match(rid)
126 def get_rid_patterns(cls):
127 """Return OrderedDict of name: friendly regex.pattern
128 "friendly" means the group notation is replaced with
129 braces, and the trailing "$" is removed.
132 nested parentheses in any rid_pattern will break this parser.
133 The final character is ASSUMED to be a dollar sign.
135 friendly_pattern = _get_regex(r"\(\?P<(.*?)>.*?\)")
136 rid_patterns = collections.OrderedDict()
137 for name, regex in cls.re_rids.items():
138 rid_patterns[name] = friendly_pattern.sub(
139 r"{\1}", regex.pattern # replace groups with braces
142 ] # remove trailing $
146 def get_str_replace_name(cls, resource_dict):
147 """Return the name modified by str_replace of `resource_dict`,
148 a resource (i.e. a value in some template's resources).
149 Return None, if there is no name, str_replace, its template,
150 or any missing parameters.
152 str_replace = Heat.nested_get(
153 resource_dict, "properties", "name", "str_replace"
157 template = Heat.nested_get(str_replace, "template")
158 if not isinstance(template, str):
160 params = Heat.nested_get(str_replace, "params", default={})
161 if not isinstance(params, dict):
164 # The user must choose non-overlapping keys for params since they
165 # are replaced in the template in arbitrary order.
167 for key, value in params.items():
168 param = cls.get_param_value(value, withIndex=True)
171 name = name.replace(key, str(param))
175 class CinderVolumeAttachmentProcessor(HeatProcessor):
176 """ Cinder VolumeAttachment
179 resource_type = "OS::Cinder::VolumeAttachment"
182 def get_config(cls, resources):
183 """Return a tuple (va_config, va_count)
184 va_config - Hashabledict of Cinder Volume Attachment config
186 va_count - dict of attachment counts indexed by rid.
188 va_count = collections.defaultdict(int)
189 va_config = Hashabledict()
190 for resource in resources.values():
191 resource_type = nested_dict.get(resource, "type")
192 if resource_type == cls.resource_type:
193 config, rids = cls.get_volume_attachment_config(resource)
195 va_config[rid] = config
197 return va_config, va_count
200 def get_volume_attachment_config(cls, resource):
201 """Returns the cinder volume attachment configuration
202 of `resource` as a tuple (config, rids)
204 - config is a Hashabledict whose keys are the keys of the
205 properties of resource, and whose values are the
206 corresponding property values (nova server resource ids)
207 replaced with the vm-type they reference.
208 - rids is the set of nova server resource ids referenced by
211 config = Hashabledict()
213 for key, value in (resource.get("properties") or {}).items():
214 rid = cls.get_resource_or_param_value(value)
216 name, match = NovaServerProcessor.get_rid_match_tuple(rid)
218 vm_type = match.groupdict()["vm_type"]
219 config[key] = vm_type
224 class ContrailV2NetworkFlavorBaseProcessor(HeatProcessor):
225 """ContrailV2 objects which have network_flavor
228 network_flavor_external = "external"
229 network_flavor_internal = "internal"
230 network_flavor_subint = "subinterface"
233 def get_network_flavor(cls, resource):
234 """Return the network flavor of resource, one of
235 "internal" - get_resource, or get_param contains _int_
236 "subint" - get_param contains _subint_
237 "external" - otherwise
238 None - no parameters found to decide the flavor.
240 resource.properties.virtual_network_refs should be a list.
241 All the parameters in the list should have the same "flavor"
242 so the flavor is determined from the first item.
244 network_flavor = None
245 network_refs = nested_dict.get(resource, "properties", "virtual_network_refs")
246 if network_refs and isinstance(network_refs, list):
247 param = network_refs[0]
248 if isinstance(param, dict):
249 if "get_resource" in param:
250 network_flavor = cls.network_flavor_internal
252 p = param.get("get_param")
253 network_flavor = cls.get_network_format(p)
254 return network_flavor
257 def get_network_format(cls, param):
258 if isinstance(param, str):
259 if "_int_" in param or param.startswith("int_"):
260 return cls.network_flavor_internal
261 elif "_subint_" in param:
262 return cls.network_flavor_subint
264 return cls.network_flavor_external
267 class ContrailV2InstanceIpProcessor(ContrailV2NetworkFlavorBaseProcessor):
268 """ ContrailV2 InstanceIp
271 resource_type = "OS::ContrailV2::InstanceIp"
272 re_rids = collections.OrderedDict(
278 r"_(?P<vm_type_index>\d+)"
280 r"_(?P<network_role>.+)"
282 r"_(?P<vmi_index>\d+)"
293 r"_(?P<vm_type_index>\d+)"
295 r"_(?P<network_role>.+)"
297 r"_(?P<vmi_index>\d+)"
308 r"_(?P<vm_type_index>\d+)"
309 r"_(?P<network_role>.+)"
311 r"_(?P<vmi_index>\d+)"
322 class ContrailV2InterfaceRouteTableProcessor(HeatProcessor):
323 """ ContrailV2 InterfaceRouteTable
326 resource_type = "OS::ContrailV2::InterfaceRouteTable"
329 class ContrailV2NetworkIpamProcessor(HeatProcessor):
330 """ ContrailV2 NetworkIpam
333 resource_type = "OS::ContrailV2::NetworkIpam"
336 class ContrailV2PortTupleProcessor(HeatProcessor):
337 """ ContrailV2 PortTuple
340 resource_type = "OS::ContrailV2::PortTuple"
343 class ContrailV2ServiceHealthCheckProcessor(HeatProcessor):
344 """ ContrailV2 ServiceHealthCheck
347 resource_type = "OS::ContrailV2::ServiceHealthCheck"
350 class ContrailV2ServiceInstanceProcessor(HeatProcessor):
351 """ ContrailV2 ServiceInstance
354 resource_type = "OS::ContrailV2::ServiceInstance"
357 class ContrailV2ServiceInstanceIpProcessor(HeatProcessor):
358 """ ContrailV2 ServiceInstanceIp
361 resource_type = "OS::ContrailV2::ServiceInstanceIp"
364 class ContrailV2ServiceTemplateProcessor(HeatProcessor):
365 """ ContrailV2 ServiceTemplate
368 resource_type = "OS::ContrailV2::ServiceTemplate"
371 class ContrailV2VirtualMachineInterfaceProcessor(ContrailV2NetworkFlavorBaseProcessor):
372 """ ContrailV2 Virtual Machine Interface resource
375 resource_type = "OS::ContrailV2::VirtualMachineInterface"
376 re_rids = collections.OrderedDict(
382 r"_(?P<vm_type_index>\d+)"
384 r"_(?P<network_role>.+)"
386 r"_(?P<vmi_index>\d+)"
394 r"_(?P<vm_type_index>\d+)"
396 r"_(?P<network_role>.+)"
398 r"_(?P<vmi_index>\d+)"
406 r"_(?P<vm_type_index>\d+)"
407 r"_(?P<network_role>.+)"
409 r"_(?P<vmi_index>\d+)"
417 class ContrailV2VirtualNetworkProcessor(HeatProcessor):
418 """ ContrailV2 VirtualNetwork
421 resource_type = "OS::ContrailV2::VirtualNetwork"
422 re_rids = collections.OrderedDict(
424 ("network", _get_regex(r"int" r"_(?P<network_role>.+)" r"_network" r"$")),
425 # ("rvn", _get_regex(r"int" r"_(?P<network_role>.+)" r"_RVN" r"$")),
430 class NeutronNetProcessor(HeatProcessor):
431 """ Neutron Net resource
434 resource_type = "OS::Neutron::Net"
435 re_rids = collections.OrderedDict(
436 [("network", _get_regex(r"int" r"_(?P<network_role>.+)" r"_network" r"$"))]
440 class NeutronPortProcessor(HeatProcessor):
441 """ Neutron Port resource
444 resource_type = "OS::Neutron::Port"
445 re_rids = collections.OrderedDict(
451 r"_(?P<vm_type_index>\d+)"
453 r"_(?P<network_role>.+)"
454 r"_port_(?P<port_index>\d+)"
462 r"_(?P<vm_type_index>\d+)"
463 r"_(?P<network_role>.+)"
464 r"_port_(?P<port_index>\d+)"
472 def uses_sr_iov(cls, resource):
473 """Returns True/False as `resource` is/not
474 An OS::Nova:Port with the property binding:vnic_type
476 resource_properties = nested_dict.get(resource, "properties", default={})
478 nested_dict.get(resource, "type") == cls.resource_type
479 and resource_properties.get("binding:vnic_type", "") == "direct"
486 class NovaServerProcessor(HeatProcessor):
487 """ Nova Server resource
490 resource_type = "OS::Nova::Server"
491 re_rids = collections.OrderedDict(
495 _get_regex(r"(?P<vm_type>.+)" r"_server_(?P<vm_type_index>\d+)" r"$"),
501 def get_flavor(cls, resource):
502 """Return the flavor property of `resource`
504 return cls.get_param_value(nested_dict.get(resource, "properties", "flavor"))
507 def get_image(cls, resource):
508 """Return the image property of `resource`
510 return cls.get_param_value(nested_dict.get(resource, "properties", "image"))
513 def get_network(cls, resource):
514 """Return the network configuration of `resource` as a
515 frozenset of network-roles.
518 networks = nested_dict.get(resource, "properties", "networks")
519 if isinstance(networks, list):
520 for port in networks:
521 value = cls.get_resource_or_param_value(nested_dict.get(port, "port"))
522 name, match = NeutronPortProcessor.get_rid_match_tuple(value)
524 network_role = match.groupdict().get("network_role")
526 network.add(network_role)
527 return frozenset(network)
530 def get_vm_class(cls, resource):
531 """Return the vm_class of `resource`, a Hashabledict (of
532 hashable values) whose keys are only the required keys.
533 Return empty Hashabledict if `resource` is not a NovaServer.
535 vm_class = Hashabledict()
536 resource_type = nested_dict.get(resource, "type")
537 if resource_type == cls.resource_type:
539 flavor=cls.get_flavor(resource),
540 image=cls.get_image(resource),
541 network_role=cls.get_network(resource),
550 filepath - absolute path to template file.
551 envpath - absolute path to environmnt file.
554 type_bool = "boolean"
555 type_boolean = "boolean"
556 type_cdl = "comma_delimited_list"
557 type_comma_delimited_list = "comma_delimited_list"
560 type_number = "number"
562 type_string = "string"
564 def __init__(self, filepath=None, envpath=None):
569 self.heat_template_version = None
570 self.description = None
571 self.parameter_groups = None
572 self.parameters = None
573 self.resources = None
575 self.conditions = None
580 self.load_env(envpath)
581 self.heat_processors = self.get_heat_processors()
585 return "heat_template_version" in self.yml
588 def contrail_resources(self):
589 """This attribute is a dict of Contrail resources.
591 return self.get_resource_by_type(
592 resource_type=ContrailV2VirtualMachineInterfaceProcessor.resource_type
595 def get_all_resources(self, base_dir=None, count=1):
597 Like ``resources``, but this returns all the resources definitions
598 defined in the template, resource groups, and nested YAML files.
600 A special variable will be added to all resource properties (__count__).
601 This will normally be 1, but if the resource is generated by a
602 ResourceGroup **and** an env file is present, then the count will be
603 the value from the env file (assuming this follows standard VNF Heat
606 base_dir = base_dir or self.dirname
608 for r_id, r_data in self.resources.items():
609 r_data["__count__"] = count
610 resources[r_id] = r_data
611 resource = Resource(r_id, r_data)
612 if resource.is_nested():
613 nested_count = resource.get_count(self.env)
614 nested = Heat(os.path.join(base_dir, resource.get_nested_filename()))
615 nested_resources = nested.get_all_resources(count=nested_count)
616 resources.update(nested_resources)
620 def get_heat_processors():
621 """Return a dict, key is resource_type, value is the
622 HeatProcessor subclass whose resource_type is the key.
624 return _HEAT_PROCESSORS
626 def get_resource_by_type(self, resource_type, all_resources=False):
627 """Return dict of resources whose type is `resource_type`.
628 key is resource_id, value is resource.
630 resources = self.get_all_resources() if all_resources else self.resources
633 for rid, resource in resources.items()
634 if self.nested_get(resource, "type") == resource_type
637 def get_rid_match_tuple(self, rid, resource_type):
638 """return get_rid_match_tuple(rid) called on the class
639 corresponding to the given resource_type.
641 processor = self.heat_processors.get(resource_type, HeatProcessor)
642 return processor.get_rid_match_tuple(rid)
644 def get_vm_type(self, rid, resource=None):
645 """return the vm_type
649 resource_type = self.nested_get(resource, "type")
650 match = self.get_rid_match_tuple(rid, resource_type)[1]
651 vm_type = match.groupdict().get("vm_type") if match else None
654 def load(self, filepath):
655 """Load the Heat template given a filepath.
657 self.filepath = filepath
658 self.basename = os.path.basename(self.filepath)
659 self.dirname = os.path.dirname(self.filepath)
660 with open(self.filepath) as fi:
661 self.yml = yaml.load(fi)
662 self.heat_template_version = self.yml.get("heat_template_version", None)
663 self.description = self.yml.get("description", "")
664 self.parameter_groups = self.yml.get("parameter_groups") or {}
665 self.parameters = self.yml.get("parameters") or {}
666 self.resources = self.yml.get("resources") or {}
667 self.outputs = self.yml.get("outputs") or {}
668 self.conditions = self.yml.get("conditions") or {}
670 def load_env(self, envpath):
671 """Load the Environment template given a envpath.
673 self.env = Env(filepath=envpath)
676 def nested_get(dic, *keys, **kwargs):
677 """make utils.nested_dict.get available as a class method.
679 return nested_dict.get(dic, *keys, **kwargs)
682 def neutron_port_resources(self):
683 """This attribute is a dict of Neutron Ports
685 return self.get_resource_by_type(
686 resource_type=NeutronPortProcessor.resource_type
690 def nova_server_resources(self):
691 """This attribute is a dict of Nova Servers
693 return self.get_resource_by_type(
694 resource_type=NovaServerProcessor.resource_type
698 def part_is_in_name(part, name):
700 Return True if any of
701 - name starts with part + '_'
702 - name contains '_' + part + '_'
703 - name ends with '_' + part
707 re.search("(^(%(x)s)_)|(_(%(x)s)_)|(_(%(x)s)$)" % dict(x=part), name)
710 def iter_nested_heat(self):
712 Returns an iterable of tuples (int, heat) where the first parameter is the
713 depth of the nested file and the second item is an instance of Heat
716 def walk_nested(heat, level=1):
717 resources = [Resource(r_id, data) for r_id, data in heat.resources.items()]
718 for resource in resources:
719 if resource.is_nested():
720 nested_path = os.path.join(
721 self.dirname, resource.get_nested_filename()
723 nested_heat = Heat(nested_path)
724 yield level, nested_heat
725 yield from walk_nested(nested_heat, level + 1)
727 yield from walk_nested(self)
730 return "Heat({})".format(self.filepath)
737 """An Environment file
743 class Resource(object):
747 def __init__(self, resource_id=None, resource=None):
748 self.resource_id = resource_id or ""
749 self.resource = resource or {}
750 self.properties = self.resource.get("properties", {})
751 self.resource_type = self.resource.get("type", "")
754 def get_index_var(resource):
755 """Return the index_var for this resource.
757 index_var = nested_dict.get(resource, "properties", "index_var") or "index"
760 def get_nested_filename(self):
761 """Returns the filename of the nested YAML file if the
762 resource is a nested YAML or ResourceDef, returns '' otherwise."""
763 typ = self.resource.get("type", "")
764 if typ == "OS::Heat::ResourceGroup":
765 rd = nested_dict.get(self.resource, "properties", "resource_def")
766 typ = rd.get("type", "") if rd else ""
767 ext = os.path.splitext(typ)[1]
769 if ext == ".yml" or ext == ".yaml":
774 def get_nested_properties(self):
776 Returns {} if not nested
777 Returns resource: properties if nested
778 Returns resource: properties: resource_def: properties if RG
780 if not bool(self.get_nested_filename()):
782 elif self.resource_type == "OS::Heat::ResourceGroup":
783 return nested_dict.get(
784 self.properties, "resource_def", "properties", default={}
787 return self.properties
789 def get_count(self, env):
790 if self.resource_type == "OS::Heat::ResourceGroup":
793 env_params = env.parameters
794 count_param = get_param(self.properties["count"])
795 count_value = env_params.get(count_param) if count_param else 1
797 return int(count_value)
798 except (ValueError, TypeError):
801 "WARNING: Invalid value for count parameter {}. Expected "
802 "an integer, but got {}. Defaulting to 1"
803 ).format(count_param, count_value)
808 def depends_on(self):
810 Returns the list of resources this resource depends on. Always
813 :return: list of all resource IDs this resource depends on. If none,
814 then returns an empty list
816 parents = self.resource.get("depends_on", [])
817 return parents if isinstance(parents, list) else [parents]
820 """Returns True if the resource represents a Nested YAML resource
821 using either type: {filename} or ResourceGroup -> resource_def"""
822 return bool(self.get_nested_filename())
824 def get_nested_yaml(self, base_dir):
825 """If the resource represents a Nested YAML resource, then it
826 returns the loaded YAML. If the resource is not nested or the
827 file cannot be found, then an empty dict is returned"""
828 filename = self.get_nested_filename()
830 file_path = os.path.join(base_dir, filename)
831 return load_yaml(file_path) if os.path.exists(file_path) else {}
836 return "Resource(id={}, type={})".format(self.resource_id, self.resource_type)
842 def get_all_resources(yaml_files):
843 """Return a dict, resource id: resource
844 of the union of resources across all files.
847 for heat_template in yaml_files:
848 heat = Heat(filepath=heat_template)
849 dirname = os.path.dirname(heat_template)
850 resources.update(heat.get_all_resources(dirname))
854 def _get_heat_processors():
855 """Introspect this module and return a
856 dict of all HeatProcessor sub-classes with a (True) resource_type.
857 Key is the resource_type, value is the corrresponding class.
859 mod_classes = inspect.getmembers(sys.modules[__name__], inspect.isclass)
862 for _, c in mod_classes
863 if issubclass(c, HeatProcessor) and c.resource_type
865 return heat_processors
868 _HEAT_PROCESSORS = _get_heat_processors()