aboutsummaryrefslogtreecommitdiffstats
path: root/ice_validator/tests/structures.py
blob: 12bfc633571934c2caf6d8f1db0534659ed85c65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
# -*- coding: utf8 -*-
# ============LICENSE_START====================================================
# org.onap.vvp/validation-scripts
# ===================================================================
# Copyright © 2019 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the "License");
# you may not use this software except in compliance with the License.
# You may obtain a copy of the License at
#
#             http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the "License");
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ============LICENSE_END============================================
#
#
"""structures
"""
import collections
import inspect
import os
import re
import sys

from tests import cached_yaml as yaml
from tests.helpers import load_yaml, get_param
from .utils import nested_dict

VERSION = "4.2.0"

# key = pattern, value = regex compiled from pattern
_REGEX_CACHE = {}


def _get_regex(pattern):
    """Return a compiled version of pattern.
    Keep result in _REGEX_CACHE to avoid re-compiling.
    """
    regex = _REGEX_CACHE.get(pattern, None)
    if regex is None:
        regex = re.compile(pattern)
        _REGEX_CACHE[pattern] = regex
    return regex


class Hashabledict(dict):
    """A hashable dict.
    dicts with the same keys and whose keys have the same values
    are assigned the same hash.
    """

    def __hash__(self):
        return hash((frozenset(self), frozenset(self.values())))


class HeatProcessor(object):
    """base class for xxxx::xxxx::xxxx processors
    """

    resource_type = None  # string 'xxxx::xxxx::xxxx'
    re_rids = collections.OrderedDict()  # OrderedDict of name: regex
    # name is a string to name the regex.
    # regex parses the proper resource id format.

    @staticmethod
    def get_param_value(value, withIndex=False):
        """Return get_param value of `value`
        """
        if isinstance(value, dict) and len(value) == 1:
            v = value.get("get_param")
            if isinstance(v, list) and v:
                if withIndex and len(v) > 1:
                    idx = v[1]
                    if isinstance(idx, dict):
                        idx = idx.get("get_param", idx)
                    v = "{}{}".format(v[0], idx)
                else:
                    v = v[0]
        else:
            v = None
        return v

    @classmethod
    def get_resource_or_param_value(cls, value):
        """Return the get_resource or get_param value of `value`
        """
        if isinstance(value, dict) and len(value) == 1:
            v = value.get("get_resource") or cls.get_param_value(value)
        else:
            v = None
        return v

    @classmethod
    def get_rid_match_tuple(cls, rid):
        """find the first regex matching `rid` and return the tuple
        (name, match object) or ('', None) if no match.
        """
        rid = "" if rid is None else rid
        for name, regex in cls.re_rids.items():
            match = regex.match(rid)
            if match:
                return name, match
        return "", None

    @classmethod
    def get_rid_patterns(cls):
        """Return OrderedDict of name: friendly regex.pattern
        "friendly" means the group notation is replaced with
        braces, and the trailing "$" is removed.

        NOTE
        nested parentheses in any rid_pattern will break this parser.
        The final character is ASSUMED to be a dollar sign.
        """
        friendly_pattern = _get_regex(r"\(\?P<(.*?)>.*?\)")
        rid_patterns = collections.OrderedDict()
        for name, regex in cls.re_rids.items():
            rid_patterns[name] = friendly_pattern.sub(
                r"{\1}", regex.pattern  # replace groups with braces
            )[
                :-1
            ]  # remove trailing $
        return rid_patterns

    @classmethod
    def get_str_replace_name(cls, resource_dict):
        """Return the name modified by str_replace of `resource_dict`,
        a resource (i.e. a value in some template's resources).
        Return None, if there is no name, str_replace, its template,
        or any missing parameters.
        """
        str_replace = Heat.nested_get(
            resource_dict, "properties", "name", "str_replace"
        )
        if not str_replace:
            return None
        template = Heat.nested_get(str_replace, "template")
        if not isinstance(template, str):
            return None
        params = Heat.nested_get(str_replace, "params", default={})
        if not isinstance(params, dict):
            return None
        # WARNING
        # The user must choose non-overlapping keys for params since they
        # are replaced in the template in arbitrary order.
        name = template
        for key, value in params.items():
            param = cls.get_param_value(value, withIndex=True)
            if param is None:
                return None
            name = name.replace(key, str(param))
        return name


class CinderVolumeAttachmentProcessor(HeatProcessor):
    """ Cinder VolumeAttachment
    """

    resource_type = "OS::Cinder::VolumeAttachment"

    @classmethod
    def get_config(cls, resources):
        """Return a tuple (va_config, va_count)
        va_config - Hashabledict of Cinder Volume Attachment config
                    indexed by rid.
        va_count - dict of attachment counts indexed by rid.
        """
        va_count = collections.defaultdict(int)
        va_config = Hashabledict()
        for resource in resources.values():
            resource_type = nested_dict.get(resource, "type")
            if resource_type == cls.resource_type:
                config, rids = cls.get_volume_attachment_config(resource)
                for rid in rids:
                    va_config[rid] = config
                    va_count[rid] += 1
        return va_config, va_count

    @classmethod
    def get_volume_attachment_config(cls, resource):
        """Returns the cinder volume attachment configuration
        of `resource` as a tuple (config, rids)
        where:
        - config is a Hashabledict whose keys are the keys of the
            properties of resource, and whose values are the
            corresponding property values (nova server resource ids)
            replaced with the vm-type they reference.
        - rids is the set of nova server resource ids referenced by
            the property values.
        """
        config = Hashabledict()
        rids = set()
        for key, value in (resource.get("properties") or {}).items():
            rid = cls.get_resource_or_param_value(value)
            if rid:
                name, match = NovaServerProcessor.get_rid_match_tuple(rid)
                if name == "server":
                    vm_type = match.groupdict()["vm_type"]
                    config[key] = vm_type
                    rids.add(rid)
        return config, rids


class ContrailV2NetworkFlavorBaseProcessor(HeatProcessor):
    """ContrailV2 objects which have network_flavor
    """

    network_flavor_external = "external"
    network_flavor_internal = "internal"
    network_flavor_subint = "subinterface"

    @classmethod
    def get_network_flavor(cls, resource):
        """Return the network flavor of resource, one of
        "internal" - get_resource, or get_param contains _int_
        "subint" - get_param contains _subint_
        "external" - otherwise
        None - no parameters found to decide the flavor.

        resource.properties.virtual_network_refs should be a list.
        All the parameters in the list should have the same "flavor"
        so the flavor is determined from the first item.
        """
        network_flavor = None
        network_refs = nested_dict.get(resource, "properties", "virtual_network_refs")
        if network_refs and isinstance(network_refs, list):
            param = network_refs[0]
            if isinstance(param, dict):
                if "get_resource" in param:
                    network_flavor = cls.network_flavor_internal
                else:
                    p = param.get("get_param")
                    if isinstance(p, str):
                        if "_int_" in p or p.startswith("int_"):
                            network_flavor = cls.network_flavor_internal
                        elif "_subint_" in p:
                            network_flavor = cls.network_flavor_subint
                        else:
                            network_flavor = cls.network_flavor_external
        return network_flavor


class ContrailV2InstanceIpProcessor(ContrailV2NetworkFlavorBaseProcessor):
    """ ContrailV2 InstanceIp
    """

    resource_type = "OS::ContrailV2::InstanceIp"
    re_rids = collections.OrderedDict(
        [
            (
                "internal",
                _get_regex(
                    r"(?P<vm_type>.+)"
                    r"_(?P<vm_type_index>\d+)"
                    r"_int"
                    r"_(?P<network_role>.+)"
                    r"_vmi"
                    r"_(?P<vmi_index>\d+)"
                    r"(_v6)?"
                    r"_IP"
                    r"_(?P<index>\d+)"
                    r"$"
                ),
            ),
            (
                "subinterface",
                _get_regex(
                    r"(?P<vm_type>.+)"
                    r"_(?P<vm_type_index>\d+)"
                    r"_subint"
                    r"_(?P<network_role>.+)"
                    r"_vmi"
                    r"_(?P<vmi_index>\d+)"
                    r"(_v6)?"
                    r"_IP"
                    r"_(?P<index>\d+)"
                    r"$"
                ),
            ),
            (
                "external",
                _get_regex(
                    r"(?P<vm_type>.+)"
                    r"_(?P<vm_type_index>\d+)"
                    r"_(?P<network_role>.+)"
                    r"_vmi"
                    r"_(?P<vmi_index>\d+)"
                    r"(_v6)?"
                    r"_IP"
                    r"_(?P<index>\d+)"
                    r"$"
                ),
            ),
        ]
    )


class ContrailV2InterfaceRouteTableProcessor(HeatProcessor):
    """ ContrailV2 InterfaceRouteTable
    """

    resource_type = "OS::ContrailV2::InterfaceRouteTable"


class ContrailV2NetworkIpamProcessor(HeatProcessor):
    """ ContrailV2 NetworkIpam
    """

    resource_type = "OS::ContrailV2::NetworkIpam"


class ContrailV2PortTupleProcessor(HeatProcessor):
    """ ContrailV2 PortTuple
    """

    resource_type = "OS::ContrailV2::PortTuple"


class ContrailV2ServiceHealthCheckProcessor(HeatProcessor):
    """ ContrailV2 ServiceHealthCheck
    """

    resource_type = "OS::ContrailV2::ServiceHealthCheck"


class ContrailV2ServiceInstanceProcessor(HeatProcessor):
    """ ContrailV2 ServiceInstance
    """

    resource_type = "OS::ContrailV2::ServiceInstance"


class ContrailV2ServiceInstanceIpProcessor(HeatProcessor):
    """ ContrailV2 ServiceInstanceIp
    """

    resource_type = "OS::ContrailV2::ServiceInstanceIp"


class ContrailV2ServiceTemplateProcessor(HeatProcessor):
    """ ContrailV2 ServiceTemplate
    """

    resource_type = "OS::ContrailV2::ServiceTemplate"


class ContrailV2VirtualMachineInterfaceProcessor(ContrailV2NetworkFlavorBaseProcessor):
    """ ContrailV2 Virtual Machine Interface resource
    """

    resource_type = "OS::ContrailV2::VirtualMachineInterface"
    re_rids = collections.OrderedDict(
        [
            (
                "internal",
                _get_regex(
                    r"(?P<vm_type>.+)"
                    r"_(?P<vm_type_index>\d+)"
                    r"_int"
                    r"_(?P<network_role>.+)"
                    r"_vmi"
                    r"_(?P<vmi_index>\d+)"
                    r"$"
                ),
            ),
            (
                "subinterface",
                _get_regex(
                    r"(?P<vm_type>.+)"
                    r"_(?P<vm_type_index>\d+)"
                    r"_subint"
                    r"_(?P<network_role>.+)"
                    r"_vmi"
                    r"_(?P<vmi_index>\d+)"
                    r"$"
                ),
            ),
            (
                "external",
                _get_regex(
                    r"(?P<vm_type>.+)"
                    r"_(?P<vm_type_index>\d+)"
                    r"_(?P<network_role>.+)"
                    r"_vmi"
                    r"_(?P<vmi_index>\d+)"
                    r"$"
                ),
            ),
        ]
    )


class ContrailV2VirtualNetworkProcessor(HeatProcessor):
    """ ContrailV2 VirtualNetwork
    """

    resource_type = "OS::ContrailV2::VirtualNetwork"
    re_rids = collections.OrderedDict(
        [
            ("network", _get_regex(r"int" r"_(?P<network_role>.+)" r"_network" r"$")),
            # ("rvn", _get_regex(r"int" r"_(?P<network_role>.+)" r"_RVN" r"$")),
        ]
    )


class HeatResourceGroupProcessor(HeatProcessor):
    """ Heat ResourceGroup
    """

    resource_type = "OS::Heat::ResourceGroup"
    re_rids = collections.OrderedDict(
        [
            (
                "subint",
                _get_regex(
                    r"(?P<vm_type>.+)"
                    r"_(?P<vm_type_index>\d+)"
                    r"_subint"
                    r"_(?P<network_role>.+)"
                    r"_port_(?P<port_index>\d+)"
                    r"_subinterfaces"
                    r"$"
                ),
            )
        ]
    )


class NeutronNetProcessor(HeatProcessor):
    """ Neutron Net resource
    """

    resource_type = "OS::Neutron::Net"
    re_rids = collections.OrderedDict(
        [("network", _get_regex(r"int" r"_(?P<network_role>.+)" r"_network" r"$"))]
    )


class NeutronPortProcessor(HeatProcessor):
    """ Neutron Port resource
    """

    resource_type = "OS::Neutron::Port"
    re_rids = collections.OrderedDict(
        [
            (
                "internal",
                _get_regex(
                    r"(?P<vm_type>.+)"
                    r"_(?P<vm_type_index>\d+)"
                    r"_int"
                    r"_(?P<network_role>.+)"
                    r"_port_(?P<port_index>\d+)"
                    r"$"
                ),
            ),
            (
                "external",
                _get_regex(
                    r"(?P<vm_type>.+)"
                    r"_(?P<vm_type_index>\d+)"
                    r"_(?P<network_role>.+)"
                    r"_port_(?P<port_index>\d+)"
                    r"$"
                ),
            ),
        ]
    )

    @classmethod
    def uses_sr_iov(cls, resource):
        """Returns True/False as `resource` is/not
        An OS::Nova:Port with the property binding:vnic_type
        """
        resource_properties = nested_dict.get(resource, "properties", default={})
        if nested_dict.get(resource, "type") == cls.resource_type and resource_properties.get("binding:vnic_type", "") == "direct":
            return True

        return False


class NovaServerProcessor(HeatProcessor):
    """ Nova Server resource
    """

    resource_type = "OS::Nova::Server"
    re_rids = collections.OrderedDict(
        [
            (
                "server",
                _get_regex(r"(?P<vm_type>.+)" r"_server_(?P<vm_type_index>\d+)" r"$"),
            )
        ]
    )

    @classmethod
    def get_flavor(cls, resource):
        """Return the flavor property of `resource`
        """
        return cls.get_param_value(nested_dict.get(resource, "properties", "flavor"))

    @classmethod
    def get_image(cls, resource):
        """Return the image property of `resource`
        """
        return cls.get_param_value(nested_dict.get(resource, "properties", "image"))

    @classmethod
    def get_network(cls, resource):
        """Return the network configuration of `resource` as a
        frozenset of network-roles.
        """
        network = set()
        networks = nested_dict.get(resource, "properties", "networks")
        if isinstance(networks, list):
            for port in networks:
                value = cls.get_resource_or_param_value(nested_dict.get(port, "port"))
                name, match = NeutronPortProcessor.get_rid_match_tuple(value)
                if name:
                    network_role = match.groupdict().get("network_role")
                    if network_role:
                        network.add(network_role)
        return frozenset(network)

    @classmethod
    def get_vm_class(cls, resource):
        """Return the vm_class of `resource`, a Hashabledict (of
        hashable values) whose keys are only the required keys.
        Return empty Hashabledict if `resource` is not a NovaServer.
        """
        vm_class = Hashabledict()
        resource_type = nested_dict.get(resource, "type")
        if resource_type == cls.resource_type:
            d = dict(
                flavor=cls.get_flavor(resource),
                image=cls.get_image(resource),
                networks=cls.get_network(resource),
            )
            if all(d.values()):
                vm_class.update(d)
        return vm_class


class Heat(object):
    """A Heat template.
    filepath - absolute path to template file.
    envpath - absolute path to environmnt file.
    """

    type_bool = "boolean"
    type_boolean = "boolean"
    type_cdl = "comma_delimited_list"
    type_comma_delimited_list = "comma_delimited_list"
    type_json = "json"
    type_num = "number"
    type_number = "number"
    type_str = "string"
    type_string = "string"

    def __init__(self, filepath=None, envpath=None):
        self.filepath = None
        self.basename = None
        self.dirname = None
        self.yml = None
        self.heat_template_version = None
        self.description = None
        self.parameter_groups = None
        self.parameters = None
        self.resources = None
        self.outputs = None
        self.conditions = None
        if filepath:
            self.load(filepath)
        self.env = None
        if envpath:
            self.load_env(envpath)
        self.heat_processors = self.get_heat_processors()

    @property
    def contrail_resources(self):
        """This attribute is a dict of Contrail resources.
        """
        return self.get_resource_by_type(
            resource_type=ContrailV2VirtualMachineInterfaceProcessor.resource_type
        )

    def get_all_resources(self, base_dir=None, count=1):
        """
        Like ``resources``, but this returns all the resources definitions
        defined in the template, resource groups, and nested YAML files.

        A special variable will be added to all resource properties (__count__).
        This will normally be 1, but if the resource is generated by a
        ResourceGroup **and** an env file is present, then the count will be
        the value from the env file (assuming this follows standard VNF Heat
        Guidelines)
        """
        base_dir = base_dir or self.dirname
        resources = {}
        for r_id, r_data in self.resources.items():
            r_data["__count__"] = count
            resources[r_id] = r_data
            resource = Resource(r_id, r_data)
            if resource.is_nested():
                nested_count = resource.get_count(self.env)
                nested = Heat(os.path.join(base_dir, resource.get_nested_filename()))
                nested_resources = nested.get_all_resources(count=nested_count)
                resources.update(nested_resources)
        return resources

    @staticmethod
    def get_heat_processors():
        """Return a dict, key is resource_type, value is the
        HeatProcessor subclass whose resource_type is the key.
        """
        return _HEAT_PROCESSORS

    def get_resource_by_type(self, resource_type, all_resources=False):
        """Return dict of resources whose type is `resource_type`.
        key is resource_id, value is resource.
        """
        resources = self.get_all_resources() if all_resources else self.resources
        return {
            rid: resource
            for rid, resource in resources.items()
            if self.nested_get(resource, "type") == resource_type
        }

    def get_rid_match_tuple(self, rid, resource_type):
        """return get_rid_match_tuple(rid) called on the class
        corresponding to the given resource_type.
        """
        processor = self.heat_processors.get(resource_type, HeatProcessor)
        return processor.get_rid_match_tuple(rid)

    def get_vm_type(self, rid, resource=None):
        """return the vm_type
        """
        if resource is None:
            resource = self
        resource_type = self.nested_get(resource, "type")
        match = self.get_rid_match_tuple(rid, resource_type)[1]
        vm_type = match.groupdict().get("vm_type") if match else None
        return vm_type

    def load(self, filepath):
        """Load the Heat template given a filepath.
        """
        self.filepath = filepath
        self.basename = os.path.basename(self.filepath)
        self.dirname = os.path.dirname(self.filepath)
        with open(self.filepath) as fi:
            self.yml = yaml.load(fi)
        self.heat_template_version = self.yml.get("heat_template_version", None)
        self.description = self.yml.get("description", "")
        self.parameter_groups = self.yml.get("parameter_groups") or {}
        self.parameters = self.yml.get("parameters") or {}
        self.resources = self.yml.get("resources") or {}
        self.outputs = self.yml.get("outputs") or {}
        self.conditions = self.yml.get("conditions") or {}

    def load_env(self, envpath):
        """Load the Environment template given a envpath.
        """
        self.env = Env(filepath=envpath)

    @staticmethod
    def nested_get(dic, *keys, **kwargs):
        """make utils.nested_dict.get available as a class method.
        """
        return nested_dict.get(dic, *keys, **kwargs)

    @property
    def neutron_port_resources(self):
        """This attribute is a dict of Neutron Ports
        """
        return self.get_resource_by_type(
            resource_type=NeutronPortProcessor.resource_type
        )

    @property
    def nova_server_resources(self):
        """This attribute is a dict of Nova Servers
        """
        return self.get_resource_by_type(
            resource_type=NovaServerProcessor.resource_type
        )

    @staticmethod
    def part_is_in_name(part, name):
        """
        Return True if any of
        - name starts with part + '_'
        - name contains '_' + part + '_'
        - name ends with '_' + part
        False otherwise
        """
        return bool(
            re.search("(^(%(x)s)_)|(_(%(x)s)_)|(_(%(x)s)$)" % dict(x=part), name)
        )


class Env(Heat):
    """An Environment file
    """

    pass


class Resource(object):
    """A Resource
    """

    def __init__(self, resource_id=None, resource=None):
        self.resource_id = resource_id or ""
        self.resource = resource or {}
        self.properties = self.resource.get("properties", {})
        self.resource_type = self.resource.get("type", "")

    @staticmethod
    def get_index_var(resource):
        """Return the index_var for this resource.
        """
        index_var = nested_dict.get(resource, "properties", "index_var") or "index"
        return index_var

    def get_nested_filename(self):
        """Returns the filename of the nested YAML file if the
        resource is a nested YAML or ResourceDef, returns '' otherwise."""
        typ = self.resource.get("type", "")
        if typ == "OS::Heat::ResourceGroup":
            rd = nested_dict.get(self.resource, "properties", "resource_def")
            typ = rd.get("type", "") if rd else ""
        ext = os.path.splitext(typ)[1]
        ext = ext.lower()
        if ext == ".yml" or ext == ".yaml":
            return typ
        else:
            return ""

    def get_nested_properties(self):
        """
        Returns {} if not nested
        Returns resource: properties if nested
        Returns resource: properties: resource_def: properties if RG
        """
        if not bool(self.get_nested_filename()):
            return {}
        elif self.resource_type == "OS::Heat::ResourceGroup":
            return nested_dict.get(
                self.properties, "resource_def", "properties", default={}
            )
        else:
            return self.properties

    def get_count(self, env):
        if self.resource_type == "OS::Heat::ResourceGroup":
            if not env:
                return 1
            env_params = env.parameters
            count_param = get_param(self.properties["count"])
            count_value = env_params.get(count_param) if count_param else 1
            try:
                return int(count_value)
            except (ValueError, TypeError):
                print((
                    "WARNING: Invalid value for count parameter {}. Expected "
                    "an integer, but got {}. Defaulting to 1"
                ).format(count_param, count_value))
        return 1

    @property
    def depends_on(self):
        """
        Returns the list of resources this resource depends on.  Always
        returns a list.

        :return: list of all resource IDs this resource depends on.  If none,
                 then returns an empty list
        """
        parents = self.resource.get("depends_on", [])
        return parents if isinstance(parents, list) else [parents]

    def is_nested(self):
        """Returns True if the resource represents a Nested YAML resource
        using either type: {filename} or ResourceGroup -> resource_def"""
        return bool(self.get_nested_filename())

    def get_nested_yaml(self, base_dir):
        """If the resource represents a Nested YAML resource, then it
        returns the loaded YAML.  If the resource is not nested or the
        file cannot be found, then an empty dict is returned"""
        filename = self.get_nested_filename()
        if filename:
            file_path = os.path.join(base_dir, filename)
            return load_yaml(file_path) if os.path.exists(file_path) else {}
        else:
            return {}


def get_all_resources(yaml_files):
    """Return a dict, resource id: resource
    of the union of resources across all files.
    """
    resources = {}
    for heat_template in yaml_files:
        heat = Heat(filepath=heat_template)
        dirname = os.path.dirname(heat_template)
        resources.update(heat.get_all_resources(dirname))
    return resources


def _get_heat_processors():
    """Introspect this module and return a
    dict of all HeatProcessor sub-classes with a (True) resource_type.
    Key is the resource_type, value is the corrresponding class.
    """
    mod_classes = inspect.getmembers(sys.modules[__name__], inspect.isclass)
    heat_processors = {
        c.resource_type: c
        for _, c in mod_classes
        if issubclass(c, HeatProcessor) and c.resource_type
    }
    return heat_processors


_HEAT_PROCESSORS = _get_heat_processors()