[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 / MsoRequest.java
index c077558..af507c6 100644 (file)
@@ -7,12 +7,14 @@
  * ================================================================================
  * Modifications Copyright (c) 2019 Samsung
  * ================================================================================
+ * Modifications Copyright (c) 2020 Nokia
+ * ================================================================================
  * 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.
@@ -30,7 +32,7 @@ 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.StringTokenizer;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
@@ -40,7 +42,9 @@ import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
+import org.onap.aai.domain.yang.Tenant;
 import org.onap.so.apihandler.common.ResponseBuilder;
+import org.onap.so.apihandlerinfra.infra.rest.AAIDataRetrieval;
 import org.onap.so.apihandlerinfra.tasksbeans.TasksRequest;
 import org.onap.so.apihandlerinfra.validation.ApplyUpdatedConfigValidation;
 import org.onap.so.apihandlerinfra.validation.CloudConfigurationValidation;
@@ -69,7 +73,6 @@ import org.onap.so.db.request.client.RequestsDbClient;
 import org.onap.so.exceptions.ValidationException;
 import org.onap.so.logger.LogConstants;
 import org.onap.so.serviceinstancebeans.CloudConfiguration;
-import org.onap.so.serviceinstancebeans.InstanceDirection;
 import org.onap.so.serviceinstancebeans.ModelInfo;
 import org.onap.so.serviceinstancebeans.ModelType;
 import org.onap.so.serviceinstancebeans.PolicyException;
@@ -96,7 +99,6 @@ import com.fasterxml.jackson.core.JsonGenerationException;
 import com.fasterxml.jackson.databind.JsonMappingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 
-
 @Component
 public class MsoRequest {
 
@@ -106,6 +108,9 @@ public class MsoRequest {
     @Autowired
     private ResponseBuilder builder;
 
+    @Autowired
+    private AAIDataRetrieval aaiDataRet;
+
     @Value("${mso.enforceDLP:false}")
     private boolean enforceDLP;
 
@@ -148,11 +153,9 @@ public class MsoRequest {
     }
 
 
-
     // Parse request JSON
-    public void parse(ServiceInstancesRequest sir, HashMap<String, String> instanceIdMap, Actions action,
-            String version, String originalRequestJSON, int reqVersion, Boolean aLaCarteFlag)
-            throws ValidationException, IOException {
+    public void parse(ServiceInstancesRequest sir, Map<String, String> instanceIdMap, Actions action, String version,
+            String originalRequestJSON, int reqVersion, Boolean aLaCarteFlag) throws ValidationException, IOException {
 
         logger.debug("Validating the Service Instance request");
         List<ValidationRule> rules = new ArrayList<>();
@@ -222,44 +225,31 @@ public class MsoRequest {
         }
     }
 
-    public Map<String, List<String>> getOrchestrationFilters(MultivaluedMap<String, String> queryParams)
-            throws ValidationException {
-
-        String queryParam = null;
+    public Map<String, List<String>> getOrchestrationFilters(MultivaluedMap<String, String> queryParams) {
+        final String FILTER_KEY = "filter";
         Map<String, List<String>> orchestrationFilterParams = new HashMap<>();
 
+        Optional.ofNullable(queryParams.get(FILTER_KEY)).ifPresent(listValues -> listValues
+                .forEach(value -> addValueToOrchestrationFilterParamsMap(orchestrationFilterParams, value)));
 
-        for (Entry<String, List<String>> entry : queryParams.entrySet()) {
-            queryParam = entry.getKey();
-
-            try {
-                if ("filter".equalsIgnoreCase(queryParam)) {
-                    for (String value : entry.getValue()) {
-                        StringTokenizer st = new StringTokenizer(value, ":");
-
-                        int counter = 0;
-                        String mapKey = null;
-                        List<String> orchestrationList = new ArrayList<>();
-                        while (st.hasMoreElements()) {
-                            if (counter == 0) {
-                                mapKey = st.nextElement() + "";
-                            } else {
-                                orchestrationList.add(st.nextElement() + "");
-                            }
-                            counter++;
-                        }
-                        orchestrationFilterParams.put(mapKey, orchestrationList);
-                    }
-                }
+        return orchestrationFilterParams;
+    }
 
-            } catch (Exception e) {
-                throw new ValidationException("QueryParam ServiceInfo", e);
-            }
+    private void addValueToOrchestrationFilterParamsMap(Map<String, List<String>> orchestrationFilterParams,
+            String value) {
+        final String TOKEN_DELIMITER = ":";
+        StringTokenizer stringTokenizer = new StringTokenizer(value, TOKEN_DELIMITER);
 
+        if (!stringTokenizer.hasMoreTokens()) {
+            return;
+        }
+        String mapKey = stringTokenizer.nextToken();
+        List<String> orchestrationList = new ArrayList<>();
+        while (stringTokenizer.hasMoreTokens()) {
+            orchestrationList.add(stringTokenizer.nextToken());
         }
 
-
-        return orchestrationFilterParams;
+        orchestrationFilterParams.put(mapKey, orchestrationList);
     }
 
     public InfraActiveRequests createRequestObject(ServiceInstancesRequest servInsReq, Actions action, String requestId,
@@ -324,11 +314,30 @@ public class MsoRequest {
                     aq.setVnfId(servInsReq.getVnfInstanceId());
                 }
 
-                if (servInsReq.getPnfId() != null) {
+                if (servInsReq.getPnfName() != null) {
                     aq.setRequestScope(requestScope);
-                    aq.setPnfId(servInsReq.getPnfId());
+                    aq.setPnfName(servInsReq.getPnfName());
                 }
 
+                if (servInsReq.getRequestDetails() != null && servInsReq.getRequestDetails().getRequestInfo() != null
+                        && servInsReq.getRequestDetails().getRequestInfo().getProductFamilyId() != null) {
+                    logger.debug("Retrieving productFamilyName to put into requests db");
+
+                    org.onap.aai.domain.yang.Service service =
+                            aaiDataRet.getService(servInsReq.getRequestDetails().getRequestInfo().getProductFamilyId());
+                    if (service != null) {
+                        logger.debug("Found service by service-id");
+                        String productFamilyName = service.getServiceDescription();
+                        if (productFamilyName != null) {
+                            aq.setProductFamilyName(productFamilyName);
+                        }
+                    }
+                }
+
+                aq.setProductFamilyName(getProductFamilyNameFromAAI(servInsReq));
+
+                aq.setTenantName(getTenantNameFromAAI(servInsReq));
+
                 if (ModelType.service.name().equalsIgnoreCase(requestScope)) {
                     if (servInsReq.getRequestDetails().getRequestInfo() != null) {
                         if (servInsReq.getRequestDetails().getRequestInfo().getInstanceName() != null) {
@@ -367,7 +376,9 @@ public class MsoRequest {
                     aq.setInstanceGroupId(servInsReq.getInstanceGroupId());
                     aq.setInstanceGroupName(requestInfo.getInstanceName());
                 }
-                if (ModelType.vnf.name().equalsIgnoreCase(requestScope)) {
+
+                if (ModelType.vnf.name().equalsIgnoreCase(requestScope)
+                        || ModelType.cnf.name().equalsIgnoreCase(requestScope)) {
                     if (requestInfo != null) {
                         aq.setVnfName(requestInfo.getInstanceName());
                     }
@@ -442,7 +453,7 @@ public class MsoRequest {
     }
 
     public void createErrorRequestRecord(Status status, String requestId, String errorMessage, Actions action,
-            String requestScope, String requestJSON) {
+            String requestScope, String requestJSON, String serviceInstanceId, ServiceInstancesRequest sir) {
         try {
             InfraActiveRequests request = new InfraActiveRequests(requestId);
             Timestamp startTimeStamp = new Timestamp(System.currentTimeMillis());
@@ -454,9 +465,23 @@ public class MsoRequest {
             request.setRequestAction(action.toString());
             request.setRequestScope(requestScope);
             request.setRequestBody(requestJSON);
+            if (serviceInstanceId != null) {
+                request.setServiceInstanceId(serviceInstanceId);
+            }
             Timestamp endTimeStamp = new Timestamp(System.currentTimeMillis());
             request.setEndTime(endTimeStamp);
             request.setRequestUrl(MDC.get(LogConstants.HTTP_URL));
+            if (sir != null) {
+                if (sir.getRequestDetails() != null && sir.getRequestDetails().getRequestInfo() != null) {
+                    request.setRequestorId(sir.getRequestDetails().getRequestInfo().getRequestorId());
+                    request.setSource(sir.getRequestDetails().getRequestInfo().getSource());
+                    if (ModelType.service.name().equalsIgnoreCase(requestScope)) {
+                        if (sir.getRequestDetails().getRequestInfo().getInstanceName() != null) {
+                            request.setServiceInstanceName(sir.getRequestDetails().getRequestInfo().getInstanceName());
+                        }
+                    }
+                }
+            }
             requestsDbClient.save(request);
         } catch (Exception e) {
             logger.error("Exception when updating record in DB", e);
@@ -464,8 +489,6 @@ public class MsoRequest {
         }
     }
 
-
-
     public Response buildResponse(int httpResponseCode, String errorCode, InfraActiveRequests inProgress) {
         return buildResponseWithError(httpResponseCode, errorCode, inProgress, null);
     }
@@ -474,7 +497,6 @@ public class MsoRequest {
             String errorString) {
 
 
-
         // Log the failed request into the MSO Requests database
 
         return Response.status(httpResponseCode).entity(null).build();
@@ -487,7 +509,6 @@ public class MsoRequest {
     }
 
 
-
     public String getServiceType(VnfInputs vnfInputs) {
         if (vnfInputs.getServiceType() != null)
             return vnfInputs.getServiceType();
@@ -625,156 +646,106 @@ public class MsoRequest {
     }
 
 
-    public String getVfModuleType(ServiceInstancesRequest sir, String requestScope, Actions action, int reqVersion) {
-
-        String serviceInstanceType = null;
+    public String getVfModuleType(ServiceInstancesRequest sir, String requestScope) {
 
-        String vnfType = null;
+        String vnfType;
         String vfModuleType = null;
-        String vfModuleModelName = null;
+        String vfModuleModelName;
         ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
         RelatedInstanceList[] instanceList = sir.getRequestDetails().getRelatedInstanceList();
         String serviceModelName = null;
         String vnfModelName = null;
-        String asdcServiceModelVersion = null;
         String volumeGroupId = null;
-        boolean isRelatedServiceInstancePresent = false;
-        boolean isRelatedVnfInstancePresent = false;
-        boolean isSourceVnfPresent = false;
-        boolean isDestinationVnfPresent = false;
-        boolean isConnectionPointPresent = false;
-
-        if (instanceList != null) {
-            for (RelatedInstanceList relatedInstanceList : instanceList) {
-                RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
-                ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo();
 
-                if (action != Action.deleteInstance) {
-
-                    if (ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
-                        if (InstanceDirection.source.equals(relatedInstance.getInstanceDirection())
-                                && relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
-                            isSourceVnfPresent = true;
-                        } else if (InstanceDirection.destination.equals(relatedInstance.getInstanceDirection())
-                                && (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
-                                        || (relatedInstanceModelInfo.getModelType().equals(ModelType.pnf)
-                                                && reqVersion == 6))) {
-                            isDestinationVnfPresent = true;
-                        }
-                    }
-
-                    if (ModelType.connectionPoint.equals(relatedInstanceModelInfo.getModelType())
-                            && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
-                        isConnectionPointPresent = true;
-                    }
-                }
-
-
-                if (relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
-                    isRelatedServiceInstancePresent = true;
-                    serviceModelName = relatedInstanceModelInfo.getModelName();
-                    asdcServiceModelVersion = relatedInstanceModelInfo.getModelVersion();
-                } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
-                        && !(ModelType.configuration.name().equalsIgnoreCase(requestScope))) {
-                    isRelatedVnfInstancePresent = true;
-                    vnfModelName = relatedInstanceModelInfo.getModelCustomizationName();
-                } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)) {
-                    volumeGroupId = relatedInstance.getInstanceId();
-                }
+        if (instanceList == null) {
+            return null;
+        }
+        for (RelatedInstanceList relatedInstanceList : instanceList) {
+            RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
+            ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo();
+
+            if (relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
+                serviceModelName = relatedInstanceModelInfo.getModelName();
+            } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
+                    && !(ModelType.configuration.name().equalsIgnoreCase(requestScope))) {
+                vnfModelName = relatedInstanceModelInfo.getModelCustomizationName();
+            } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)) {
+                volumeGroupId = relatedInstance.getInstanceId();
             }
-
-            if (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())) {
-                serviceInstanceType = serviceModelName;
-                vnfType = serviceModelName + "/" + vnfModelName;
-            } else if (requestScope.equalsIgnoreCase(ModelType.vfModule.name())) {
-                vfModuleModelName = modelInfo.getModelName();
-                serviceInstanceType = serviceModelName;
-                vnfType = serviceModelName + "/" + vnfModelName;
-                vfModuleType = vnfType + "::" + vfModuleModelName;
-                sir.setVolumeGroupInstanceId(volumeGroupId);
-            } else if (requestScope.equalsIgnoreCase(ModelType.vnf.name()))
-                vnfType = serviceModelName + "/" + sir.getRequestDetails().getModelInfo().getModelCustomizationName();
-
+        }
+        if (requestScope.equalsIgnoreCase(ModelType.vfModule.name())) {
+            vfModuleModelName = modelInfo.getModelName();
+            vnfType = serviceModelName + "/" + vnfModelName;
+            vfModuleType = vnfType + "::" + vfModuleModelName;
+            sir.setVolumeGroupInstanceId(volumeGroupId);
         }
 
         return vfModuleType;
 
     }
 
-    public String getVnfType(ServiceInstancesRequest sir, String requestScope, Actions action, int reqVersion) {
+    public String getVnfType(ServiceInstancesRequest sir, String requestScope) {
 
-        String serviceInstanceType = null;
-        String networkType = null;
         String vnfType = null;
-        String vfModuleType = null;
-        String vfModuleModelName = null;
-        ModelInfo modelInfo = sir.getRequestDetails().getModelInfo();
         RelatedInstanceList[] instanceList = sir.getRequestDetails().getRelatedInstanceList();
         String serviceModelName = null;
         String vnfModelName = null;
-        String asdcServiceModelVersion = null;
         String volumeGroupId = null;
-        boolean isRelatedServiceInstancePresent = false;
-        boolean isRelatedVnfInstancePresent = false;
-        boolean isSourceVnfPresent = false;
-        boolean isDestinationVnfPresent = false;
-        boolean isConnectionPointPresent = false;
-
-        if (instanceList != null) {
-            for (RelatedInstanceList relatedInstanceList : instanceList) {
-                RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
-                ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo();
 
-                if (action != Action.deleteInstance) {
-
-                    if (ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
-                        if (InstanceDirection.source.equals(relatedInstance.getInstanceDirection())
-                                && relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)) {
-                            isSourceVnfPresent = true;
-                        } else if (InstanceDirection.destination.equals(relatedInstance.getInstanceDirection())
-                                && (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
-                                        || (relatedInstanceModelInfo.getModelType().equals(ModelType.pnf)
-                                                && reqVersion == 6))) {
-                            isDestinationVnfPresent = true;
-                        }
-                    }
+        if (instanceList == null) {
+            return null;
+        }
+        for (RelatedInstanceList relatedInstanceList : instanceList) {
+            RelatedInstance relatedInstance = relatedInstanceList.getRelatedInstance();
+            ModelInfo relatedInstanceModelInfo = relatedInstance.getModelInfo();
+
+            if (relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
+                serviceModelName = relatedInstanceModelInfo.getModelName();
+            } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
+                    && !(ModelType.configuration.name().equalsIgnoreCase(requestScope))) {
+                vnfModelName = relatedInstanceModelInfo.getModelCustomizationName();
+            } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)) {
+                volumeGroupId = relatedInstance.getInstanceId();
+            }
+        }
 
-                    if (ModelType.connectionPoint.equals(relatedInstanceModelInfo.getModelType())
-                            && ModelType.configuration.name().equalsIgnoreCase(requestScope)) {
-                        isConnectionPointPresent = true;
-                    }
-                }
+        if (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())) {
+            vnfType = serviceModelName + "/" + vnfModelName;
+        } else if (requestScope.equalsIgnoreCase(ModelType.vfModule.name())) {
+            vnfType = serviceModelName + "/" + vnfModelName;
+            sir.setVolumeGroupInstanceId(volumeGroupId);
+        } else if (requestScope.equalsIgnoreCase(ModelType.vnf.name())
+                || requestScope.equalsIgnoreCase(ModelType.cnf.name()))
+            vnfType = serviceModelName + "/" + sir.getRequestDetails().getModelInfo().getModelCustomizationName();
 
+        return vnfType;
+    }
 
-                if (relatedInstanceModelInfo.getModelType().equals(ModelType.service)) {
-                    isRelatedServiceInstancePresent = true;
-                    serviceModelName = relatedInstanceModelInfo.getModelName();
-                    asdcServiceModelVersion = relatedInstanceModelInfo.getModelVersion();
-                } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.vnf)
-                        && !(ModelType.configuration.name().equalsIgnoreCase(requestScope))) {
-                    isRelatedVnfInstancePresent = true;
-                    vnfModelName = relatedInstanceModelInfo.getModelCustomizationName();
-                } else if (relatedInstanceModelInfo.getModelType().equals(ModelType.volumeGroup)) {
-                    volumeGroupId = relatedInstance.getInstanceId();
-                }
+    protected String getTenantNameFromAAI(ServiceInstancesRequest servInsReq) {
+        String tenantName = null;
+        if (servInsReq.getRequestDetails() != null && servInsReq.getRequestDetails().getCloudConfiguration() != null
+                && servInsReq.getRequestDetails().getCloudConfiguration().getTenantId() != null) {
+            Tenant tenant = aaiDataRet.getTenant(servInsReq.getRequestDetails().getCloudConfiguration().getCloudOwner(),
+                    servInsReq.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId(),
+                    servInsReq.getRequestDetails().getCloudConfiguration().getTenantId());
+            if (tenant != null) {
+                tenantName = tenant.getTenantName();
             }
-
-            if (requestScope.equalsIgnoreCase(ModelType.volumeGroup.name())) {
-                serviceInstanceType = serviceModelName;
-                vnfType = serviceModelName + "/" + vnfModelName;
-            } else if (requestScope.equalsIgnoreCase(ModelType.vfModule.name())) {
-                vfModuleModelName = modelInfo.getModelName();
-                serviceInstanceType = serviceModelName;
-                vnfType = serviceModelName + "/" + vnfModelName;
-                vfModuleType = vnfType + "::" + vfModuleModelName;
-                sir.setVolumeGroupInstanceId(volumeGroupId);
-            } else if (requestScope.equalsIgnoreCase(ModelType.vnf.name()))
-                vnfType = serviceModelName + "/" + sir.getRequestDetails().getModelInfo().getModelCustomizationName();
-
         }
+        return tenantName;
+    }
 
-        return vnfType;
-
+    protected String getProductFamilyNameFromAAI(ServiceInstancesRequest servInsReq) {
+        String productFamilyName = null;
+        if (servInsReq.getRequestDetails() != null && servInsReq.getRequestDetails().getRequestInfo() != null
+                && servInsReq.getRequestDetails().getRequestInfo().getProductFamilyId() != null) {
+            org.onap.aai.domain.yang.Service service =
+                    aaiDataRet.getService(servInsReq.getRequestDetails().getRequestInfo().getProductFamilyId());
+            if (service != null) {
+                productFamilyName = service.getServiceDescription();
+            }
+        }
+        return productFamilyName;
     }
 
 }