Populate serviceResourceId in homing solution
[optf/osdf.git] / osdf / optimizers / placementopt / conductor / translation.py
index e703c36..f74f461 100644 (file)
@@ -54,12 +54,13 @@ def gen_optimization_policy(vnf_list, optimization_policy):
     for policy in optimization_policy:
         content = policy['content']
         parameter_list = []
+        parameters = ["cloud_version", "hpa_score"]
 
         for attr in content['objectiveParameter']['parameterAttributes']:
-            parameter = attr['parameter'] if attr['parameter'] == "cloud_version" else attr['parameter']+"_between"
+            parameter = attr['parameter'] if attr['parameter'] in parameters else attr['parameter']+"_between"
             vnfs = get_matching_vnfs(attr['resources'], vnf_list)
             for vnf in vnfs:
-                value = [vnf] if attr['parameter'] == "cloud_version" else [attr['customerLocationInfo'], vnf]
+                value = [vnf] if attr['parameter'] in parameters else [attr['customerLocationInfo'], vnf]
                 parameter_list.append({
                     attr['operator']: [attr['weight'], {parameter: value}]
                 })
@@ -77,11 +78,13 @@ def get_matching_vnfs(resources, vnf_list, match_type="intersection"):
     :param match_type: "intersection" or "all" or "any" (any => send all_vnfs if there is any intersection)
     :return: List of matching VNFs
     """
+    resources_lcase = [x.lower() for x in resources]
     if match_type == "all":  # don't bother with any comparisons
-        return resources if set(resources) <= set(vnf_list) else None
-    common_vnfs = set(vnf_list) & set(resources)
+        return resources if set(resources_lcase) <= set(vnf_list) else None
+    common_vnfs = set(vnf_list) & set(resources_lcase)
+    common_resources = [x for x in resources if x.lower() in common_vnfs]
     if match_type == "intersection":  # specifically requested intersection
-        return list(common_vnfs)
+        return list(common_resources)
     return resources if common_vnfs else None  # "any" match => all resources to be returned
 
 
@@ -209,7 +212,8 @@ def get_candidates_demands(demand):
 def get_policy_properties(demand, policies):
     """Get policy_properties for cases where there is a match with the demand"""
     for policy in policies:
-        if demand['resourceModuleName'] not in set(policy['content'].get('resources', [])):
+        policy_demands = set([x.lower() for x in policy['content'].get('resources', [])])
+        if demand['resourceModuleName'].lower() not in policy_demands:
             continue  # no match for this policy
         for policy_property in policy['content']['vnfProperties']:
             yield policy_property
@@ -221,22 +225,18 @@ def get_demand_properties(demand, policies):
     for policy_property in get_policy_properties(demand, policies):
         prop = dict(inventory_provider=policy_property['inventoryProvider'],
                     inventory_type=policy_property['inventoryType'],
-                    service_type=demand['serviceResourceId'])
-        attributes = policy_config_mapping['attributes']
-        prop['attributes'] = {
-            'customer-id': policy_property['customerId'],
-            'orchestration-status': "",
-            'model-invariant-id': demand['resourceModelInfo']['modelInvariantId'],
-            'model-version-id': demand['resourceModelInfo']['modelVersionId'],
-            'service-type': demand['serviceResourceId'],
-            'equipment-role': policy_property['equipmentRole']
-        }
-        # if 'attributes' in policy_property:
-        #     prop['attributes'] = get_augmented_policy_attributes(policy_property, demand)
-        # for k1, v1, k2, v2 in policy_config_mapping['extra_fields']:
-        #     if k1 == v1:
-        #         prop[k2] = v2
-        prop.update(get_candidates_demands(demand))  # for excluded and partial-rehoming cases
+                    service_type=demand['serviceResourceId'],
+                    service_resource_id=demand['serviceResourceId'])
+        prop['attributes'] = dict()
+        prop['attributes'].update({'global-customer-id': policy_property['customerId']}
+                                  if policy_property['customerId'] else {})
+        prop['attributes'].update({'model-invariant-id': demand['resourceModelInfo']['modelInvariantId']}
+                                  if demand['resourceModelInfo']['modelInvariantId'] else {})
+        prop['attributes'].update({'model-version-id': demand['resourceModelInfo']['modelVersionId']}
+                                  if demand['resourceModelInfo']['modelVersionId'] else {})
+        prop['attributes'].update({'equipment-role': policy_property['equipmentRole']}
+                                  if policy_property['equipmentRole'] else {})
+        prop.update(get_candidates_demands(demand))
         demand_properties.append(prop)
     return demand_properties
 
@@ -249,6 +249,7 @@ def gen_demands(req_json, vnf_policies):
     """
     demand_dictionary = {}
     for demand in req_json['placementInfo']['placementDemands']:
-        demand_dictionary.update(
-            {demand['resourceModuleName']: get_demand_properties(demand, vnf_policies)})
+        prop = get_demand_properties(demand, vnf_policies)
+        if len(prop) > 0:
+            demand_dictionary.update({demand['resourceModuleName']: prop})
     return demand_dictionary