[SO] Create changes for SO-API and BPMN-INFRA to support CNF's through ASD
[so.git] / mso-api-handlers / mso-api-handler-infra / src / main / java / org / onap / so / apihandlerinfra / RequestHandlerUtils.java
index a68309f..4338e61 100644 (file)
@@ -11,9 +11,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -33,7 +33,10 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
 import javax.ws.rs.container.ContainerRequestContext;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
@@ -326,7 +329,7 @@ public class RequestHandlerUtils extends AbstractRestHandler {
             String requestScope = requestScopeFromUri(requestUri);
 
             msoRequest.createErrorRequestRecord(Status.FAILED, requestId, validateException.getMessage(), action,
-                    requestScope, requestJSON);
+                    requestScope, requestJSON, null, null);
 
             throw validateException;
         }
@@ -403,7 +406,8 @@ public class RequestHandlerUtils extends AbstractRestHandler {
         if (StringUtils.isNotBlank(instanceId)) {
             if (ModelType.service.name().equalsIgnoreCase(requestScope)) {
                 currentActiveReq.setServiceInstanceId(instanceId);
-            } else if (ModelType.vnf.name().equalsIgnoreCase(requestScope)) {
+            } else if (ModelType.vnf.name().equalsIgnoreCase(requestScope)
+                    || ModelType.cnf.name().equalsIgnoreCase(requestScope)) {
                 currentActiveReq.setVnfId(instanceId);
             } else if (ModelType.vfModule.name().equalsIgnoreCase(requestScope)) {
                 currentActiveReq.setVfModuleId(instanceId);
@@ -518,20 +522,16 @@ public class RequestHandlerUtils extends AbstractRestHandler {
     }
 
     protected String setServiceInstanceId(String requestScope, ServiceInstancesRequest sir) {
+        String serviceInstanceId = null;
         if (sir.getServiceInstanceId() != null) {
-            return sir.getServiceInstanceId();
-        } else if (requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())) {
-            RelatedInstanceList[] relatedInstances = sir.getRequestDetails().getRelatedInstanceList();
-            if (relatedInstances != null) {
-                for (RelatedInstanceList relatedInstanceList : relatedInstances) {
-                    RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
-                    if (relatedInstance.getModelInfo().getModelType() == ModelType.service) {
-                        return relatedInstance.getInstanceId();
-                    }
-                }
+            serviceInstanceId = sir.getServiceInstanceId();
+        } else {
+            Optional<String> serviceInstanceIdForInstance = getServiceInstanceIdForInstanceGroup(requestScope, sir);
+            if (serviceInstanceIdForInstance.isPresent()) {
+                serviceInstanceId = serviceInstanceIdForInstance.get();
             }
         }
-        return null;
+        return serviceInstanceId;
     }
 
     private String requestScopeFromUri(String requestUri) {
@@ -548,6 +548,9 @@ public class RequestHandlerUtils extends AbstractRestHandler {
             requestScope = ModelType.vnf.name();
         } else if (requestUri.contains(ModelType.pnf.name())) {
             requestScope = ModelType.pnf.name();
+
+        } else if (requestUri.contains(ModelType.cnf.name())) {
+            requestScope = ModelType.cnf.name();
         } else {
             requestScope = ModelType.service.name();
         }
@@ -722,8 +725,14 @@ public class RequestHandlerUtils extends AbstractRestHandler {
 
                 throw validateException;
             }
+        }
+
+        else if (modelInfo.getModelType().equals(ModelType.pnf)) {
+            recipeLookupResult = new RecipeLookupResult("/mso/async/services/WorkflowActionBB", 180);
         } else if (modelInfo.getModelType().equals(ModelType.instanceGroup)) {
             recipeLookupResult = new RecipeLookupResult("/mso/async/services/WorkflowActionBB", 180);
+        } else if (modelInfo.getModelType().equals(ModelType.cnf)) {
+            recipeLookupResult = new RecipeLookupResult("/mso/async/services/WorkflowActionBB", 180);
         }
 
         if (recipeLookupResult == null) {
@@ -845,7 +854,11 @@ public class RequestHandlerUtils extends AbstractRestHandler {
             if (params.containsKey("service")) {
                 Service service = serviceMapper(params);
 
-                addUserParams(userParams, service.getInstanceParams());
+                // Filter out non-string params for backward compatibility
+                Map<String, String> svcStrParams = service.getInstanceParams().stream().map(Map::entrySet)
+                        .flatMap(Set::stream).filter(e -> e.getValue() instanceof String)
+                        .collect(Collectors.toMap(Entry::getKey, e -> (String) e.getValue()));
+                userParams.putAll(svcStrParams);
 
                 for (Networks network : service.getResources().getNetworks()) {
                     addUserParams(userParams, network.getInstanceParams());
@@ -916,6 +929,30 @@ public class RequestHandlerUtils extends AbstractRestHandler {
         return null;
     }
 
+    protected Optional<String> getServiceInstanceIdForValidationError(ServiceInstancesRequest sir,
+            HashMap<String, String> instanceIdMap, String requestScope) {
+        if (instanceIdMap != null && !instanceIdMap.isEmpty() && instanceIdMap.get("serviceInstanceId") != null) {
+            return Optional.of(instanceIdMap.get("serviceInstanceId"));
+        } else {
+            return getServiceInstanceIdForInstanceGroup(requestScope, sir);
+        }
+    }
+
+    protected Optional<String> getServiceInstanceIdForInstanceGroup(String requestScope, ServiceInstancesRequest sir) {
+        if (requestScope.equalsIgnoreCase(ModelType.instanceGroup.toString())) {
+            RelatedInstanceList[] relatedInstances = sir.getRequestDetails().getRelatedInstanceList();
+            if (relatedInstances != null) {
+                for (RelatedInstanceList relatedInstanceList : relatedInstances) {
+                    RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
+                    if (relatedInstance.getModelInfo().getModelType() == ModelType.service) {
+                        return Optional.ofNullable(relatedInstance.getInstanceId());
+                    }
+                }
+            }
+        }
+        return Optional.empty();
+    }
+
     private RecipeLookupResult getDefaultVnfUri(ServiceInstancesRequest sir, Actions action) {
         String defaultSource = getDefaultModel(sir);
         VnfRecipe vnfRecipe = catalogDbClient.getFirstVnfRecipeByNfRoleAndAction(defaultSource, action.toString());
@@ -1164,7 +1201,7 @@ public class RequestHandlerUtils extends AbstractRestHandler {
                     throw new ValidationException("vfModuleCustomization");
                 } else if (vfModule == null && vfmc != null) {
                     vfModule = vfmc.getVfModule(); // can't be null as vfModuleModelUUID is not-null property in
-                                                   // VfModuleCustomization table
+                    // VfModuleCustomization table
                 }
 
                 if (modelInfo.getModelVersionId() == null) {