add new format to fetch requests from MSO to the CM
[vid.git] / vid-app-common / src / main / java / org / onap / vid / mso / MsoBusinessLogicImpl.java
index 9146e8f..2e141ad 100644 (file)
@@ -30,6 +30,7 @@ import static org.onap.vid.controller.MsoController.REQUEST_TYPE;
 import static org.onap.vid.controller.MsoController.SVC_INSTANCE_ID;
 import static org.onap.vid.controller.MsoController.VNF_INSTANCE_ID;
 import static org.onap.vid.controller.MsoController.WORKFLOW_ID;
+import static org.onap.vid.logging.Headers.PARTNER_NAME;
 import static org.onap.vid.mso.MsoProperties.MSO_REST_API_CLOUD_RESOURCES_REQUEST_STATUS;
 import static org.onap.vid.mso.MsoProperties.MSO_REST_API_OPERATIONAL_ENVIRONMENT_ACTIVATE;
 import static org.onap.vid.mso.MsoProperties.MSO_REST_API_OPERATIONAL_ENVIRONMENT_CREATE;
@@ -60,6 +61,8 @@ import javax.ws.rs.BadRequestException;
 import org.apache.commons.collections4.ListUtils;
 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
 import org.onap.portalsdk.core.util.SystemProperties;
+import org.onap.vid.aai.ExceptionWithRequestInfo;
+import org.onap.vid.aai.HttpResponseWithRequestInfo;
 import org.onap.vid.changeManagement.ChangeManagementRequest;
 import org.onap.vid.changeManagement.RequestDetailsWrapper;
 import org.onap.vid.changeManagement.WorkflowRequestDetail;
@@ -67,6 +70,7 @@ import org.onap.vid.controller.OperationalEnvironmentController;
 import org.onap.vid.exceptions.GenericUncheckedException;
 import org.onap.vid.model.SOWorkflowList;
 import org.onap.vid.model.SoftDeleteRequest;
+import org.onap.vid.model.probes.ErrorMetadata;
 import org.onap.vid.model.probes.ExternalComponentStatus;
 import org.onap.vid.model.probes.HttpRequestMetadata;
 import org.onap.vid.model.probes.StatusMetadata;
@@ -84,9 +88,11 @@ import org.onap.vid.mso.rest.RequestList;
 import org.onap.vid.mso.rest.RequestWrapper;
 import org.onap.vid.mso.rest.Task;
 import org.onap.vid.mso.rest.TaskList;
+import org.onap.vid.properties.Features;
+import org.onap.vid.utils.Logging;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpMethod;
 import org.springframework.http.HttpStatus;
+import org.togglz.core.manager.FeatureManager;
 
 public class MsoBusinessLogicImpl implements MsoBusinessLogic {
 
@@ -119,9 +125,13 @@ public class MsoBusinessLogicImpl implements MsoBusinessLogic {
      */
     private final MsoInterface msoClientInterface;
 
+
+    private final FeatureManager featureManager;
+
     @Autowired
-    public MsoBusinessLogicImpl(MsoInterface msoClientInterface) {
+    public MsoBusinessLogicImpl(MsoInterface msoClientInterface, FeatureManager featureManager) {
         this.msoClientInterface = msoClientInterface;
+        this.featureManager = featureManager;
     }
 
     public static String validateEndpointPath(String endpointEnvVariable) {
@@ -137,7 +147,7 @@ public class MsoBusinessLogicImpl implements MsoBusinessLogic {
     public MsoResponseWrapper createSvcInstance(RequestDetails msoRequest) {
         logInvocationInDebug("createSvcInstance");
 
-        String endpoint = validateEndpointPath(MsoProperties.MSO_REST_API_SVC_INSTANCE);
+        String endpoint = validateEndpointPath(MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE);
 
         return msoClientInterface.createSvcInstance(msoRequest, endpoint);
     }
@@ -227,7 +237,7 @@ public class MsoBusinessLogicImpl implements MsoBusinessLogic {
 
         UUID requestId = UUID.randomUUID();
         extraHeaders.put("X-ONAP-RequestID",requestId.toString());
-        extraHeaders.put("X-ONAP-PartnerName","VID");
+        extraHeaders.put(PARTNER_NAME.getHeaderName(), PARTNER_NAME.getHeaderValue());
         extraHeaders.put("X-RequestorID",userId);
 
         return msoClientInterface.invokeWorkflow(request,final_endpoint,extraHeaders);
@@ -257,7 +267,7 @@ public class MsoBusinessLogicImpl implements MsoBusinessLogic {
         logInvocationInDebug("deleteSvcInstance");
         String endpoint;
 
-        endpoint = validateEndpointPath(MsoProperties.MSO_DELETE_OR_UNASSIGN_REST_API_SVC_INSTANCE);
+        endpoint = validateEndpointPath(MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE);
         if (shouldUnassignService(serviceStatus)){
             logger.debug(EELFLoggerDelegate.debugLogger, "unassign service");
             String svcEndpoint = endpoint + "/" + serviceInstanceId + "/unassign";
@@ -377,18 +387,30 @@ public class MsoBusinessLogicImpl implements MsoBusinessLogic {
         return dashboardOrchestrationReqs;
     }
 
+    private String simpleNoTaskInfoFilter()
+    {
+        if (featureManager.isActive(Features.FLAG_EXP_USE_FORMAT_PARAMETER_FOR_CM_DASHBOARD)) {
+            return "format=simpleNoTaskInfo&";
+        }
+
+        return "";
+
+    }
+
     private String constructOrchestrationRequestFilter(String filterName, String filterValue) {
-        return String.format("%sfilter=%s:EQUALS:%s",
-                SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQS), filterName, filterValue);
+
+        return String.format("%s%sfilter=%s:EQUALS:%s",
+                    SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQS),simpleNoTaskInfoFilter(), filterName, filterValue);
     }
 
     private List<RequestWrapper> getOrchestrationRequestsByFilter(String filterName, String filterValue) {
+        HttpResponseWithRequestInfo<String> msoResponseWrapper = getRawOrchestrationRequestsByFilter(filterName, filterValue);
+        return deserializeOrchestrationRequestsJson(msoResponseWrapper.getResponse().getBody());
+    }
+
+    private HttpResponseWithRequestInfo<String> getRawOrchestrationRequestsByFilter(String filterName, String filterValue) {
         String orchestrationReqPath = constructOrchestrationRequestFilter(filterName, filterValue);
-        RestObject<String> restObjStr = new RestObject<>();
-        String str = new String();
-        restObjStr.set(str);
-        MsoResponseWrapper msoResponseWrapper = msoClientInterface.getOrchestrationRequest(str, "", orchestrationReqPath, restObjStr, true);
-        return deserializeOrchestrationRequestsJson(msoResponseWrapper.getEntity());
+        return msoClientInterface.getOrchestrationRequest(orchestrationReqPath, true);
     }
 
     private List<RequestWrapper> deserializeOrchestrationRequestsJson(String orchestrationRequestsJson) {
@@ -466,7 +488,7 @@ public class MsoBusinessLogicImpl implements MsoBusinessLogic {
         String methodName = "activateServiceInstance";
         logInvocationInDebug(methodName);
         try {
-            String serviceEndpoint = SystemProperties.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);
+            String serviceEndpoint = SystemProperties.getProperty(MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE);
             String activateServicePath = serviceEndpoint + "/" + serviceInstanceId + ACTIVATE;
 
             RestObject<String> restObjStr = new RestObject<>();
@@ -530,7 +552,7 @@ public class MsoBusinessLogicImpl implements MsoBusinessLogic {
 
     @Override
     public String getActivateFabricConfigurationPath(String serviceInstanceId) {
-        String path = validateEndpointPath(MsoProperties.MSO_REST_API_SERVICE_INSTANCE_CREATE);
+        String path = validateEndpointPath(MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE);
         path += "/" + serviceInstanceId + ACTIVATE_FABRIC_CONFIGURATION;
 
         return path;
@@ -680,7 +702,7 @@ public class MsoBusinessLogicImpl implements MsoBusinessLogic {
         logInvocationInDebug("setServiceInstanceStatus");
         String methodName = "setServiceInstanceStatus";
         try {
-            String serviceEndpoint = validateEndpointPath(MsoProperties.MSO_REST_API_SVC_INSTANCE);
+            String serviceEndpoint = validateEndpointPath(MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE);
             String endpoint = serviceEndpoint + "/" + serviceInstanceId;
 
             String isActivateState = (isActivate ? ACTIVATE : DEACTIVATE);
@@ -818,7 +840,7 @@ public class MsoBusinessLogicImpl implements MsoBusinessLogic {
     public MsoResponseWrapper removeRelationshipFromServiceInstance(RequestDetails requestDetails, String serviceInstanceId) {
         logInvocationInDebug("removeRelationshipFromServiceInstance");
 
-        String serviceEndpoint = SystemProperties.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);
+        String serviceEndpoint = SystemProperties.getProperty(MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE);
         String removeRelationshipsPath = serviceEndpoint + "/" + serviceInstanceId + "/removeRelationships";
 
         return msoClientInterface.removeRelationshipFromServiceInstance(requestDetails, removeRelationshipsPath);
@@ -828,7 +850,7 @@ public class MsoBusinessLogicImpl implements MsoBusinessLogic {
     public MsoResponseWrapper addRelationshipToServiceInstance(RequestDetails requestDetails, String serviceInstanceId) {
         logInvocationInDebug("addRelationshipToServiceInstance");
 
-        String serviceEndpoint = SystemProperties.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);
+        String serviceEndpoint = SystemProperties.getProperty(MsoProperties.MSO_RESTAPI_SERVICE_INSTANCE);
         String addRelationshipsPath = serviceEndpoint + "/" + serviceInstanceId + "/addRelationships";
 
         return msoClientInterface.addRelationshipToServiceInstance(requestDetails, addRelationshipsPath);
@@ -837,22 +859,38 @@ public class MsoBusinessLogicImpl implements MsoBusinessLogic {
 
     @Override
     public ExternalComponentStatus probeComponent() {
-        String url = SystemProperties.getProperty(
-                MsoProperties.MSO_SERVER_URL) + "/" + SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQS);
-        long startTime = System.currentTimeMillis();
-        ExternalComponentStatus externalComponentStatus;
-
+        final long startTime = System.currentTimeMillis();
+        HttpResponseWithRequestInfo<String> responseWithRequestInfo = null;
         try {
-            String rawBody = objectMapper.writeValueAsString(getOrchestrationRequestsForDashboard());
-            StatusMetadata statusMetadata=new HttpRequestMetadata(HttpMethod.GET,200,url,rawBody,"VID-SO",System.currentTimeMillis() - startTime);
+            responseWithRequestInfo = getRawOrchestrationRequestsByFilter("requestExecutionDate", "01-01-2100" );
+            int httpCode = responseWithRequestInfo.getResponse().getStatus();
+            boolean isAvailable = httpCode == 200 || httpCode == 202;
+            if (isAvailable) {
+                //make sure response can be parsed to RequestList.class
+                JACKSON_OBJECT_MAPPER.readValue(responseWithRequestInfo.getResponse().getBody(), RequestList.class);
+            }
 
-            externalComponentStatus = new ExternalComponentStatus(ExternalComponentStatus.Component.MSO, true, statusMetadata);
+            HttpRequestMetadata metadata = new HttpRequestMetadata(responseWithRequestInfo,
+                isAvailable ? "OK" : "MSO returned no orchestration requests",
+                System.currentTimeMillis() - startTime, true);
+            return new ExternalComponentStatus(ExternalComponentStatus.Component.MSO, isAvailable, metadata);
+
+        } catch (ExceptionWithRequestInfo e) {
+            long duration = System.currentTimeMillis() - startTime;
+            return new ExternalComponentStatus(ExternalComponentStatus.Component.MSO, false,
+                new HttpRequestMetadata(e, duration));
         } catch (Exception e) {
-            StatusMetadata statusMetadata = new HttpRequestMetadata(HttpMethod.GET, HttpStatus.INTERNAL_SERVER_ERROR.value(), url, "", e.getMessage(), System.currentTimeMillis() - startTime);
-            externalComponentStatus = new ExternalComponentStatus(ExternalComponentStatus.Component.MSO, false, statusMetadata);
-        }
+            StatusMetadata metadata;
+            long duration = System.currentTimeMillis() - startTime;
 
-        return externalComponentStatus;
+            if (responseWithRequestInfo == null) {
+                metadata = new ErrorMetadata(Logging.exceptionToDescription(e), duration);
+            } else {
+                metadata = new HttpRequestMetadata(responseWithRequestInfo, Logging.exceptionToDescription(e), duration, true);
+            }
+
+            return new ExternalComponentStatus(ExternalComponentStatus.Component.MSO, false, metadata);
+        }
     }
 
     private void validateUpdateVnfConfig(RequestDetails requestDetails) {