Merge "Push policy adapter code (adapted from ECOMP)"
[optf/osdf.git] / adapters / sdc / constraint_handler.py
1 # -------------------------------------------------------------------------\r
2 #   Copyright (c) 2015-2017 AT&T Intellectual Property\r
3 #\r
4 #   Licensed under the Apache License, Version 2.0 (the "License");\r
5 #   you may not use this file except in compliance with the License.\r
6 #   You may obtain a copy of the License at\r
7 #\r
8 #       http://www.apache.org/licenses/LICENSE-2.0\r
9 #\r
10 #   Unless required by applicable law or agreed to in writing, software\r
11 #   distributed under the License is distributed on an "AS IS" BASIS,\r
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 #   See the License for the specific language governing permissions and\r
14 #   limitations under the License.\r
15 #\r
16 # -------------------------------------------------------------------------\r
17 #\r
18 \r
19 from osdf.config.base import osdf_config\r
20 from osdf.utils.programming_utils import dot_notation\r
21 \r
22 ns = {'p': 'http://xmlns.onap.org/sdc/license-model/1.0'}\r
23 config_local = osdf_config.core\r
24 \r
25 \r
26 def choose_license(license_artifacts, order_info, service_type):\r
27     entitlement_pool_uuids = []\r
28     license_key_group_uuids = []\r
29 \r
30     for license_artifact in license_artifacts:\r
31         for feature in license_artifact.findall('./p:feature-group-list/', ns):\r
32             for entitlement in feature.findall('./p:entitlement-pool-list/', ns):\r
33                 if is_valid(entitlement, order_info, service_type):\r
34                     entitlement_pool_uuid = entitlement.find('p:entitlement-pool-uuid', ns).text\r
35                     entitlement_pool_uuids.append(entitlement_pool_uuid)\r
36             for license_key_group in feature.findall('./p:license-key-group-list/', ns):\r
37                 if is_valid(license_key_group, order_info, service_type):\r
38                     license_key_group_uuid = license_key_group.find('p:license-key-group-uuid', ns).text\r
39                     license_key_group_uuids.append(license_key_group_uuid)\r
40     return entitlement_pool_uuids, license_key_group_uuids\r
41 \r
42 \r
43 # element is expected to be a license-key-group or entitlement-pool\r
44 # if these elements diverge at a later date this method should be refactored\r
45 def is_valid(element, order_info, service_type):\r
46     for limit in element.findall('./p:sp-limits/p:limit', ns):\r
47         # description = limit.find('p:description', ns).text\r
48         metric_value = limit.find('p:values', ns).text\r
49         metric = limit.find('p:metric', ns).text\r
50         try:\r
51             order_value = dot_notation(order_info, config_local['service_info'][service_type][metric])\r
52             # print("The order has the value %s for the metric %s and the limit specifies the value %s. The limit has the description %s." % (order_value, metric, metric_value, description))\r
53             if isinstance(order_value, list): # it is possible a list is returned, for example a list of vnfs for vCPE\r
54                 for arr_value in order_value:\r
55                     if str(metric_value) != str(arr_value):\r
56                         return False\r
57             else:\r
58                 if str(metric_value) != str(order_value):\r
59                     return False\r
60         except KeyError:\r
61             return False\r
62     # vendor limits\r
63     for limit in element.findall('./p:vendor-limits/p:limit', ns):\r
64             # description = limit.find('p:description', ns).text\r
65             metric_value = limit.find('p:values', ns).text\r
66             metric = limit.find('p:metric', ns).text\r
67             try:\r
68                 order_value = dot_notation(order_info, config_local['service_info'][service_type][metric])\r
69                 if isinstance(order_value, list): # it is possible a list is returned, for example a list of vnfs for vCPE\r
70                     for arr_value in order_value:\r
71                         if str(metric_value) != str(arr_value):\r
72                             return False\r
73                 else:\r
74                     if str(metric_value) != str(order_value):\r
75                         return False\r
76                 # print("The order has the value %s for the metric %s and the limit specifies the value %s. The limit has the description %s." % (order_value, metric, metric_value, description))\r
77 \r
78             except KeyError:\r
79                 return False\r
80     return True\r
81 \r