Merge "Enhanced List Level flow with backward support"
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / ServiceInstances.java
index 45319de..5b827d9 100644 (file)
@@ -50,8 +50,9 @@ import org.onap.so.apihandlerinfra.exceptions.ApiException;
 import org.onap.so.apihandlerinfra.exceptions.RecipeNotFoundException;
 import org.onap.so.apihandlerinfra.exceptions.RequestDbFailureException;
 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
-import org.onap.so.apihandlerinfra.exceptions.VfModuleNotFoundException;
+import org.onap.so.apihandlerinfra.infra.rest.handler.AbstractRestHandler;
 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
+import org.onap.so.constants.Status;
 import org.onap.so.db.catalog.beans.NetworkResource;
 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
 import org.onap.so.db.catalog.beans.Recipe;
@@ -86,6 +87,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.env.Environment;
 import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestTemplate;
 import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonMappingException;
@@ -96,7 +98,7 @@ import io.swagger.annotations.ApiOperation;
 @Component
 @Path("/onap/so/infra/serviceInstantiation")
 @Api(value = "/onap/so/infra/serviceInstantiation", description = "Infrastructure API Requests for Service Instances")
-public class ServiceInstances {
+public class ServiceInstances extends AbstractRestHandler {
 
     private static Logger logger = LoggerFactory.getLogger(MsoRequest.class);
     private static String NAME = "name";
@@ -107,6 +109,9 @@ public class ServiceInstances {
     @Autowired
     private Environment env;
 
+    @Autowired
+    private RestTemplate restTemplate;
+
     @Autowired
     private CatalogDbClient catalogDbClient;
 
@@ -203,7 +208,7 @@ public class ServiceInstances {
             @PathParam("serviceInstanceId") String serviceInstanceId, @Context ContainerRequestContext requestContext)
             throws ApiException {
         String requestId = requestHandlerUtils.getRequestId(requestContext);
-        HashMap<String, String> instanceIdMap = new HashMap<String, String>();
+        HashMap<String, String> instanceIdMap = new HashMap<>();
         instanceIdMap.put("serviceInstanceId", serviceInstanceId);
         return serviceInstances(request, Action.unassignInstance, instanceIdMap, version, requestId,
                 requestHandlerUtils.getRequestUri(requestContext, uriPrefix));
@@ -759,10 +764,9 @@ public class ServiceInstances {
 
     public Response serviceInstances(String requestJSON, Actions action, HashMap<String, String> instanceIdMap,
             String version, String requestId, String requestUri) throws ApiException {
-        String serviceInstanceId = (instanceIdMap == null) ? null : instanceIdMap.get("serviceInstanceId");
+        String serviceInstanceId;
         Boolean aLaCarte = null;
-        long startTime = System.currentTimeMillis();
-        ServiceInstancesRequest sir = null;
+        ServiceInstancesRequest sir;
         String apiVersion = version.substring(1);
 
         sir = requestHandlerUtils.convertJsonToServiceInstanceRequest(requestJSON, action, requestId, requestUri);
@@ -814,64 +818,20 @@ public class ServiceInstances {
         referencesResponse.setRequestId(requestId);
 
         serviceResponse.setRequestReferences(referencesResponse);
-        Boolean isBaseVfModule = false;
-
         RecipeLookupResult recipeLookupResult =
                 getServiceInstanceOrchestrationURI(sir, action, alaCarteFlag, currentActiveReq);
         String serviceInstanceType = requestHandlerUtils.getServiceType(requestScope, sir, alaCarteFlag);
-        ModelType modelType;
-        ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
-        if (action == Action.applyUpdatedConfig || action == Action.inPlaceSoftwareUpdate) {
-            modelType = ModelType.vnf;
-        } else if (action == Action.addMembers || action == Action.removeMembers) {
-            modelType = ModelType.instanceGroup;
-        } else {
-            modelType = modelInfo.getModelType();
-        }
-
-        if (modelType.equals(ModelType.vfModule)) {
-
-
-            // Get VF Module-specific base module indicator
-            VfModule vfm = null;
 
-            String modelVersionId = modelInfo.getModelVersionId();
-
-            if (modelVersionId != null) {
-                vfm = catalogDbClient.getVfModuleByModelUUID(modelVersionId);
-            } else if (modelInfo.getModelInvariantId() != null && modelInfo.getModelVersion() != null) {
-                vfm = catalogDbClient.getVfModuleByModelInvariantUUIDAndModelVersion(modelInfo.getModelInvariantId(),
-                        modelInfo.getModelVersion());
-            }
-
-            if (vfm != null) {
-                if (vfm.getIsBase()) {
-                    isBaseVfModule = true;
-                }
-            } else if (action == Action.createInstance || action == Action.updateInstance) {
-                // There is no entry for this vfModuleType with this version, if specified, in VF_MODULE table in
-                // Catalog DB.
-                // This request cannot proceed
-
-                String serviceVersionText = "";
-                if (sdcServiceModelVersion != null && !sdcServiceModelVersion.isEmpty()) {
-                    serviceVersionText = " with version " + sdcServiceModelVersion;
-                }
+        ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
+        ModelType modelType = requestHandlerUtils.getModelType(action, modelInfo);
 
-                String errorMessage = "VnfType " + vnfType + " and VF Module Model Name " + modelInfo.getModelName()
-                        + serviceVersionText + " not found in MSO Catalog DB";
-                ErrorLoggerInfo errorLoggerInfo =
-                        new ErrorLoggerInfo.Builder(MessageEnum.APIH_DB_ATTRIBUTE_NOT_FOUND, ErrorCode.DataError)
-                                .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
-                VfModuleNotFoundException vfModuleException = new VfModuleNotFoundException.Builder(errorMessage,
-                        HttpStatus.SC_NOT_FOUND, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
-                requestHandlerUtils.updateStatus(currentActiveReq, Status.FAILED, vfModuleException.getMessage());
+        Boolean isBaseVfModule = false;
 
-                throw vfModuleException;
-            }
+        if (modelType.equals(ModelType.vfModule)) {
+            isBaseVfModule = requestHandlerUtils.getIsBaseVfModule(modelInfo, action, vnfType, sdcServiceModelVersion,
+                    currentActiveReq);
         }
 
-
         serviceInstanceId = requestHandlerUtils.setServiceInstanceId(requestScope, sir);
         String vnfId = "";
         String vfModuleId = "";
@@ -958,7 +918,6 @@ public class ServiceInstances {
             String requestId, String requestUri, ContainerRequestContext requestContext) throws ApiException {
         String instanceGroupId = instanceIdMap.get(CommonConstants.INSTANCE_GROUP_INSTANCE_ID);
         Boolean aLaCarte = true;
-        long startTime = System.currentTimeMillis();
         String apiVersion = version.substring(1);
         ServiceInstancesRequest sir = new ServiceInstancesRequest();
         sir.setInstanceGroupId(instanceGroupId);
@@ -1030,7 +989,7 @@ public class ServiceInstances {
                 .orElse("");
     }
 
-    private RecipeLookupResult getServiceInstanceOrchestrationURI(ServiceInstancesRequest sir, Actions action,
+    protected RecipeLookupResult getServiceInstanceOrchestrationURI(ServiceInstancesRequest sir, Actions action,
             boolean alaCarteFlag, InfraActiveRequests currentActiveReq) throws ApiException {
         RecipeLookupResult recipeLookupResult = null;
         // if the aLaCarte flag is set to TRUE, the API-H should choose the VID_DEFAULT recipe for the requested action
@@ -1275,8 +1234,7 @@ public class ServiceInstances {
         return mapFlatMapToNameValue(userParams);
     }
 
-    private Service serviceMapper(Map<String, Object> params)
-            throws JsonProcessingException, IOException, JsonParseException, JsonMappingException {
+    private Service serviceMapper(Map<String, Object> params) throws IOException {
         ObjectMapper obj = new ObjectMapper();
         String input = obj.writeValueAsString(params.get("service"));
         return obj.readValue(input, Service.class);
@@ -1313,7 +1271,7 @@ public class ServiceInstances {
             instanceList = servInstReq.getRequestDetails().getRelatedInstanceList();
         }
 
-        Recipe recipe = null;
+        Recipe recipe;
         String defaultSource = requestHandlerUtils.getDefaultModel(servInstReq);
         String modelCustomizationId = modelInfo.getModelCustomizationId();
         String modelCustomizationName = modelInfo.getModelCustomizationName();
@@ -1603,13 +1561,11 @@ public class ServiceInstances {
 
     private Response configurationRecipeLookup(String requestJSON, Action action, HashMap<String, String> instanceIdMap,
             String version, String requestId, String requestUri) throws ApiException {
-        String serviceInstanceId = (instanceIdMap == null) ? null : instanceIdMap.get("serviceInstanceId");
+        String serviceInstanceId;
         Boolean aLaCarte = null;
         String apiVersion = version.substring(1);
         boolean inProgress = false;
-
-        long startTime = System.currentTimeMillis();
-        ServiceInstancesRequest sir = null;
+        ServiceInstancesRequest sir;
 
         sir = requestHandlerUtils.convertJsonToServiceInstanceRequest(requestJSON, action, requestId, requestUri);
         String requestScope = requestHandlerUtils.deriveRequestScope(action, sir, requestUri);