From 781b1a6df324419c846c84ea983c18fc8362bfd3 Mon Sep 17 00:00:00 2001 From: Patrick Brady Date: Wed, 13 Dec 2017 11:19:06 -0800 Subject: Third part of onap rename This part of the commit changes the folder structure on all other folders of appc. Change-Id: I8acfa11cdfcdcd36be0e137245d1dd7324f1abd3 Signed-off-by: Patrick Brady Issue-ID: APPC-13 --- .../appc/executor/impl/CommandExecutorImpl.java | 149 +++++++++++ .../org/onap/appc/executor/impl/CommandTask.java | 85 ++++++ .../appc/executor/impl/CommandTaskFactory.java | 90 +++++++ .../org/onap/appc/executor/impl/CommonMethods.java | 75 ++++++ .../appc/executor/impl/ExpiredMessageHandler.java | 63 +++++ .../onap/appc/executor/impl/LCMCommandTask.java | 294 +++++++++++++++++++++ .../appc/executor/impl/LCMReadonlyCommandTask.java | 78 ++++++ 7 files changed, 834 insertions(+) create mode 100644 appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandExecutorImpl.java create mode 100644 appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTask.java create mode 100644 appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTaskFactory.java create mode 100644 appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommonMethods.java create mode 100644 appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/ExpiredMessageHandler.java create mode 100644 appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/LCMCommandTask.java create mode 100644 appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/LCMReadonlyCommandTask.java (limited to 'appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap') 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 new file mode 100644 index 000000000..d23a3fdd6 --- /dev/null +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandExecutorImpl.java @@ -0,0 +1,149 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +/** + * + */ +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.RuntimeContext; +import org.onap.appc.exceptions.APPCException; +import org.onap.appc.executionqueue.ExecutionQueueService; +import org.onap.appc.executor.CommandExecutor; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.concurrent.TimeUnit; + + +public class CommandExecutorImpl implements CommandExecutor { + + private CommandTaskFactory executionTaskFactory; + private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandExecutorImpl.class); + + private ExecutionQueueService executionQueueService; + private ExpiredMessageHandler expiredMessageHandler; + + public CommandExecutorImpl() { + + } + + /** + * Injected by blueprint + * + * @param executionQueueService + */ + public void setExecutionQueueService(ExecutionQueueService executionQueueService) { + this.executionQueueService = executionQueueService; + } + + /** + * Injected by blueprint + * @param expiredMessageHandler + */ + public void setExpiredMessageHandler(ExpiredMessageHandler expiredMessageHandler) { + this.expiredMessageHandler = expiredMessageHandler; + } + + public void initialize() { + logger.info("initialization started of CommandExecutorImpl"); + executionQueueService.registerMessageExpirationListener(expiredMessageHandler); + } + + public void setExecutionTaskFactory(CommandTaskFactory executionTaskFactory) { + this.executionTaskFactory = executionTaskFactory; + } + + /** + * Execute given command + * Create command request and enqueue it for execution. + * + * @param commandExecutorInput Contains CommandHeader, command , target Id , payload and conf ID (optional) + * @throws APPCException in case of error. + */ + @Override + public void executeCommand(RuntimeContext commandExecutorInput) throws APPCException { + if (logger.isTraceEnabled()) { + logger.trace("Entering to executeCommand with CommandExecutorInput = " + ObjectUtils.toString(commandExecutorInput)); + } + enqueRequest(commandExecutorInput); + if (logger.isTraceEnabled()) { + logger.trace("Exiting from executeCommand"); + } + } + + private RuntimeContext getCommandRequest(RuntimeContext commandExecutorInput) { + if (logger.isTraceEnabled()) { + logger.trace("Entering to getCommandRequest with CommandExecutorInput = " + ObjectUtils.toString(commandExecutorInput)); + } + RuntimeContext commandRequest; + commandRequest = commandExecutorInput; + if (logger.isTraceEnabled()) { + logger.trace("Exiting from getCommandRequest with (CommandRequest = " + ObjectUtils.toString(commandRequest) + ")"); + } + return commandRequest; + } + + @SuppressWarnings("unchecked") + private void enqueRequest(RuntimeContext request) throws APPCException { + if (logger.isTraceEnabled()) { + logger.trace("Entering to enqueRequest with CommandRequest = " + ObjectUtils.toString(request)); + } + try { + CommandTask commandTask = executionTaskFactory.getExecutionTask(request); + + long remainingTTL = getRemainingTTL(request); + + executionQueueService.putMessage(commandTask, remainingTTL, TimeUnit.MILLISECONDS); + } catch (Exception e) { + logger.error("Exception: " + e.getMessage()); + throw new APPCException(e); + } + + if (logger.isTraceEnabled()) { + logger.trace("Exiting from enqueRequest"); + } + } + + private long getRemainingTTL(RuntimeContext request) { + Instant requestTimestamp = request.getRequestContext().getCommonHeader().getTimeStamp(); + int ttl = request.getRequestContext().getCommonHeader().getFlags().getTtl(); + return ChronoUnit.MILLIS.between(Instant.now(), requestTimestamp.plusSeconds(ttl)); + } + + private CommandTask getMessageExecutor(RuntimeContext request) { + if (logger.isTraceEnabled()) { + logger.trace("Entering to getMessageExecutor with command = " + request); + } + CommandTask executionTask = executionTaskFactory.getExecutionTask(request); + if (logger.isTraceEnabled()) { + logger.trace("Exiting from getMessageExecutor"); + } + return executionTask; + } +} 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 new file mode 100644 index 000000000..1df756efa --- /dev/null +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTask.java @@ -0,0 +1,85 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.executor.impl; + +import org.onap.appc.domainmodel.lcm.Status; +import org.onap.appc.executor.objects.CommandResponse; +import org.onap.appc.executor.objects.LCMCommandStatus; +import org.onap.appc.executor.objects.Params; +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.appc.workflow.objects.WorkflowResponse; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +/** + * This abstract class is base class for all Command tasks. All command task must inherit this class. + */ + +public abstract class CommandTask implements Runnable { + + protected final RequestHandler requestHandler; + protected final WorkFlowManager workflowManager; + protected final RuntimeContext commandRequest; + + protected CommandTask(RuntimeContext commandRequest, RequestHandler requestHandler, + WorkFlowManager workflowManager) { + super(); + this.commandRequest = commandRequest; + this.requestHandler = requestHandler; + this.workflowManager = workflowManager; + } + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(CommandTask.class); + + public void onRequestCompletion(CommandResponse response, boolean isAAIUpdated) { + logger.debug("Entry: onRequestCompletion()"); + requestHandler.onRequestExecutionEnd(commandRequest, isAAIUpdated); + } + + public abstract void onRequestCompletion(CommandResponse response); + + protected CommandResponse buildCommandResponse(WorkflowResponse response) { + + return new CommandResponse(commandRequest); + } + + + public void execute() { + final RuntimeContext runtimeContext = commandRequest; + WorkflowRequest workflowRequest = new WorkflowRequest(); + workflowRequest.setRequestContext(runtimeContext.getRequestContext()); + workflowRequest.setResponseContext(runtimeContext.getResponseContext()); + workflowRequest.setVnfContext(runtimeContext.getVnfContext()); + + WorkflowResponse response = workflowManager.executeWorkflow(workflowRequest); + + CommandResponse commandResponse = buildCommandResponse(response); + this.onRequestCompletion(commandResponse); + } + +} diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTaskFactory.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTaskFactory.java new file mode 100644 index 000000000..bd7247854 --- /dev/null +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommandTaskFactory.java @@ -0,0 +1,90 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.executor.impl; + + +import org.onap.appc.domainmodel.lcm.ActionLevel; +import org.onap.appc.domainmodel.lcm.RuntimeContext; +import org.onap.appc.domainmodel.lcm.VNFOperation; +import org.onap.appc.lifecyclemanager.LifecycleManager; +import org.onap.appc.requesthandler.RequestHandler; +import org.onap.appc.workflow.WorkFlowManager; + + + + +public class CommandTaskFactory { + +// private LCMCommandTask lcmCommandTask; +// private LCMReadonlyCommandTask LCMReadonlyCommandTask; + + private RequestHandler vnfRequestHandler; + private RequestHandler vmRequestHandler; + private WorkFlowManager workflowManager; + private LifecycleManager lifecyclemanager; + + + public void setWorkflowManager(WorkFlowManager workflowManager) { + this.workflowManager = workflowManager; + } + + public void setVnfRequestHandler(RequestHandler vnfRequestHandler) { + this.vnfRequestHandler = vnfRequestHandler; + } + + public void setVmRequestHandler(RequestHandler vmRequestHandler) { + this.vmRequestHandler = vmRequestHandler; + } + + public void setLifecyclemanager(LifecycleManager lifecyclemanager) { + this.lifecyclemanager = lifecyclemanager; + } + + + public synchronized CommandTask getExecutionTask(RuntimeContext runtimeContext){ + String action = runtimeContext.getRequestContext().getAction().name(); + ActionLevel actionLevel = runtimeContext.getRequestContext().getActionLevel(); + RequestHandler requestHandler = readRequestHandler(actionLevel); + if(ActionLevel.VM.equals(actionLevel)){ + return new LCMReadonlyCommandTask(runtimeContext,requestHandler,workflowManager); + } + switch (runtimeContext.getRequestContext().getAction().getOperationType()){ + case ReadOnly: + case OperationStatusUpdate: + return new LCMReadonlyCommandTask(runtimeContext,requestHandler,workflowManager); + default: + return new LCMCommandTask(runtimeContext,requestHandler,workflowManager, + lifecyclemanager); + } + } + + private RequestHandler readRequestHandler(ActionLevel actionLevel) { + if (ActionLevel.VM.equals(actionLevel)) { + return vmRequestHandler; + } + return vnfRequestHandler; + } + +} diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommonMethods.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommonMethods.java new file mode 100644 index 000000000..a032f3737 --- /dev/null +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/CommonMethods.java @@ -0,0 +1,75 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.executor.impl; + +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.util.HashMap; + + +class CommonMethods { + + private static final HashMap m = new HashMap(); + + static { + m.put(34, """); // < - less-than + m.put(60, "<"); // < - less-than + m.put(62, ">"); // > - greater-than + m.put(38, "&"); // & - Ampersand + } + + static String escapeHtml(String source) { + try { + StringWriter writer = new StringWriter((int) (source.length() * 1.5)); + escape(writer, source); + return writer.toString(); + } catch (IOException ioe) { + ioe.printStackTrace(); + return null; + } + } + + private static void escape(Writer writer, String str) throws IOException { + int len = str.length(); + for (int i = 0; i < len; i++) { + char c = str.charAt(i); + int ascii = (int) c; + String entityName = (String) m.get(ascii); + if (entityName == null) { + if (c > 0x7F) { + writer.write("&#"); + writer.write(Integer.toString(c, 10)); + writer.write(';'); + } else { + writer.write(c); + } + } else { + writer.write(entityName); + } + } + } + +} diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/ExpiredMessageHandler.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/ExpiredMessageHandler.java new file mode 100644 index 000000000..bd5773216 --- /dev/null +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/ExpiredMessageHandler.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.executor.impl; + +import org.onap.appc.domainmodel.lcm.RuntimeContext; +import org.onap.appc.domainmodel.lcm.ActionLevel; +import org.onap.appc.executionqueue.MessageExpirationListener; +import org.onap.appc.requesthandler.RequestHandler; + + +public class ExpiredMessageHandler implements MessageExpirationListener{ + private RequestHandler vnfRequestHandler; + + private RequestHandler vmRequestHandler; + + public ExpiredMessageHandler(){ + + } + + public void setVnfRequestHandler(RequestHandler vnfRequestHandler) { + this.vnfRequestHandler = vnfRequestHandler; + } + + public void setVmRequestHandler(RequestHandler vmRequestHandler) { + this.vmRequestHandler = vmRequestHandler; + } + + @Override + public void onMessageExpiration(M message) { + RuntimeContext commandRequest = (RuntimeContext)message; + RequestHandler requestHandler = readRequestHandler(commandRequest); + requestHandler.onRequestTTLEnd(commandRequest, true); + } + + private RequestHandler readRequestHandler(RuntimeContext runtimeContext) { + if(ActionLevel.VM.equals(runtimeContext.getRequestContext().getActionLevel())){ + return vmRequestHandler; + } + return vnfRequestHandler; + } +} diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/LCMCommandTask.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/LCMCommandTask.java new file mode 100644 index 000000000..999957fc8 --- /dev/null +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/LCMCommandTask.java @@ -0,0 +1,294 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.executor.impl; + + +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.lang3.StringUtils; +import org.onap.appc.domainmodel.lcm.CommonHeader; +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.UnstableVNFException; +import org.onap.appc.executor.objects.CommandResponse; +import org.onap.appc.executor.objects.LCMCommandStatus; +import org.onap.appc.executor.objects.Params; +import org.onap.appc.executor.objects.UniqueRequestIdentifier; +import org.onap.appc.lifecyclemanager.LifecycleManager; +import org.onap.appc.lifecyclemanager.objects.LifecycleException; +import org.onap.appc.lifecyclemanager.objects.NoTransitionDefinedException; +import org.onap.appc.lifecyclemanager.objects.VNFOperationOutcome; +import org.onap.appc.logging.LoggingConstants; +import org.onap.appc.requesthandler.RequestHandler; +import org.onap.appc.workflow.WorkFlowManager; +import org.onap.appc.workflow.objects.WorkflowResponse; +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.AAIService; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; +import org.slf4j.MDC; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +import java.net.InetAddress; +import static com.att.eelf.configuration.Configuration.*; +import static com.att.eelf.configuration.Configuration.MDC_SERVICE_INSTANCE_ID; +import static com.att.eelf.configuration.Configuration.MDC_SERVICE_NAME; + + +public class LCMCommandTask extends CommandTask { + + private final AAIService aaiService; + private final LifecycleManager lifecyclemanager; + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(LCMCommandTask.class); + + public LCMCommandTask(RuntimeContext commandRequest, RequestHandler requestHandler, WorkFlowManager workflowManager, + LifecycleManager lifecyclemanager) { + super(commandRequest, requestHandler, workflowManager); + this.lifecyclemanager = lifecyclemanager; + + BundleContext bctx = FrameworkUtil.getBundle(AAIService.class).getBundleContext(); + // Get AAIadapter reference + ServiceReference sref = bctx.getServiceReference(AAIService.class.getName()); + if (sref != null) { + logger.info("AAIService from bundlecontext"); + aaiService = (AAIService) bctx.getService(sref); + + } else { + logger.info("AAIService error from bundlecontext"); + logger.warn("Cannot find service reference for org.openecomp.sdnc.sli.aai.AAIService"); + aaiService = null; + } + } + + + @Override + public void onRequestCompletion(CommandResponse response) { + final RuntimeContext request = commandRequest; + boolean isAAIUpdated = false; + try { + + final int statusCode = request.getResponseContext().getStatus().getCode(); + + if (logger.isDebugEnabled()) { + logger.debug("Workflow Execution Status = "+ statusCode); + } + + boolean isSuccess = statusCode == 100 || statusCode == 400; + + if (isSuccess && VNFOperation.Terminate == request.getRequestContext().getAction()) { + SvcLogicContext ctx = new SvcLogicContext(); + ctx = getVnfdata(request.getVnfContext().getId(), "vnf", ctx); + isAAIUpdated = aaiService.deleteGenericVnfData(request.getVnfContext().getId(), ctx.getAttribute("vnf.resource-version")); + } + else{ + isAAIUpdated = updateAAI(request.getVnfContext().getId() , false, isSuccess); + } + logger.debug("isAAIUpdated = " + isAAIUpdated); + if(!isAAIUpdated){ + throw new Exception(); + } + } + catch(Exception e1) { + logger.error("Exception = " + e1); + // In case of any errors we are updating the response status code and message + Status updatedStatus = new Status(401, "Fail to update VNF status in A&AI"); + request.getResponseContext().setStatus(updatedStatus); + throw new RuntimeException(e1); + } + finally { + super.onRequestCompletion(response, isAAIUpdated); + } + } + + @Override + public void run() { + final RuntimeContext request = commandRequest; + setInitialLogProperties(request); + boolean isAAIUpdated = false; + final String vnfId = request.getVnfContext().getId(); + final String vnfType = request.getVnfContext().getType(); + try { + final CommonHeader commonHeader = request.getRequestContext().getCommonHeader(); + final boolean forceFlag = commonHeader.getFlags().isForce(); + UniqueRequestIdentifier requestIdentifier = new UniqueRequestIdentifier(commonHeader.getOriginatorId(), + commonHeader.getRequestId(), commonHeader.getSubRequestId()); + String requestIdentifierString = requestIdentifier.toIdentifierString(); + requestHandler.onRequestExecutionStart(vnfId,false, requestIdentifierString, forceFlag); + + final String currentStatus = request.getVnfContext().getStatus(); + final VNFOperation action = request.getRequestContext().getAction(); + + final String nextState = lifecyclemanager.getNextState(vnfType, currentStatus, action.name()); + + SvcLogicContext ctx = new SvcLogicContext(); + ctx=getVnfdata(vnfId, "onRequestExecutionStart", ctx); + isAAIUpdated= postVnfdata(vnfId, nextState,"onRequestExecutionStart",ctx); + } catch (NoTransitionDefinedException e) { + logger.error("Error getting Next State for AAI Update: " + e.getMessage(), e); + Params params = new Params().addParam("actionName",e.event).addParam("currentState",e.currentState); + request.getResponseContext().setStatus(LCMCommandStatus.NO_TRANSITION_DEFINE_FAILURE.toStatus(params)); + isAAIUpdated = false; + } catch (UnstableVNFException e) { + logger.error(e.getMessage(), e); + Params params = new Params().addParam("vnfId",vnfId); + request.getResponseContext().setStatus(LCMCommandStatus.UNSTABLE_VNF_FAILURE.toStatus(params)); + isAAIUpdated = false; + }catch (Exception e) { + logger.error("Error before Request Execution starts.", e); + String errorMsg = StringUtils.isEmpty(e.getMessage()) ? e.toString() : e.getMessage(); + Params params = new Params().addParam("errorMsg",errorMsg); + request.getResponseContext().setStatus(LCMCommandStatus.UNEXPECTED_FAILURE.toStatus(params)); + isAAIUpdated = false; + } + + if (isAAIUpdated){ + super.execute(); + }else{ + String errorMsg = "Error updating A& AI before Workflow execution"; + logger.error(errorMsg); + WorkflowResponse response = new WorkflowResponse(); + response.setResponseContext(request.getResponseContext()); + CommandResponse commandResponse = super.buildCommandResponse(response); + this.onRequestCompletion(commandResponse); + } + + clearRequestLogProperties(); + } + + + private boolean updateAAI(String vnf_id, boolean isTTLEnd, boolean executionStatus) + { + String orchestrationStatus = null; + String nextState; + boolean callbackResponse; + VNFOperationOutcome outcome; + SvcLogicContext ctx = new SvcLogicContext(); + try { + ctx=getVnfdata(vnf_id, "onRequestExecutionEnd",ctx); + orchestrationStatus=ctx.getAttribute("onRequestExecutionEnd.orchestration-status"); + + if(isTTLEnd){ + outcome = VNFOperationOutcome.FAILURE; + } + else if(executionStatus){ + outcome = VNFOperationOutcome.SUCCESS; + } + else{ + outcome = VNFOperationOutcome.FAILURE; + } + nextState = lifecyclemanager.getNextState(null,orchestrationStatus, outcome.toString()) ; + callbackResponse= postVnfdata(vnf_id, nextState,"onRequestExecutionEnd",ctx); + logger.debug("AAI posting status: " + callbackResponse); + + } catch (NoTransitionDefinedException e) { + logger.debug("Transition not defined for State = " + orchestrationStatus); + callbackResponse =false; + } catch (LifecycleException e) { + logger.debug("State or command not registered with State Machine. State = " + orchestrationStatus); + callbackResponse =false; + } + return callbackResponse; + } + + + 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)){ + 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)){ + throw new RuntimeException("Error Querying AAI with vnfID = " +vnf_id); + } + logger.info("AAIResponse: " + response.toString()); + } catch (SvcLogicException e) { + logger.error("Error in getVnfdata "+ e); + throw new RuntimeException(e); + } + return ctx; + } + + private boolean postVnfdata(String vnf_id, String status,String prefix,SvcLogicContext ctx) { + String key="vnf-id = '"+ vnf_id+"'"; + logger.debug("inside postVnfdata=== "+key); + Map data = new HashMap<>(); + data.put("orchestration-status", status); + try { + SvcLogicResource.QueryStatus response = aaiService.update("generic-vnf", key, data, prefix, ctx); + if(SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)){ + logger.warn("VNF " + vnf_id + " not found while updating A&AI"); + return false; + } + logger.info("AAIResponse: " + response.toString()); + if(response.toString().equals("SUCCESS")) + { + return true; + } + } catch (SvcLogicException e) { + logger.error("Error in postVnfdata "+ e); + throw new RuntimeException(e); + } + return false; + } + + protected void setInitialLogProperties(RuntimeContext request) + { + MDC.put(MDC_KEY_REQUEST_ID, request.getRequestContext().getCommonHeader().getRequestId()); + if (request.getRequestContext().getActionIdentifiers().getServiceInstanceId() != null) + MDC.put(MDC_SERVICE_INSTANCE_ID, request.getRequestContext().getActionIdentifiers().getServiceInstanceId()); + MDC.put(LoggingConstants.MDCKeys.PARTNER_NAME, request.getRequestContext().getCommonHeader().getOriginatorId()); + MDC.put(MDC_SERVICE_NAME, request.getRequestContext().getAction().name()); + try { + MDC.put(MDC_SERVER_FQDN, InetAddress.getLocalHost().getCanonicalHostName()); + MDC.put(MDC_SERVER_IP_ADDRESS, InetAddress.getLocalHost().getHostAddress()); + } catch (Exception e) { + logger.debug(e.getMessage()); + } + MDC.put(MDC_INSTANCE_UUID, ""); //TODO make instanse_UUID generation once during APPC-instanse deploying + } + + protected void clearRequestLogProperties() + { + try { + MDC.remove(MDC_KEY_REQUEST_ID); + MDC.remove(MDC_SERVICE_INSTANCE_ID); + MDC.remove(MDC_SERVICE_NAME); + MDC.remove(LoggingConstants.MDCKeys.PARTNER_NAME); + } catch (Exception e) { + + } + } +} diff --git a/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/LCMReadonlyCommandTask.java b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/LCMReadonlyCommandTask.java new file mode 100644 index 000000000..0eeefe540 --- /dev/null +++ b/appc-dispatcher/appc-command-executor/appc-command-executor-core/src/main/java/org/onap/appc/executor/impl/LCMReadonlyCommandTask.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.executor.impl; + + +import org.apache.commons.lang3.StringUtils; +import org.onap.appc.domainmodel.lcm.CommonHeader; +import org.onap.appc.domainmodel.lcm.RuntimeContext; +import org.onap.appc.executor.UnstableVNFException; +import org.onap.appc.executor.objects.CommandResponse; +import org.onap.appc.executor.objects.LCMCommandStatus; +import org.onap.appc.executor.objects.Params; +import org.onap.appc.executor.objects.UniqueRequestIdentifier; +import org.onap.appc.requesthandler.RequestHandler; +import org.onap.appc.workflow.WorkFlowManager; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +public class LCMReadonlyCommandTask extends CommandTask { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(LCMReadonlyCommandTask.class); + + public LCMReadonlyCommandTask(RuntimeContext commandRequest, RequestHandler requestHandler, + WorkFlowManager workflowManager) { + super(commandRequest, requestHandler, workflowManager); + } + + @Override + public void onRequestCompletion(CommandResponse response) { + super.onRequestCompletion(response, true); + } + + @Override + public void run() { + RuntimeContext request = commandRequest; + final CommonHeader commonHeader = request.getRequestContext().getCommonHeader(); + final boolean forceFlag = commonHeader.getFlags().isForce(); + UniqueRequestIdentifier requestIdentifier = new UniqueRequestIdentifier(commonHeader.getOriginatorId(), commonHeader.getRequestId(), commonHeader.getSubRequestId()); + String requestIdentifierString = requestIdentifier.toIdentifierString(); + final String vnfId = request.getVnfContext().getId(); + try { + requestHandler.onRequestExecutionStart(vnfId,true, requestIdentifierString, forceFlag); + super.execute(); + } catch (UnstableVNFException e) { + logger.error(e.getMessage(), e); + Params params = new Params().addParam("vnfId",vnfId); + request.getResponseContext().setStatus(LCMCommandStatus.UNSTABLE_VNF_FAILURE.toStatus(params)); + }catch (Exception e) { + logger.error("Error during runing LCMReadonlyCommandTask.", e); + String errorMsg = StringUtils.isEmpty(e.getMessage()) ? e.toString() : e.getMessage(); + Params params = new Params().addParam("errorMsg",errorMsg); + request.getResponseContext().setStatus(LCMCommandStatus.UNEXPECTED_FAILURE.toStatus(params)); + } + } +} -- cgit 1.2.3-korg