8499b6f7d16c14e829d299fd62838514f98310d6
[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.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.sdnc.common.SDNCConstants;
33 import org.onap.so.client.sdnc.lcm.*;
34 import org.onap.so.client.sdnc.lcm.beans.*;
35 import org.onap.so.client.sdnc.lcm.beans.payload.*;
36 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_ID;
37 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_CORRELATION_ID;
38 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_PAYLOAD;
39
40 @Component
41 public class SdncControllerDE extends LcmControllerDE {
42
43     private static final int SDNC_DELEGATE_EXECUTION_ERROR_CODE = 1103;
44
45     @Override
46     public Boolean understand(ControllerContext<DelegateExecution> context) {
47         return context.getControllerActor().equalsIgnoreCase("sdnc");
48     }
49
50     @Override
51     public Boolean ready(ControllerContext<DelegateExecution> context) {
52         return true;
53     }
54
55     @Override
56     protected int callLcmClient(ControllerContext<DelegateExecution> context) {
57         DelegateExecution execution = context.getExecution();
58
59         logger.debug("Running activity for id: {}, name: {}", execution.getCurrentActivityId(),
60                 execution.getCurrentActivityName());
61
62         boolean result;
63         try {
64             LcmInput lcmInput = buildLcmInput(execution);
65             if (lcmInput != null) {
66                 result = sendLcmRequest(execution, lcmInput);
67             } else {
68                 logger.error("Build LCM Input error");
69                 result = false;
70             }
71         } catch (Exception e) {
72             logger.error("Call SDNC LCM Client failure: ", e);
73             result = false;
74         }
75
76         if (result) {
77             execution.setVariable(SoPropertyConstants.CONTROLLER_STATUS, "Success");
78         } else {
79             execution.setVariable(SoPropertyConstants.CONTROLLER_STATUS, "Failure");
80         }
81
82         return 0;
83     }
84
85     @Override
86     protected int getErrorCode() {
87         return SDNC_DELEGATE_EXECUTION_ERROR_CODE;
88     }
89
90     private LcmOutput sendSyncRequest(String operation, LcmInput lcmInput) {
91         SDNCLcmClientBuilder sdncLcmClientBuilder = new SDNCLcmClientBuilder();
92         SDNCLcmRestClient sdncLcmRestClient;
93         try {
94             sdncLcmRestClient = sdncLcmClientBuilder.newSDNCLcmRestClient(operation);
95         } catch (SDNCLcmClientBuilderException e) {
96             logger.error("Create SDNCLcmRestClient error: ", e);
97             return null;
98         }
99
100         return sdncLcmRestClient.sendRequest(lcmInput);
101     }
102
103     private LcmOutput selectLcmOutputFromDmaapResponses(List<LcmDmaapResponse> lcmDmaapResponses, LcmInput lcmInput) {
104         String requestId = lcmInput.getCommonHeader().getRequestId();
105         String subRequestId = lcmInput.getCommonHeader().getSubRequestId();
106
107         for (LcmDmaapResponse lcmDmaapResponse : lcmDmaapResponses) {
108             LcmOutput lcmOutput = lcmDmaapResponse.getBody().getOutput();
109             if (requestId.equals(lcmOutput.getCommonHeader().getRequestId())
110                     && subRequestId.equals(lcmOutput.getCommonHeader().getSubRequestId())) {
111                 return lcmOutput;
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         String subRequestId = UUID.randomUUID().toString();
208         LcmInput lcmInput =
209                 SDNCLcmMessageBuilder.buildLcmInputForPnf(requestId, subRequestId, pnfName, lcmAction, lcmPayload);
210
211         ObjectMapper mapper = new ObjectMapper();
212         String lcmInputMsg = mapper.writeValueAsString(lcmInput);
213         logger.debug("SDNC input message for {}: {}", lcmAction, lcmInputMsg);
214
215         return lcmInput;
216     }
217
218     private boolean parseLcmOutput(LcmOutput lcmOutput, String lcmAction) {
219         if (lcmOutput == null) {
220             logger.error("Call SDNC LCM API failure");
221             return false;
222         }
223
224         LcmStatus lcmStatus = lcmOutput.getStatus();
225         String outputPayload = lcmOutput.getPayload();
226         logger.debug("SDNC LCM output payload of action {}: {}", lcmAction, outputPayload);
227
228         if (lcmStatus.getCode() == SDNCConstants.LCM_OUTPUT_SUCCESS_CODE) {
229             logger.debug("Call SDNC LCM API for {} success, message: {}", lcmAction, lcmStatus.getMessage());
230             return true;
231         } else {
232             logger.error("Call SDNC LCM API for {} failure, message: {}", lcmAction, lcmStatus.getMessage());
233             return false;
234         }
235     }
236
237     private boolean sendLcmRequest(DelegateExecution execution, LcmInput lcmInput) {
238         String actionMode = String.valueOf(execution.getVariable(SoPropertyConstants.SO_ACTION_MODE));
239         String lcmOperation = toLowerHyphen(lcmInput.getAction());
240
241         LcmOutput lcmOutput;
242         if ("async".equals(actionMode)) {
243             lcmOutput = sendAsyncRequest(lcmOperation, lcmInput);
244         } else {
245             lcmOutput = sendSyncRequest(lcmOperation, lcmInput);
246         }
247
248         return parseLcmOutput(lcmOutput, lcmInput.getAction());
249     }
250 }