1 # -------------------------------------------------------------------------
2 # Copyright (c) 2015-2017 AT&T Intellectual Property
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 # -------------------------------------------------------------------------
21 from jinja2 import Template
23 import osdf.optimizers.placementopt.conductor.translation as tr
24 from osdf.adapters.policy.utils import group_policies_gen
25 from osdf.utils.programming_utils import list_flatten
28 def conductor_api_builder(request_json, flat_policies: list, local_config,
29 template="osdf/templates/conductor_interface.json"):
30 """Build an OSDF southbound API call for HAS-Conductor/Placement optimization
31 :param request_json: parameter data received from a client
32 :param flat_policies: policy data received from the policy platform (flat policies)
33 :param template: template to generate southbound API call to conductor
34 :param local_config: local configuration file with pointers for the service specific information
35 :param prov_status: provStatus retrieved from Subscriber policy
36 :return: json to be sent to Conductor/placement optimization
38 templ = Template(open(template).read())
39 gp = group_policies_gen(flat_policies, local_config)
40 demand_vnf_name_list = []
42 for placementDemand in request_json['placementInfo']['placementDemands']:
43 demand_vnf_name_list.append(placementDemand['resourceModuleName'].lower())
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 capacity_policy_list = tr.gen_capacity_policy(demand_vnf_name_list, gp['vim_fit'])
56 hpa_policy_list = tr.gen_hpa_policy(demand_vnf_name_list, gp['hpa'])
57 req_params_dict = tr.get_opt_query_data(request_json, gp['optimizationQueryPolicy'])
58 conductor_policies = [attribute_policy_list, distance_to_location_policy_list, inventory_policy_list,
59 resource_instance_policy_list, resource_region_policy_list, zone_policy_list,
60 reservation_policy_list, capacity_policy_list, hpa_policy_list]
61 filtered_policies = [x for x in conductor_policies if len(x) > 0]
62 policy_groups = list_flatten(filtered_policies)
63 req_info = request_json['requestInfo']
64 request_type = req_info.get('requestType', None)
65 rendered_req = templ.render(
66 requestType=request_type,
67 demand_list=demand_list,
68 policy_groups=policy_groups,
69 optimization_policies=optimization_policy_list,
70 name=req_info['requestId'],
71 timeout=req_info['timeout'],
72 limit=req_info['numSolutions'],
73 service_type=request_json['serviceInfo']['serviceName'],
74 service_id=request_json['serviceInfo']['serviceInstanceId'],
75 latitude=req_params_dict.get("customerLatitude", 0.0),
76 longitude=req_params_dict.get("customerLongitude", 0.0),
78 json_payload = json.dumps(json.loads(rendered_req)) # need this because template's JSON is ugly!