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
|
/*-
* ============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.onap.so.logger.LoggingAnchor;
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.so.bpmn.common.BuildingBlockExecution;
import org.onap.so.bpmn.common.DelegateExecutionImpl;
import org.onap.so.bpmn.core.WorkflowException;
import org.onap.so.logger.ErrorCode;
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.client.aai.AAIObjectType;
import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
import org.onap.so.logger.MessageEnum;
import org.onap.so.objects.audit.AAIObjectAudit;
import org.onap.so.objects.audit.AAIObjectAuditList;
import org.onap.logging.filter.base.ONAPComponentsList;
import org.onap.logging.filter.base.ONAPComponents;
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.toString());
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.toString());
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.toString());
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.toString());
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(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) {
String processKey = getProcessKey(execution);
logger.info("Building a WorkflowException for Subflow");
WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage);
execution.setVariable("WorkflowException", exception);
execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
logger.info("Outgoing WorkflowException is {}", exception);
logger.info("Throwing MSOWorkflowException");
throw new BpmnError("MSOWorkflowException");
}
public void buildAndThrowWorkflowException(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);
logger.info("Throwing MSOWorkflowException");
throw new BpmnError("MSOWorkflowException");
}
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 = (String) 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(AAIObjectType.VSERVER.typeName())
|| auditObject.getAaiObjectType().equals(AAIObjectType.L_INTERFACE.typeName()));
List<AAIObjectAudit> filteredAuditStream =
vServerLInterfaceAuditStream.filter(a -> !a.isDoesObjectExist()).collect(Collectors.toList());
for (AAIObjectAudit object : filteredAuditStream) {
if (object.getAaiObjectType().equals(AAIObjectType.L_INTERFACE.typeName())) {
LInterface li = objectMapper.getMapper().convertValue(object.getAaiObject(), LInterface.class);
errorMessage = errorMessage
.append(AAIObjectType.L_INTERFACE.typeName() + " " + li.getInterfaceId() + ", ");
} else {
Vserver vs = objectMapper.getMapper().convertValue(object.getAaiObject(), Vserver.class);
errorMessage =
errorMessage.append(AAIObjectType.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. ");
}
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 MSOWorkflowException");
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 MSOWorkflowException");
throw new BpmnError("AAIInventoryFailure");
}
}
}
|