diff options
author | Dunietz, Irwin <id1681@att.com> | 2020-01-16 15:13:14 -0500 |
---|---|---|
committer | Takamune Cho <takamune.cho@att.com> | 2020-01-29 19:44:48 +0000 |
commit | b5fe8a69e90b950c07dc11af481eab7e9bab52c6 (patch) | |
tree | 3da81ce60554e65b93776b9aea647f3c6d8679ab /appc-dispatcher/appc-command-executor | |
parent | 9b32cb60360a2a2973c621053510718de0072111 (diff) |
Change code in appc dispatcher for new LCMs in R6
Also introduce some minor improvements to robustness, efficiency, & formatting.
Issue-ID: APPC-1789
Signed-off-by: Dunietz, Irwin <id1681@att.com>
Change-Id: I82d970c2f7cde6c8dab1222af86ea70ce93b7e50
Diffstat (limited to 'appc-dispatcher/appc-command-executor')
4 files changed, 155 insertions, 132 deletions
diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandExecutorImpl.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandExecutorImpl.java index 9c12f4741..4c7698cb1 100644 --- a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandExecutorImpl.java +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandExecutorImpl.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP : APPC * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs * ================================================================================ @@ -11,15 +11,14 @@ * 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========================================================= */ @@ -29,6 +28,7 @@ package org.onap.appc.executor.impl; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import org.apache.commons.lang.ObjectUtils; +import org.onap.appc.domainmodel.lcm.CommonHeader; import org.onap.appc.exceptions.APPCException; import org.onap.appc.executionqueue.ExecutionQueueService; import org.onap.appc.executor.CommandExecutor; @@ -54,7 +54,7 @@ public class CommandExecutorImpl implements CommandExecutor { * <p>Used through blueprint. */ public void initialize() { - logger.info("initialization started of CommandExecutorImpl"); + logger.info("initialization of CommandExecutorImpl started"); } public void setExecutionQueueService(ExecutionQueueService executionQueueService) { @@ -78,24 +78,27 @@ public class CommandExecutorImpl implements CommandExecutor { */ @Override public void executeCommand (CommandExecutorInput commandExecutorInput) throws APPCException{ - logger.trace("Entering to executeCommand with CommandExecutorInput = "+ ObjectUtils.toString(commandExecutorInput)); + logger.trace("Entering executeCommand with CommandExecutorInput = " + + ObjectUtils.toString(commandExecutorInput)); CommandTask commandTask; try { - commandTask= getCommandTask(requestHandler, workflowManager); + commandTask = getCommandTask(requestHandler, workflowManager); commandTask.setCommandRequest(new CommandRequest(commandExecutorInput)); long remainingTTL = getRemainingTTL(commandTask.getCommandRequest()); - logger.trace("Queuing request with CommandRequest = "+ ObjectUtils.toString(commandTask.getCommandRequest())); - executionQueueService.putMessage(commandTask,remainingTTL, TimeUnit.MILLISECONDS); + logger.trace("Queuing request with CommandRequest = " + + ObjectUtils.toString(commandTask.getCommandRequest())); + executionQueueService.putMessage(commandTask, remainingTTL, TimeUnit.MILLISECONDS); } catch (Exception e) { - logger.error("Exception: "+e.getMessage()); + logger.error("Exception: " + e.getMessage()); throw new APPCException(e); } logger.trace("Exiting from executeCommand"); } private long getRemainingTTL(CommandRequest request) { - Date requestTimestamp = request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getTimeStamp(); - int ttl = request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getFlags().getTtl(); + CommonHeader hdr = request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader(); + Date requestTimestamp = hdr.getTimeStamp(); + int ttl = hdr.getFlags().getTtl(); return ttl*1000 + requestTimestamp.getTime() - System.currentTimeMillis(); } diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTask.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTask.java index cfd4ff8d0..8cf5930f7 100644 --- a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTask.java +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTask.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP : APPC * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs * ================================================================================ @@ -11,15 +11,15 @@ * 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========================================================= */ @@ -30,27 +30,29 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import java.net.InetAddress; import java.util.UUID; +import org.onap.appc.domainmodel.lcm.CommonHeader; +import org.onap.appc.domainmodel.lcm.RequestContext; +import org.onap.appc.domainmodel.lcm.ResponseContext; +import org.onap.appc.domainmodel.lcm.RuntimeContext; import org.onap.appc.domainmodel.lcm.Status; import org.onap.appc.domainmodel.lcm.VNFOperation; import org.onap.appc.executor.impl.objects.CommandRequest; import org.onap.appc.logging.LoggingConstants; import org.onap.appc.requesthandler.RequestHandler; -import org.onap.appc.domainmodel.lcm.RuntimeContext; import org.onap.appc.workflow.WorkFlowManager; import org.onap.appc.workflow.objects.WorkflowRequest; -import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; -import org.onap.ccsdk.sli.core.sli.SvcLogicResource; import org.onap.ccsdk.sli.adaptors.aai.AAIRequest; import org.onap.ccsdk.sli.adaptors.aai.AAIService; import org.onap.ccsdk.sli.adaptors.aai.AAIServiceException; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.core.sli.SvcLogicResource; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; import org.slf4j.MDC; - /** * This abstract class is base class for all Command tasks. All command task must inherit this class. */ @@ -81,8 +83,7 @@ public class CommandTask implements Runnable { this.requestHandler = requestHandler; } - public CommandTask(RequestHandler requestHandler, - WorkFlowManager workflowManager){ + public CommandTask(RequestHandler requestHandler, WorkFlowManager workflowManager) { this.requestHandler = requestHandler; this.workflowManager = workflowManager; getAAIservice(); @@ -104,19 +105,21 @@ public class CommandTask implements Runnable { @Override public void run() { - logger.debug("Starting execution of command :" + commandRequest); + logger.debug("Starting execution of command: " + commandRequest); setInitialLogProperties(commandRequest); final RuntimeContext runtimeContext = commandRequest.getCommandExecutorInput().getRuntimeContext(); WorkflowRequest workflowRequest = new WorkflowRequest(); - workflowRequest.setRequestContext(runtimeContext.getRequestContext()); - workflowRequest.setResponseContext(runtimeContext.getResponseContext()); + final RequestContext reqContext = runtimeContext.getRequestContext(); + workflowRequest.setRequestContext(reqContext); + final ResponseContext respContext = runtimeContext.getResponseContext(); + workflowRequest.setResponseContext(respContext); workflowRequest.setVnfContext(runtimeContext.getVnfContext()); - logger.debug("Executing workflow :" + workflowRequest); + logger.debug("Executing workflow: " + workflowRequest); workflowManager.executeWorkflow(workflowRequest); - logger.debug("Completed execution workflow with response:"+ commandRequest.getCommandExecutorInput().getRuntimeContext().getResponseContext()); + logger.debug("Completed execution workflow with response: " + respContext); try { - if (VNFOperation.Terminate == commandRequest.getCommandExecutorInput().getRuntimeContext().getRequestContext().getAction()) + if (VNFOperation.Terminate == reqContext.getAction()) updateAAIForTerminate(commandRequest); } catch (AAIServiceException e) { logger.error("Exception = " + e); @@ -124,36 +127,37 @@ public class CommandTask implements Runnable { Status updatedStatus = new Status(); updatedStatus.setCode(401); updatedStatus.setMessage("Failed to update VNF status in A&AI"); - commandRequest.getCommandExecutorInput().getRuntimeContext().getResponseContext().setStatus(updatedStatus); + respContext.setStatus(updatedStatus); throw new RuntimeException(e); - }finally { - requestHandler.onRequestExecutionEnd(commandRequest.getCommandExecutorInput().getRuntimeContext()); + } finally { + requestHandler.onRequestExecutionEnd(runtimeContext); clearRequestLogProperties(); } } private void updateAAIForTerminate(CommandRequest commandRequest) throws AAIServiceException { - final int statusCode = commandRequest.getCommandExecutorInput().getRuntimeContext().getResponseContext().getStatus().getCode(); + final RuntimeContext runtimeContext = commandRequest.getCommandExecutorInput().getRuntimeContext(); + final int statusCode = runtimeContext.getResponseContext().getStatus().getCode(); - logger.debug("Workflow Execution Status = "+ statusCode); + logger.debug("Workflow Execution Status = " + statusCode); if (statusCode == 100 || statusCode == 400) { + String id = runtimeContext.getVnfContext().getId(); SvcLogicContext ctx = new SvcLogicContext(); - ctx = getVnfdata(commandRequest.getCommandExecutorInput().getRuntimeContext().getVnfContext().getId(), "vnf", ctx); - deleteGenericVnfData(commandRequest.getCommandExecutorInput().getRuntimeContext().getVnfContext().getId(), - ctx.getAttribute("vnf.resource-version")); + ctx = getVnfdata(id, "vnf", ctx); + deleteGenericVnfData(id, ctx.getAttribute("vnf.resource-version")); } } - private SvcLogicContext getVnfdata(String vnf_id, String prefix,SvcLogicContext ctx) { - String key="generic-vnf.vnf-id = '" + vnf_id + "'" + " AND http-header.Real-Time = 'true'"; + private SvcLogicContext getVnfdata(String vnf_id, String prefix, SvcLogicContext ctx) { + String key = "generic-vnf.vnf-id = '" + vnf_id + "'" + " AND http-header.Real-Time = 'true'"; logger.debug("inside getVnfdata=== " + key); try { - SvcLogicResource.QueryStatus response = aaiService.query("generic-vnf", false, null, key,prefix, null, ctx); - if(SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)){ + SvcLogicResource.QueryStatus response = + aaiService.query("generic-vnf", false, null, key, prefix, null, ctx); + if (SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)) { logger.warn("VNF " + vnf_id + " not found while updating A&AI"); throw new RuntimeException("VNF not found for vnf_id = " + vnf_id); - } - else if(SvcLogicResource.QueryStatus.FAILURE.equals(response)){ + } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) { throw new RuntimeException("Error Querying AAI with vnfID = " + vnf_id); } logger.info("AAIResponse: " + response.toString()); @@ -166,24 +170,24 @@ public class CommandTask implements Runnable { private void setInitialLogProperties(CommandRequest request) { - String reqId = request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getRequestId(); + RequestContext reqContext = request.getCommandExecutorInput().getRuntimeContext().getRequestContext(); + CommonHeader reqHdr = reqContext.getCommonHeader(); + String reqId = reqHdr.getRequestId(); try { MDC.put(Configuration.MDC_KEY_REQUEST_ID, UUID.fromString(reqId).toString()); //reaching here without exception means existing RequestId is - //valid UUID as per ECOMP logging standards + //valid UUID as per ONAP logging standards } catch (Exception e) { String reqIdUUID = UUID.randomUUID().toString(); MDC.put(Configuration.MDC_KEY_REQUEST_ID, reqIdUUID); logger.info("Replaced invalid requestID of " + reqId + ". New value is " + reqIdUUID + "."); } - if (request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getActionIdentifiers().getServiceInstanceId() != null) { - MDC.put(Configuration.MDC_SERVICE_INSTANCE_ID, - request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getActionIdentifiers().getServiceInstanceId()); - MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, - request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getCommonHeader().getOriginatorId()); - MDC.put(Configuration.MDC_SERVICE_NAME, - request.getCommandExecutorInput().getRuntimeContext().getRequestContext().getAction().name()); + String svcInstanceId = reqContext.getActionIdentifiers().getServiceInstanceId(); + if (svcInstanceId != null) { + MDC.put(Configuration.MDC_SERVICE_INSTANCE_ID, svcInstanceId); + MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, reqHdr.getOriginatorId()); + MDC.put(Configuration.MDC_SERVICE_NAME, reqContext.getAction().name()); } try { MDC.put(Configuration.MDC_SERVER_FQDN, InetAddress.getLocalHost().getCanonicalHostName()); @@ -191,11 +195,10 @@ public class CommandTask implements Runnable { } catch (Exception e) { logger.error(e.getMessage(), e); } - MDC.put(Configuration.MDC_INSTANCE_UUID, ""); // make instanse_UUID generation once during APPC-instance deploying + MDC.put(Configuration.MDC_INSTANCE_UUID, ""); // confine instance_UUID generation to APPC-instance deployment } - private void clearRequestLogProperties() - { + private void clearRequestLogProperties() { try { MDC.remove(Configuration.MDC_KEY_REQUEST_ID); MDC.remove(Configuration.MDC_SERVICE_INSTANCE_ID); @@ -213,7 +216,7 @@ public class CommandTask implements Runnable { AAIRequest request = aaiService.getRequestFromResource("generic-vnf"); request.addRequestProperty("generic-vnf.vnf-id", vnf_id); response = aaiService.delete(request, resourceVersion); - } catch(AAIServiceException aaiexc) { + } catch (AAIServiceException aaiexc) { throw aaiexc; } catch (Exception exc) { logger.warn("deleteGenericVnfData", exc); diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandExecutor.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandExecutor.java index 83c6a0f42..3385b8489 100644 --- a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandExecutor.java +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandExecutor.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP : APPC * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs * ================================================================================ @@ -11,23 +11,19 @@ * 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.executor.impl; -/** - * - */ - import org.junit.Assert; import org.junit.Before; @@ -48,8 +44,8 @@ import java.util.concurrent.TimeUnit; public class TestCommandExecutor { - private static final String API_VERSION= "2.0.0"; - private static final String ORIGINATOR_ID= "1"; + private static final String API_VERSION = "2.0.0"; + private static final String ORIGINATOR_ID = "1"; private CommandExecutorImpl commandExecutor; @@ -59,16 +55,16 @@ public class TestCommandExecutor { private Date timeStamp = new Date(); private String requestId = "1"; - private CommandExecutorInput commandExecutorInputConfigure = pouplateCommandExecutorInput("FIREWALL", 30000, "1.0", - timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Configure,"15","") ; - private CommandExecutorInput commandExecutorInputSync = pouplateCommandExecutorInput("FIREWALL", 30, "1.0", - timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Sync,"15","") ; + private CommandExecutorInput commandExecutorInputConfigure = populateCommandExecutorInput("FIREWALL", 30000, "1.0", + timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Configure, "15", ""); + private CommandExecutorInput commandExecutorInputSync = populateCommandExecutorInput("FIREWALL", 30, "1.0", + timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "2", VNFOperation.Sync, "15", ""); private CommandTask commandTask; @Before - public void init()throws Exception { - requestHandler= Mockito.mock(RequestHandler.class); - workflowManager= Mockito.mock(WorkFlowManager.class); + public void init() throws Exception { + requestHandler = Mockito.mock(RequestHandler.class); + workflowManager = Mockito.mock(WorkFlowManager.class); executionQueueService = Mockito.mock(ExecutionQueueService.class); @@ -79,7 +75,10 @@ public class TestCommandExecutor { commandExecutor.initialize(); commandTask = Mockito.mock(CommandTask.class); Mockito.when(commandTask.getCommandRequest()).thenReturn(new CommandRequest(commandExecutorInputConfigure)); - PowerMockito.whenNew(CommandTask.class).withParameterTypes(RequestHandler.class,WorkFlowManager.class).withArguments(requestHandler,workflowManager).thenReturn(commandTask); + PowerMockito.whenNew(CommandTask.class) + .withParameterTypes(RequestHandler.class, WorkFlowManager.class) + .withArguments(requestHandler, workflowManager) + .thenReturn(commandTask); } @Test @@ -93,21 +92,26 @@ public class TestCommandExecutor { } @Test(expected = APPCException.class) - public void testNegativeFlow_LCM() throws APPCException{ - Mockito.doThrow(new APPCException("Failed to enqueue request")).when(executionQueueService).putMessage((Runnable) Mockito.anyObject(),Mockito.anyLong(),(TimeUnit) Mockito.anyObject()); + public void testNegativeFlow_LCM() throws APPCException { + Mockito.doThrow(new APPCException("Failed to enqueue request")) + .when(executionQueueService) + .putMessage((Runnable) Mockito.anyObject(), Mockito.anyLong(), (TimeUnit) Mockito.anyObject()); commandExecutor.executeCommand(commandExecutorInputSync); } - private CommandExecutorInput pouplateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload){ + private CommandExecutorInput populateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, + Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID, + VNFOperation action, String vnfId, String payload) { CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects(); RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext(); RequestContext requestContext = runtimeContext.getRequestContext(); - requestContext.getCommonHeader().getFlags().setTtl(ttl); - requestContext.getCommonHeader().setApiVer(apiVersion); - requestContext.getCommonHeader().setTimestamp(timeStamp); - requestContext.getCommonHeader().setRequestId(requestId); - requestContext.getCommonHeader().setSubRequestId(subRequestID); - requestContext.getCommonHeader().setOriginatorId(originatorID); + CommonHeader commonHeader = requestContext.getCommonHeader(); + commonHeader.getFlags().setTtl(ttl); + commonHeader.setApiVer(apiVersion); + commonHeader.setTimestamp(timeStamp); + commonHeader.setRequestId(requestId); + commonHeader.setSubRequestId(subRequestID); + commonHeader.setOriginatorId(originatorID); requestContext.setAction(action); requestContext.setPayload(payload); requestContext.getActionIdentifiers().setVnfId(vnfId); @@ -135,4 +139,3 @@ public class TestCommandExecutor { return commandExecutorInput; } } - diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandTask.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandTask.java index 1bcf8d3f1..4b94282ab 100644 --- a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandTask.java +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/test/java/org/onap/appc/executor/impl/TestCommandTask.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP : APPC * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Copyright (C) 2017 Amdocs * ================================================================================ @@ -11,15 +11,15 @@ * 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========================================================= */ @@ -72,7 +72,7 @@ import java.util.Map; @RunWith(PowerMockRunner.class) @PrepareForTest({FrameworkUtil.class, InetAddress.class}) public class TestCommandTask { - CommandTask task ; + CommandTask task; private RequestHandler requestHandler; private WorkFlowManager workflowManager; private AAIService aaiService; @@ -81,66 +81,72 @@ public class TestCommandTask { private Bundle bundleService = Mockito.mock(Bundle.class); private ServiceReference sref = Mockito.mock(ServiceReference.class); - private static final String TTL_FLAG= "TTL"; - private static final String API_VERSION= "2.0.0"; - private static final String ORIGINATOR_ID= "1"; + private static final String TTL_FLAG = "TTL"; + private static final String API_VERSION = "2.0.0"; + private static final String ORIGINATOR_ID = "1"; @Rule public ExpectedException expectedEx = ExpectedException.none(); @Before - public void init() throws UnknownHostException{ - aaiService = Mockito.mock(AAIService.class); + public void init() throws UnknownHostException { + aaiService = Mockito.mock(AAIService.class); PowerMockito.mockStatic(FrameworkUtil.class); PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(bundleService); PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext); PowerMockito.when(bundleContext.getServiceReference(AAIService.class.getName())).thenReturn(sref); PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiService); - requestHandler = Mockito.mock(RequestHandler.class); + requestHandler = Mockito.mock(RequestHandler.class); workflowManager = Mockito.mock(WorkFlowManager.class); task = Mockito.spy(new CommandTask(requestHandler, workflowManager)); } @Test - public void testRunPositive(){ + public void testRunPositive() { Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); task.setWorkflowManager(workflowManager); task.setRequestHandler(requestHandler); - task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "11", setTTLInFlags("30"), VNFOperation.Sync, "1", "1.0")); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "11", setTTLInFlags("30"), + VNFOperation.Sync, "1", "1.0")); task.run(); - Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext()); + Assert.assertNotNull( + task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext()); } @Test - public void testRunPositiveTerminateFailed(){ + public void testRunPositiveTerminateFailed() { Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); task.setWorkflowManager(workflowManager); task.setRequestHandler(requestHandler); - task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0")); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12", setTTLInFlags("30"), + VNFOperation.Terminate, "2", "1.0")); setResponseContext(300, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); task.run(); - Assert.assertNotNull(task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext()); + Assert.assertNotNull( + task.getCommandRequest().getCommandExecutorInput().getRuntimeContext().getResponseContext()); } @Test - public void testRunPositiveTerminateSucceeded() throws SvcLogicException, AAIServiceException{ + public void testRunPositiveTerminateSucceeded() throws SvcLogicException, AAIServiceException { AAIService mockAai = Mockito.mock(AAIService.class); Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), - Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class))).thenReturn(SvcLogicResource.QueryStatus.SUCCESS); + Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class))) + .thenReturn(SvcLogicResource.QueryStatus.SUCCESS); Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class)); Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenReturn(true); Whitebox.setInternalState(task, "aaiService", mockAai); Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); task.setWorkflowManager(workflowManager); task.setRequestHandler(requestHandler); - task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0")); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12", setTTLInFlags("30"), + VNFOperation.Terminate, "2", "1.0")); setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); task.run(); } @Test - public void testRunPositiveTerminateNotFound() throws SvcLogicException, AAIServiceException{ + public void testRunPositiveTerminateNotFound() throws SvcLogicException, AAIServiceException { AAIService mockAai = Mockito.mock(AAIService.class); Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), @@ -152,7 +158,8 @@ public class TestCommandTask { Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); task.setWorkflowManager(workflowManager); task.setRequestHandler(requestHandler); - task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0")); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12", setTTLInFlags("30"), + VNFOperation.Terminate, "2", "1.0")); setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); expectedEx.expect(RuntimeException.class); expectedEx.expectMessage("VNF not found for vnf_id = "); @@ -160,7 +167,7 @@ public class TestCommandTask { } @Test - public void testRunPositiveTerminateFailure() throws SvcLogicException, AAIServiceException{ + public void testRunPositiveTerminateFailure() throws SvcLogicException, AAIServiceException { AAIService mockAai = Mockito.mock(AAIService.class); Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), @@ -172,7 +179,8 @@ public class TestCommandTask { Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); task.setWorkflowManager(workflowManager); task.setRequestHandler(requestHandler); - task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0")); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12", setTTLInFlags("30"), + VNFOperation.Terminate, "2", "1.0")); setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); expectedEx.expect(RuntimeException.class); expectedEx.expectMessage("Error Querying AAI with vnfID = "); @@ -180,19 +188,21 @@ public class TestCommandTask { } @Test - public void testRunPositiveAaiServiceException() throws SvcLogicException, AAIServiceException{ + public void testRunPositiveAaiServiceException() throws SvcLogicException, AAIServiceException { AAIService mockAai = Mockito.mock(AAIService.class); Mockito.when(mockAai.query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class))) .thenReturn(SvcLogicResource.QueryStatus.SUCCESS); Mockito.when(mockAai.getRequestFromResource("generic-vnf")).thenReturn(Mockito.mock(AAIRequest.class)); - Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())).thenThrow(new AAIServiceException("ERROR IN DELETE")); + Mockito.when(mockAai.delete(Mockito.any(AAIRequest.class), Mockito.anyString())) + .thenThrow(new AAIServiceException("ERROR IN DELETE")); Whitebox.setInternalState(task, "aaiService", mockAai); Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); task.setWorkflowManager(workflowManager); task.setRequestHandler(requestHandler); - task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0")); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12", setTTLInFlags("30"), + VNFOperation.Terminate, "2", "1.0")); setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); expectedEx.expect(RuntimeException.class); expectedEx.expectCause(allOf(isA(AAIServiceException.class), @@ -213,14 +223,15 @@ public class TestCommandTask { Mockito.when(workflowManager.executeWorkflow(anyObject())).thenReturn(getWorkflowResponse()); task.setWorkflowManager(workflowManager); task.setRequestHandler(requestHandler); - task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12" , setTTLInFlags("30"), VNFOperation.Terminate, "2", "1.0")); + task.setCommandRequest(getCommandRequest("FIREWALL", 30, new Date(), "12", setTTLInFlags("30"), + VNFOperation.Terminate, "2", "1.0")); setResponseContext(100, task.getCommandRequest().getCommandExecutorInput().getRuntimeContext()); expectedEx.expect(RuntimeException.class); expectedEx.expectCause(isA(SvcLogicException.class)); task.run(); } - private WorkflowResponse getWorkflowResponse (){ + private WorkflowResponse getWorkflowResponse () { WorkflowResponse wfResponse = new WorkflowResponse(); ResponseContext responseContext = createResponseContextWithObjects(); wfResponse.setResponseContext(responseContext); @@ -229,7 +240,7 @@ public class TestCommandTask { return wfResponse; } - private ResponseContext createResponseContextWithObjects(){ + private ResponseContext createResponseContextWithObjects() { ResponseContext responseContext = new ResponseContext(); CommonHeader commonHeader = new CommonHeader(); Flags flags = new Flags(); @@ -240,41 +251,44 @@ public class TestCommandTask { return responseContext; } - private void setResponseContext(int statusCode ,RuntimeContext runtimeContext ){ + private void setResponseContext(int statusCode, RuntimeContext runtimeContext) { ResponseContext responseContext = createResponseContextWithObjects(); responseContext.getStatus().setCode(statusCode); runtimeContext.setResponseContext(responseContext); } private CommandRequest getCommandRequest(String vnfType, Integer ttl, Date timeStamp, String requestId, - Map<String, Object> flags, VNFOperation command, String vnfId, String vnfVersion ){ + Map<String, Object> flags, VNFOperation command, String vnfId, String vnfVersion) { - CommandExecutorInput commandExecutorInput = - pouplateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, ""); + CommandExecutorInput commandExecutorInput = populateCommandExecutorInput(vnfType, ttl, vnfVersion, timeStamp, + API_VERSION, requestId, ORIGINATOR_ID, "", command, vnfId, ""); CommandRequest request = new CommandRequest(commandExecutorInput); request.setCommandExecutorInput(commandExecutorInput); request.setCommandInTimeStamp(new Date()); return request; } - private CommandExecutorInput pouplateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, Date timeStamp, String apiVersion, - String requestId, String originatorID, String subRequestID, VNFOperation action, String vnfId , String payload){ + private CommandExecutorInput populateCommandExecutorInput(String vnfType, int ttl, String vnfVersion, + Date timeStamp, String apiVersion, String requestId, String originatorID, String subRequestID, + VNFOperation action, String vnfId, String payload) { CommandExecutorInput commandExecutorInput = createCommandExecutorInputWithSubObjects(); RuntimeContext runtimeContext = commandExecutorInput.getRuntimeContext(); RequestContext requestContext = runtimeContext.getRequestContext(); + CommonHeader commonHeader = requestContext.getCommonHeader(); ResponseContext responseContext = createResponseContextWithSuObjects(); runtimeContext.setResponseContext(responseContext); - requestContext.getCommonHeader().getFlags().setTtl(ttl); - requestContext.getCommonHeader().setApiVer(apiVersion); - requestContext.getCommonHeader().setTimestamp(timeStamp); - requestContext.getCommonHeader().setRequestId(requestId); - requestContext.getCommonHeader().setSubRequestId(subRequestID); - requestContext.getCommonHeader().setOriginatorId(originatorID); + commonHeader.getFlags().setTtl(ttl); + commonHeader.setApiVer(apiVersion); + commonHeader.setTimestamp(timeStamp); + commonHeader.setRequestId(requestId); + commonHeader.setSubRequestId(subRequestID); + commonHeader.setOriginatorId(originatorID); requestContext.setAction(action); requestContext.setPayload(payload); - requestContext.getActionIdentifiers().setVnfId(vnfId); - requestContext.getActionIdentifiers().setServiceInstanceId("test"); + ActionIdentifiers actionIdentifiers = requestContext.getActionIdentifiers(); + actionIdentifiers.setVnfId(vnfId); + actionIdentifiers.setServiceInstanceId("test"); VNFContext vnfContext = runtimeContext.getVnfContext(); vnfContext.setType(vnfType); vnfContext.setId(vnfId); @@ -289,7 +303,7 @@ public class TestCommandTask { return commandExecutorInput; } - private ResponseContext createResponseContextWithSuObjects(){ + private ResponseContext createResponseContextWithSuObjects() { ResponseContext responseContext = new ResponseContext(); CommonHeader commonHeader = new CommonHeader(); Flags flags = new Flags(); @@ -315,9 +329,9 @@ public class TestCommandTask { return runtimeContext; } - private Map<String,Object> setTTLInFlags( String value){ - Map<String,Object> flags = new HashMap<String,Object>(); - if( value != null || !("".equalsIgnoreCase(value))){ + private Map<String, Object> setTTLInFlags(String value) { + Map<String, Object> flags = new HashMap<String, Object>(); + if (value != null || !("".equalsIgnoreCase(value))) { flags.put(TTL_FLAG, value); } return flags; |