Add NSSI candidate
[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                 }
24
25
26 def convert_hyphen_to_under_score(hyphened_dict):
27     converted_dict = dict()
28     if hyphened_dict:
29         for key in hyphened_dict:
30             if '-' in key:
31                 converted_dict[key.replace('-', '_')] = hyphened_dict[key]
32             else:
33                 converted_dict[key] = hyphened_dict[key]
34         if 'resource_version' in converted_dict:
35             converted_dict.pop('resource_version')
36     return converted_dict
37
38
39 def add_query_params(filtering_attributes):
40     if not filtering_attributes:
41         return ''
42     url = '?'
43     for key, value in filtering_attributes.items():
44         url = f'{url}{key}={value}&'
45     return url
46
47
48 def add_query_params_and_depth(filtering_attributes, depth):
49     url_with_query_params = add_query_params(filtering_attributes)
50     if url_with_query_params:
51         return f"{url_with_query_params}depth={depth}"
52     else:
53         return f"?depth={depth}"
54
55
56 def get_first_level_and_second_level_filter(filtering_attributes, aai_node):
57     second_level_filters = dict()
58     valid_query_params = QUERY_PARAMS.get(aai_node)
59     for key in list(filtering_attributes):
60         if key not in valid_query_params:
61             second_level_filters[key] = filtering_attributes[key]
62             del filtering_attributes[key]
63     return second_level_filters
64
65
66 def get_inv_values_for_second_level_filter(second_level_filters, nssi_instance):
67     if not second_level_filters:
68         return None
69     inventory_attributes = dict()
70     for key in list(second_level_filters):
71         inventory_attributes[key] = nssi_instance.get(key)
72     return inventory_attributes
73
74
75 def get_instance_info(nxi_instance):
76     nxi_dict = dict()
77     nxi_dict['instance_id'] = nxi_instance.get('service-instance-id')
78     nxi_dict['instance_name'] = nxi_instance.get('service-instance-name')
79     if nxi_instance.get('service-function'):
80         nxi_dict['domain'] = nxi_instance.get('service-function')
81     return nxi_dict