Update AAI plugin to fetch service profile
[optf/has.git] / conductor / conductor / data / plugins / inventory_provider / utils / aai_utils.py
1 #
2 # -------------------------------------------------------------------------
3 #   Copyright (C) 2020 Wipro Limited.
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #       http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 #
17 # -------------------------------------------------------------------------
18 #
19
20 QUERY_PARAMS = {'service_instance': ["service-instance-id", "service-instance-name", "environment-context",
21                                      "workload-context", "model-invariant-id", "model-version-id", "widget-model-id",
22                                      "widget-model-version", "service-instance-location-id", "orchestration-status"],
23                 'nst': ["model-role"]
24                 }
25
26
27 def convert_hyphen_to_under_score(hyphened_dict):
28     converted_dict = dict()
29     if hyphened_dict:
30         for key in hyphened_dict:
31             if '-' in key:
32                 converted_dict[key.replace('-', '_').lower()] = hyphened_dict[key]
33             else:
34                 converted_dict[key] = hyphened_dict[key]
35         if 'resource_version' in converted_dict:
36             converted_dict.pop('resource_version')
37     return converted_dict
38
39
40 def add_query_params(filtering_attributes):
41     if not filtering_attributes:
42         return ''
43     url = '?'
44     for key, value in filtering_attributes.items():
45         url = f'{url}{key}={value}&'
46     return url
47
48
49 def add_query_params_and_depth(filtering_attributes, depth):
50     url_with_query_params = add_query_params(filtering_attributes)
51     if url_with_query_params:
52         return f"{url_with_query_params}depth={depth}"
53     else:
54         return f"?depth={depth}"
55
56
57 def get_first_level_and_second_level_filter(filtering_attributes, aai_node):
58     second_level_filters = dict()
59     valid_query_params = QUERY_PARAMS.get(aai_node)
60     for key in list(filtering_attributes):
61         if key not in valid_query_params:
62             second_level_filters[key] = filtering_attributes[key]
63             del filtering_attributes[key]
64     return second_level_filters
65
66
67 def get_inv_values_for_second_level_filter(second_level_filters, nssi_instance):
68     if not second_level_filters:
69         return None
70     inventory_attributes = dict()
71     for key in list(second_level_filters):
72         inventory_attributes[key] = nssi_instance.get(key)
73     return inventory_attributes
74
75
76 def get_instance_info(nxi_instance):
77     nxi_dict = dict()
78     nxi_dict['instance_id'] = nxi_instance.get('service-instance-id')
79     nxi_dict['instance_name'] = nxi_instance.get('service-instance-name')
80     if nxi_instance.get('workload-context'):
81         nxi_dict['domain'] = nxi_instance.get('workload-context')
82     return nxi_dict
83
84
85 def get_nst_info(nst_instance):
86     nst_dict = {}
87     nst_dict['model_invariant_id'] = nst_instance.get('model-invariant-id')
88     nst_dict['model_type'] = nst_instance.get('model-type')
89     nst_dict['model_role'] = nst_instance.get('model-role')
90     return nst_dict
91
92
93 def get_model_ver_info(model_version):
94     for key in list(model_version):
95         if "model-elements" in key:
96             del model_version["model-elements"]
97     return model_version
98
99
100 def get_profiles(profile_instances, profile_type):
101     profile_type_plural = profile_type + 's'
102     return [x[profile_type_plural][profile_type][0] for x in profile_instances]