[VVP] stand alone tool, script updates
[vvp/validation-scripts.git] / ice_validator / tests / utils / vm_types.py
1 # -*- coding: utf8 -*-
2 # ============LICENSE_START=======================================================
3 # org.onap.vvp/validation-scripts
4 # ===================================================================
5 # Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6 # ===================================================================
7 #
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
12 #
13 #             http://www.apache.org/licenses/LICENSE-2.0
14 #
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.
20 #
21 #
22 #
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
27 #
28 #             https://creativecommons.org/licenses/by/4.0/
29 #
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.
35 #
36 # ============LICENSE_END============================================
37 #
38 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
39 #
40
41 import re
42 from tests import cached_yaml as yaml
43
44
45 def get_vm_types_for_resource(resource):
46     """
47     Get all unique vm_types for a resource
48     Notes:
49     - Returns set([]) if the resource is not formatted
50     properly, the passed resource is not a nova server
51     - If more than one vm_type is detected all vm_types will
52     be returned
53     """
54     if not isinstance(resource, dict):
55         return set()
56     if "type" not in resource:
57         return set()
58     if resource["type"] != "OS::Nova::Server":
59         return set()
60     if "properties" not in resource:
61         return set()
62
63     key_values = ["name", "flavor", "image"]
64     key_value_formats = [
65         ["name", "string", re.compile(r"(.+?)_name_\d+")],
66         ["name", "comma_delimited_list", re.compile(r"(.+?)_names")],
67         ["flavor", "string", re.compile(r"(.+?)_flavor_name")],
68         ["image", "string", re.compile(r"(.+?)_image_name")],
69     ]
70
71     vm_types = []
72     for k2, v2 in resource["properties"].items():
73         if k2 not in key_values:
74             continue
75         if "get_param" not in v2:
76             continue
77         formats = [v for v in key_value_formats if v[0] == k2]
78         for v3 in formats:
79             param = v2["get_param"]
80             if isinstance(param, list):
81                 param = param[0]
82             m = v3[2].match(param)
83             if m and m.group(1):
84                 vm_types.append(m.group(1))
85
86     return set(vm_types)
87
88
89 def get_vm_type_for_nova_server(resource):
90     """
91     Get the vm_type for a resource
92     Note: Returns None if not exactly one vm_type
93     is detected, if the resource is not formatted properly, or
94     the passed resource is not a nova server
95     """
96     vm_types = get_vm_types_for_resource(resource)
97
98     # if more than one vm_type was identified, return None
99     if len(vm_types) > 1:
100         return None
101
102     return vm_types.pop()
103
104
105 def get_vm_types(resources):
106     """
107     Get all vm_types for a list of heat resources, do note that
108     some of the values retrieved may be invalid
109     """
110     vm_types = []
111     for v in resources.values():
112         vm_types.extend(list(get_vm_types_for_resource(v)))
113
114     return set(vm_types)
115
116
117 def get_all_vm_types(yaml_files):
118     """
119     Get all vm_types for a list of yaml files
120     """
121     vm_types = []
122     for yaml_file in yaml_files:
123         with open(yaml_file, "r") as f:
124             yml = yaml.load(f)
125
126         if "resources" not in yml:
127             continue
128
129         vm_types.extend(get_vm_types(yml["resources"]))
130
131     return set(vm_types)