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 # -------------------------------------------------------------------------
20 from osdf.utils.data_conversion import text_to_symbol
21 from osdf.utils import data_mapping
23 def gen_optimization_policy(vnf_list, optimization_policy):
24 """Generate optimization policy details to pass to Conductor
25 :param vnf_list: List of vnf's to used in placement request
26 :param optimization_policy: optimization policy information provided in the incoming request
27 :return: List of optimization policies in a format required by Conductor
29 optimization_policy_list = []
30 for policy in optimization_policy:
31 content = policy['content']
34 for attr in content['objectiveParameter']['parameterAttributes']:
35 parameter = attr['parameter'] if attr['parameter'] == "cloud_version" else attr['parameter']+"_between"
36 for res in attr['resource']:
37 vnf = get_matching_vnf(res, vnf_list)
38 value = [vnf] if attr['parameter'] == "cloud_version" else [attr['customerLocationInfo'], vnf]
39 parameter_list.append({
40 attr['operator']: [attr['weight'], {parameter: value}]
43 optimization_policy_list.append({
44 content['objective']: {content['objectiveParameter']['operator']: parameter_list }
46 return optimization_policy_list
49 def get_matching_vnf(resource, vnf_list):
57 def get_matching_vnfs(resources, vnf_list, match_type="intersection"):
58 """Get a list of matching VNFs from the list of resources
60 :param vnf_list: List of vnf's to used in placement request
61 :param match_type: "intersection" or "all" or "any" (any => send all_vnfs if there is any intersection)
62 :return: List of matching VNFs
66 for resource in resources:
68 common_vnfs.append(vnf)
69 if match_type == "intersection": # specifically requested intersection
71 elif common_vnfs or match_type == "all": # ("any" and common) OR "all"
76 def gen_policy_instance(vnf_list, resource_policy, match_type="intersection", rtype=None):
77 """Generate a list of policies
78 :param vnf_list: List of vnf's to used in placement request
79 :param resource_policy: policy for this specific resource
80 :param match_type: How to match the vnf_names with the vnf_list (intersection or "any")
81 intersection => return intersection; "any" implies return all vnf_names if intersection is not null
82 :param rtype: resource type (e.g. resourceRegionProperty or resourceInstanceProperty)
83 None => no controller information added to the policy specification to Conductor
84 :return: resource policy list in a format required by Conductor
86 resource_policy_list = []
88 for policy in resource_policy:
89 pc = policy['content']
90 demands = get_matching_vnfs(pc['resourceInstanceType'], vnf_list, match_type=match_type)
91 resource = {pc['identity']: {'type': pc['type'], 'demands': demands}}
94 resource[pc['identity']]['properties'] = {'controller': pc[rtype]['controller'],
95 'request': json.loads(pc[rtype]['request'])}
96 if demands and len(demands) != 0:
97 resource_policy_list.append(resource)
98 related_policies.append(policy)
99 return resource_policy_list, related_policies
102 def gen_resource_instance_policy(vnf_list, resource_instance_policy):
103 """Get policies governing resource instances in order to populate the Conductor API call"""
104 cur_policies, _ = gen_policy_instance(vnf_list, resource_instance_policy, rtype='resourceInstanceProperty')
108 def gen_resource_region_policy(vnf_list, resource_region_policy):
109 """Get policies governing resource region in order to populate the Conductor API call"""
110 cur_policies, _ = gen_policy_instance(vnf_list, resource_region_policy, rtype='resourceRegionProperty')
114 def gen_inventory_group_policy(vnf_list, inventory_group_policy):
115 """Get policies governing inventory group in order to populate the Conductor API call"""
116 cur_policies, _ = gen_policy_instance(vnf_list, inventory_group_policy, rtype=None)
120 def gen_reservation_policy(vnf_list, reservation_policy):
121 """Get policies governing resource instances in order to populate the Conductor API call"""
122 cur_policies, _ = gen_policy_instance(vnf_list, reservation_policy, rtype='instanceReservationProperty')
126 def gen_distance_to_location_policy(vnf_list, distance_to_location_policy):
127 """Get policies governing distance-to-location for VNFs in order to populate the Conductor API call"""
128 cur_policies, related_policies = gen_policy_instance(vnf_list, distance_to_location_policy, rtype=None)
129 for p_new, p_main in zip(cur_policies, related_policies): # add additional fields to each policy
130 properties = p_main['content']['distanceToLocationProperty']
131 pcp_d = properties['distanceCondition']
132 p_new[p_main['content']['identity']]['properties'] = {
133 'distance': text_to_symbol[pcp_d['operator']] + " " + pcp_d['value'].lower(),
134 'location': properties['locationInfo']
139 def gen_attribute_policy(vnf_list, attribute_policy):
140 """Get policies governing attributes of VNFs in order to populate the Conductor API call"""
141 cur_policies, related_policies = gen_policy_instance(vnf_list, attribute_policy, rtype=None)
142 for p_new, p_main in zip(cur_policies, related_policies): # add additional fields to each policy
143 properties = p_main['content']['cloudAttributeProperty']
144 p_new[p_main['content']['identity']]['properties'] = {
146 'hypervisor': properties.get('hypervisor', ''),
147 'cloud_version': properties.get('cloudVersion', ''),
148 'cloud_type': properties.get('cloudType', ''),
149 'dataplane': properties.get('dataPlane', ''),
150 'network_roles': properties.get('networkRoles', ''),
151 'complex': properties.get('complex', ''),
152 'state': properties.get('state', ''),
153 'country': properties.get('country', ''),
154 'geo_region': properties.get('geoRegion', ''),
155 'exclusivity_groups': properties.get('exclusivityGroups', ''),
156 'replication_role': properties.get('replicationRole', '')
162 def gen_zone_policy(vnf_list, zone_policy):
163 """Get zone policies in order to populate the Conductor API call"""
164 cur_policies, related_policies = gen_policy_instance(vnf_list, zone_policy, rtype=None)
165 for p_new, p_main in zip(cur_policies, related_policies): # add additional fields to each policy
166 pmz = p_main['content']['zoneProperty']
167 p_new[p_main['content']['identity']]['properties'] = {'category': pmz['category'], 'qualifier': pmz['qualifier']}
171 def get_demand_properties(demand, policies):
172 """Get list demand properties objects (named tuples) from policy"""
173 def _get_candidates(candidate_info):
174 return [dict(inventory_type=x['candidateType'], candidate_id=x['candidates']) for x in candidate_info]
176 for policy in policies:
177 for resourceInstanceType in policy['content']['resourceInstanceType']:
178 if resourceInstanceType in demand['resourceModuleName']:
179 for x in policy['content']['property']:
180 property = dict(inventory_provider=x['inventoryProvider'],
181 inventory_type=x['inventoryType'],
182 service_resource_id=demand['serviceResourceId'])
183 if 'attributes' in x:
185 for k,v in x['attributes'].items():
186 key=data_mapping.convert(k)
188 if(key=="model-invariant-id"):
189 attributes[key]=demand['resourceModelInfo']['modelInvariantId']
190 elif(key=="model-version-id"):
191 attributes[key]=demand['resourceModelInfo']['modelVersionId']
192 property.update({"attributes": attributes})
193 if x['inventoryType'] == "cloud":
194 property['region'] = {'get_param': "CHOSEN_REGION"}
195 if 'exclusionCandidateInfo' in demand:
196 property['excluded_candidates'] = _get_candidates(demand['exclusionCandidateInfo'])
197 if 'requiredCandidateInfo' in demand:
198 property['required_candidates'] = _get_candidates(demand['requiredCandidateInfo'])
199 properties.append(property)
200 if len(properties) == 0:
201 properties.append(dict(customer_id="", service_type="", inventory_provider="", inventory_type=""))
205 def gen_demands(req_json, vnf_policies):
206 """Generate list of demands based on request and VNF policies
207 :param req_json: Request object from the client (e.g. MSO)
208 :param vnf_policies: Policies associated with demand resources (e.g. from grouped_policies['vnfPolicy'])
209 :return: list of demand parameters to populate the Conductor API call
211 demand_dictionary = {}
212 for placementDemand in req_json['placementDemand']:
213 demand_dictionary.update({placementDemand['resourceModuleName']: get_demand_properties(placementDemand, vnf_policies)})
215 return demand_dictionary