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.ActivateNESwPayload;
38 import org.onap.so.client.sdnc.lcm.beans.payload.DownloadNESwPayload;
39 import org.onap.so.client.sdnc.lcm.beans.payload.UpgradePostCheckPayload;
40 import org.onap.so.client.sdnc.lcm.beans.payload.UpgradePreCheckPayload;
41 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_ID;
42 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_CORRELATION_ID;
43 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_PAYLOAD;
46 public class SdncControllerDE extends LcmControllerDE {
48 private static final int SDNC_DELEGATE_EXECUTION_ERROR_CODE = 1103;
49 private static final ObjectMapper mapper = new ObjectMapper();
52 public Boolean understand(ControllerContext<DelegateExecution> context) {
53 return context.getControllerActor().equalsIgnoreCase("sdnc");
57 public Boolean ready(ControllerContext<DelegateExecution> context) {
62 protected int callLcmClient(ControllerContext<DelegateExecution> context) {
63 DelegateExecution execution = context.getExecution();
65 logger.debug("Running activity for id: {}, name: {}", execution.getCurrentActivityId(),
66 execution.getCurrentActivityName());
69 LcmInput lcmInput = buildLcmInput(execution);
70 sendLcmRequest(execution, lcmInput);
72 execution.setVariable(SoPropertyConstants.CONTROLLER_STATUS, "Success");
73 } catch (Exception e) {
74 execution.setVariable(SoPropertyConstants.CONTROLLER_STATUS, "Failure");
76 exceptionUtil.buildAndThrowWorkflowException(execution, SDNC_DELEGATE_EXECUTION_ERROR_CODE, e);
79 logger.debug("Finish activity for id: {}, name: {}", execution.getCurrentActivityId(),
80 execution.getCurrentActivityName());
86 protected int getErrorCode() {
87 return SDNC_DELEGATE_EXECUTION_ERROR_CODE;
90 private LcmOutput sendSyncRequest(String operation, LcmInput lcmInput) throws BadResponseException {
91 SDNCLcmClientBuilder sdncLcmClientBuilder = new SDNCLcmClientBuilder();
92 SDNCLcmRestClient sdncLcmRestClient;
94 sdncLcmRestClient = sdncLcmClientBuilder.newSDNCLcmRestClient(operation);
95 } catch (SDNCLcmClientBuilderException e) {
96 logger.error("Create SDNCLcmRestClient error: ", e);
97 throw new BadResponseException("Can not send request to SDNC.");
102 lcmOutput = sdncLcmRestClient.sendRequest(lcmInput);
103 } catch (Exception e) {
104 logger.error("SDNCLcmRestClient sends request failure: ", e);
105 throw new BadResponseException("Send request to SDNC failure.");
110 private LcmOutput selectLcmOutputFromDmaapResponses(List<LcmDmaapResponse> lcmDmaapResponses, LcmInput lcmInput) {
111 String requestId = lcmInput.getCommonHeader().getRequestId();
112 String subRequestId = lcmInput.getCommonHeader().getSubRequestId();
114 for (LcmDmaapResponse lcmDmaapResponse : lcmDmaapResponses) {
115 LcmOutput lcmOutput = lcmDmaapResponse.getBody().getOutput();
116 if (requestId.equals(lcmOutput.getCommonHeader().getRequestId())
117 && subRequestId.equals(lcmOutput.getCommonHeader().getSubRequestId())) {
125 private LcmOutput sendAsyncRequest(String operation, LcmInput lcmInput) throws BadResponseException {
126 SDNCLcmClientBuilder sdncLcmClientBuilder = new SDNCLcmClientBuilder();
127 SDNCLcmDmaapClient sdncLcmDmaapClient;
129 sdncLcmDmaapClient = sdncLcmClientBuilder.newSDNCLcmDmaapClient();
130 } catch (SDNCLcmClientBuilderException e) {
131 logger.error("Create SDNCLcmDmaapClient error: ", e);
132 throw new BadResponseException("Can not send request to SDNC.");
135 LcmDmaapRequest lcmDmaapRequest = SDNCLcmMessageBuilder.buildLcmDmaapRequest(operation, lcmInput);
137 sdncLcmDmaapClient.sendRequest(lcmDmaapRequest);
138 } catch (Exception e) {
139 logger.error("SDNCLcmDmaapClient sends request failure: ", e);
140 throw new BadResponseException("Send request to SDNC failure.");
143 long timeout = sdncLcmClientBuilder.getSDNCLcmProperties().getActionTimeout();
144 long startTime = System.currentTimeMillis();
146 List<LcmDmaapResponse> lcmDmaapResponses = sdncLcmDmaapClient.getResponse();
147 if (lcmDmaapResponses.size() > 0) {
148 LcmOutput lcmOutput = selectLcmOutputFromDmaapResponses(lcmDmaapResponses, lcmInput);
149 if (lcmOutput != null) {
154 long stopTime = System.currentTimeMillis();
155 if ((stopTime - startTime) > timeout) {
156 String msg = "Timeout for SDNC LCM action " + lcmInput.getAction();
158 throw new BadResponseException(msg);
163 public static String toLowerHyphen(String lcmAction) {
164 String regex = "([a-z0-9A-Z])(?=[A-Z])";
165 String replacement = "$1-";
166 return lcmAction.replaceAll(regex, replacement).toLowerCase();
169 private String convertToSting(Object msgObject) throws PayloadGenerationException {
171 return SDNCLcmPayloadBuilder.convertToSting(msgObject);
172 } catch (JsonProcessingException e) {
173 throw new PayloadGenerationException(e.getMessage());
177 private LcmInput buildLcmInput(DelegateExecution execution) throws PayloadGenerationException {
178 String requestId = String.valueOf(execution.getVariable(REQUEST_ID));
179 String requestAction = String.valueOf(execution.getVariable(SoPropertyConstants.SO_ACTION));
180 String pnfName = String.valueOf(execution.getVariable(PNF_CORRELATION_ID));
181 logger.debug(String.format("requestId: %s, action: %s, pnfName: %s", requestId, requestAction, pnfName));
183 String requestPayload = String.valueOf(execution.getVariable(REQUEST_PAYLOAD));
184 logger.debug("SO request payload: " + requestPayload);
189 switch (requestAction) {
190 case SoPropertyConstants.ACTION_PRE_CHECK:
191 lcmAction = SDNCLcmActionConstants.UPGRADE_PRE_CHECK;
193 UpgradePreCheckPayload upgradePreCheckPayload;
194 upgradePreCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePreCheckPayload(execution);
195 lcmPayload = convertToSting(upgradePreCheckPayload);
197 case SoPropertyConstants.ACTION_DOWNLOAD_N_E_SW:
198 lcmAction = SDNCLcmActionConstants.DOWNLOAD_N_E_SW;
200 DownloadNESwPayload downloadNESwPayload;
201 downloadNESwPayload = SDNCLcmPayloadBuilder.buildDownloadNESwPayload(execution);
202 lcmPayload = convertToSting(downloadNESwPayload);
204 case SoPropertyConstants.ACTION_ACTIVATE_N_E_SW:
205 lcmAction = SDNCLcmActionConstants.ACTIVATE_N_E_SW;
207 ActivateNESwPayload activateNESwPayload;
208 activateNESwPayload = SDNCLcmPayloadBuilder.buildActivateNESwPayload(execution);
209 lcmPayload = convertToSting(activateNESwPayload);
211 case SoPropertyConstants.ACTION_POST_CHECK:
212 lcmAction = SDNCLcmActionConstants.UPGRADE_POST_CHECK;
214 UpgradePostCheckPayload upgradePostCheckPayload;
215 upgradePostCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePostCheckPayload(execution);
216 lcmPayload = convertToSting(upgradePostCheckPayload);
219 String msg = "Unsupported SO Action: " + requestAction;
221 throw new PayloadGenerationException(msg);
224 String subRequestId = UUID.randomUUID().toString();
226 SDNCLcmMessageBuilder.buildLcmInputForPnf(requestId, subRequestId, pnfName, lcmAction, lcmPayload);
229 String lcmInputMsg = mapper.writeValueAsString(lcmInput);
230 logger.debug("SDNC input message for {}: {}", lcmAction, lcmInputMsg);
231 } catch (JsonProcessingException e) {
232 throw new PayloadGenerationException(e.getMessage());
238 private void parseLcmOutput(LcmOutput lcmOutput, String lcmAction) throws BadResponseException {
239 LcmStatus lcmStatus = lcmOutput.getStatus();
240 int lcmStatusCode = lcmStatus.getCode();
241 String outputPayload = lcmOutput.getPayload();
243 if (lcmStatusCode == SDNCConstants.LCM_OUTPUT_SUCCESS_CODE) {
244 logger.debug("Call SDNC LCM API for {} success, code: {}, message: {}, payload: {}", lcmAction,
245 lcmStatusCode, lcmStatus.getMessage(), outputPayload);
247 String msg = String.format("Call SDNC LCM API for %s failure, code: %d, message: %s, payload: %s",
248 lcmAction, lcmStatusCode, lcmStatus.getMessage(), outputPayload);
250 throw new BadResponseException(msg);
254 private void sendLcmRequest(DelegateExecution execution, LcmInput lcmInput) throws BadResponseException {
255 String actionMode = String.valueOf(execution.getVariable(SoPropertyConstants.SO_ACTION_MODE));
256 String lcmOperation = toLowerHyphen(lcmInput.getAction());
259 if ("async".equals(actionMode)) {
260 lcmOutput = sendAsyncRequest(lcmOperation, lcmInput);
262 lcmOutput = sendSyncRequest(lcmOperation, lcmInput);
265 parseLcmOutput(lcmOutput, lcmInput.getAction());