Adding DistributeTraffic LCM API 47/63847/6
authorTomek Osinski <tomasz.osinski2@orange.com>
Wed, 12 Sep 2018 13:10:08 +0000 (15:10 +0200)
committerTakamune Cho <tc012c@att.com>
Mon, 17 Sep 2018 20:16:32 +0000 (20:16 +0000)
This commit contains code modifications for APPC to support DistributeTraffic LCM API. Tests has been implemented for new service.

Change-Id: I853120eced93928268074cf89dd62fb89fea9ff8
Issue-ID: APPC-1169
Signed-off-by: Tomek Osinski <tomasz.osinski2@orange.com>
appc-dispatcher/appc-dispatcher-common/domain-model-lib/src/main/java/org/onap/appc/domainmodel/lcm/VNFOperation.java
appc-dispatcher/appc-request-handler/appc-request-handler-core/src/main/java/org/onap/appc/requesthandler/conv/Converter.java
appc-dispatcher/appc-request-handler/appc-request-handler-core/src/test/java/org/onap/appc/requesthandler/ConverterTest.java
appc-provider/appc-provider-bundle/src/main/java/org/onap/appc/provider/AppcProviderLcm.java
appc-provider/appc-provider-bundle/src/main/java/org/onap/appc/provider/lcm/service/DistributeTrafficService.java [new file with mode: 0644]
appc-provider/appc-provider-bundle/src/test/java/org/onap/appc/provider/lcm/service/DistributeTrafficServiceTest.java [new file with mode: 0644]
appc-provider/appc-provider-model/src/main/yang/appc-provider-lcm.yang

index 1c4a6bb..edf9a45 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2018 Orange
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -45,6 +47,7 @@ public enum VNFOperation {
     Query,
     QuiesceTraffic,
     ResumeTraffic,
+    DistributeTraffic,
     Reboot,
     Rebuild,
     Restart,
index 0b8002b..1071b76 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2018 Orange
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -271,6 +273,11 @@ public class Converter {
                 ((DetachVolumeOutputBuilder)outObj).setCommonHeader(commonHeader);
                 ((DetachVolumeOutputBuilder)outObj).setStatus(status);
                 return outObj;
+            case DistributeTraffic:
+                outObj = new DistributeTrafficOutputBuilder();
+                ((DistributeTrafficOutputBuilder)outObj).setCommonHeader(commonHeader);
+                ((DistributeTrafficOutputBuilder)outObj).setStatus(status);
+                return outObj;
             default:
                 throw new IllegalArgumentException(action+" action is not supported");
         }
index 3d3c350..46a367a 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2018 Orange
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -58,6 +60,7 @@ public class ConverterTest {
 
     private String expectedJsonBodyStrwithPayload ="{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"payload\":\"{}\",\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}}";
     private String expectedDmaapOutgoingMessageJsonStringReboot ="{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"correlation-id\":\"reqid\",\"rpc-name\":\"reboot\",\"type\":\"response\"}";
+    private String expectedDmaapOutgoingMessageJsonStringDistributeTraffc = "{\"body\":{\"output\":{\"common-header\":{\"api-ver\":\"2.0.0\",\"flags\":{},\"originator-id\":\"oid\",\"request-id\":\"reqid\",\"timestamp\":\"1970-01-01T00:00:01.000Z\"},\"status\":{\"code\":400,\"message\":\"SUCCESS - request has been processed successfully\"}}},\"cambria.partition\":\"MSO\",\"correlation-id\":\"reqid\",\"rpc-name\":\"distribute-traffic\",\"type\":\"response\"}";
 
     @Test
     public void convDateToZuluStringTest(){
@@ -426,6 +429,25 @@ public class ConverterTest {
         Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringUpgradeBackup,jsonStr);
     }
 
+    @Test
+    public void convAsyncResponseToBuilderDistributeTrafficTest() throws JsonProcessingException {
+        ResponseContext asyncResponse = buildAsyncResponse();
+        VNFOperation action = VNFOperation.DistributeTraffic;
+        String rpcName = convertActionNameToUrl(action.name());
+        String jsonStr = Converter.convAsyncResponseToJsonStringBody(action, rpcName, asyncResponse);
+        Assert.assertEquals(expectedJsonBodyStr,jsonStr);
+    }
+
+    @Test
+    public void convAsyncResponseToDmaapOutgoingMessageJsonStringDistributeTrafficTest() throws JsonProcessingException {
+        ResponseContext asyncResponse = buildAsyncResponse();
+        VNFOperation action = VNFOperation.DistributeTraffic;
+        String rpcName = convertActionNameToUrl(action.name());
+        String jsonStr = Converter.convAsyncResponseToDmaapOutgoingMessageJsonString(action, rpcName, asyncResponse);
+        System.out.println("jsonStr = " + jsonStr);
+        Assert.assertEquals(expectedDmaapOutgoingMessageJsonStringDistributeTraffc,jsonStr);
+    }
+
     /*@Test
     public void convAsyncResponseToBuilderTest() throws JsonProcessingException {
         AsyncResponse asyncResponse = buildAsyncResponse();
index 410a9ea..ff11951 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2018 Orange
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -43,6 +45,7 @@ import org.onap.appc.provider.lcm.service.ResumeTrafficService;
 import org.onap.appc.provider.lcm.service.UpgradeService;
 import org.onap.appc.provider.lcm.service.VolumeService;
 import org.onap.appc.provider.lcm.service.ConfigScaleOutService;
+import org.onap.appc.provider.lcm.service.DistributeTrafficService;
 import org.onap.appc.provider.lcm.util.RequestInputBuilder;
 import org.onap.appc.provider.lcm.util.ValidationService;
 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
@@ -487,6 +490,15 @@ public class AppcProviderLcm extends AbstractBaseUtils implements AutoCloseable,
         return Futures.immediateFuture(result);
     }
 
+    @Override
+    public Future<RpcResult<DistributeTrafficOutput>> distributeTraffic(DistributeTrafficInput input) {
+        logger.debug(String.format("LCM DistributeTraffic, received input: %s", input.toString()));
+        DistributeTrafficOutputBuilder outputBuilder = new DistributeTrafficService().process(input);
+        RpcResult<DistributeTrafficOutput> result =
+                RpcResultBuilder.<DistributeTrafficOutput>status(true).withResult(outputBuilder.build()).build();
+        return Futures.immediateFuture(result);
+    }
+
     @Override
     public Future<RpcResult<UpgradePreCheckOutput>> upgradePreCheck(UpgradePreCheckInput input) {
         logger.debug(String.format("LCM upgradeprecheck received input: %s", input.toString()));
diff --git a/appc-provider/appc-provider-bundle/src/main/java/org/onap/appc/provider/lcm/service/DistributeTrafficService.java b/appc-provider/appc-provider-bundle/src/main/java/org/onap/appc/provider/lcm/service/DistributeTrafficService.java
new file mode 100644 (file)
index 0000000..3671352
--- /dev/null
@@ -0,0 +1,114 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2018 Orange
+ * =============================================================================
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.appc.provider.lcm.service;
+
+
+import org.onap.appc.executor.objects.LCMCommandStatus;
+import org.onap.appc.requesthandler.objects.RequestHandlerInput;
+import org.onap.appc.util.JsonUtil;
+import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Action;
+import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.DistributeTrafficInput;
+import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.DistributeTrafficOutputBuilder;
+
+import java.io.IOException;
+import java.util.Map;
+
+/**
+ * Provide LCM command service for Distribute Traffic between VNFs.
+ */
+public class DistributeTrafficService extends AbstractBaseService {
+
+
+    private static final String CONFIG_FILE_NAME_PARAMETER = "ConfigFileName";
+    private static final String PAYLOAD = "payload";
+
+    /**
+     * Constructor
+     */
+    public DistributeTrafficService() {
+        super(Action.DistributeTraffic);
+        logger.debug("DistributeTrafficService starts");
+    }
+
+    /**
+     * Process a DistributeTraffic request
+     * @param input of DistributeTrafficInput from the REST API input
+     * @return DistributeTrafficOutputBuilder which has the process results
+     */
+    public DistributeTrafficOutputBuilder process(DistributeTrafficInput input) {
+
+        validate(input);
+        if (status == null) {
+            proceedAction(input);
+        }
+
+        DistributeTrafficOutputBuilder outputBuilder = new DistributeTrafficOutputBuilder();
+        outputBuilder.setStatus(status);
+        outputBuilder.setCommonHeader(input.getCommonHeader());
+        return outputBuilder;
+    }
+
+    /**
+     * Validate input.
+     * Set Status if any error detected.
+     *
+     * @param input of DistributeTrafficInput from the REST API input
+     */
+    void validate(DistributeTrafficInput input) {
+        status = validateVnfId(input.getCommonHeader(), input.getAction(), input.getActionIdentifiers());
+        if (status != null) {
+            return;
+        }
+
+        if (input.getPayload() == null) {
+            status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, PAYLOAD);
+            return;
+        }
+        String payloadString = input.getPayload().getValue();
+        status = validateMustHaveParamValue(payloadString == null ? payloadString : payloadString.trim(), PAYLOAD);
+        if (status != null) {
+            return;
+        }
+
+        try {
+            Map<String, String> payloadMap = JsonUtil.convertJsonStringToFlatMap(payloadString);
+            // ConfigFileName validation
+            final String configFileName = payloadMap.get(CONFIG_FILE_NAME_PARAMETER);
+            if (configFileName == null) {
+                status = buildStatusForParamName(LCMCommandStatus.MISSING_MANDATORY_PARAMETER, CONFIG_FILE_NAME_PARAMETER);
+            }
+
+        } catch(IOException e) {
+            logger.error(String.format("DistributeTrafficService (%s) got IOException when converting payload", rpcName), e);
+            status = buildStatusForErrorMsg(LCMCommandStatus.UNEXPECTED_ERROR, e.getMessage());
+        }
+    }
+
+    void proceedAction(DistributeTrafficInput input) {
+        RequestHandlerInput requestHandlerInput = getRequestHandlerInput(
+                input.getCommonHeader(), input.getActionIdentifiers(), input.getPayload(), this.getClass().getName());
+        if (requestHandlerInput != null) {
+            executeAction(requestHandlerInput);
+        }
+    }
+
+}
diff --git a/appc-provider/appc-provider-bundle/src/test/java/org/onap/appc/provider/lcm/service/DistributeTrafficServiceTest.java b/appc-provider/appc-provider-bundle/src/test/java/org/onap/appc/provider/lcm/service/DistributeTrafficServiceTest.java
new file mode 100644 (file)
index 0000000..e0c06f7
--- /dev/null
@@ -0,0 +1,189 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2018 Orange
+ * =============================================================================
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.appc.provider.lcm.service;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.appc.domainmodel.lcm.ResponseContext;
+import org.onap.appc.executor.objects.LCMCommandStatus;
+import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
+import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.*;
+import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.action.identifiers.ActionIdentifiers;
+import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.common.header.CommonHeader;
+import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.status.Status;
+import org.powermock.reflect.Whitebox;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Matchers.any;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.powermock.api.mockito.PowerMockito.whenNew;
+
+@RunWith(MockitoJUnitRunner.class)
+public class DistributeTrafficServiceTest {
+
+    private final Action myAction = Action.DistributeTraffic;
+    private final String rpcName = "distribute-traffic";
+    private  String PAYLOAD_STRING = "{\"ConfigFileName\":\"test\"}";
+
+    private final DistributeTrafficInput mockInput = mock(DistributeTrafficInput.class);
+    private final CommonHeader mockCommonHeader = mock(CommonHeader.class);
+    private final ActionIdentifiers mockActionIdentifiers = mock(ActionIdentifiers.class);
+    private final Payload mockPayload = mock(Payload.class);
+
+    private DistributeTrafficService distributeTrafficService;
+
+    @Before
+    public void setUp() throws Exception {
+        distributeTrafficService = spy(new DistributeTrafficService());
+    }
+
+    @Test
+    public void testConstructor() throws Exception {
+        assertEquals("Should have proper ACTION", myAction,
+                (Action) Whitebox.getInternalState(distributeTrafficService, "expectedAction"));
+        assertEquals("Should have action-status RPC name", rpcName,
+                Whitebox.getInternalState(distributeTrafficService, "rpcName").toString());
+    }
+
+    private void helpInitializeRequestParameters() {
+        Mockito.doReturn(mockCommonHeader).when(mockInput).getCommonHeader();
+        Mockito.doReturn(mockPayload).when(mockInput).getPayload();
+        Mockito.doReturn(myAction).when(mockInput).getAction();
+        Mockito.doReturn(mockActionIdentifiers).when(mockInput).getActionIdentifiers();
+        Mockito.doReturn(PAYLOAD_STRING).when(mockPayload).getValue();
+        ZULU zuluTimeStamp = new ZULU("2017-06-29T21:44:00.35Z");
+        Mockito.doReturn(zuluTimeStamp).when(mockCommonHeader).getTimestamp();
+        Mockito.doReturn("api ver").when(mockCommonHeader).getApiVer();
+        Mockito.doReturn("orignator Id").when(mockCommonHeader).getOriginatorId();
+        Mockito.doReturn("request Id").when(mockCommonHeader).getRequestId();
+        Mockito.doReturn("vnfId").when(mockActionIdentifiers).getVnfId();
+    }
+
+    @Test
+    public void testProcess() throws Exception {
+
+        helpInitializeRequestParameters();
+
+        // test processAction return without error
+        RequestExecutor mockExecutor = mock(RequestExecutor.class);
+        whenNew(RequestExecutor.class).withNoArguments().thenReturn(mockExecutor);
+
+        RequestHandlerOutput mockOutput = mock(RequestHandlerOutput.class);
+        Mockito.doReturn(mockOutput).when(mockExecutor).executeRequest(any());
+
+        ResponseContext mockResponseContext = mock(ResponseContext.class);
+        Mockito.doReturn(mockResponseContext).when(mockOutput).getResponseContext();
+
+        Mockito.when(distributeTrafficService.executeAction(any())).thenReturn(mockOutput);
+
+        DistributeTrafficOutputBuilder  outputBuilder = distributeTrafficService.process(mockInput);
+
+        Mockito.verify(distributeTrafficService, times(1)).proceedAction(mockInput);
+        assertNotNull("Should have commonHeader", outputBuilder.getCommonHeader());
+    }
+
+    @Test
+    public void testValidateMissingParameters() throws Exception {
+        DistributeTrafficOutputBuilder outputBuilder = distributeTrafficService.process(mockInput);
+        Mockito.verify(distributeTrafficService, times(0)).proceedAction(any());
+        assertNull("Should not have commonHeader as we did not mock it", outputBuilder.getCommonHeader());
+        assertEquals("should return missing parameter status",
+                Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()),
+                outputBuilder.getStatus().getCode());
+    }
+
+    @Test
+    public void testValidateForMissingOrInvalidAction() throws Exception {
+        helpInitializeRequestParameters();
+
+        // check missing Action
+        Mockito.doReturn(null).when(mockInput).getAction();
+        distributeTrafficService.validate(mockInput);
+        Status status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
+        assertEquals("Should return missing parameter for action",
+                Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
+
+        // check invalid Action
+        Mockito.doReturn(Action.Migrate).when(mockInput).getAction();
+
+        distributeTrafficService.validate(mockInput);
+        status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
+        assertEquals("should return missing parameter",
+                Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
+    }
+
+    @Test
+    public void testValidateForMissingActionIdentifiers() throws Exception {
+
+        helpInitializeRequestParameters();
+        Mockito.doReturn(null).when(mockInput).getActionIdentifiers();
+
+        // test missing ActionIdentifiers
+        distributeTrafficService.validate(mockInput);
+        Status status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
+        assertEquals("should return missing parameter",
+                Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
+    }
+
+    @Test
+    public void testValidateEmptyOrMissingPayload() throws Exception {
+        helpInitializeRequestParameters();
+
+        // validate empty payload
+        Mockito.doReturn("").when(mockPayload).getValue();
+        distributeTrafficService.validate(mockInput);
+        Status status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
+        assertEquals("should return invalid parameter",
+                Integer.valueOf(LCMCommandStatus.INVALID_INPUT_PARAMETER.getResponseCode()), status.getCode());
+
+        // validate missing payload
+        Mockito.doReturn(null).when(mockInput).getPayload();
+        distributeTrafficService.validate(mockInput);
+        status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
+
+        assertEquals("should return missing parameter",
+                Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
+
+    }
+
+    @Test
+    public void testValidateMissingConfigFileName() throws Exception {
+        helpInitializeRequestParameters();
+        String wrongPayload = "{\"test\":\"test\"}";
+        Mockito.doReturn(wrongPayload).when(mockPayload).getValue();
+        distributeTrafficService.validate(mockInput);
+        Status status = (Status) Whitebox.getInternalState(distributeTrafficService, "status");
+        assertEquals("should return status null",
+                Integer.valueOf(LCMCommandStatus.MISSING_MANDATORY_PARAMETER.getResponseCode()), status.getCode());
+    }
+
+
+
+}
index 423a1e8..2e369fa 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
+ * ================================================================================
+ * Modifications Copyright (C) 2018 Orange
  * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -121,6 +123,7 @@ module appc-provider-lcm {
             enum "Reboot";
             enum "AttachVolume";
             enum "DetachVolume";
+            enum "DistributeTraffic";
             
         }
         description "The action to be taken by APP-C, e.g. Restart, Rebuild, Migrate";
@@ -1314,6 +1317,29 @@ module appc-provider-lcm {
         }
     }
 
+    /**********************************************************************************
+     * Define the traffic distribution service
+     **********************************************************************************/
+    rpc distribute-traffic {
+        description "An operation to distribute traffic between VMs";
+        input {
+            uses common-header;
+            leaf action {
+                type action;
+                mandatory true;
+            }
+            uses action-identifiers;
+            leaf payload {
+                type payload;
+                mandatory true;
+            }
+        }
+        output {
+            uses common-header;
+            uses status;
+        }
+    }
+
 
 
  /**********************************************************************************