New policies and required code changes
[optf/osdf.git] / osdf / optimizers / placementopt / conductor / api_builder.py
1 # -------------------------------------------------------------------------
2 #   Copyright (c) 2015-2017 AT&T Intellectual Property
3 #
4 #   Licensed under the Apache License, Version 2.0 (the "License");
5 #   you may not use this file except in compliance with the License.
6 #   You may obtain a copy of the License at
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #   Unless required by applicable law or agreed to in writing, software
11 #   distributed under the License is distributed on an "AS IS" BASIS,
12 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 #   See the License for the specific language governing permissions and
14 #   limitations under the License.
15 #
16 # -------------------------------------------------------------------------
17 #
18
19 import copy
20 import json
21 from jinja2 import Template
22 from osdf.utils.programming_utils import list_flatten, dot_notation
23 import osdf.optimizers.placementopt.conductor.translation as tr
24 from osdf.adapters.policy.utils import group_policies
25
26
27 def conductor_api_builder(request_json, flat_policies: list, local_config, prov_status,
28                           template="templates/conductor_interface.json"):
29     """Build a SNIRO southbound API call for Conductor/Placement optimization
30     :param request_json: parameter data received from a client
31     :param flat_policies: policy data received from the policy platform (flat policies)
32     :param template: template to generate southbound API call to conductor
33     :param local_config: local configuration file with pointers for the service specific information
34     :param prov_status: provStatus retrieved from Subscriber policy
35     :return: json to be sent to Conductor/placement optimization
36     """
37     templ = Template(open(template).read())
38     gp = group_policies(flat_policies)
39     demand_vnf_name_list = []
40
41     for placementDemand in request_json['placementInfo']['placementDemands']:
42         demand_vnf_name_list.append(placementDemand['resourceModuleName'])
43
44     demand_list = tr.gen_demands(request_json, gp['vnfPolicy'])
45     attribute_policy_list = tr.gen_attribute_policy(demand_vnf_name_list, gp['attribute'])
46     distance_to_location_policy_list = tr.gen_distance_to_location_policy(
47         demand_vnf_name_list, gp['distance_to_location'])
48     inventory_policy_list = tr.gen_inventory_group_policy(demand_vnf_name_list, gp['inventory_group'])
49     resource_instance_policy_list = tr.gen_resource_instance_policy(
50         demand_vnf_name_list, gp['instance_fit'])
51     resource_region_policy_list = tr.gen_resource_region_policy(demand_vnf_name_list, gp['region_fit'])
52     zone_policy_list = tr.gen_zone_policy(demand_vnf_name_list, gp['zone'])
53     optimization_policy_list = tr.gen_optimization_policy(demand_vnf_name_list, gp['placementOptimization'])
54     reservation_policy_list = tr.gen_reservation_policy(demand_vnf_name_list, gp['instance_reservation'])
55     conductor_policies = [attribute_policy_list, distance_to_location_policy_list, inventory_policy_list,
56                           resource_instance_policy_list, resource_region_policy_list, zone_policy_list]
57     filtered_policies = [x for x in conductor_policies if len(x) > 0]
58     policy_groups = list_flatten(filtered_policies)
59     reservation_policies = [x for x in reservation_policy_list if len(x) > 0]
60     reservation_groups = list_flatten(reservation_policies)
61     req_info = request_json['requestInfo']
62     model_name = request_json['serviceInfo']['serviceName']
63     service_type = model_name
64     # service_type = data_mapping.get_service_type(model_name)
65     service_info = local_config.get('service_info', {}).get(service_type, {})
66     order_info = {}
67     if 'orderInfo' in request_json["placementInfo"]:
68         order_info = json.loads(request_json["placementInfo"]["orderInfo"])
69     request_type = req_info.get('requestType', None)
70     subs_com_site_id = ""
71     if 'subscriberInfo' in request_json['placementInfo']: 
72         subs_com_site_id = request_json['placementInfo']['subscriberInfo'].get('subscriberCommonSiteId', "")
73     rendered_req = None
74     if service_type == 'vCPE':
75         # data_mapping.normalize_user_params(order_info)
76         rendered_req = templ.render(
77             requestType=request_type,
78             chosenComplex=subs_com_site_id,
79             demand_list=demand_list,
80             policy_groups=policy_groups,
81             optimization_policies=optimization_policy_list,
82             name=req_info['requestId'],
83             timeout=req_info['timeout'],
84             limit=req_info['numSolutions'],
85             serviceType=service_type,
86             serviceInstance=request_json['serviceInfo']['serviceInstanceId'],
87             provStatus = prov_status,
88             chosenRegion=order_info.get('requestParameters',{}).get('lcpCloudRegionId'),
89             json=json)
90     json_payload = json.dumps(json.loads(rendered_req)) # need this because template's JSON is ugly!
91     return json_payload
92
93
94 def retrieve_node(req_json, reference):
95     """
96     Get the child node(s) from the dot-notation [reference] and parent [req_json].
97     For placement and other requests, there are encoded JSONs inside the request or policy,
98     so we need to expand it and then do a search over the parent plus expanded JSON.
99     """
100     req_json_copy = copy.deepcopy(req_json)  # since we expand the JSON in place, we work on a copy
101     if 'orderInfo' in req_json_copy['placementInfo']:
102         req_json_copy['placementInfo']['orderInfo'] = json.loads(req_json_copy['placementInfo']['orderInfo'])
103     info = dot_notation(req_json_copy, reference)
104     return list_flatten(info) if isinstance(info, list) else info
105