Custom Query Code
[policy/models.git] / models-interactions / model-actors / actor.so / src / main / java / org / onap / policy / controlloop / actor / so / SoActorServiceProvider.java
index 6d8fa4b..25b3a4e 100644 (file)
@@ -28,6 +28,11 @@ import java.lang.reflect.Type;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import org.onap.aai.domain.yang.CloudRegion;
+import org.onap.aai.domain.yang.GenericVnf;
+import org.onap.aai.domain.yang.ServiceInstance;
+import org.onap.aai.domain.yang.Tenant;
+import org.onap.policy.aai.AaiCqResponse;
 import org.onap.policy.aai.AaiNqExtraProperty;
 import org.onap.policy.aai.AaiNqInventoryResponseItem;
 import org.onap.policy.aai.AaiNqResponseWrapper;
@@ -62,10 +67,10 @@ public class SoActorServiceProvider implements Actor {
     private static final String RECIPE_VF_MODULE_CREATE = "VF Module Create";
     private static final String RECIPE_VF_MODULE_DELETE = "VF Module Delete";
 
-    private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_VF_MODULE_CREATE,
-            RECIPE_VF_MODULE_DELETE);
-    private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
-                    .put(RECIPE_VF_MODULE_CREATE, ImmutableList.of(TARGET_VFC))
+    private static final ImmutableList<String> recipes =
+            ImmutableList.of(RECIPE_VF_MODULE_CREATE, RECIPE_VF_MODULE_DELETE);
+    private static final ImmutableMap<String, List<String>> targets =
+            new ImmutableMap.Builder<String, List<String>>().put(RECIPE_VF_MODULE_CREATE, ImmutableList.of(TARGET_VFC))
                     .put(RECIPE_VF_MODULE_DELETE, ImmutableList.of(TARGET_VFC)).build();
 
     // name of request parameters within policy payload
@@ -109,19 +114,17 @@ public class SoActorServiceProvider implements Actor {
     }
 
     /**
-     * Constructs a SO request conforming to the lcm API. The actual request is
-     * constructed and then placed in a wrapper object used to send through DMAAP.
+     * Constructs a SO request conforming to the lcm API. The actual request is constructed and then placed in a wrapper
+     * object used to send through DMAAP.
      *
      * @param onset the event that is reporting the alert for policy to perform an action
-     * @param operation the control loop operation specifying the actor, operation,
-     *        target, etc.
-     * @param policy the policy the was specified from the yaml generated by CLAMP or
-     *        through the Policy GUI/API
+     * @param operation the control loop operation specifying the actor, operation, target, etc.
+     * @param policy the policy the was specified from the yaml generated by CLAMP or through the Policy GUI/API
      * @param aaiResponseWrapper wrapper for AAI vserver named-query response
      * @return a SO request conforming to the lcm API using the DMAAP wrapper
      */
     public SoRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy,
-                    AaiNqResponseWrapper aaiResponseWrapper) {
+            AaiNqResponseWrapper aaiResponseWrapper) {
         if (!SO_ACTOR.equals(policy.getActor()) || !recipes().contains(policy.getRecipe())) {
             return null;
         }
@@ -138,10 +141,10 @@ public class SoActorServiceProvider implements Actor {
         // Extract the items we're interested in from the response
         try {
             vnfItem = aaiResponseWrapper.getAaiNqResponse().getInventoryResponseItems().get(0).getItems()
-                            .getInventoryResponseItems().get(0);
+                    .getInventoryResponseItems().get(0);
         } catch (Exception e) {
             logger.error("VNF Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiResponseWrapper),
-                            e);
+                    e);
             return null;
         }
 
@@ -149,16 +152,16 @@ public class SoActorServiceProvider implements Actor {
             vnfServiceItem = vnfItem.getItems().getInventoryResponseItems().get(0);
         } catch (Exception e) {
             logger.error("VNF Service Item not found in AAI response {}",
-                            Serialization.gsonPretty.toJson(aaiResponseWrapper), e);
+                    Serialization.gsonPretty.toJson(aaiResponseWrapper), e);
             return null;
         }
 
         try {
             tenantItem = aaiResponseWrapper.getAaiNqResponse().getInventoryResponseItems().get(0).getItems()
-                            .getInventoryResponseItems().get(1);
+                    .getInventoryResponseItems().get(1);
         } catch (Exception e) {
             logger.error("Tenant Item not found in AAI response {}",
-                            Serialization.gsonPretty.toJson(aaiResponseWrapper), e);
+                    Serialization.gsonPretty.toJson(aaiResponseWrapper), e);
             return null;
         }
 
@@ -175,8 +178,7 @@ public class SoActorServiceProvider implements Actor {
 
         // Construct SO Request for a policy's recipe
         if (RECIPE_VF_MODULE_CREATE.equals(policy.getRecipe())) {
-            return constructCreateRequest(aaiResponseWrapper, policy, tenantItem, vnfItem, vnfServiceItem,
-                    soModelInfo);
+            return constructCreateRequest(aaiResponseWrapper, policy, tenantItem, vnfItem, vnfServiceItem, soModelInfo);
         } else if (RECIPE_VF_MODULE_DELETE.equals(policy.getRecipe())) {
             return constructDeleteRequest(tenantItem, vnfItem, vnfServiceItem, soModelInfo, policy);
         } else {
@@ -187,12 +189,9 @@ public class SoActorServiceProvider implements Actor {
     private SoModelInfo prepareSoModelInfo(Policy policy) {
 
         SoModelInfo soModelInfo = new SoModelInfo();
-        if ((policy.getTarget() != null
-                && (policy.getTarget().getModelCustomizationId() != null))
-                && (policy.getTarget().getModelInvariantId() != null)
-                && (policy.getTarget().getModelName() != null)
-                && (policy.getTarget().getModelVersion() != null)
-                && (policy.getTarget().getModelVersionId() != null)) {
+        if ((policy.getTarget() != null && (policy.getTarget().getModelCustomizationId() != null))
+                && (policy.getTarget().getModelInvariantId() != null) && (policy.getTarget().getModelName() != null)
+                && (policy.getTarget().getModelVersion() != null) && (policy.getTarget().getModelVersionId() != null)) {
 
             soModelInfo.setModelCustomizationId(policy.getTarget().getModelCustomizationId());
             soModelInfo.setModelInvariantId(policy.getTarget().getModelInvariantId());
@@ -210,17 +209,16 @@ public class SoActorServiceProvider implements Actor {
      * Construct SO request to create vf-module.
      *
      * @param aaiResponseWrapper the AAI response containing the VF modules
-     * @param policy             the policy
-     * @param tenantItem         tenant item from A&AI named-query response
-     * @param vnfItem            vnf item from A&AI named-query response
-     * @param vnfServiceItem     vnf service item from A&AI named-query response
-     * @param vfModuleItem       vf module item from A&AI named-query response
+     * @param policy the policy
+     * @param tenantItem tenant item from A&AI named-query response
+     * @param vnfItem vnf item from A&AI named-query response
+     * @param vnfServiceItem vnf service item from A&AI named-query response
+     * @param vfModuleItem vf module item from A&AI named-query response
      * @return SO create vf-module request
      */
     private SoRequest constructCreateRequest(AaiNqResponseWrapper aaiResponseWrapper, Policy policy,
-                                             AaiNqInventoryResponseItem tenantItem, AaiNqInventoryResponseItem vnfItem,
-                                             AaiNqInventoryResponseItem vnfServiceItem,
-                                             SoModelInfo vfModuleItem) {
+            AaiNqInventoryResponseItem tenantItem, AaiNqInventoryResponseItem vnfItem,
+            AaiNqInventoryResponseItem vnfServiceItem, SoModelInfo vfModuleItem) {
         SoRequest request = new SoRequest();
         request.setOperationType(SoOperationType.SCALE_OUT);
         //
@@ -283,9 +281,8 @@ public class SoActorServiceProvider implements Actor {
                         .setModelVersionId(prop.getPropertyValue());
             }
         }
-        relatedInstanceListElement2.getRelatedInstance().getModelInfo()
-                .setModelCustomizationName(vnfItem.getGenericVnf().getVnfType()
-                        .substring(vnfItem.getGenericVnf().getVnfType().lastIndexOf('/') + 1));
+        relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelCustomizationName(vnfItem
+                .getGenericVnf().getVnfType().substring(vnfItem.getGenericVnf().getVnfType().lastIndexOf('/') + 1));
         relatedInstanceListElement2.getRelatedInstance().getModelInfo()
                 .setModelCustomizationId(vnfItem.getGenericVnf().getModelCustomizationId());
 
@@ -300,8 +297,8 @@ public class SoActorServiceProvider implements Actor {
         buildConfigurationParameters(policy, request.getRequestDetails());
         // Save the instance IDs for the VNF and service to static fields
         // vfModuleId is not required for the create vf-module
-        preserveInstanceIds(vnfItem.getGenericVnf().getVnfId(), vnfServiceItem.getServiceInstance()
-                .getServiceInstanceId(), null);
+        preserveInstanceIds(vnfItem.getGenericVnf().getVnfId(),
+                vnfServiceItem.getServiceInstance().getServiceInstanceId(), null);
         if (logger.isDebugEnabled()) {
             logger.debug("Constructed SO request: {}", Serialization.gsonPretty.toJson(request));
         }
@@ -311,14 +308,14 @@ public class SoActorServiceProvider implements Actor {
     /**
      * Construct SO request to delete vf-module.
      *
-     * @param tenantItem         tenant item from A&AI named-query response
-     * @param vnfItem            vnf item from A&AI named-query response
-     * @param vnfServiceItem     vnf service item from A&AI named-query response
-     * @param vfModuleItem       vf module item from A&AI named-query response
+     * @param tenantItem tenant item from A&AI named-query response
+     * @param vnfItem vnf item from A&AI named-query response
+     * @param vnfServiceItem vnf service item from A&AI named-query response
+     * @param vfModuleItem vf module item from A&AI named-query response
      * @return SO delete vf-module request
      */
-    private SoRequest constructDeleteRequest(AaiNqInventoryResponseItem tenantItem, AaiNqInventoryResponseItem
-            vnfItem, AaiNqInventoryResponseItem vnfServiceItem, SoModelInfo vfModuleItem, Policy policy) {
+    private SoRequest constructDeleteRequest(AaiNqInventoryResponseItem tenantItem, AaiNqInventoryResponseItem vnfItem,
+            AaiNqInventoryResponseItem vnfServiceItem, SoModelInfo vfModuleItem, Policy policy) {
         SoRequest request = new SoRequest();
         request.setOperationType(SoOperationType.DELETE_VF_MODULE);
         request.setRequestDetails(new SoRequestDetails());
@@ -332,8 +329,8 @@ public class SoActorServiceProvider implements Actor {
         // requestInfo
         request.getRequestDetails().setRequestInfo(constructRequestInfo());
         // Save the instance IDs for the VNF, service and vfModule to static fields
-        preserveInstanceIds(vnfItem.getGenericVnf().getVnfId(), vnfServiceItem.getServiceInstance()
-                .getServiceInstanceId(), null);
+        preserveInstanceIds(vnfItem.getGenericVnf().getVnfId(),
+                vnfServiceItem.getServiceInstance().getServiceInstanceId(), null);
 
         if (logger.isDebugEnabled()) {
             logger.debug("Constructed SO request: {}", Serialization.gsonPretty.toJson(request));
@@ -363,14 +360,13 @@ public class SoActorServiceProvider implements Actor {
     private SoCloudConfiguration constructCloudConfiguration(AaiNqInventoryResponseItem tenantItem) {
         SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
         cloudConfiguration.setTenantId(tenantItem.getTenant().getTenantId());
-        cloudConfiguration.setLcpCloudRegionId(tenantItem.getItems().getInventoryResponseItems().get(0)
-                .getCloudRegion().getCloudRegionId());
+        cloudConfiguration.setLcpCloudRegionId(
+                tenantItem.getItems().getInventoryResponseItems().get(0).getCloudRegion().getCloudRegionId());
         return cloudConfiguration;
     }
 
     /**
-     * This method is needed to get the serviceInstanceId and vnfInstanceId which is used
-     * in the asyncSORestCall.
+     * This method is needed to get the serviceInstanceId and vnfInstanceId which is used in the asyncSORestCall.
      *
      * @param requestId the request Id
      * @param callback callback method
@@ -379,20 +375,19 @@ public class SoActorServiceProvider implements Actor {
      * @param user username
      * @param password password
      */
-    public static void sendRequest(String requestId, SoManager.SoCallback callback, Object request,
-            String url, String user, String password) {
+    public static void sendRequest(String requestId, SoManager.SoCallback callback, Object request, String url,
+            String user, String password) {
         SoManager soManager = new SoManager(url, user, password);
         soManager.asyncSoRestCall(requestId, callback, lastServiceItemServiceInstanceId, lastVNFItemVnfId,
                 lastVfModuleItemVfModuleInstanceId, (SoRequest) request);
     }
 
     /**
-     * Find the base or non base VF module item in an AAI response.
-     * If there is more than one item, then the <i>last</i> item is returned
+     * Find the base or non base VF module item in an AAI response. If there is more than one item, then the <i>last</i>
+     * item is returned
      *
      * @param aaiResponseWrapper the AAI response containing the VF modules
-     * @param baseFlag true if we are searching for the base, false if we are searching
-     *        for the non base
+     * @param baseFlag true if we are searching for the base, false if we are searching for the non base
      * @return the base or non base VF module item or null if the module was not found
      */
     private AaiNqInventoryResponseItem findVfModule(AaiNqResponseWrapper aaiResponseWrapper, boolean baseFlag) {
@@ -445,17 +440,220 @@ public class SoActorServiceProvider implements Actor {
     }
 
     /**
-     * This method is called to remember the last service instance ID, VNF Item VNF ID and vf module ID.
-     * Note these fields are static, beware for multithreaded deployments
+     * This method is called to remember the last service instance ID, VNF Item VNF ID and vf module ID. Note these
+     * fields are static, beware for multithreaded deployments
      *
      * @param vnfInstanceId update the last VNF instance ID to this value
      * @param serviceInstanceId update the last service instance ID to this value
      * @param vfModuleId update the vfModule instance ID to this value
      */
     private static void preserveInstanceIds(final String vnfInstanceId, final String serviceInstanceId,
-                                            final String vfModuleId) {
+            final String vfModuleId) {
         lastVNFItemVnfId = vnfInstanceId;
         lastServiceItemServiceInstanceId = serviceInstanceId;
         lastVfModuleItemVfModuleInstanceId = vfModuleId;
     }
+
+    /**
+     * Constructs a SO request conforming to the lcm API. The actual request is constructed and then placed in a wrapper
+     * object used to send through DMAAP.
+     *
+     * @param onset the event that is reporting the alert for policy to perform an action
+     * @param operation the control loop operation specifying the actor, operation, target, etc.
+     * @param policy the policy the was specified from the yaml generated by CLAMP or through the Policy GUI/API
+     * @param aaiCqResponse response from A&AI custom query
+     * @return a SO request conforming to the lcm API using the DMAAP wrapper
+     */
+    public SoRequest constructRequestCq(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy,
+            AaiCqResponse aaiCqResponse) {
+        if (!SO_ACTOR.equals(policy.getActor()) || !recipes().contains(policy.getRecipe())) {
+            return null;
+        }
+
+        // A&AI named query should have been performed by now. If not, return null
+        if (aaiCqResponse == null) {
+            return null;
+        }
+
+        GenericVnf vnfItem;
+        ServiceInstance vnfServiceItem;
+        Tenant tenantItem;
+        CloudRegion cloudRegionItem;
+
+        // Extract the items we're interested in from the response
+        try {
+            vnfItem = aaiCqResponse.getDefaultGenericVnf();
+        } catch (Exception e) {
+            logger.error("VNF Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiCqResponse), e);
+            return null;
+        }
+
+        try {
+            vnfServiceItem = aaiCqResponse.getServiceInstance();
+        } catch (Exception e) {
+            logger.error("VNF Service Item not found in AAI response {}",
+                    Serialization.gsonPretty.toJson(aaiCqResponse), e);
+            return null;
+        }
+
+        try {
+            tenantItem = aaiCqResponse.getDefaultTenant();
+        } catch (Exception e) {
+            logger.error("Tenant Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiCqResponse), e);
+            return null;
+        }
+
+        try {
+            cloudRegionItem = aaiCqResponse.getDefaultCloudRegion();
+        } catch (Exception e) {
+            logger.error("Tenant Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiCqResponse), e);
+            return null;
+        }
+
+        SoModelInfo soModelInfo = prepareSoModelInfo(policy);
+
+        // Report the error vf module is not found
+        if (soModelInfo == null) {
+            logger.error("vf module is not found.");
+            return null;
+        }
+
+        // Construct SO Request for a policy's recipe
+        if (RECIPE_VF_MODULE_CREATE.equals(policy.getRecipe())) {
+            return constructCreateRequestCq(aaiCqResponse, policy, tenantItem, vnfItem, vnfServiceItem, soModelInfo,
+                    cloudRegionItem);
+        } else if (RECIPE_VF_MODULE_DELETE.equals(policy.getRecipe())) {
+            return constructDeleteRequestCq(tenantItem, vnfItem, vnfServiceItem, soModelInfo, policy, cloudRegionItem);
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Construct the So request, based on Custom Query response from A&AI.
+     *
+     * @param aaiCqResponse Custom query response from A&AI
+     * @param policy policy information
+     * @param tenantItem Tenant from CQ response
+     * @param vnfItem Generic VNF from CQ response
+     * @param vnfServiceItem Service Instance from CQ response
+     * @param vfModuleItem VF Module from CustomQuery response
+     * @param cloudRegionItem Cloud Region from Custom query response
+     * @return SoRequest well formed So Request
+     */
+    private SoRequest constructCreateRequestCq(AaiCqResponse aaiCqResponse, Policy policy, Tenant tenantItem,
+            GenericVnf vnfItem, ServiceInstance vnfServiceItem, SoModelInfo vfModuleItem, CloudRegion cloudRegionItem) {
+        SoRequest request = new SoRequest();
+        request.setOperationType(SoOperationType.SCALE_OUT);
+        //
+        //
+        // Do NOT send So the requestId, they do not support this field
+        //
+        request.setRequestDetails(new SoRequestDetails());
+        request.getRequestDetails().setRequestParameters(new SoRequestParameters());
+        request.getRequestDetails().getRequestParameters().setUserParams(null);
+
+        // cloudConfiguration
+        request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
+        // modelInfo
+        request.getRequestDetails().setModelInfo(vfModuleItem);
+
+        // requestInfo
+        request.getRequestDetails().setRequestInfo(constructRequestInfo());
+        String vfModuleName = aaiCqResponse.getDefaultVfModule().getVfModuleName();
+        request.getRequestDetails().getRequestInfo().setInstanceName(vfModuleName);
+
+        // relatedInstanceList
+        SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
+        SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
+        relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
+        relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
+
+        // Service Item (Note that Model Name and Model Version are not available in A&AI schema for ServiceInstance)
+        relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
+        relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
+        relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
+        relatedInstanceListElement1.getRelatedInstance().getModelInfo()
+                .setModelInvariantId(vnfServiceItem.getModelInvariantId());
+        relatedInstanceListElement1.getRelatedInstance().getModelInfo()
+                .setModelVersionId(vnfServiceItem.getModelVersionId());
+
+
+        // VNF Item (Note that Model Name, Model Version, and Model Customization Name are not available in A&AI schema
+        // for Generic VNF)
+        relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
+        relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
+        relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
+        relatedInstanceListElement2.getRelatedInstance().getModelInfo()
+                .setModelInvariantId(vnfItem.getModelInvariantId());
+        relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
+        relatedInstanceListElement2.getRelatedInstance().getModelInfo()
+                .setModelCustomizationId(vnfItem.getModelCustomizationId());
+
+        // Insert the Service Item and VNF Item
+        request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
+        request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
+
+        // Request Parameters
+        buildRequestParameters(policy, request.getRequestDetails());
+
+        // Configuration Parameters
+        buildConfigurationParameters(policy, request.getRequestDetails());
+        // Save the instance IDs for the VNF and service to static fields
+        // vfModuleId is not required for the create vf-module
+        preserveInstanceIds(vnfItem.getVnfId(), vnfServiceItem.getServiceInstanceId(), null);
+        if (logger.isDebugEnabled()) {
+            logger.debug("Constructed SO request: {}", Serialization.gsonPretty.toJson(request));
+        }
+        return request;
+    }
+
+    /**
+     * constructs delete request for So.
+     *
+     * @param tenantItem Tenant from A&AI CQ request
+     * @param vnfItem Generic VNF from A&AI CQ request
+     * @param vnfServiceItem ServiceInstance from A&AI CQ request
+     * @param vfModuleItem VFModule from A&AI CQ request
+     * @param policy policy information
+     * @param cloudRegionItem CloudRegion from A&AI CQ request
+     * @return SoRequest deleted
+     */
+    private SoRequest constructDeleteRequestCq(Tenant tenantItem, GenericVnf vnfItem, ServiceInstance vnfServiceItem,
+            SoModelInfo vfModuleItem, Policy policy, CloudRegion cloudRegionItem) {
+        SoRequest request = new SoRequest();
+        request.setOperationType(SoOperationType.DELETE_VF_MODULE);
+        request.setRequestDetails(new SoRequestDetails());
+        request.getRequestDetails().setRelatedInstanceList(null);
+        request.getRequestDetails().setConfigurationParameters(null);
+
+        // cloudConfiguration
+        request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
+        // modelInfo
+        request.getRequestDetails().setModelInfo(prepareSoModelInfo(policy));
+        // requestInfo
+        request.getRequestDetails().setRequestInfo(constructRequestInfo());
+        // Save the instance IDs for the VNF, service and vfModule to static fields
+        preserveInstanceIds(vnfItem.getVnfId(), vnfServiceItem.getServiceInstanceId(), null);
+
+        if (logger.isDebugEnabled()) {
+            logger.debug("Constructed SO request: {}", Serialization.gsonPretty.toJson(request));
+        }
+        return request;
+    }
+
+
+    /**
+     * Construct cloudConfiguration for the SO requestDetails. Overridden for custom query.
+     *
+     * @param tenantItem tenant item from A&AI named-query response
+     * @return SO cloud configuration
+     */
+    private SoCloudConfiguration constructCloudConfigurationCq(Tenant tenantItem, CloudRegion cloudRegionItem) {
+        SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
+        cloudConfiguration.setTenantId(tenantItem.getTenantId());
+        cloudConfiguration.setLcpCloudRegionId(cloudRegionItem.getCloudRegionId());
+        return cloudConfiguration;
+    }
+
 }