fix of the instanceParams adding to the CDS request 18/95218/1
authorLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Mon, 9 Sep 2019 10:20:58 +0000 (12:20 +0200)
committerLukasz Muszkieta <lukasz.muszkieta@nokia.com>
Mon, 9 Sep 2019 10:20:58 +0000 (12:20 +0200)
Change-Id: I3bff9b5cac47d5fa29fbe503e2c3752a2b207aab
Issue-ID: SO-2208
Signed-off-by: Lukasz Muszkieta <lukasz.muszkieta@nokia.com>
bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/flowspecific/tasks/ConfigAssignVnf.java
bpmn/so-bpmn-tasks/src/test/java/org/onap/so/bpmn/infrastructure/flowspecific/tasks/ConfigAssignVnfTest.java

index bc71fc6..9413e8e 100644 (file)
@@ -3,13 +3,14 @@
  * ONAP - SO
  * ================================================================================
  * Copyright (C) 2019 TechMahindra.
+ * Copyright (C) 2019 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.
 
 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.UUID;
 import org.onap.so.bpmn.common.BuildingBlockExecution;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
@@ -33,15 +36,15 @@ import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
 import org.onap.so.client.cds.beans.ConfigAssignPropertiesForVnf;
 import org.onap.so.client.cds.beans.ConfigAssignRequestVnf;
 import org.onap.so.client.exception.ExceptionBuilder;
+import org.onap.so.serviceinstancebeans.Service;
+import org.onap.so.serviceinstancebeans.Vnfs;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 /**
- * 
  * Get vnf related data and config assign
- *
  */
 @Component
 public class ConfigAssignVnf {
@@ -51,48 +54,42 @@ public class ConfigAssignVnf {
     private static final String ACTION_NAME = "config-assign";
     private static final String MODE = "sync";
 
+    private final ExtractPojosForBB extractPojosForBB;
+    private final ExceptionBuilder exceptionBuilder;
+
     @Autowired
-    private ExceptionBuilder exceptionUtil;
-    @Autowired
-    private ExtractPojosForBB extractPojosForBB;
+    public ConfigAssignVnf(ExtractPojosForBB extractPojosForBB, ExceptionBuilder exceptionBuilder) {
+        this.extractPojosForBB = extractPojosForBB;
+        this.exceptionBuilder = exceptionBuilder;
+    }
 
     /**
      * Getting the vnf data, blueprint name, blueprint version etc and setting them in execution object and calling the
      * subprocess.
-     * 
-     * @param execution
      */
     public void preProcessAbstractCDSProcessing(BuildingBlockExecution execution) {
         logger.info("Start preProcessAbstractCDSProcessing ");
         try {
-            GenericVnf vnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
+            GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
             ServiceInstance serviceInstance =
                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
-
-            List<Map<String, Object>> userParams =
-                    execution.getGeneralBuildingBlock().getRequestContext().getRequestParameters().getUserParams();
-
             ConfigAssignPropertiesForVnf configAssignPropertiesForVnf = new ConfigAssignPropertiesForVnf();
             configAssignPropertiesForVnf.setServiceInstanceId(serviceInstance.getServiceInstanceId());
             configAssignPropertiesForVnf
                     .setServiceModelUuid(serviceInstance.getModelInfoServiceInstance().getModelUuid());
             configAssignPropertiesForVnf
-                    .setVnfCustomizationUuid(vnf.getModelInfoGenericVnf().getModelCustomizationUuid());
-            configAssignPropertiesForVnf.setVnfId(vnf.getVnfId());
-            configAssignPropertiesForVnf.setVnfName(vnf.getVnfName());
-
-            for (Map<String, Object> params : userParams) {
-                for (Map.Entry<String, Object> entry : params.entrySet()) {
-                    configAssignPropertiesForVnf.setUserParam(entry.getKey(), entry.getValue());
-                }
-            }
-
+                    .setVnfCustomizationUuid(genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid());
+            configAssignPropertiesForVnf.setVnfId(genericVnf.getVnfId());
+            configAssignPropertiesForVnf.setVnfName(genericVnf.getVnfName());
+            setUserParamsInConfigAssignPropertiesForVnf(configAssignPropertiesForVnf,
+                    execution.getGeneralBuildingBlock().getRequestContext().getRequestParameters().getUserParams(),
+                    genericVnf);
             ConfigAssignRequestVnf configAssignRequestVnf = new ConfigAssignRequestVnf();
-            configAssignRequestVnf.setResolutionKey(vnf.getVnfName());
+            configAssignRequestVnf.setResolutionKey(genericVnf.getVnfName());
             configAssignRequestVnf.setConfigAssignPropertiesForVnf(configAssignPropertiesForVnf);
 
-            String blueprintName = vnf.getModelInfoGenericVnf().getBlueprintName();
-            String blueprintVersion = vnf.getModelInfoGenericVnf().getBlueprintVersion();
+            String blueprintName = genericVnf.getModelInfoGenericVnf().getBlueprintName();
+            String blueprintVersion = genericVnf.getModelInfoGenericVnf().getBlueprintVersion();
             logger.debug(" BlueprintName : " + blueprintName + " BlueprintVersion : " + blueprintVersion);
 
             AbstractCDSPropertiesBean abstractCDSPropertiesBean = new AbstractCDSPropertiesBean();
@@ -109,9 +106,48 @@ public class ConfigAssignVnf {
             abstractCDSPropertiesBean.setActionName(ACTION_NAME);
             abstractCDSPropertiesBean.setMode(MODE);
             execution.setVariable("executionObject", abstractCDSPropertiesBean);
-
         } catch (Exception ex) {
-            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
+            logger.error("An exception occurred when creating ConfigAssignPropertiesForVnf for CDS request", ex);
+            exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, ex);
+        }
+    }
+
+    private void setUserParamsInConfigAssignPropertiesForVnf(ConfigAssignPropertiesForVnf configAssignProperties,
+            List<Map<String, Object>> userParamsFromRequest, GenericVnf vnf) throws Exception {
+        Service service = getServiceFromRequestUserParams(userParamsFromRequest);
+        List<Map<String, String>> instanceParamsList =
+                getInstanceParamForVnf(service, vnf.getModelInfoGenericVnf().getModelCustomizationUuid());
+        instanceParamsList
+                .forEach(instanceParamsMap -> instanceParamsMap.forEach(configAssignProperties::setUserParam));
+    }
+
+    private Service getServiceFromRequestUserParams(List<Map<String, Object>> userParams) throws Exception {
+        Map<String, Object> serviceMap = userParams.stream().filter(key -> key.containsKey("service")).findFirst()
+                .orElseThrow(() -> new Exception("Can not find service in userParams section in generalBuildingBlock"));
+        return convertServiceFromJsonToServiceObject((String) serviceMap.get("service"));
+    }
+
+    private Service convertServiceFromJsonToServiceObject(String serviceFromJson) throws Exception {
+        try {
+            return new ObjectMapper().readValue(serviceFromJson, Service.class);
+        } catch (Exception e) {
+            logger.error(String.format(
+                    "An exception occurred while converting json object to Service object. The json is: %s",
+                    serviceFromJson), e);
+            throw e;
+        }
+    }
+
+    private List<Map<String, String>> getInstanceParamForVnf(Service service, String genericVnfModelCustomizationUuid)
+            throws Exception {
+        Optional<Vnfs> foundedVnf = service.getResources().getVnfs().stream()
+                .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(genericVnfModelCustomizationUuid))
+                .findFirst();
+        if (foundedVnf.isPresent()) {
+            return foundedVnf.get().getInstanceParams();
+        } else {
+            throw new Exception(String.format("Can not find vnf for genericVnfModelCustomizationUuid: %s",
+                    genericVnfModelCustomizationUuid));
         }
     }
 
index 7d96a18..468bc7d 100644 (file)
@@ -3,13 +3,14 @@
  * ONAP - SO
  * ================================================================================
  * Copyright (C) 2019 TechMahindra.
+ * Copyright (C) 2019 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.
 
 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
 
-import static org.junit.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.doThrow;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
-import java.util.UUID;
-import org.camunda.bpm.engine.delegate.BpmnError;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.camunda.bpm.engine.delegate.DelegateExecution;
+import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
 import org.junit.Before;
 import org.junit.Test;
-import org.mockito.ArgumentMatchers;
-import org.mockito.InjectMocks;
-import org.onap.so.bpmn.BaseTaskTest;
 import org.onap.so.bpmn.common.BuildingBlockExecution;
+import org.onap.so.bpmn.common.DelegateExecutionImpl;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
+import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
-import org.onap.so.client.exception.BBObjectNotFoundException;
+import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
+import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
+import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoServiceInstance;
+import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
+import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
+import org.onap.so.client.exception.ExceptionBuilder;
 
-public class ConfigAssignVnfTest extends BaseTaskTest {
-    @InjectMocks
-    private ConfigAssignVnf configAssignVnf = new ConfigAssignVnf();
+public class ConfigAssignVnfTest {
 
-    private GenericVnf genericVnf;
-    private ServiceInstance serviceInstance;
-    private RequestContext requestContext;
-    private String msoRequestId;
+    private static final String GENERIC_VNF_ID = "vnfId_configVnfTest";
+    private static final String GENERIC_VNF_NAME = "vnfName_configVnfTest";
+    private static final String VNF_MODEL_CUSTOMIZATION_UUID = "0c1ac643-377e-475b-be50-6be65f91a7ad";
+    private static final String SERVICE_INSTANCE_ID = "serviceInst_configTest";
+    private static final String SERVICE_MODEL_UUID = "5af91c26-8418-4d3f-944c-965842deda94";
+    private static final String TARGET_VNF_MODEL_CUSTOMIZATION_UUID = "0c1ac643-377e-475b-be50-6be65f91a7ad";
+    private static final String GENERAL_BLOCK_EXECUTION_MAP_KEY = "gBBInput";
+    private static final int THE_NUMBER_OF_EXPECTED_CONFIG_PROPERTIES = 8;
+
+    private static final String USER_PARAMS_FROM_REQUEST = "{\"resources\":{\"vnfs\":["
+            + "{\"modelInfo\":{\"modelCustomizationId\":\"" + VNF_MODEL_CUSTOMIZATION_UUID + "\"},"
+            + "\"instanceParams\":[{\"paramName1\":\"paramValue1\",\"paramName2\":\"paramValue2\"},{\"paramName3\":\"paramValue3\"}]},"
+            + "{\"modelInfo\":{\"modelCustomizationId\":\"2d1ac656-377e-467b-be50-6ce65f66a7ca\"},"
+            + "\"instanceParams\":[{\"parName4\":\"parValue4\",\"parName5\":\"parValue5\"}]}]}}\n";
+
+
+    private ConfigAssignVnf testedObject;
+
+    private BuildingBlockExecution buildingBlockExecution;
+    private ExtractPojosForBB extractPojosForBB;
 
     @Before
-    public void before() throws BBObjectNotFoundException {
-        genericVnf = setGenericVnf();
-        serviceInstance = setServiceInstance();
-        msoRequestId = UUID.randomUUID().toString();
-        requestContext = setRequestContext();
-        requestContext.setMsoRequestId(msoRequestId);
-        gBBInput.setRequestContext(requestContext);
-
-        doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
-                .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
-        when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID)))
-                .thenReturn(genericVnf);
-        when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID)))
-                .thenReturn(serviceInstance);
+    public void setup() {
+        buildingBlockExecution = createBuildingBlockExecution();
+        extractPojosForBB = mock(ExtractPojosForBB.class);
+        testedObject = new ConfigAssignVnf(extractPojosForBB, new ExceptionBuilder());
     }
 
     @Test
-    public void preProcessAbstractCDSProcessingTest() throws Exception {
+    public void prepareAbstractCDSPropertiesBean_success() throws Exception {
+        // given
+        when(extractPojosForBB.extractByKey(buildingBlockExecution, ResourceKey.GENERIC_VNF_ID))
+                .thenReturn(createGenericVnf());
+        when(extractPojosForBB.extractByKey(buildingBlockExecution, ResourceKey.SERVICE_INSTANCE_ID))
+                .thenReturn(createServiceInstance());
+        // when
+        testedObject.preProcessAbstractCDSProcessing(buildingBlockExecution);
+        // then
+        verifyConfigAssignPropertiesJsonContent();
+    }
 
-        configAssignVnf.preProcessAbstractCDSProcessing(execution);
+    private void verifyConfigAssignPropertiesJsonContent() throws Exception {
+        AbstractCDSPropertiesBean abstractCDSPropertiesBean = buildingBlockExecution.getVariable("executionObject");
+        String payload = abstractCDSPropertiesBean.getRequestObject();
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode payloadJson = mapper.readTree(payload);
+        JsonNode configAssignPropertiesNode = payloadJson.findValue("config-assign-properties");
+        assertThat(configAssignPropertiesNode.size()).isEqualTo(THE_NUMBER_OF_EXPECTED_CONFIG_PROPERTIES);
+        assertThat(configAssignPropertiesNode.get("service-instance-id").asText()).isEqualTo(SERVICE_INSTANCE_ID);
+        assertThat(configAssignPropertiesNode.get("vnf-id").asText()).isEqualTo(GENERIC_VNF_ID);
+        assertThat(configAssignPropertiesNode.get("vnf-name").asText()).isEqualTo(GENERIC_VNF_NAME);
+        assertThat(configAssignPropertiesNode.get("service-model-uuid").asText()).isEqualTo(SERVICE_MODEL_UUID);
+        assertThat(configAssignPropertiesNode.get("vnf-customization-uuid").asText())
+                .isEqualTo(VNF_MODEL_CUSTOMIZATION_UUID);
+        assertThat(configAssignPropertiesNode.has("paramName1")).isTrue();
+        assertThat(configAssignPropertiesNode.get("paramName1").asText()).isEqualTo("paramValue1");
+        assertThat(configAssignPropertiesNode.has("paramName2")).isTrue();
+        assertThat(configAssignPropertiesNode.get("paramName2").asText()).isEqualTo("paramValue2");
+        assertThat(configAssignPropertiesNode.has("paramName3")).isTrue();
+        assertThat(configAssignPropertiesNode.get("paramName3").asText()).isEqualTo("paramValue3");
+    }
 
-        assertTrue(true);
+    private BuildingBlockExecution createBuildingBlockExecution() {
+        DelegateExecution execution = new DelegateExecutionFake();
+        execution.setVariable(GENERAL_BLOCK_EXECUTION_MAP_KEY, createGeneralBuildingBlock());
+        return new DelegateExecutionImpl(execution);
     }
 
+    private ServiceInstance createServiceInstance() {
+        ServiceInstance serviceInstance = new ServiceInstance();
+        serviceInstance.setServiceInstanceId(SERVICE_INSTANCE_ID);
+        ModelInfoServiceInstance modelInfoServiceInstance = new ModelInfoServiceInstance();
+        modelInfoServiceInstance.setModelUuid(SERVICE_MODEL_UUID);
+        serviceInstance.setModelInfoServiceInstance(modelInfoServiceInstance);
+        return serviceInstance;
+    }
+
+    private GenericVnf createGenericVnf() {
+        GenericVnf genericVnf = new GenericVnf();
+        genericVnf.setVnfId(GENERIC_VNF_ID);
+        genericVnf.setVnfName(GENERIC_VNF_NAME);
+        ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
+        modelInfoGenericVnf.setModelCustomizationUuid(TARGET_VNF_MODEL_CUSTOMIZATION_UUID);
+        modelInfoGenericVnf.setBlueprintName("blueprintTest");
+        modelInfoGenericVnf.setBlueprintVersion("blueprintVerTest");
+        genericVnf.setModelInfoGenericVnf(modelInfoGenericVnf);
+        return genericVnf;
+    }
+
+    private GeneralBuildingBlock createGeneralBuildingBlock() {
+        GeneralBuildingBlock generalBuildingBlock = new GeneralBuildingBlock();
+        RequestContext requestContext = new RequestContext();
+        RequestParameters requestParameters = new RequestParameters();
+        requestParameters.setUserParams(createRequestUserParams());
+        requestContext.setRequestParameters(requestParameters);
+        generalBuildingBlock.setRequestContext(requestContext);
+        return generalBuildingBlock;
+    }
+
+    private List<Map<String, Object>> createRequestUserParams() {
+        List<Map<String, Object>> userParams = new ArrayList<>();
+        Map<String, Object> userParamMap = new HashMap<>();
+        userParamMap.put("service", USER_PARAMS_FROM_REQUEST);
+        userParams.add(userParamMap);
+        return userParams;
+    }
 }