aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/CommonExceptionUtil.groovy
blob: cce05feca8ea976f67a44fc6b9bbae204c629312 (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
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.openecomp.mso.bpmn.common.scripts

import org.camunda.bpm.engine.runtime.Execution
import org.apache.commons.lang3.*

class CommonExceptionUtil {
	
	
	
	public static enum Error {
		SVC_GENERAL_SERVICE_ERROR("SVC0001","Internal Error"),
		SVC_BAD_PARAMETER("SVC0002", "Invalid input value for message part %1"),
		SVC_NO_SERVER_RESOURCES("SVC1000", "No server resources available to process the request"),
		SVC_DETAILED_SERVICE_ERROR("SVC2000", "The following service error occurred: %1. Error code is %2."),
		POL_GENERAL_POLICY_ERROR("POL0001", "A policy error occurred."),
		POL_USER_NOT_PROVISIONED("POL1009", "User has not been provisioned for service"),
		POL_USER_SUSPENDED("POL1010", "User has been suspended from service"),
		POL_DETAILED_POLICY_ERROR("POL2000", "The following policy error occurred: %1. Error code is %2."),
		POL_MSG_SIZE_EXCEEDS_LIMIT("POL9003", "Message content size exceeds the allowable limit")
	

		private final String msgId
		private final String msgTxt

		private Error(String msgId, String msgTxt) {
			this.msgId = msgId
			this.msgTxt = msgTxt
		}

		public String getMsgId() {
		     return msgId
		  }

		public String getMsgTxt() {
			return msgTxt
		}

	}


	
	
	String mapAdapterExecptionToCommonException(String response, Execution execution)
	{
		def utils=new MsoUtils()
		def method = getClass().getSimpleName() + '.mapAdapterExecptionToCommonException(' +
			'execution=' + execution.getId() +
			')'

		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)

		
		def errorCode

		
		try {
			  errorCode = MapCategoryToErrorCode(utils.getNodeText(response, "category")) 
			  execution.setVariable(prefix+"err",errorCode)
			  String message = buildException(response, execution)
			  utils.log("DEBUG","=========== End MapAdapterExecptionToWorkflowException ===========",isDebugLogEnabled)
			  return message
		}catch (Exception ex) {
			//Ignore the exception - cases include non xml payload
			utils.log("DEBUG","error mapping error, ignoring: " + ex,isDebugLogEnabled)
			utils.log("DEBUG","=========== End MapAdapterExecptionToWorkflowException ===========",isDebugLogEnabled)
			return buildException(response, execution)
		} 
	}
	
	/**
	 * @param response
	 * @param execution
	 * @return mapped exception
	 */
	String mapAOTSExecptionToCommonException(String response, Execution execution)
	{
		def utils=new MsoUtils()

		def prefix=execution.getVariable("prefix")
		def method = getClass().getSimpleName() + '.mapAOTSExecptionToCommonException(' +
			'execution=' + execution.getId() +
			')'

		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)
		
		
		try {
			  def errorCode = utils.getNodeText(response,"code")
			  def descr = utils.getNodeText(response, "description")
			  def mappedErr = mapErrorCodetoError(errorCode, descr)
			  if(mappedErr == Error.SVC_DETAILED_SERVICE_ERROR || mappedError == Error.POL_DETAILED_POLICY_ERROR){
				  ArrayList myVars = new ArrayList()
				  myVars.add(descr)
				  myVars.add(errorCode)
				  execution.setVariable(prefix+"errVariables", myVars)
			  }
			  execution.setVariable(prefix+"err",mappedErr)
			  def message = buildException("Received error from AOTS: " + descr, execution)
			  utils.log("DEBUG","=========== End MapAOTSExecptionToCommonException ===========",isDebugLogEnabled)
			  return message
		}catch (Exception ex) {
			//Ignore the exception - cases include non xml payload
			utils.log("DEBUG","error mapping error, ignoring: " + ex,isDebugLogEnabled)
			utils.log("DEBUG","=========== End MapAOTSExecptionToCommonException ===========",isDebugLogEnabled)
			return buildException(response, execution)
		}
	}
	
	String mapSDNCAdapterExceptionToErrorResponse(String sdncAdapterCallbackRequest, Execution execution) {
		def utils=new MsoUtils()
		def prefix=execution.getVariable("prefix")
		def method = getClass().getSimpleName() + '.mapSDNCAdapterExceptionToErrorResponse(' +
			'execution=' + execution.getId() +
			')'

		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)
		
		def sdncResponseCode
		String responseCode = execution.getVariable(prefix+"ResponseCode")
		utils.log("DEBUG",'responseCode to map: ' + responseCode, isDebugLogEnabled)
		def errorMessage
		
		try {
			
			if(utils.nodeExists(sdncAdapterCallbackRequest, "RequestData")) {
				def reqDataXml = StringEscapeUtils.unescapeXml(utils.getNodeXml(sdncAdapterCallbackRequest, "RequestData"))
				errorMessage = utils.getNodeText(reqDataXml, "response-message")
				sdncResponseCode = utils.getNodeText(reqDataXml, "response-code")
			}else{
				errorMessage = utils.getNodeText(sdncAdapterCallbackRequest, "ResponseMessage")
				sdncResponseCode = responseCode
		    }
			def mappedErr = mapErrorCodetoError(responseCode, errorMessage)
			errorMessage = errorMessage.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
			def modifiedErrorMessage = "Received error from SDN-C: " + errorMessage
			if(mappedErr == Error.SVC_DETAILED_SERVICE_ERROR || mappedErr == Error.POL_DETAILED_POLICY_ERROR){
				ArrayList myVars = new ArrayList()
				myVars.add(errorMessage)
				myVars.add(sdncResponseCode)
				execution.setVariable(prefix+"errVariables", myVars)
			}
			execution.setVariable(prefix+"err",mappedErr)
			def message = buildException(modifiedErrorMessage, execution)

			
			utils.log("DEBUG","=========== End MapSDNCAdapterException ===========",isDebugLogEnabled)
		    return message
		}catch (Exception ex) {
			//Ignore the exception - cases include non xml payload
			utils.log("DEBUG","error mapping sdnc error, ignoring: " + ex,isDebugLogEnabled)
			utils.log("DEBUG","=========== End MapSDNCAdapterException ===========",isDebugLogEnabled)
			return null
		} 
		
	}
	
	/**
	 * @param response message from called component (ex: AAI)
	 * @param execution
	 * @return an error response conforming to the common 
	 */
	String mapAAIExceptionTCommonException(String response, Execution execution)
	{
		def utils=new MsoUtils()
		def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
		def prefix=execution.getVariable("prefix")
		def method = getClass().getSimpleName() + '.mapAAIExceptionTCommonException(' +
			'execution=' + execution.getId() +
			')'

		utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)
		def variables
		def message
		String errorCode = 'SVC0001'
		utils.log("DEBUG","response: " + response, isDebugLogEnabled)
		//they use the same format we do, pass their error along
		//TODO add Received error from A&AI at beg of text
		try {
			 message = utils.getNodeXml(response, "requestError")
			 message = utils.removeXmlNamespaces(message)
		} catch (Exception ex) {
			//Ignore the exception - cases include non xml payload
				message = buildException("Received error from A&AI, unable to parse",execution)
			utils.log("DEBUG","error mapping error, ignoring: " + ex,isDebugLogEnabled)
		}
		
		if(message != null) { 
			 execution.setVariable(prefix+"ErrorResponse",message)
			 utils.log("ERROR","Fault:"+ execution.getVariable(prefix+"ErrorResponse"))
			 return message
		} else {
			
				return null
			
		}
	}
	
	/**
	 * @param execution
	 * @return an error response conforming to the common API with default text msg
	 */
	String buildException(execution){
		return buildException(null, execution)
	}
	
	/**
	 * @param response message from called component (ex: AAI)
	 * @param execution
	 * @return an error response conforming to the common
	 */
	String buildException(response, execution){
		def utils=new MsoUtils()
		def method = getClass().getSimpleName() + '.buildException(' +
			'execution=' + execution.getId() +
			')'

		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)
		def prefix=execution.getVariable("prefix")
		def responseCode = String.valueOf(execution.getVariable(prefix+"ResponseCode"))
		def variables
		utils.log("DEBUG","response: " + response, isDebugLogEnabled)
		
			try {
				utils.log("DEBUG","formatting error message" ,isDebugLogEnabled)
				def msgVars = execution.getVariable(prefix+"errVariables")
				def myErr = execution.getVariable(prefix+"err")
				def messageTxt = execution.getVariable(prefix+"errTxt")
				def messageId = null
				
				if(myErr == null){
					utils.log("DEBUG","mapping response code: " + responseCode, isDebugLogEnabled)
					myErr = mapErrorCodetoError(responseCode, response)
					if(myErr == null){
						//not a service or policy error, just return error code
						return ""
					}
				}
				messageId = myErr.getMsgId()
				
				if(messageTxt == null){
					if(myErr!=null){
						messageTxt = myErr.getMsgTxt()
					}else{
						messageTxt = response
					}
				}
				
				if(msgVars==null && (myErr == Error.SVC_DETAILED_SERVICE_ERROR || myErr == Error.POL_DETAILED_POLICY_ERROR)){
					msgVars = new ArrayList()
					msgVars.add(response)
					msgVars.add(responseCode)
				}
				
				def msgVarsXML=""
				StringBuffer msgVarsBuff = new StringBuffer()
				if(msgVars!=null){
					for(String msgVar : msgVars){
						msgVarsBuff.append(
							"""
			<tns:variables>${msgVar}</tns:variables>""")
					}
					
				}
				def message = ""
				if(messageId.startsWith("SVC")){
					message = """<tns:requestError xmlns:tns="http://org.openecomp/mso/request/types/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://org.openecomp/mso/request/types/v1 MsoServiceInstanceTypesV1.xsd">
	<tns:serviceException>
		<tns:messageId>${messageId}</tns:messageId>
		<tns:text>${messageTxt}</tns:text>${msgVarsBuff}
	</tns:serviceException>
</tns:requestError>""" 
				}else{
					message ="""<tns:requestError xmlns:tns="http://org.openecomp/mso/request/types/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://org.openecomp/mso/request/types/v1 MsoServiceInstanceTypesV1.xsd">
	<tns:policyException>
		<tns:messageId>${messageId}</tns:messageId>
		<tns:text>${messageTxt}</tns:text>${msgVarsBuff}
	</tns:policyException>
</tns:requestError>""" 
				}
				 utils.log("DEBUG", "message " + message, isDebugLogEnabled)
				 execution.setVariable(prefix+"ErrorResponse",message)
				 execution.setVariable(prefix+"err", myErr)
				 execution.setVariable(prefix+"errTxt", messageTxt)
				 execution.setVariable(prefix+"errVariables", msgVars)
				 utils.log("ERROR","Fault:"+ execution.getVariable(prefix+"ErrorResponse"))
				 return message
			}catch(Exception ex) {
				utils.log("DEBUG","error mapping error, return null: " + ex,isDebugLogEnabled)
				return null
			}

	}
	
	String parseError(Execution execution){
		def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
		def utils=new MsoUtils()
		def prefix=execution.getVariable("prefix")
		def text = execution.getVariable(prefix+"errTxt")
		def msgVars = execution.getVariable(prefix+"errVariables")
		utils.log("DEBUG",'parsing message: ' + text, isDebugLogEnabled)
		if(text == null){
			return 'failed'
		}
		if(msgVars!=null && !msgVars.isEmpty()){
			for(int i=0; i<msgVars.size(); i++){
				text = text.replaceFirst("%"+(i+1), msgVars[i])
			}
		}
		utils.log("DEBUG",'parsed message is: ' + text, isDebugLogEnabled)
		return text
	}
	
	

	Error mapErrorCodetoError(responseCode, descr)
	{
		
		if(responseCode==null || responseCode=='0' || responseCode=='500' || responseCode =='408'){
			return Error.SVC_NO_SERVER_RESOURCES
		}else if(responseCode == '401' || responseCode == '405' || responseCode == '409' || responseCode == '503'){
			return null
		}else if(responseCode == '400'){
			if(descr==null){
				return Error.SVC_GENERAL_SERVICE_ERROR
			}else{
				return Error.SVC_DETAILED_SERVICE_ERROR
			}
		}else if(responseCode == '401'){
			if(descr==null){
				return Error.POL_GENERAL_POLICY_ERROR
			}else{
				return Error.POL_DETAILED_POLICY_ERROR
			}
		}else{
			return Error.SVC_NO_SERVER_RESOURCES
		}
	}
	
	String mapCategoryToErrorCode(String errorCategory)
	{
		if(errorCategory.equals('OPENSTACK'))
			return Error.SVC_NO_SERVER_RESOURCES
		else if (errorCategory.equals('IO'))
			return Error.SVC_NO_SERVER_RESOURCES
		else if (errorCategory.equals('INTERNAL'))
			return Error.SVC_NO_SERVER_RESOURCES
		else if (errorCategory.equals('USERDATA'))
			return Error.SVC_GENERAL_SERVICE_ERROR
		else
			return Error.SVC_GENERAL_SERVICE_ERROR
	}
	
	
	

	
	
}