2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix
4 * Modifications Copyright (C) 2020 Huawei Technologies Co., Ltd.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.sdnc;
23 import java.util.List;
24 import java.util.UUID;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.springframework.stereotype.Component;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerContext;
30 import org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.common.SoPropertyConstants;
31 import org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.LcmControllerDE;
32 import org.onap.so.client.exception.BadResponseException;
33 import org.onap.so.client.exception.PayloadGenerationException;
34 import org.onap.so.client.sdnc.common.SDNCConstants;
35 import org.onap.so.client.sdnc.lcm.*;
36 import org.onap.so.client.sdnc.lcm.beans.*;
37 import org.onap.so.client.sdnc.lcm.beans.payload.*;
38 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_ID;
39 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_CORRELATION_ID;
40 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_PAYLOAD;
43 public class SdncControllerDE extends LcmControllerDE {
45 private static final int SDNC_DELEGATE_EXECUTION_ERROR_CODE = 1103;
46 private static final ObjectMapper mapper = new ObjectMapper();
49 public Boolean understand(ControllerContext<DelegateExecution> context) {
50 return context.getControllerActor().equalsIgnoreCase("sdnc");
54 public Boolean ready(ControllerContext<DelegateExecution> context) {
59 protected int callLcmClient(ControllerContext<DelegateExecution> context) {
60 DelegateExecution execution = context.getExecution();
62 logger.debug("Running activity for id: {}, name: {}", execution.getCurrentActivityId(),
63 execution.getCurrentActivityName());
66 LcmInput lcmInput = buildLcmInput(execution);
67 sendLcmRequest(execution, lcmInput);
69 execution.setVariable(SoPropertyConstants.CONTROLLER_STATUS, "Success");
70 } catch (Exception e) {
71 execution.setVariable(SoPropertyConstants.CONTROLLER_STATUS, "Failure");
73 exceptionUtil.buildAndThrowWorkflowException(execution, SDNC_DELEGATE_EXECUTION_ERROR_CODE, e);
76 logger.debug("Finish activity for id: {}, name: {}", execution.getCurrentActivityId(),
77 execution.getCurrentActivityName());
83 protected int getErrorCode() {
84 return SDNC_DELEGATE_EXECUTION_ERROR_CODE;
87 private LcmOutput sendSyncRequest(String operation, LcmInput lcmInput) throws BadResponseException {
88 SDNCLcmClientBuilder sdncLcmClientBuilder = new SDNCLcmClientBuilder();
89 SDNCLcmRestClient sdncLcmRestClient;
91 sdncLcmRestClient = sdncLcmClientBuilder.newSDNCLcmRestClient(operation);
92 } catch (SDNCLcmClientBuilderException e) {
93 logger.error("Create SDNCLcmRestClient error: ", e);
94 throw new BadResponseException("Can not send request to SDNC.");
99 lcmOutput = sdncLcmRestClient.sendRequest(lcmInput);
100 } catch (Exception e) {
101 logger.error("SDNCLcmRestClient sends request failure: ", e);
102 throw new BadResponseException("Send request to SDNC failure.");
107 private LcmOutput selectLcmOutputFromDmaapResponses(List<LcmDmaapResponse> lcmDmaapResponses, LcmInput lcmInput) {
108 String requestId = lcmInput.getCommonHeader().getRequestId();
109 String subRequestId = lcmInput.getCommonHeader().getSubRequestId();
111 for (LcmDmaapResponse lcmDmaapResponse : lcmDmaapResponses) {
112 LcmOutput lcmOutput = lcmDmaapResponse.getBody().getOutput();
113 if (requestId.equals(lcmOutput.getCommonHeader().getRequestId())
114 && subRequestId.equals(lcmOutput.getCommonHeader().getSubRequestId())) {
122 private LcmOutput sendAsyncRequest(String operation, LcmInput lcmInput) throws BadResponseException {
123 SDNCLcmClientBuilder sdncLcmClientBuilder = new SDNCLcmClientBuilder();
124 SDNCLcmDmaapClient sdncLcmDmaapClient;
126 sdncLcmDmaapClient = sdncLcmClientBuilder.newSDNCLcmDmaapClient();
127 } catch (SDNCLcmClientBuilderException e) {
128 logger.error("Create SDNCLcmDmaapClient error: ", e);
129 throw new BadResponseException("Can not send request to SDNC.");
132 LcmDmaapRequest lcmDmaapRequest = SDNCLcmMessageBuilder.buildLcmDmaapRequest(operation, lcmInput);
134 sdncLcmDmaapClient.sendRequest(lcmDmaapRequest);
135 } catch (Exception e) {
136 logger.error("SDNCLcmDmaapClient sends request failure: ", e);
137 throw new BadResponseException("Send request to SDNC failure.");
140 long timeout = sdncLcmClientBuilder.getSDNCLcmProperties().getActionTimeout();
141 long startTime = System.currentTimeMillis();
143 List<LcmDmaapResponse> lcmDmaapResponses = sdncLcmDmaapClient.getResponse();
144 if (lcmDmaapResponses.size() > 0) {
145 LcmOutput lcmOutput = selectLcmOutputFromDmaapResponses(lcmDmaapResponses, lcmInput);
146 if (lcmOutput != null) {
151 long stopTime = System.currentTimeMillis();
152 if ((stopTime - startTime) > timeout) {
153 String msg = "Timeout for SDNC LCM action " + lcmInput.getAction();
155 throw new BadResponseException(msg);
160 public static String toLowerHyphen(String lcmAction) {
161 String regex = "([a-z0-9A-Z])(?=[A-Z])";
162 String replacement = "$1-";
163 return lcmAction.replaceAll(regex, replacement).toLowerCase();
166 private String convertToSting(Object msgObject) throws PayloadGenerationException {
168 return SDNCLcmPayloadBuilder.convertToSting(msgObject);
169 } catch (JsonProcessingException e) {
170 throw new PayloadGenerationException(e.getMessage());
174 private LcmInput buildLcmInput(DelegateExecution execution) throws PayloadGenerationException {
175 String requestId = String.valueOf(execution.getVariable(REQUEST_ID));
176 String requestAction = String.valueOf(execution.getVariable(SoPropertyConstants.SO_ACTION));
177 String pnfName = String.valueOf(execution.getVariable(PNF_CORRELATION_ID));
178 logger.debug(String.format("requestId: %s, action: %s, pnfName: %s", requestId, requestAction, pnfName));
180 String requestPayload = String.valueOf(execution.getVariable(REQUEST_PAYLOAD));
181 logger.debug("SO request payload: " + requestPayload);
186 switch (requestAction) {
187 case SoPropertyConstants.ACTION_PRE_CHECK:
188 lcmAction = SDNCLcmActionConstants.UPGRADE_PRE_CHECK;
190 UpgradePreCheckPayload upgradePreCheckPayload;
191 upgradePreCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePreCheckPayload(execution);
192 lcmPayload = convertToSting(upgradePreCheckPayload);
194 case SoPropertyConstants.ACTION_DOWNLOAD_N_E_SW:
195 lcmAction = SDNCLcmActionConstants.DOWNLOAD_N_E_SW;
197 DownloadNESwPayload downloadNESwPayload;
198 downloadNESwPayload = SDNCLcmPayloadBuilder.buildDownloadNESwPayload(execution);
199 lcmPayload = convertToSting(downloadNESwPayload);
201 case SoPropertyConstants.ACTION_ACTIVATE_N_E_SW:
202 lcmAction = SDNCLcmActionConstants.ACTIVATE_N_E_SW;
204 ActivateNESwPayload activateNESwPayload;
205 activateNESwPayload = SDNCLcmPayloadBuilder.buildActivateNESwPayload(execution);
206 lcmPayload = convertToSting(activateNESwPayload);
208 case SoPropertyConstants.ACTION_POST_CHECK:
209 lcmAction = SDNCLcmActionConstants.UPGRADE_POST_CHECK;
211 UpgradePostCheckPayload upgradePostCheckPayload;
212 upgradePostCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePostCheckPayload(execution);
213 lcmPayload = convertToSting(upgradePostCheckPayload);
216 String msg = "Unsupported SO Action: " + requestAction;
218 throw new PayloadGenerationException(msg);
221 String subRequestId = UUID.randomUUID().toString();
223 SDNCLcmMessageBuilder.buildLcmInputForPnf(requestId, subRequestId, pnfName, lcmAction, lcmPayload);
226 String lcmInputMsg = mapper.writeValueAsString(lcmInput);
227 logger.debug("SDNC input message for {}: {}", lcmAction, lcmInputMsg);
228 } catch (JsonProcessingException e) {
229 throw new PayloadGenerationException(e.getMessage());
235 private void parseLcmOutput(LcmOutput lcmOutput, String lcmAction) throws BadResponseException {
236 LcmStatus lcmStatus = lcmOutput.getStatus();
237 int lcmStatusCode = lcmStatus.getCode();
238 String outputPayload = lcmOutput.getPayload();
240 if (lcmStatusCode == SDNCConstants.LCM_OUTPUT_SUCCESS_CODE) {
241 logger.debug("Call SDNC LCM API for {} success, code: {}, message: {}, payload: {}", lcmAction,
242 lcmStatusCode, lcmStatus.getMessage(), outputPayload);
244 String msg = String.format("Call SDNC LCM API for %s failure, code: %d, message: %s, payload: %s",
245 lcmAction, lcmStatusCode, lcmStatus.getMessage(), outputPayload);
247 throw new BadResponseException(msg);
251 private void sendLcmRequest(DelegateExecution execution, LcmInput lcmInput) throws BadResponseException {
252 String actionMode = String.valueOf(execution.getVariable(SoPropertyConstants.SO_ACTION_MODE));
253 String lcmOperation = toLowerHyphen(lcmInput.getAction());
256 if ("async".equals(actionMode)) {
257 lcmOutput = sendAsyncRequest(lcmOperation, lcmInput);
259 lcmOutput = sendSyncRequest(lcmOperation, lcmInput);
262 parseLcmOutput(lcmOutput, lcmInput.getAction());