2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.provider.lcm.service;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.att.eelf.i18n.EELFResourceManager;
29 import com.google.common.base.Strings;
30 import org.opendaylight.yang.gen.v1.org.onap.appc.lcm.rev160108.Payload;
31 import org.onap.appc.Constants;
32 import org.onap.appc.configuration.Configuration;
33 import org.onap.appc.configuration.ConfigurationFactory;
34 import org.onap.appc.domainmodel.lcm.ActionLevel;
35 import org.onap.appc.domainmodel.lcm.ResponseContext;
36 import org.onap.appc.exceptions.APPCException;
37 import org.onap.appc.executor.objects.LCMCommandStatus;
38 import org.onap.appc.executor.objects.Params;
39 import org.onap.appc.i18n.Msg;
40 import org.onap.appc.logging.LoggingConstants;
41 import org.onap.appc.logging.LoggingUtils;
42 import org.onap.appc.provider.AppcProviderLcm;
43 import org.onap.appc.provider.lcm.mock.MockRequestExecutor;
44 import org.onap.appc.requesthandler.RequestHandler;
45 import org.onap.appc.requesthandler.objects.RequestHandlerInput;
46 import org.onap.appc.requesthandler.objects.RequestHandlerOutput;
47 import org.osgi.framework.BundleContext;
48 import org.osgi.framework.FrameworkUtil;
49 import org.osgi.framework.InvalidSyntaxException;
50 import org.osgi.framework.ServiceReference;
52 import java.util.Collection;
55 * Provider LCM request executor
57 public class RequestExecutor {
58 final String CANNOT_PROCESS = "LCM request cannot be processed at the moment because APPC isn't running";
60 private final Configuration configuration = ConfigurationFactory.getConfiguration();
61 private final EELFLogger logger = EELFManager.getInstance().getLogger(AppcProviderLcm.class);
64 * Execute the request.
65 * @param requestHandlerInput of the RequestHandlerInput
66 * @return RequestHandlerOutput
68 public RequestHandlerOutput executeRequest(RequestHandlerInput requestHandlerInput) {
69 // TODO mock backend should be removed when backend implementation is done
70 RequestHandlerOutput requestHandlerOutput = new MockRequestExecutor().executeRequest(requestHandlerInput);
71 if (requestHandlerOutput != null) {
72 // mock support, return mock results
73 return requestHandlerOutput;
76 RequestHandler handler = getRequestHandler(requestHandlerInput.getRequestContext().getActionLevel());
77 if (handler == null) {
78 logger.debug("execute while requesthandler is null");
79 requestHandlerOutput = createRequestHandlerOutput(requestHandlerInput,
80 LCMCommandStatus.REJECTED, Msg.REQUEST_HANDLER_UNAVAILABLE, new APPCException(CANNOT_PROCESS));
83 logger.debug("execute while requesthandler is not null");
84 requestHandlerOutput = handler.handleRequest(requestHandlerInput);
85 } catch (Exception e) {
86 logger.info(String.format("UNEXPECTED FAILURE while executing %s action",
87 requestHandlerInput.getRequestContext().getAction().name()));
88 requestHandlerOutput = createRequestHandlerOutput(requestHandlerInput,
89 LCMCommandStatus.UNEXPECTED_ERROR, Msg.EXCEPTION_CALLING_DG, e);
92 return requestHandlerOutput;
96 * Get Request handler by ActionLevel
97 * @param actionLevel the ActionLevel
98 * @return RequestHandler if found, otherwise return null or throw RuntimeException
100 RequestHandler getRequestHandler(ActionLevel actionLevel) {
101 final BundleContext context = FrameworkUtil.getBundle(RequestHandler.class).getBundleContext();
102 if (context == null) {
106 String filter = null;
108 filter = "(level=" + actionLevel.name() + ")";
109 Collection<ServiceReference<RequestHandler>> serviceReferences =
110 context.getServiceReferences(RequestHandler.class, filter);
111 if (serviceReferences.size() == 1) {
112 ServiceReference<RequestHandler> serviceReference = serviceReferences.iterator().next();
113 return context.getService(serviceReference);
116 logger.error(String.format("Cannot find service reference for %s", RequestHandler.class.getName()));
117 throw new RuntimeException();
119 } catch (InvalidSyntaxException e) {
120 logger.error(String.format("Cannot find service reference for %s: Invalid Syntax %s",
121 RequestHandler.class.getName(), filter), e);
122 throw new RuntimeException(e);
127 * Create request handler output
128 * @param request of RequestHandlerInput
129 * @param cmdStatus of LCMCommandStatus
130 * @param msg of Msg for audit log
131 * @param e of the Exception
132 * @return generated RequestHandlerOutput based on the input
134 RequestHandlerOutput createRequestHandlerOutput(RequestHandlerInput request,
135 LCMCommandStatus cmdStatus,
138 String errorMsg = e.getMessage() != null ? e.getMessage() : e.toString();
139 Params params = new Params().addParam("errorMsg", errorMsg);
141 final org.onap.appc.domainmodel.lcm.Status status = new org.onap.appc.domainmodel.lcm.Status();
142 status.setMessage(cmdStatus.getFormattedMessage(params));
143 status.setCode(cmdStatus.getResponseCode());
145 final ResponseContext responseContext = new ResponseContext();
146 responseContext.setCommonHeader(request.getRequestContext().getCommonHeader());
147 responseContext.setStatus(status);
149 RequestHandlerOutput requestHandlerOutput = new RequestHandlerOutput();
150 requestHandlerOutput.setResponseContext(responseContext);
152 final String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
153 final String reason = EELFResourceManager.format(
154 msg, e, appName, e.getClass().getSimpleName(), "", e.getMessage());
155 LoggingUtils.logErrorMessage(
156 LoggingConstants.TargetNames.APPC_PROVIDER,
158 this.getClass().getName());
160 return requestHandlerOutput;
164 * Get payload from passed in RequestHandlerOutput
165 * @param output of the RequestHandlerOutput
166 * @return If the passed in RequestHandlerOutput contains payload, return a Payload object of the payload.
167 * Otherwise, return null.
169 public Payload getPayload(RequestHandlerOutput output) {
170 if (output.getResponseContext() == null
171 || Strings.isNullOrEmpty(output.getResponseContext().getPayload())) {
175 return new Payload(output.getResponseContext().getPayload());