aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/servicedecomposition/tasks/ExecuteBuildingBlockRainyDay.java
blob: ae7addcc57b7817af11c735ab9c7908613dfbe28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.bpmn.servicedecomposition.tasks;

import java.util.Map;
import org.camunda.bpm.engine.delegate.BpmnError;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.onap.logging.filter.base.ONAPComponentsList;
import org.onap.so.bpmn.core.WorkflowException;
import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
import org.onap.so.constants.Status;
import org.onap.so.db.catalog.beans.macro.RainyDayHandlerStatus;
import org.onap.so.db.catalog.client.CatalogDbClient;
import org.onap.so.db.request.beans.InfraActiveRequests;
import org.onap.so.db.request.client.RequestsDbClient;
import org.onap.so.utils.Components;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class ExecuteBuildingBlockRainyDay {

    private static final Logger logger = LoggerFactory.getLogger(ExecuteBuildingBlockRainyDay.class);
    public static final String HANDLING_CODE = "handlingCode";
    public static final String ROLLBACK_TARGET_STATE = "rollbackTargetState";

    @Autowired
    private CatalogDbClient catalogDbClient;
    @Autowired
    private RequestsDbClient requestDbclient;
    private static final String ASTERISK = "*";

    @Autowired
    private Environment environment;
    protected String retryDurationPath = "mso.rainyDay.retryDurationMultiplier";
    protected String defaultCode = "mso.rainyDay.defaultCode";
    protected String maxRetries = "mso.rainyDay.maxRetries";

    public void setRetryTimer(DelegateExecution execution) {
        try {
            int retryDurationMult = Integer.parseInt(this.environment.getProperty(retryDurationPath));
            int retryCount = (int) execution.getVariable("retryCount");
            int retryTimeToWait = (int) Math.pow(retryDurationMult, retryCount) * 10;
            String RetryDuration = "PT" + retryTimeToWait + "S";
            execution.setVariable("RetryDuration", RetryDuration);
        } catch (Exception e) {
            logger.error("Exception occurred", e);
            throw new BpmnError("Unknown error incrementing retry counter");
        }
    }

    public void queryRainyDayTable(DelegateExecution execution, boolean primaryPolicy) {
        try {
            ExecuteBuildingBlock ebb = (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
            String bbName = ebb.getBuildingBlock().getBpmnFlowName();
            GeneralBuildingBlock gBBInput = (GeneralBuildingBlock) execution.getVariable("gBBInput");
            String requestId = (String) execution.getVariable("mso-request-id");
            Map<ResourceKey, String> lookupKeyMap = (Map<ResourceKey, String>) execution.getVariable("lookupKeyMap");
            String serviceType = ASTERISK;
            boolean aLaCarte = (boolean) execution.getVariable("aLaCarte");
            boolean suppressRollback = (boolean) execution.getVariable("suppressRollback");
            WorkflowException workflowException = (WorkflowException) execution.getVariable("WorkflowException");
            if (workflowException != null) {
                execution.setVariable("WorkflowExceptionErrorMessage", workflowException.getErrorMessage());
            } else {
                logger.debug("WorkflowException is null, unable to set WorkflowExceptionErrorMessage");
            }
            String handlingCode = "";

            if (suppressRollback) {
                handlingCode = "Abort";
            } else {
                try {
                    serviceType = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
                            .getModelInfoServiceInstance().getServiceType();
                    if (serviceType == null || serviceType.isEmpty()) {
                        serviceType = ASTERISK;
                    }
                } catch (Exception ex) {
                    // keep default serviceType value
                    logger.error("Exception in serviceType retrieval", ex);
                }
                String vnfType = ASTERISK;
                try {
                    for (GenericVnf vnf : gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
                            .getVnfs()) {
                        if (vnf.getVnfId().equalsIgnoreCase(lookupKeyMap.get(ResourceKey.GENERIC_VNF_ID))) {
                            vnfType = vnf.getVnfType();
                        }
                    }
                } catch (Exception ex) {
                    // keep default vnfType value
                    logger.error("Exception in vnfType retrieval", ex);
                }

                String errorCode = ASTERISK;
                if (workflowException != null) {
                    errorCode = "" + workflowException.getErrorCode();
                } else {
                    logger.debug("WorkflowException is null, unable to get error code");
                }

                try {
                    errorCode = "" + (String) execution.getVariable("WorkflowExceptionCode");
                } catch (Exception ex) {
                    // keep default errorCode value
                    logger.error("Exception in errorCode retrieval", ex);
                }

                String workStep = ASTERISK;
                try {
                    workStep = workflowException.getWorkStep();
                } catch (Exception ex) {
                    // keep default workStep value
                    logger.error("Exception in workStep retrieval", ex);
                }

                String errorMessage = ASTERISK;
                try {
                    errorMessage = workflowException.getErrorMessage();
                } catch (Exception ex) {
                    // keep default workStep value
                    logger.error("Exception in errorMessage retrieval", ex);
                }

                String serviceRole = ASTERISK;
                try {
                    serviceRole = gBBInput.getCustomer().getServiceSubscription().getServiceInstances().get(0)
                            .getModelInfoServiceInstance().getServiceRole();
                    if (serviceRole == null || serviceRole.isEmpty()) {
                        serviceRole = ASTERISK;
                    }
                } catch (Exception ex) {
                    // keep default serviceRole value
                }

                RainyDayHandlerStatus rainyDayHandlerStatus;
                rainyDayHandlerStatus = catalogDbClient.getRainyDayHandlerStatus(bbName, serviceType, vnfType,
                        errorCode, workStep, errorMessage, serviceRole);

                if (rainyDayHandlerStatus == null) {
                    handlingCode = "Abort";
                } else {
                    if (primaryPolicy) {
                        handlingCode = rainyDayHandlerStatus.getPolicy();
                    } else {
                        handlingCode = rainyDayHandlerStatus.getSecondaryPolicy();
                    }
                }
                if (!primaryPolicy) {
                    try {
                        InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
                        request.setRetryStatusMessage("Retries have been exhausted.");
                        requestDbclient.updateInfraActiveRequests(request);
                    } catch (Exception ex) {
                        logger.error("Failed to update Request Db Infra Active Requests with Retry Status", ex);
                    }
                }
                if ("RollbackToAssigned".equals(handlingCode) && !aLaCarte) {
                    handlingCode = "Rollback";
                }
                if (handlingCode.startsWith("Rollback")) {
                    String targetState = "";
                    if ("RollbackToAssigned".equalsIgnoreCase(handlingCode)) {
                        targetState = Status.ROLLED_BACK_TO_ASSIGNED.toString();
                    } else if ("RollbackToCreated".equalsIgnoreCase(handlingCode)) {
                        targetState = Status.ROLLED_BACK_TO_CREATED.toString();
                    } else {
                        targetState = Status.ROLLED_BACK.toString();
                    }
                    execution.setVariable(ROLLBACK_TARGET_STATE, targetState);
                    logger.debug("Rollback target state is: {}", targetState);
                }
            }
            logger.debug("RainyDayHandler Status Code is: {}", handlingCode);
            execution.setVariable(HANDLING_CODE, handlingCode);
        } catch (Exception e) {
            String code = this.environment.getProperty(defaultCode);
            logger.error("Failed to determine RainyDayHandler Status. Seting handlingCode = {}", code, e);
            execution.setVariable(HANDLING_CODE, code);
        }
        try {
            int envMaxRetries = Integer.parseInt(this.environment.getProperty(maxRetries));
            execution.setVariable("maxRetries", envMaxRetries);
        } catch (Exception ex) {
            logger.error("Could not read maxRetries from config file. Setting max to 5 retries", ex);
            execution.setVariable("maxRetries", 5);
        }
    }

    public void setHandlingStatusSuccess(DelegateExecution execution) {
        execution.setVariable(HANDLING_CODE, "Success");
    }

    public void updateExtSystemErrorSource(DelegateExecution execution) {
        try {
            String requestId = (String) execution.getVariable("mso-request-id");
            WorkflowException exception = (WorkflowException) execution.getVariable("WorkflowException");
            ONAPComponentsList extSystemErrorSource = exception.getExtSystemErrorSource();
            InfraActiveRequests request = requestDbclient.getInfraActiveRequestbyRequestId(requestId);
            Boolean isRollbackFailure = (Boolean) execution.getVariable("isRollback");
            if (isRollbackFailure == null) {
                isRollbackFailure = false;
            }

            if (extSystemErrorSource != null) {
                String extSystemErrorSourceString = extSystemErrorSource.toString();
                if (isRollbackFailure) {
                    logger.debug("Updating extSystemErrorSource for isRollbackFailure to {} for request: {}",
                            extSystemErrorSourceString, requestId);
                    request.setRollbackExtSystemErrorSource(extSystemErrorSourceString);
                } else {
                    logger.debug("Updating extSystemErrorSource to {} for request: {}", extSystemErrorSourceString,
                            requestId);
                    request.setExtSystemErrorSource(extSystemErrorSourceString);
                }
            } else if (isRollbackFailure) {
                logger.debug(
                        "rollbackExtSystemErrorSource is null for isRollbackFailure. Setting rollbackExtSystemErrorSource to UNKNOWN");
                request.setRollbackExtSystemErrorSource(Components.UNKNOWN.toString());
            } else {
                logger.debug("extSystemErrorSource is null. Setting extSystemErrorSource to UNKNOWN");
                request.setExtSystemErrorSource(Components.UNKNOWN.toString());
            }

            request.setLastModifiedBy("CamundaBPMN");
            requestDbclient.updateInfraActiveRequests(request);
        } catch (Exception e) {
            logger.error("Failed to update Request db with extSystemErrorSource or rollbackExtSystemErrorSource: ", e);
        }
    }

}