289e185522f7109aa50e4e52411adfbdd14cb039
[so.git] /
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - MSO\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.infrastructure.scripts\r
22 \r
23 import org.openecomp.mso.bpmn.common.scripts.AaiUtil;\r
24 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor;\r
25 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil;\r
26 import org.openecomp.mso.bpmn.common.scripts.VidUtils;\r
27 import org.openecomp.mso.bpmn.core.WorkflowException\r
28 import org.openecomp.mso.rest.APIResponse\r
29 \r
30 import groovy.json.JsonSlurper\r
31 \r
32 import org.camunda.bpm.engine.delegate.BpmnError\r
33 import org.camunda.bpm.engine.runtime.Execution;\r
34 import org.apache.commons.lang3.*\r
35 \r
36 class CreateVfModuleVolumeInfraV1 extends AbstractServiceTaskProcessor {\r
37 \r
38         public static final String  prefix='CVMVINFRAV1_'\r
39 \r
40         /**\r
41          * Perform initial processing, such as request validation, initialization of variables, etc.\r
42          * * @param execution\r
43          */\r
44         public void preProcessRequest (Execution execution) {\r
45                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
46                 preProcessRequest(execution, isDebugEnabled)\r
47         }\r
48 \r
49 \r
50         /**\r
51          * Perform initial processing, such as request validation, initialization of variables, etc.\r
52          * @param execution\r
53          * @param isDebugEnabled\r
54          */\r
55         public void preProcessRequest (Execution execution, isDebugEnabled) {\r
56 \r
57                 execution.setVariable("prefix",prefix)\r
58                 setSuccessIndicator(execution, false)\r
59                 execution.setVariable(prefix+'syncResponseSent', false)\r
60 \r
61                 String createVolumeIncoming = validateRequest(execution, 'vnfId')\r
62                 utils.logAudit(createVolumeIncoming)\r
63 \r
64                 try {\r
65                         def jsonSlurper = new JsonSlurper()\r
66                         Map reqMap = jsonSlurper.parseText(createVolumeIncoming)\r
67 \r
68                         def serviceInstanceId = execution.getVariable('serviceInstanceId')\r
69                         def vnfId = execution.getVariable('vnfId')\r
70 \r
71                         def vidUtils = new VidUtils(this)\r
72                         createVolumeIncoming = vidUtils.createXmlVolumeRequest(reqMap, 'CREATE_VF_MODULE_VOL', serviceInstanceId)\r
73 \r
74                         execution.setVariable(prefix+'Request', createVolumeIncoming)\r
75                         execution.setVariable(prefix+'vnfId', vnfId)\r
76                         execution.setVariable(prefix+'isVidRequest', true)\r
77 \r
78                         utils.log("DEBUG", "XML request:\n" + createVolumeIncoming, isDebugEnabled)\r
79 \r
80                 }\r
81                 catch(groovy.json.JsonException je) {\r
82                         (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, 'Request is not a valid JSON document')\r
83                 }\r
84 \r
85                 execution.setVariable(prefix+'source', utils.getNodeText1(createVolumeIncoming, "source"))\r
86                 execution.setVariable(prefix+'volumeGroupName', utils.getNodeText1(createVolumeIncoming, 'volume-group-name'))\r
87                 execution.setVariable(prefix+'volumeOutputs', utils.getNodeXml(createVolumeIncoming, 'volume-outputs', false))\r
88 \r
89                 execution.setVariable(prefix+'serviceType', 'service-instance')\r
90                 execution.setVariable(prefix+'serviceInstanceId', utils.getNodeText1(createVolumeIncoming, "service-instance-id"))\r
91 \r
92                 // Generate volume group id\r
93                 String volumeGroupId = UUID.randomUUID()\r
94                 utils.log("DEBUG", "Generated volume group id: " + volumeGroupId, isDebugEnabled)\r
95 \r
96                 def testGroupId = execution.getVariable('test-volume-group-id')\r
97                 if (testGroupId != null && testGroupId.trim() != '') {\r
98                         volumeGroupId = testGroupId\r
99                 }\r
100 \r
101                 execution.setVariable(prefix+'volumeGroupId', volumeGroupId)\r
102 \r
103         }\r
104 \r
105 \r
106         public void sendSyncResponse (Execution execution, isDebugEnabled) {\r
107                 def volumeGroupId = execution.getVariable(prefix+'volumeGroupId')\r
108                 def requestId = execution.getVariable("mso-request-id")\r
109                 def serviceInstanceId = execution.getVariable("serviceInstanceId")\r
110 \r
111                 String syncResponse = """{"requestReferences":{"instanceId":"${volumeGroupId}","requestId":"${requestId}"}}""".trim()\r
112 \r
113                 utils.log("DEBUG", "Sync Response: " + "\n" + syncResponse, isDebugEnabled)\r
114                 sendWorkflowResponse(execution, 200, syncResponse)\r
115 \r
116                 execution.setVariable(prefix+'syncResponseSent', true)\r
117         }\r
118 \r
119 \r
120         public void sendSyncError (Execution execution, isDebugEnabled) {\r
121                 WorkflowException we = execution.getVariable('WorkflowException')\r
122                 def errorCode = we?.getErrorCode()\r
123                 def errorMessage = we?.getErrorMessage()\r
124                 //default to 400 since only invalid request will trigger this method\r
125                 sendWorkflowResponse(execution, 400, errorMessage)\r
126         }\r
127 \r
128 \r
129         /**\r
130          * Create a WorkflowException\r
131          * @param execution\r
132          * @param isDebugEnabled\r
133          */\r
134         public void buildWorkflowException(Execution execution, int errorCode, errorMessage, isDebugEnabled) {\r
135                 utils.log("DEBUG", errorMessage, isDebugEnabled)\r
136                 (new ExceptionUtil()).buildWorkflowException(execution, 2500, errorMessage)\r
137         }\r
138 \r
139 \r
140         public void prepareDbInfraSuccessRequest(Execution execution, isDebugEnabled) {\r
141                 ExceptionUtil exceptionUtil = new ExceptionUtil()\r
142                 def dbVnfOutputs = execution.getVariable(prefix+'volumeOutputs')\r
143                 def requestId = execution.getVariable('mso-request-id')\r
144                 def statusMessage = "VolumeGroup successfully created."\r
145                 def requestStatus = "COMPLETED"\r
146                 def progress = "100"\r
147 \r
148                 try {\r
149                         String basicAuthValueDB = execution.getVariable("URN_mso_adapters_db_auth")\r
150                         utils.log("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB, isDebugEnabled)\r
151                         \r
152                         def encodedString = utils.getBasicAuth(basicAuthValueDB, execution.getVariable("URN_mso_msoKey"))\r
153                         execution.setVariable("BasicAuthHeaderValueDB",encodedString)\r
154                 } catch (IOException ex) {\r
155                         String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()\r
156                         utils.log("DEBUG", dataErrorMessage, isDebugEnabled)\r
157                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)\r
158                 }\r
159                 \r
160                 /*\r
161                 from: $gVolumeGroup/aai:volume-group-id/text()\r
162                 to: vnfreq:volume-outputs/vnfreq:volume-group-id\r
163                 */\r
164                 // for now assume, generated volumeGroupId is accepted\r
165                 def volumeGroupId = execution.getVariable(prefix+'volumeGroupId')\r
166 \r
167                 String dbRequest =\r
168                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">\r
169                                 <soapenv:Header/>\r
170                                 <soapenv:Body>\r
171                                         <ns:updateInfraRequest xmlns:ns="http://org.openecomp.mso/requestsdb">\r
172                                                 <requestId>${requestId}</requestId>\r
173                                                 <lastModifiedBy>BPMN</lastModifiedBy>\r
174                                                 <statusMessage>${statusMessage}</statusMessage>\r
175                                                 <responseBody></responseBody>\r
176                                                 <requestStatus>${requestStatus}</requestStatus>\r
177                                                 <progress>${progress}</progress>\r
178                                                 <vnfOutputs>${dbVnfOutputs}</vnfOutputs>\r
179                                                 <volumeGroupId>${volumeGroupId}</volumeGroupId>\r
180                                         </ns:updateInfraRequest>\r
181                                 </soapenv:Body>\r
182                            </soapenv:Envelope>"""\r
183 \r
184                 String buildDeleteDBRequestAsString = utils.formatXml(dbRequest)\r
185                 execution.setVariable(prefix+"createDBRequest", buildDeleteDBRequestAsString)\r
186 \r
187                 utils.logAudit(buildDeleteDBRequestAsString)\r
188         }\r
189 \r
190 \r
191 \r
192 \r
193 \r
194         public void postProcessResponse (Execution execution, isDebugEnabled) {\r
195 \r
196                 def dbReturnCode = execution.getVariable(prefix+'dbReturnCode')\r
197                 def createDBResponse =  execution.getVariable(prefix+'createDBResponse')\r
198 \r
199                 utils.logAudit('DB return code: ' + dbReturnCode)\r
200                 utils.logAudit('DB response: ' + createDBResponse)\r
201 \r
202                 def requestId = execution.getVariable("mso-request-id")\r
203                 def source = execution.getVariable(prefix+'source')\r
204 \r
205                 String msoCompletionRequest =\r
206                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
207                                                         xmlns:ns="http://org.openecomp/mso/request/types/v1">\r
208                                         <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
209                                                 <request-id>${requestId}</request-id>\r
210                                                 <action>CREATE</action>\r
211                                                 <source>${source}</source>\r
212                                         </request-info>\r
213                                         <aetgt:status-message>Volume Group has been created successfully.</aetgt:status-message>\r
214                                         <aetgt:mso-bpel-name>BPMN VF Module Volume action: CREATE</aetgt:mso-bpel-name>\r
215                                 </aetgt:MsoCompletionRequest>"""\r
216 \r
217                 String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)\r
218 \r
219                 utils.logAudit(createDBResponse)\r
220                 utils.logAudit(xmlMsoCompletionRequest)\r
221                 execution.setVariable(prefix+'Success', true)\r
222                 execution.setVariable(prefix+'CompleteMsoProcessRequest', xmlMsoCompletionRequest)\r
223                 utils.log("DEBUG", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)\r
224 \r
225         }\r
226 \r
227         public void prepareFalloutHandlerRequest(Execution execution, isDebugEnabled) {\r
228 \r
229                 WorkflowException we = execution.getVariable('WorkflowException')\r
230                 def errorCode = we?.getErrorCode()\r
231                 def errorMessage = we?.getErrorMessage()\r
232 \r
233                 def requestId = execution.getVariable("mso-request-id")\r
234                 def source = execution.getVariable(prefix+'source')\r
235 \r
236                 String falloutHandlerRequest =\r
237                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"\r
238                                                              xmlns:ns="http://org.openecomp/mso/request/types/v1"\r
239                                                              xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">\r
240                                    <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
241                                       <request-id>${requestId}</request-id>\r
242                                       <action>CREATE</action>\r
243                                       <source>${source}</source>\r
244                                    </request-info>\r
245                                            <aetgt:WorkflowException>\r
246                                               <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>\r
247                                               <aetgt:ErrorCode>${errorCode}</aetgt:ErrorCode>\r
248                                                 </aetgt:WorkflowException>\r
249 \r
250                                 </aetgt:FalloutHandlerRequest>"""\r
251 \r
252                 // Format Response\r
253                 String xmlHandlerRequest = utils.formatXml(falloutHandlerRequest)\r
254                 utils.logAudit(xmlHandlerRequest)\r
255 \r
256                 execution.setVariable(prefix+'FalloutHandlerRequest', xmlHandlerRequest)\r
257                 utils.log("ERROR", "Overall Error Response going to FalloutHandler: " + "\n" + xmlHandlerRequest, isDebugEnabled)\r
258         }\r
259 \r
260 \r
261         /**\r
262          * Query AAI service instance\r
263          * @param execution\r
264          * @param isDebugEnabled\r
265          */\r
266         public void callRESTQueryAAIServiceInstance(Execution execution, isDebugEnabled) {\r
267 \r
268                 def request = execution.getVariable(prefix+"Request")\r
269                 def serviceInstanceId = utils.getNodeText1(request, "service-instance-id")\r
270 \r
271                 AaiUtil aaiUtil = new AaiUtil(this)\r
272                 String aaiEndpoint = aaiUtil.getSearchNodesQueryEndpoint(execution)\r
273 \r
274                 def String queryAAIRequest = aaiEndpoint + "?search-node-type=service-instance&filter=service-instance-id:EQUALS:" + serviceInstanceId\r
275                 utils.logAudit("AAI query service instance request: " + queryAAIRequest)\r
276 \r
277                 APIResponse response = aaiUtil.executeAAIGetCall(execution, queryAAIRequest)\r
278 \r
279                 String returnCode = response.getStatusCode()\r
280                 String aaiResponseAsString = response.getResponseBodyAsString()\r
281                 aaiResponseAsString = StringEscapeUtils.unescapeXml(aaiResponseAsString)\r
282 \r
283                 utils.logAudit("AAI query service instance return code: " + returnCode)\r
284                 utils.logAudit("AAI query service instance response: " + aaiResponseAsString)\r
285 \r
286                 utils.log("DEBUG", "AAI query service instance return code: " + returnCode, isDebugEnabled)\r
287                 utils.log("DEBUG", "AAI query service instance response: " + aaiResponseAsString, isDebugEnabled)\r
288 \r
289                 ExceptionUtil exceptionUtil = new ExceptionUtil()\r
290 \r
291                 if (returnCode=='200') {\r
292                         utils.log("DEBUG", 'Service instance ' + serviceInstanceId + ' found in AAI.', isDebugEnabled)\r
293                 } else {\r
294                         if (returnCode=='404') {\r
295                                 def message = 'Service instance ' + serviceInstanceId + ' was not found in AAI. Return code: 404.'\r
296                                 utils.log("DEBUG", message, isDebugEnabled)\r
297                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, message)\r
298                         } else {\r
299                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)\r
300                                 throw new BpmnError("MSOWorkflowException")\r
301                         }\r
302                 }\r
303         }\r
304 }\r