2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.mso.bpmn.common.scripts
\r
23 import org.camunda.bpm.engine.runtime.Execution
\r
24 import org.apache.commons.lang3.*
\r
26 class TrinityExceptionUtil {
\r
30 public static enum Error {
\r
31 SVC_GENERAL_SERVICE_ERROR("SVC0001","Internal Error"),
\r
32 SVC_BAD_PARAMETER("SVC0002", "Invalid input value for message part %1"),
\r
33 SVC_NO_SERVER_RESOURCES("SVC1000", "No server resources available to process the request"),
\r
34 SVC_DETAILED_SERVICE_ERROR("SVC2000", "The following service error occurred: %1. Error code is %2."),
\r
35 POL_GENERAL_POLICY_ERROR("POL0001", "A policy error occurred."),
\r
36 POL_USER_NOT_PROVISIONED("POL1009", "User has not been provisioned for service"),
\r
37 POL_USER_SUSPENDED("POL1010", "User has been suspended from service"),
\r
38 POL_DETAILED_POLICY_ERROR("POL2000", "The following policy error occurred: %1. Error code is %2."),
\r
39 POL_MSG_SIZE_EXCEEDS_LIMIT("POL9003", "Message content size exceeds the allowable limit")
\r
42 private final String msgId
\r
43 private final String msgTxt
\r
45 private Error(String msgId, String msgTxt) {
\r
47 this.msgTxt = msgTxt
\r
50 public String getMsgId() {
\r
54 public String getMsgTxt() {
\r
63 String mapAdapterExecptionToCommonException(String response, Execution execution)
\r
65 def utils=new MsoUtils()
\r
66 def method = getClass().getSimpleName() + '.mapAdapterExecptionToCommonException(' +
\r
67 'execution=' + execution.getId() +
\r
70 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
71 utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)
\r
78 errorCode = MapCategoryToErrorCode(utils.getNodeText(response, "category"))
\r
79 execution.setVariable(prefix+"err",errorCode)
\r
80 String message = buildException(response, execution)
\r
81 utils.log("DEBUG","=========== End MapAdapterExecptionToWorkflowException ===========",isDebugLogEnabled)
\r
83 }catch (Exception ex) {
\r
84 //Ignore the exception - cases include non xml payload
\r
85 utils.log("DEBUG","error mapping error, ignoring: " + ex,isDebugLogEnabled)
\r
86 utils.log("DEBUG","=========== End MapAdapterExecptionToWorkflowException ===========",isDebugLogEnabled)
\r
87 return buildException(response, execution)
\r
94 * @return mapped exception
\r
96 String mapAOTSExecptionToCommonException(String response, Execution execution)
\r
98 def utils=new MsoUtils()
\r
100 def prefix=execution.getVariable("prefix")
\r
101 def method = getClass().getSimpleName() + '.mapAOTSExecptionToCommonException(' +
\r
102 'execution=' + execution.getId() +
\r
105 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
106 utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)
\r
110 def errorCode = utils.getNodeText(response,"code")
\r
111 def descr = utils.getNodeText(response, "description")
\r
112 def mappedErr = mapErrorCodetoError(errorCode, descr)
\r
113 if(mappedErr == Error.SVC_DETAILED_SERVICE_ERROR || mappedError == Error.POL_DETAILED_POLICY_ERROR){
\r
114 ArrayList myVars = new ArrayList()
\r
116 myVars.add(errorCode)
\r
117 execution.setVariable(prefix+"errVariables", myVars)
\r
119 execution.setVariable(prefix+"err",mappedErr)
\r
120 def message = buildException("Received error from AOTS: " + descr, execution)
\r
121 utils.log("DEBUG","=========== End MapAOTSExecptionToCommonException ===========",isDebugLogEnabled)
\r
123 }catch (Exception ex) {
\r
124 //Ignore the exception - cases include non xml payload
\r
125 utils.log("DEBUG","error mapping error, ignoring: " + ex,isDebugLogEnabled)
\r
126 utils.log("DEBUG","=========== End MapAOTSExecptionToCommonException ===========",isDebugLogEnabled)
\r
127 return buildException(response, execution)
\r
131 String mapSDNCAdapterExceptionToErrorResponse(String sdncAdapterCallbackRequest, Execution execution) {
\r
132 def utils=new MsoUtils()
\r
133 def prefix=execution.getVariable("prefix")
\r
134 def method = getClass().getSimpleName() + '.mapSDNCAdapterExceptionToErrorResponse(' +
\r
135 'execution=' + execution.getId() +
\r
138 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
139 utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)
\r
141 def sdncResponseCode
\r
142 String responseCode = execution.getVariable(prefix+"ResponseCode")
\r
143 utils.log("DEBUG",'responseCode to map: ' + responseCode, isDebugLogEnabled)
\r
148 if(utils.nodeExists(sdncAdapterCallbackRequest, "RequestData")) {
\r
149 def reqDataXml = StringEscapeUtils.unescapeXml(utils.getNodeXml(sdncAdapterCallbackRequest, "RequestData"))
\r
150 errorMessage = utils.getNodeText(reqDataXml, "response-message")
\r
151 sdncResponseCode = utils.getNodeText(reqDataXml, "response-code")
\r
153 errorMessage = utils.getNodeText(sdncAdapterCallbackRequest, "ResponseMessage")
\r
154 sdncResponseCode = responseCode
\r
156 def mappedErr = mapErrorCodetoError(responseCode, errorMessage)
\r
157 errorMessage = errorMessage.replace("&", "&").replace("<", "<").replace(">", ">")
\r
158 def modifiedErrorMessage = "Received error from SDN-C: " + errorMessage
\r
159 if(mappedErr == Error.SVC_DETAILED_SERVICE_ERROR || mappedErr == Error.POL_DETAILED_POLICY_ERROR){
\r
160 ArrayList myVars = new ArrayList()
\r
161 myVars.add(errorMessage)
\r
162 myVars.add(sdncResponseCode)
\r
163 execution.setVariable(prefix+"errVariables", myVars)
\r
165 execution.setVariable(prefix+"err",mappedErr)
\r
166 def message = buildException(modifiedErrorMessage, execution)
\r
169 utils.log("DEBUG","=========== End MapSDNCAdapterException ===========",isDebugLogEnabled)
\r
171 }catch (Exception ex) {
\r
172 //Ignore the exception - cases include non xml payload
\r
173 utils.log("DEBUG","error mapping sdnc error, ignoring: " + ex,isDebugLogEnabled)
\r
174 utils.log("DEBUG","=========== End MapSDNCAdapterException ===========",isDebugLogEnabled)
\r
181 * @param response message from called component (ex: AAI)
\r
183 * @return an error response conforming to the common
\r
185 String mapAAIExceptionTCommonException(String response, Execution execution)
\r
187 def utils=new MsoUtils()
\r
188 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")
\r
189 def prefix=execution.getVariable("prefix")
\r
190 def method = getClass().getSimpleName() + '.mapAAIExceptionTCommonException(' +
\r
191 'execution=' + execution.getId() +
\r
194 utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)
\r
197 String errorCode = 'SVC0001'
\r
198 utils.log("DEBUG","response: " + response, isDebugLogEnabled)
\r
199 //they use the same format we do, pass their error along
\r
200 //TODO add Received error from A&AI at beg of text
\r
202 message = utils.getNodeXml(response, "requestError")
\r
203 message = utils.removeXmlNamespaces(message)
\r
204 } catch (Exception ex) {
\r
205 //Ignore the exception - cases include non xml payload
\r
206 message = buildException("Received error from A&AI, unable to parse",execution)
\r
207 utils.log("DEBUG","error mapping error, ignoring: " + ex,isDebugLogEnabled)
\r
210 if(message != null) {
\r
211 execution.setVariable(prefix+"ErrorResponse",message)
\r
212 utils.log("ERROR","Fault:"+ execution.getVariable(prefix+"ErrorResponse"))
\r
223 * @return an error response conforming to the common API with default text msg
\r
225 String buildException(execution){
\r
226 return buildException(null, execution)
\r
230 * @param response message from called component (ex: AAI)
\r
232 * @return an error response conforming to the common
\r
234 String buildException(response, execution){
\r
235 def utils=new MsoUtils()
\r
236 def method = getClass().getSimpleName() + '.buildException(' +
\r
237 'execution=' + execution.getId() +
\r
240 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
241 utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)
\r
242 def prefix=execution.getVariable("prefix")
\r
243 def responseCode = String.valueOf(execution.getVariable(prefix+"ResponseCode"))
\r
245 utils.log("DEBUG","response: " + response, isDebugLogEnabled)
\r
248 utils.log("DEBUG","formatting error message" ,isDebugLogEnabled)
\r
249 def msgVars = execution.getVariable(prefix+"errVariables")
\r
250 def myErr = execution.getVariable(prefix+"err")
\r
251 def messageTxt = execution.getVariable(prefix+"errTxt")
\r
252 def messageId = null
\r
255 utils.log("DEBUG","mapping response code: " + responseCode, isDebugLogEnabled)
\r
256 myErr = mapErrorCodetoError(responseCode, response)
\r
258 //not a service or policy error, just return error code
\r
262 messageId = myErr.getMsgId()
\r
264 if(messageTxt == null){
\r
266 messageTxt = myErr.getMsgTxt()
\r
268 messageTxt = response
\r
272 if(msgVars==null && (myErr == Error.SVC_DETAILED_SERVICE_ERROR || myErr == Error.POL_DETAILED_POLICY_ERROR)){
\r
273 msgVars = new ArrayList()
\r
274 msgVars.add(response)
\r
275 msgVars.add(responseCode)
\r
279 StringBuffer msgVarsBuff = new StringBuffer()
\r
281 for(String msgVar : msgVars){
\r
282 msgVarsBuff.append(
\r
284 <tns:variables>${msgVar}</tns:variables>""")
\r
289 if(messageId.startsWith("SVC")){
\r
290 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">
\r
291 <tns:serviceException>
\r
292 <tns:messageId>${messageId}</tns:messageId>
\r
293 <tns:text>${messageTxt}</tns:text>${msgVarsBuff}
\r
294 </tns:serviceException>
\r
295 </tns:requestError>"""
\r
297 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">
\r
298 <tns:policyException>
\r
299 <tns:messageId>${messageId}</tns:messageId>
\r
300 <tns:text>${messageTxt}</tns:text>${msgVarsBuff}
\r
301 </tns:policyException>
\r
302 </tns:requestError>"""
\r
304 utils.log("DEBUG", "message " + message, isDebugLogEnabled)
\r
305 execution.setVariable(prefix+"ErrorResponse",message)
\r
306 execution.setVariable(prefix+"err", myErr)
\r
307 execution.setVariable(prefix+"errTxt", messageTxt)
\r
308 execution.setVariable(prefix+"errVariables", msgVars)
\r
309 utils.log("ERROR","Fault:"+ execution.getVariable(prefix+"ErrorResponse"))
\r
311 }catch(Exception ex) {
\r
312 utils.log("DEBUG","error mapping error, return null: " + ex,isDebugLogEnabled)
\r
318 String parseError(Execution execution){
\r
319 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
\r
320 def utils=new MsoUtils()
\r
321 def prefix=execution.getVariable("prefix")
\r
322 def text = execution.getVariable(prefix+"errTxt")
\r
323 def msgVars = execution.getVariable(prefix+"errVariables")
\r
324 utils.log("DEBUG",'parsing message: ' + text, isDebugLogEnabled)
\r
328 if(msgVars!=null && !msgVars.isEmpty()){
\r
329 for(int i=0; i<msgVars.size(); i++){
\r
330 text = text.replaceFirst("%"+(i+1), msgVars[i])
\r
333 utils.log("DEBUG",'parsed message is: ' + text, isDebugLogEnabled)
\r
339 Error mapErrorCodetoError(responseCode, descr)
\r
342 if(responseCode==null || responseCode=='0' || responseCode=='500' || responseCode =='408'){
\r
343 return Error.SVC_NO_SERVER_RESOURCES
\r
344 }else if(responseCode == '401' || responseCode == '405' || responseCode == '409' || responseCode == '503'){
\r
346 }else if(responseCode == '400'){
\r
348 return Error.SVC_GENERAL_SERVICE_ERROR
\r
350 return Error.SVC_DETAILED_SERVICE_ERROR
\r
352 }else if(responseCode == '401'){
\r
354 return Error.POL_GENERAL_POLICY_ERROR
\r
356 return Error.POL_DETAILED_POLICY_ERROR
\r
359 return Error.SVC_NO_SERVER_RESOURCES
\r
363 String mapCategoryToErrorCode(String errorCategory)
\r
365 if(errorCategory.equals('OPENSTACK'))
\r
366 return Error.SVC_NO_SERVER_RESOURCES
\r
367 else if (errorCategory.equals('IO'))
\r
368 return Error.SVC_NO_SERVER_RESOURCES
\r
369 else if (errorCategory.equals('INTERNAL'))
\r
370 return Error.SVC_NO_SERVER_RESOURCES
\r
371 else if (errorCategory.equals('USERDATA'))
\r
372 return Error.SVC_GENERAL_SERVICE_ERROR
\r
374 return Error.SVC_GENERAL_SERVICE_ERROR
\r