aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/exception/ExceptionBuilder.java
blob: f40948fc49328eaee6872bba56258a0dea3b2e2d (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 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.client.exception;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.camunda.bpm.engine.delegate.BpmnError;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.onap.aai.domain.yang.LInterface;
import org.onap.aai.domain.yang.Vserver;
import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
import org.onap.aaiclient.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
import org.onap.logging.filter.base.ErrorCode;
import org.onap.logging.filter.base.ONAPComponents;
import org.onap.logging.filter.base.ONAPComponentsList;
import org.onap.so.bpmn.common.BuildingBlockExecution;
import org.onap.so.bpmn.common.DelegateExecutionImpl;
import org.onap.so.bpmn.core.WorkflowException;
import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
import org.onap.so.logger.LoggingAnchor;
import org.onap.so.logger.MessageEnum;
import org.onap.so.objects.audit.AAIObjectAudit;
import org.onap.so.objects.audit.AAIObjectAuditList;
import org.onap.so.utils.Components;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component
public class ExceptionBuilder {
    private static final Logger logger = LoggerFactory.getLogger(ExceptionBuilder.class);


    protected ExtractPojosForBB getExtractPojosForBB() {
        return new ExtractPojosForBB();
    }

    public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, Exception exception) {
        String msg = "Exception in %s.%s ";
        try {
            logger.error("Exception occurred", exception);

            String errorVariable = "Error%s%s";

            StackTraceElement[] trace = Thread.currentThread().getStackTrace();
            for (StackTraceElement traceElement : trace) {
                if (!traceElement.getClassName().equals(this.getClass().getName())
                        && !traceElement.getClassName().equals(Thread.class.getName())) {
                    msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
                    String shortClassName =
                            traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
                    errorVariable = String.format(errorVariable, shortClassName, traceElement.getMethodName());
                    break;
                }
            }

            logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
                    ErrorCode.UnknownError.getValue(), msg);
            execution.setVariable(errorVariable, exception.getMessage());
        } catch (Exception ex) {
            // log trace, allow process to complete gracefully
            logger.error("Exception occurred", ex);
        }

        if (exception.getMessage() != null)
            msg = msg.concat(exception.getMessage());
        buildAndThrowWorkflowException(execution, errorCode, msg);
    }

    public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, Exception exception,
            ONAPComponentsList extSystemErrorSource) {
        String msg = "Exception in %s.%s ";
        try {
            logger.error("Exception occurred", exception);

            String errorVariable = "Error%s%s";

            StackTraceElement[] trace = Thread.currentThread().getStackTrace();
            for (StackTraceElement traceElement : trace) {
                if (!traceElement.getClassName().equals(this.getClass().getName())
                        && !traceElement.getClassName().equals(Thread.class.getName())) {
                    msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
                    String shortClassName =
                            traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
                    errorVariable = String.format(errorVariable, shortClassName, traceElement.getMethodName());
                    break;
                }
            }

            logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
                    ErrorCode.UnknownError.getValue(), msg);
            execution.setVariable(errorVariable, exception.getMessage());
        } catch (Exception ex) {
            // log trace, allow process to complete gracefully
            logger.error("Exception occurred", ex);
        }

        if (exception.getMessage() != null)
            msg = msg.concat(exception.getMessage());
        buildAndThrowWorkflowException(execution, errorCode, msg, extSystemErrorSource);
    }

    public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, Exception exception) {
        String msg = "Exception in %s.%s ";
        try {
            logger.error("Exception occurred", exception);

            String errorVariable = "Error%s%s";

            StackTraceElement[] trace = Thread.currentThread().getStackTrace();
            for (StackTraceElement traceElement : trace) {
                if (!traceElement.getClassName().equals(this.getClass().getName())
                        && !traceElement.getClassName().equals(Thread.class.getName())) {
                    msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
                    String shortClassName =
                            traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
                    errorVariable = String.format(errorVariable, shortClassName, traceElement.getMethodName());
                    break;
                }
            }
            logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
                    ErrorCode.UnknownError.getValue(), msg);
            execution.setVariable(errorVariable, exception.getMessage());
        } catch (Exception ex) {
            // log trace, allow process to complete gracefully
            logger.error("Exception occurred", ex);
        }

        if (exception.getMessage() != null)
            msg = msg.concat(exception.getMessage());
        buildAndThrowWorkflowException(execution, errorCode, msg);
    }

    public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, Exception exception,
            ONAPComponentsList extSystemErrorSource) {
        String msg = "Exception in %s.%s ";
        try {
            logger.error("Exception occurred", exception);

            String errorVariable = "Error%s%s";

            StackTraceElement[] trace = Thread.currentThread().getStackTrace();
            for (StackTraceElement traceElement : trace) {
                if (!traceElement.getClassName().equals(this.getClass().getName())
                        && !traceElement.getClassName().equals(Thread.class.getName())) {
                    msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
                    String shortClassName =
                            traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
                    errorVariable = String.format(errorVariable, shortClassName, traceElement.getMethodName());
                    break;
                }
            }
            logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), msg, "BPMN",
                    ErrorCode.UnknownError.getValue(), msg);
            execution.setVariable(errorVariable, exception.getMessage());
        } catch (Exception ex) {
            // log trace, allow process to complete gracefully
            logger.error("Exception occurred", ex);
        }

        if (exception.getMessage() != null)
            msg = msg.concat(exception.getMessage());
        buildAndThrowWorkflowException(execution, errorCode, msg, extSystemErrorSource);
    }

    /**
     * @deprecated Please utilize method that specifies where the failure occured
     */
    @Deprecated
    public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, String errorMessage) {
        if (execution instanceof DelegateExecutionImpl) {
            buildAndThrowWorkflowException(((DelegateExecutionImpl) execution).getDelegateExecution(), errorCode,
                    errorMessage);
        }
    }

    public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, String errorMessage,
            ONAPComponentsList extSystemErrorSource) {
        if (execution instanceof DelegateExecutionImpl) {
            buildAndThrowWorkflowException(((DelegateExecutionImpl) execution).getDelegateExecution(), errorCode,
                    errorMessage, extSystemErrorSource);
        }
    }

    public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, String errorMessage) {

        buildWorkflowException(execution, errorCode, errorMessage);
        logger.info("Throwing MSOWorkflowException");
        throw new BpmnError("MSOWorkflowException");
    }

    public void buildWorkflowException(DelegateExecution execution, int errorCode, String errorMessage) {
        String processKey = getProcessKey(execution);
        logger.info("Building a WorkflowException");

        WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage);
        execution.setVariable("WorkflowException", exception);
        execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
        logger.info("Outgoing WorkflowException is {}", exception);
    }

    public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, String errorMessage,
            ONAPComponentsList extSystemErrorSource) {

        buildWorkflowException(execution, errorCode, errorMessage, extSystemErrorSource);
        logger.info("Throwing MSOWorkflowException");
        throw new BpmnError("MSOWorkflowException");
    }

    public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, String errorMessage,
            ONAPComponentsList extSystemErrorSource, String workStep) {
        String processKey = getProcessKey(execution);
        logger.info("Building a WorkflowException for Subflow");

        WorkflowException exception =
                new WorkflowException(processKey, errorCode, errorMessage, workStep, extSystemErrorSource);
        execution.setVariable("WorkflowException", exception);
        execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
        logger.info("Outgoing WorkflowException is {}", exception);
        logger.info("Throwing MSOWorkflowException");
        throw new BpmnError("MSOWorkflowException");
    }

    public WorkflowException buildWorkflowException(DelegateExecution execution, int errorCode, String errorMessage,
            ONAPComponentsList extSystemErrorSource) {
        String processKey = getProcessKey(execution);
        logger.info("Building a WorkflowException for Subflow");

        WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage, extSystemErrorSource);
        execution.setVariable("WorkflowException", exception);
        execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
        logger.info("Outgoing WorkflowException is {}", exception);
        return exception;
    }

    public void buildAndThrowWorkflowException(DelegateExecution execution, String errorCode, String errorMessage) {
        execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
        throw new BpmnError(errorCode, errorMessage);
    }

    public String getProcessKey(DelegateExecution execution) {
        String testKey = (String) execution.getVariable("testProcessKey");
        if (testKey != null) {
            return testKey;
        }
        return execution.getProcessEngineServices().getRepositoryService()
                .getProcessDefinition(execution.getProcessDefinitionId()).getKey();
    }

    public void processAuditException(DelegateExecutionImpl execution, boolean flowShouldContinue) {
        logger.debug("Processing Audit Results");
        String auditListString = execution.getVariable("auditInventoryResult");
        String processKey = getProcessKey(execution.getDelegateExecution());
        if (auditListString != null) {
            StringBuilder errorMessage = new StringBuilder();
            try {
                ExtractPojosForBB extractPojosForBB = getExtractPojosForBB();
                VfModule module = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
                String cloudRegionId = execution.getGeneralBuildingBlock().getCloudRegion().getLcpCloudRegionId();

                GraphInventoryCommonObjectMapperProvider objectMapper = new GraphInventoryCommonObjectMapperProvider();
                AAIObjectAuditList auditList =
                        objectMapper.getMapper().readValue(auditListString, AAIObjectAuditList.class);

                errorMessage = errorMessage.append(auditList.getAuditType() + " VF-Module " + module.getVfModuleId()
                        + " failed due to incomplete AAI vserver inventory population after stack "
                        + auditList.getHeatStackName() + " was successfully " + auditList.getAuditType()
                        + "d in cloud region " + cloudRegionId + ". MSO Audit indicates that the following was not "
                        + auditList.getAuditType() + "d in AAI: ");

                Stream<AAIObjectAudit> vServerLInterfaceAuditStream = auditList.getAuditList().stream()
                        .filter(auditObject -> auditObject.getAaiObjectType().equals(Types.VSERVER.typeName())
                                || auditObject.getAaiObjectType().equals(Types.L_INTERFACE.typeName()));
                List<AAIObjectAudit> filteredAuditStream =
                        vServerLInterfaceAuditStream.filter(a -> !a.isDoesObjectExist()).collect(Collectors.toList());

                for (AAIObjectAudit object : filteredAuditStream) {
                    if (object.getAaiObjectType().equals(Types.L_INTERFACE.typeName())) {
                        LInterface li = objectMapper.getMapper().convertValue(object.getAaiObject(), LInterface.class);
                        errorMessage =
                                errorMessage.append(Types.L_INTERFACE.typeName() + " " + li.getInterfaceId() + ", ");
                    } else {
                        Vserver vs = objectMapper.getMapper().convertValue(object.getAaiObject(), Vserver.class);
                        errorMessage = errorMessage.append(Types.VSERVER.typeName() + " " + vs.getVserverId() + ", ");
                    }
                }

                if (errorMessage.length() > 0) {
                    errorMessage.setLength(errorMessage.length() - 2);
                    errorMessage = errorMessage.append(".");
                }

            } catch (IOException | BBObjectNotFoundException e) {
                errorMessage = errorMessage.append("process objects in AAI. ");
                logger.error("Exception occurred in processAuditException", e);
            }

            if (flowShouldContinue) {
                execution.setVariable("StatusMessage", errorMessage.toString());
            } else {
                WorkflowException exception =
                        new WorkflowException(processKey, 400, errorMessage.toString(), ONAPComponents.SO);
                execution.setVariable("WorkflowException", exception);
                execution.setVariable("WorkflowExceptionErrorMessage", errorMessage.toString());
                logger.info("Outgoing WorkflowException is {}", exception);
                logger.info("Throwing AAIInventoryFailure");
                throw new BpmnError("AAIInventoryFailure");
            }

        } else {
            String errorMessage = "Unable to process audit results due to auditInventoryResult being null";
            WorkflowException exception = new WorkflowException(processKey, 400, errorMessage, ONAPComponents.SO);
            execution.setVariable("WorkflowException", exception);
            execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
            logger.info("Outgoing WorkflowException is {}", exception);
            logger.info("Throwing AAIInventoryFailure");
            throw new BpmnError("AAIInventoryFailure");
        }
    }

    public void processOpenstackAdapterException(DelegateExecution execution) {
        StringBuilder workflowExceptionMessage = new StringBuilder();
        logger.debug("Processing Vnf Adapter Exception");
        try {
            String errorMessage = (String) execution.getVariable("openstackAdapterErrorMessage");
            boolean openstackRollbackPollSuccess = (boolean) execution.getVariable("OpenstackPollSuccess");
            boolean rollbackPerformed = (boolean) execution.getVariable("rollbackPerformed");
            boolean openstackRollbackSuccess = (boolean) execution.getVariable("OpenstackRollbackSuccess");
            boolean pollRollbackStatus = (boolean) execution.getVariable("PollRollbackStatus");

            workflowExceptionMessage.append("Exception occured during vnf adapter: " + errorMessage + ".");

            boolean rollbackCompleted = false;
            if (rollbackPerformed) {
                if (openstackRollbackSuccess && !pollRollbackStatus) {
                    rollbackCompleted = true;
                } else if (openstackRollbackSuccess && pollRollbackStatus) {
                    if (openstackRollbackPollSuccess) {
                        rollbackCompleted = true;
                    }
                }
                workflowExceptionMessage
                        .append(" The resource was rollbacked in openstack: " + rollbackCompleted + ".");
            }
        } catch (Exception e) {
            logger.debug("Error while Processing Vnf Adapter Exception", e);
        }
        buildWorkflowException(execution, 500, workflowExceptionMessage.toString(), Components.OPENSTACK);
        throw new BpmnError("MSOWorkflowException");


    }

    public void processInventoryException(DelegateExecution execution) {
        String errorMessage = "";
        logger.debug("Processing Inventory Exception");
        try {
            errorMessage = (String) execution.getVariable("inventoryErrorMessage");
        } catch (Exception e) {
            logger.debug("Error while Processing Inventory Exception", e);
        }
        buildWorkflowException(execution, 500, errorMessage, Components.OPENSTACK);
        throw new BpmnError("MSOWorkflowException");


    }

}