Groovy scripts header correction
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / TrinityExceptionUtil.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\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
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \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
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.common.scripts\r
22 \r
23 import org.camunda.bpm.engine.runtime.Execution\r
24 import org.apache.commons.lang3.*\r
25 \r
26 class TrinityExceptionUtil {\r
27         \r
28         \r
29         \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
40         \r
41 \r
42                 private final String msgId\r
43                 private final String msgTxt\r
44 \r
45                 private Error(String msgId, String msgTxt) {\r
46                         this.msgId = msgId\r
47                         this.msgTxt = msgTxt\r
48                 }\r
49 \r
50                 public String getMsgId() {\r
51                      return msgId\r
52                   }\r
53 \r
54                 public String getMsgTxt() {\r
55                         return msgTxt\r
56                 }\r
57 \r
58         }\r
59 \r
60 \r
61         \r
62         \r
63         String mapAdapterExecptionToCommonException(String response, Execution execution)\r
64         {\r
65                 def utils=new MsoUtils()\r
66                 def method = getClass().getSimpleName() + '.mapAdapterExecptionToCommonException(' +\r
67                         'execution=' + execution.getId() +\r
68                         ')'\r
69 \r
70                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
71                 utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)\r
72 \r
73                 \r
74                 def errorCode\r
75 \r
76                 \r
77                 try {\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
82                           return message\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
88                 } \r
89         }\r
90         \r
91         /**\r
92          * @param response\r
93          * @param execution\r
94          * @return mapped exception\r
95          */\r
96         String mapAOTSExecptionToCommonException(String response, Execution execution)\r
97         {\r
98                 def utils=new MsoUtils()\r
99 \r
100                 def prefix=execution.getVariable("prefix")\r
101                 def method = getClass().getSimpleName() + '.mapAOTSExecptionToCommonException(' +\r
102                         'execution=' + execution.getId() +\r
103                         ')'\r
104 \r
105                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
106                 utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)\r
107                 \r
108                 \r
109                 try {\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
115                                   myVars.add(descr)\r
116                                   myVars.add(errorCode)\r
117                                   execution.setVariable(prefix+"errVariables", myVars)\r
118                           }\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
122                           return message\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
128                 }\r
129         }\r
130         \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
136                         ')'\r
137 \r
138                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
139                 utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)\r
140                 \r
141                 def sdncResponseCode\r
142                 String responseCode = execution.getVariable(prefix+"ResponseCode")\r
143                 utils.log("DEBUG",'responseCode to map: ' + responseCode, isDebugLogEnabled)\r
144                 def errorMessage\r
145                 \r
146                 try {\r
147                         \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
152                         }else{\r
153                                 errorMessage = utils.getNodeText(sdncAdapterCallbackRequest, "ResponseMessage")\r
154                                 sdncResponseCode = responseCode\r
155                     }\r
156                         def mappedErr = mapErrorCodetoError(responseCode, errorMessage)\r
157                         errorMessage = errorMessage.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")\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
164                         }\r
165                         execution.setVariable(prefix+"err",mappedErr)\r
166                         def message = buildException(modifiedErrorMessage, execution)\r
167 \r
168                         \r
169                         utils.log("DEBUG","=========== End MapSDNCAdapterException ===========",isDebugLogEnabled)\r
170                     return message\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
175                         return null\r
176                 } \r
177                 \r
178         }\r
179         \r
180         /**\r
181          * @param response message from called component (ex: AAI)\r
182          * @param execution\r
183          * @return an error response conforming to the common \r
184          */\r
185         String mapAAIExceptionTCommonException(String response, Execution execution)\r
186         {\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
192                         ')'\r
193 \r
194                 utils.log("DEBUG",'Entered ' + method, isDebugLogEnabled)\r
195                 def variables\r
196                 def message\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
201                 try {\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
208                 }\r
209                 \r
210                 if(message != null) { \r
211                          execution.setVariable(prefix+"ErrorResponse",message)\r
212                          utils.log("ERROR","Fault:"+ execution.getVariable(prefix+"ErrorResponse"))\r
213                          return message\r
214                 } else {\r
215                         \r
216                                 return null\r
217                         \r
218                 }\r
219         }\r
220         \r
221         /**\r
222          * @param execution\r
223          * @return an error response conforming to the common API with default text msg\r
224          */\r
225         String buildException(execution){\r
226                 return buildException(null, execution)\r
227         }\r
228         \r
229         /**\r
230          * @param response message from called component (ex: AAI)\r
231          * @param execution\r
232          * @return an error response conforming to the common\r
233          */\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
238                         ')'\r
239 \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
244                 def variables\r
245                 utils.log("DEBUG","response: " + response, isDebugLogEnabled)\r
246                 \r
247                         try {\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
253                                 \r
254                                 if(myErr == null){\r
255                                         utils.log("DEBUG","mapping response code: " + responseCode, isDebugLogEnabled)\r
256                                         myErr = mapErrorCodetoError(responseCode, response)\r
257                                         if(myErr == null){\r
258                                                 //not a service or policy error, just return error code\r
259                                                 return ""\r
260                                         }\r
261                                 }\r
262                                 messageId = myErr.getMsgId()\r
263                                 \r
264                                 if(messageTxt == null){\r
265                                         if(myErr!=null){\r
266                                                 messageTxt = myErr.getMsgTxt()\r
267                                         }else{\r
268                                                 messageTxt = response\r
269                                         }\r
270                                 }\r
271                                 \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
276                                 }\r
277                                 \r
278                                 def msgVarsXML=""\r
279                                 StringBuffer msgVarsBuff = new StringBuffer()\r
280                                 if(msgVars!=null){\r
281                                         for(String msgVar : msgVars){\r
282                                                 msgVarsBuff.append(\r
283                                                         """\r
284                         <tns:variables>${msgVar}</tns:variables>""")\r
285                                         }\r
286                                         \r
287                                 }\r
288                                 def message = ""\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
296                                 }else{\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
303                                 }\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
310                                  return message\r
311                         }catch(Exception ex) {\r
312                                 utils.log("DEBUG","error mapping error, return null: " + ex,isDebugLogEnabled)\r
313                                 return null\r
314                         }\r
315 \r
316         }\r
317         \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
325                 if(text == null){\r
326                         return 'failed'\r
327                 }\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
331                         }\r
332                 }\r
333                 utils.log("DEBUG",'parsed message is: ' + text, isDebugLogEnabled)\r
334                 return text\r
335         }\r
336         \r
337         \r
338 \r
339         Error mapErrorCodetoError(responseCode, descr)\r
340         {\r
341                 \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
345                         return null\r
346                 }else if(responseCode == '400'){\r
347                         if(descr==null){\r
348                                 return Error.SVC_GENERAL_SERVICE_ERROR\r
349                         }else{\r
350                                 return Error.SVC_DETAILED_SERVICE_ERROR\r
351                         }\r
352                 }else if(responseCode == '401'){\r
353                         if(descr==null){\r
354                                 return Error.POL_GENERAL_POLICY_ERROR\r
355                         }else{\r
356                                 return Error.POL_DETAILED_POLICY_ERROR\r
357                         }\r
358                 }else{\r
359                         return Error.SVC_NO_SERVER_RESOURCES\r
360                 }\r
361         }\r
362         \r
363         String mapCategoryToErrorCode(String errorCategory)\r
364         {\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
373                 else\r
374                         return Error.SVC_GENERAL_SERVICE_ERROR\r
375         }\r
376         \r
377         \r
378         \r
379 \r
380         \r
381         \r
382 }