ed865e058fc251215e703852380c83d57fcf5eba
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix
4  *  Modifications Copyright (C) 2020 Huawei
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
9  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.sdnc;
22
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.core.json.JsonUtils;
30 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerContext;
31 import org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.common.SoPropertyConstants;
32 import org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.LcmControllerDE;
33 import org.onap.so.client.sdnc.common.SDNCConstants;
34 import org.onap.so.client.sdnc.lcm.*;
35 import org.onap.so.client.sdnc.lcm.beans.*;
36 import org.onap.so.client.sdnc.lcm.beans.payload.*;
37 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_ID;
38 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_CORRELATION_ID;
39 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_PAYLOAD;
40
41 @Component
42 public class SdncControllerDE extends LcmControllerDE {
43
44     private static final int SDNC_DELEGATE_EXECUTION_ERROR_CODE = 1103;
45
46     @Override
47     public Boolean understand(ControllerContext<DelegateExecution> context) {
48         return context.getControllerActor().equalsIgnoreCase("sdnc");
49     }
50
51     @Override
52     public Boolean ready(ControllerContext<DelegateExecution> context) {
53         return true;
54     }
55
56     @Override
57     protected int callLcmClient(ControllerContext<DelegateExecution> context) {
58         DelegateExecution execution = context.getExecution();
59
60         logger.debug("Running activity for id: {}, name: {}", execution.getCurrentActivityId(),
61                 execution.getCurrentActivityName());
62
63         boolean result;
64         try {
65             LcmInput lcmInput = buildLcmInput(execution);
66             if (lcmInput != null) {
67                 result = sendLcmRequest(execution, lcmInput);
68             } else {
69                 logger.error("Build LCM Input error");
70                 result = false;
71             }
72         } catch (Exception e) {
73             logger.error("Call SDNC LCM Client failure: ", e);
74             result = false;
75         }
76
77         if (result) {
78             execution.setVariable(SoPropertyConstants.CONTROLLER_STATUS, "Success");
79         } else {
80             execution.setVariable(SoPropertyConstants.CONTROLLER_STATUS, "Failure");
81         }
82
83         return 0;
84     }
85
86     @Override
87     protected int getErrorCode() {
88         return SDNC_DELEGATE_EXECUTION_ERROR_CODE;
89     }
90
91     private LcmOutput sendSyncRequest(String operation, LcmInput lcmInput) {
92         SDNCLcmClientBuilder sdncLcmClientBuilder = new SDNCLcmClientBuilder();
93         SDNCLcmRestClient sdncLcmRestClient;
94         try {
95             sdncLcmRestClient = sdncLcmClientBuilder.newSDNCLcmRestClient(operation);
96         } catch (SDNCLcmClientBuilderException e) {
97             logger.error("Create SDNCLcmRestClient error: ", e);
98             return null;
99         }
100
101         return sdncLcmRestClient.sendRequest(lcmInput);
102     }
103
104     private LcmOutput selectLcmOutputFromDmaapResponses(List<LcmDmaapResponse> lcmDmaapResponses, LcmInput lcmInput) {
105         String expectedCorrelationId =
106                 lcmInput.getCommonHeader().getRequestId() + "-" + lcmInput.getCommonHeader().getSubRequestId();
107
108         for (LcmDmaapResponse lcmDmaapResponse : lcmDmaapResponses) {
109             String correlationId = lcmDmaapResponse.getCorrelationId();
110             if (expectedCorrelationId.equals(correlationId)) {
111                 return lcmDmaapResponse.getBody().getOutput();
112             }
113         }
114
115         return null;
116     }
117
118     private LcmOutput sendAsyncRequest(String operation, LcmInput lcmInput) {
119         SDNCLcmClientBuilder sdncLcmClientBuilder = new SDNCLcmClientBuilder();
120         SDNCLcmDmaapClient sdncLcmDmaapClient;
121         try {
122             sdncLcmDmaapClient = sdncLcmClientBuilder.newSDNCLcmDmaapClient();
123         } catch (SDNCLcmClientBuilderException e) {
124             logger.error("Create SDNCLcmDmaapClient error: ", e);
125             return null;
126         }
127
128         LcmDmaapRequest lcmDmaapRequest = SDNCLcmMessageBuilder.buildLcmDmaapRequest(operation, lcmInput);
129         try {
130             sdncLcmDmaapClient.sendRequest(lcmDmaapRequest);
131         } catch (Exception e) {
132             logger.error("SDNCLcmDmaapClient sends request error: ", e);
133             return null;
134         }
135
136         long timeout = sdncLcmClientBuilder.getSDNCLcmProperties().getActionTimeout();
137         long startTime = System.currentTimeMillis();
138         while (true) {
139             List<LcmDmaapResponse> lcmDmaapResponses = sdncLcmDmaapClient.getResponse();
140             if (lcmDmaapResponses.size() > 0) {
141                 LcmOutput lcmOutput = selectLcmOutputFromDmaapResponses(lcmDmaapResponses, lcmInput);
142                 if (lcmOutput != null) {
143                     return lcmOutput;
144                 }
145             }
146
147             long stopTime = System.currentTimeMillis();
148             if ((stopTime - startTime) > timeout) {
149                 logger.error("Timeout for SDNC LCM action {}", lcmInput.getAction());
150                 return null;
151             }
152         }
153     }
154
155     public static String toLowerHyphen(String lcmAction) {
156         String regex = "([a-z0-9A-Z])(?=[A-Z])";
157         String replacement = "$1-";
158         return lcmAction.replaceAll(regex, replacement).toLowerCase();
159     }
160
161     private LcmInput buildLcmInput(DelegateExecution execution) throws JsonProcessingException {
162         String requestId = String.valueOf(execution.getVariable(REQUEST_ID));
163         String requestAction = String.valueOf(execution.getVariable(SoPropertyConstants.SO_ACTION));
164         String pnfName = String.valueOf(execution.getVariable(PNF_CORRELATION_ID));
165         logger.debug(String.format("requestId: %s, action: %s, pnfName: %s", requestId, requestAction, pnfName));
166
167         String requestPayload = String.valueOf(execution.getVariable(REQUEST_PAYLOAD));
168         logger.debug("SO request payload: " + requestPayload);
169
170         String lcmAction;
171         String lcmPayload;
172
173         switch (requestAction) {
174             case SoPropertyConstants.ACTION_PRE_CHECK:
175                 lcmAction = SDNCLcmActionConstants.UPGRADE_PRE_CHECK;
176
177                 UpgradePreCheckPayload upgradePreCheckPayload;
178                 upgradePreCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePreCheckPayload(execution);
179                 lcmPayload = SDNCLcmPayloadBuilder.convertToSting(upgradePreCheckPayload);
180                 break;
181             case SoPropertyConstants.ACTION_DOWNLOAD_N_E_SW:
182                 lcmAction = SDNCLcmActionConstants.DOWNLOAD_N_E_SW;
183
184                 DownloadNESwPayload downloadNESwPayload;
185                 downloadNESwPayload = SDNCLcmPayloadBuilder.buildDownloadNESwPayload(execution);
186                 lcmPayload = SDNCLcmPayloadBuilder.convertToSting(downloadNESwPayload);
187                 break;
188             case SoPropertyConstants.ACTION_ACTIVATE_N_E_SW:
189                 lcmAction = SDNCLcmActionConstants.ACTIVATE_N_E_SW;
190
191                 ActivateNESwPayload activateNESwPayload;
192                 activateNESwPayload = SDNCLcmPayloadBuilder.buildActivateNESwPayload(execution);
193                 lcmPayload = SDNCLcmPayloadBuilder.convertToSting(activateNESwPayload);
194                 break;
195             case SoPropertyConstants.ACTION_POST_CHECK:
196                 lcmAction = SDNCLcmActionConstants.UPGRADE_POST_CHECK;
197
198                 UpgradePostCheckPayload upgradePostCheckPayload;
199                 upgradePostCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePostCheckPayload(execution);
200                 lcmPayload = SDNCLcmPayloadBuilder.convertToSting(upgradePostCheckPayload);
201                 break;
202             default:
203                 logger.error("Unsupported SO Action: " + requestAction);
204                 return null;
205         }
206
207         logger.debug("SDNC LCM payload for {}: {}", lcmAction, lcmPayload);
208
209         String subRequestId = UUID.randomUUID().toString();
210         LcmInput lcmInput =
211                 SDNCLcmMessageBuilder.buildLcmInputForPnf(requestId, subRequestId, pnfName, lcmAction, lcmPayload);
212
213         ObjectMapper mapper = new ObjectMapper();
214         String lcmInputMsg = mapper.writeValueAsString(lcmInput);
215         logger.debug("SDNC input message:\n" + lcmInputMsg);
216
217         return lcmInput;
218     }
219
220     private boolean parseLcmOutput(LcmOutput lcmOutput, String lcmAction) {
221         if (lcmOutput == null) {
222             logger.error("Call SDNC LCM API failure");
223             return false;
224         }
225
226         LcmStatus lcmStatus = lcmOutput.getStatus();
227
228         if (lcmStatus.getCode() == SDNCConstants.LCM_OUTPUT_SUCCESS_CODE) {
229             logger.debug("Call SDNC LCM API success: " + lcmStatus.getMessage());
230         } else {
231             logger.error("Call SDNC LCM API failure: " + lcmStatus.getMessage());
232         }
233
234         String outputPayload = lcmOutput.getPayload();
235         logger.debug("SDNC LCM action: {}, result: {}", lcmAction, outputPayload);
236         if (outputPayload != null) {
237             String result = JsonUtils.getJsonValue(outputPayload, "result");
238             if ("Success".equals(result)) {
239                 logger.debug("Run SDNC LCM action {} success", lcmAction);
240                 return true;
241             } else {
242                 String reason = JsonUtils.getJsonValue(outputPayload, "reason");
243                 logger.error("Run SDNC LCM action {} failure, reason: {}", lcmAction, reason);
244             }
245         }
246
247         return false;
248     }
249
250     private boolean sendLcmRequest(DelegateExecution execution, LcmInput lcmInput) {
251         String actionMode = String.valueOf(execution.getVariable(SoPropertyConstants.SO_ACTION_MODE));
252         String lcmOperation = toLowerHyphen(lcmInput.getAction());
253
254         LcmOutput lcmOutput;
255         if ("async".equals(actionMode)) {
256             lcmOutput = sendAsyncRequest(lcmOperation, lcmInput);
257         } else {
258             lcmOutput = sendSyncRequest(lcmOperation, lcmInput);
259         }
260
261         return parseLcmOutput(lcmOutput, lcmInput.getAction());
262     }
263 }