2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.common.scripts
23 import org.camunda.bpm.engine.delegate.DelegateExecution
24 import org.apache.commons.lang3.*
25 import org.onap.so.logger.MessageEnum
26 import org.onap.so.logger.MsoLogger
30 class TrinityExceptionUtil {
31 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, TrinityExceptionUtil.class);
36 public static enum Error {
37 SVC_GENERAL_SERVICE_ERROR("SVC0001","Internal Error"),
38 SVC_BAD_PARAMETER("SVC0002", "Invalid input value for message part %1"),
39 SVC_NO_SERVER_RESOURCES("SVC1000", "No server resources available to process the request"),
40 SVC_DETAILED_SERVICE_ERROR("SVC2000", "The following service error occurred: %1. Error code is %2."),
41 POL_GENERAL_POLICY_ERROR("POL0001", "A policy error occurred."),
42 POL_USER_NOT_PROVISIONED("POL1009", "User has not been provisioned for service"),
43 POL_USER_SUSPENDED("POL1010", "User has been suspended from service"),
44 POL_DETAILED_POLICY_ERROR("POL2000", "The following policy error occurred: %1. Error code is %2."),
45 POL_MSG_SIZE_EXCEEDS_LIMIT("POL9003", "Message content size exceeds the allowable limit")
48 private final String msgId
49 private final String msgTxt
51 private Error(String msgId, String msgTxt) {
56 public String getMsgId() {
60 public String getMsgTxt() {
69 String mapAdapterExecptionToCommonException(String response, DelegateExecution execution)
71 def utils=new MsoUtils()
72 def method = getClass().getSimpleName() + '.mapAdapterExecptionToCommonException(' +
73 'execution=' + execution.getId() +
76 msoLogger.trace('Entered ' + method)
83 errorCode = MapCategoryToErrorCode(utils.getNodeText(response, "category"))
84 execution.setVariable(prefix+"err",errorCode)
85 String message = buildException(response, execution)
86 msoLogger.trace("End MapAdapterExecptionToWorkflowException ")
88 }catch (Exception ex) {
89 //Ignore the exception - cases include non xml payload
90 msoLogger.debug("error mapping error, ignoring: " + ex)
91 msoLogger.trace("End MapAdapterExecptionToWorkflowException ")
92 return buildException(response, execution)
99 * @return mapped exception
101 String mapAOTSExecptionToCommonException(String response, DelegateExecution execution)
103 def utils=new MsoUtils()
105 def prefix=execution.getVariable("prefix")
106 def method = getClass().getSimpleName() + '.mapAOTSExecptionToCommonException(' +
107 'execution=' + execution.getId() +
110 msoLogger.trace('Entered ' + method)
114 def errorCode = utils.getNodeText(response,"code")
115 def descr = utils.getNodeText(response, "description")
116 def mappedErr = mapErrorCodetoError(errorCode, descr)
117 if(mappedErr == Error.SVC_DETAILED_SERVICE_ERROR || mappedError == Error.POL_DETAILED_POLICY_ERROR){
118 ArrayList myVars = new ArrayList()
120 myVars.add(errorCode)
121 execution.setVariable(prefix+"errVariables", myVars)
123 execution.setVariable(prefix+"err",mappedErr)
124 def message = buildException("Received error from AOTS: " + descr, execution)
125 msoLogger.trace("End MapAOTSExecptionToCommonException ")
127 }catch (Exception ex) {
128 //Ignore the exception - cases include non xml payload
129 msoLogger.debug("error mapping error, ignoring: " + ex)
130 msoLogger.trace("End MapAOTSExecptionToCommonException ")
131 return buildException(response, execution)
135 String mapSDNCAdapterExceptionToErrorResponse(String sdncAdapterCallbackRequest, DelegateExecution execution) {
136 def utils=new MsoUtils()
137 def prefix=execution.getVariable("prefix")
138 def method = getClass().getSimpleName() + '.mapSDNCAdapterExceptionToErrorResponse(' +
139 'execution=' + execution.getId() +
142 msoLogger.trace('Entered ' + method)
145 String responseCode = execution.getVariable(prefix+"ResponseCode")
146 msoLogger.debug('responseCode to map: ' + responseCode)
151 if(utils.nodeExists(sdncAdapterCallbackRequest, "RequestData")) {
152 def reqDataXml = utils.getNodeXml(sdncAdapterCallbackRequest, "RequestData")
153 errorMessage = utils.getNodeText(reqDataXml, "response-message")
154 sdncResponseCode = utils.getNodeText(reqDataXml, "response-code")
156 errorMessage = utils.getNodeText(sdncAdapterCallbackRequest, "ResponseMessage")
157 sdncResponseCode = responseCode
159 def mappedErr = mapErrorCodetoError(responseCode, errorMessage)
160 errorMessage = errorMessage
161 def modifiedErrorMessage = "Received error from SDN-C: " + errorMessage
162 if(mappedErr == Error.SVC_DETAILED_SERVICE_ERROR || mappedErr == Error.POL_DETAILED_POLICY_ERROR){
163 ArrayList myVars = new ArrayList()
164 myVars.add(errorMessage)
165 myVars.add(sdncResponseCode)
166 execution.setVariable(prefix+"errVariables", myVars)
168 execution.setVariable(prefix+"err",mappedErr)
169 def message = buildException(modifiedErrorMessage, execution)
172 msoLogger.trace("End MapSDNCAdapterException ")
174 }catch (Exception ex) {
175 //Ignore the exception - cases include non xml payload
176 msoLogger.debug("error mapping sdnc error, ignoring: " + ex)
177 msoLogger.trace("End MapSDNCAdapterException ")
184 * @param response message from called component (ex: AAI)
186 * @return an error response conforming to the common
188 String mapAAIExceptionTCommonException(String response, DelegateExecution execution)
190 def utils=new MsoUtils()
191 def prefix=execution.getVariable("prefix")
192 def method = getClass().getSimpleName() + '.mapAAIExceptionTCommonException(' +
193 'execution=' + execution.getId() +
196 msoLogger.trace('Entered ' + method)
199 String errorCode = 'SVC0001'
200 msoLogger.debug("response: " + response)
201 //they use the same format we do, pass their error along
202 //TODO add Received error from A&AI at beg of text
204 message = utils.getNodeXml(response, "requestError")
205 message = utils.removeXmlNamespaces(message)
206 } catch (Exception ex) {
207 //Ignore the exception - cases include non xml payload
208 message = buildException("Received error from A&AI, unable to parse",execution)
209 msoLogger.debug("error mapping error, ignoring: " + ex)
212 if(message != null) {
213 execution.setVariable(prefix+"ErrorResponse",message)
214 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Fault", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, execution.getVariable(prefix+"ErrorResponse"));
225 * @return an error response conforming to the common API with default text msg
227 String buildException(execution){
228 return buildException(null, execution)
232 * @param response message from called component (ex: AAI)
234 * @return an error response conforming to the common
236 String buildException(response, execution){
237 def utils=new MsoUtils()
238 def method = getClass().getSimpleName() + '.buildException(' +
239 'execution=' + execution.getId() +
242 msoLogger.trace('Entered ' + method)
243 def prefix=execution.getVariable("prefix")
244 def responseCode = String.valueOf(execution.getVariable(prefix+"ResponseCode"))
246 msoLogger.debug("response: " + response)
249 msoLogger.debug("formatting error message" )
250 def msgVars = execution.getVariable(prefix+"errVariables")
251 def myErr = execution.getVariable(prefix+"err")
252 def messageTxt = execution.getVariable(prefix+"errTxt")
256 msoLogger.debug("mapping response code: " + responseCode)
257 myErr = mapErrorCodetoError(responseCode, response)
259 //not a service or policy error, just return error code
263 messageId = myErr.getMsgId()
265 if(messageTxt == null){
267 messageTxt = myErr.getMsgTxt()
269 messageTxt = response
273 if(msgVars==null && (myErr == Error.SVC_DETAILED_SERVICE_ERROR || myErr == Error.POL_DETAILED_POLICY_ERROR)){
274 msgVars = new ArrayList()
275 msgVars.add(response)
276 msgVars.add(responseCode)
280 StringBuffer msgVarsBuff = new StringBuffer()
282 for(String msgVar : msgVars){
285 <tns:variables>${MsoUtils.xmlEscape(msgVar)}</tns:variables>""")
290 if(messageId.startsWith("SVC")){
291 message = """<tns:requestError xmlns:tns="http://org.onap/so/request/types/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://org.onap/so/request/types/v1 MsoServiceInstanceTypesV1.xsd">
292 <tns:serviceException>
293 <tns:messageId>${MsoUtils.xmlEscape(messageId)}</tns:messageId>
294 <tns:text>${MsoUtils.xmlEscape(messageTxt)}</tns:text>${msgVarsBuff}
295 </tns:serviceException>
296 </tns:requestError>"""
298 message ="""<tns:requestError xmlns:tns="http://org.onap/so/request/types/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://org.onap/so/request/types/v1 MsoServiceInstanceTypesV1.xsd">
299 <tns:policyException>
300 <tns:messageId>${MsoUtils.xmlEscape(messageId)}</tns:messageId>
301 <tns:text>${MsoUtils.xmlEscape(messageTxt)}</tns:text>${msgVarsBuff}
302 </tns:policyException>
303 </tns:requestError>"""
305 msoLogger.debug("message " + message)
306 execution.setVariable(prefix+"ErrorResponse",message)
307 execution.setVariable(prefix+"err", myErr)
308 execution.setVariable(prefix+"errTxt", messageTxt)
309 execution.setVariable(prefix+"errVariables", msgVars)
310 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Fault", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, execution.getVariable(prefix+"ErrorResponse"));
312 }catch(Exception ex) {
313 msoLogger.debug("error mapping error, return null: " + ex)
319 String parseError(DelegateExecution execution){
320 def utils=new MsoUtils()
321 def prefix=execution.getVariable("prefix")
322 def text = execution.getVariable(prefix+"errTxt")
323 def msgVars = execution.getVariable(prefix+"errVariables")
324 msoLogger.debug('parsing message: ' + text)
328 if(msgVars!=null && !msgVars.isEmpty()){
329 for(int i=0; i<msgVars.size(); i++){
330 text = text.replaceFirst("%"+(i+1), msgVars[i])
333 msoLogger.debug('parsed message is: ' + text)
339 Error mapErrorCodetoError(responseCode, descr)
342 if(responseCode==null || responseCode=='0' || responseCode=='500' || responseCode =='408'){
343 return Error.SVC_NO_SERVER_RESOURCES
344 }else if(responseCode == '401' || responseCode == '405' || responseCode == '409' || responseCode == '503'){
346 }else if(responseCode == '400'){
348 return Error.SVC_GENERAL_SERVICE_ERROR
350 return Error.SVC_DETAILED_SERVICE_ERROR
352 }else if(responseCode == '401'){
354 return Error.POL_GENERAL_POLICY_ERROR
356 return Error.POL_DETAILED_POLICY_ERROR
359 return Error.SVC_NO_SERVER_RESOURCES
363 String mapCategoryToErrorCode(String errorCategory)
365 if(errorCategory.equals('OPENSTACK'))
366 return Error.SVC_NO_SERVER_RESOURCES
367 else if (errorCategory.equals('IO'))
368 return Error.SVC_NO_SERVER_RESOURCES
369 else if (errorCategory.equals('INTERNAL'))
370 return Error.SVC_NO_SERVER_RESOURCES
371 else if (errorCategory.equals('USERDATA'))
372 return Error.SVC_GENERAL_SERVICE_ERROR
374 return Error.SVC_GENERAL_SERVICE_ERROR