1be4989490523ce40bcb1f23387d400e09615c0f
[so.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.so.bpmn.vcpe.scripts;
22
23 import org.onap.so.bpmn.common.scripts.*;
24 import org.onap.so.bpmn.common.scripts.AaiUtil
25 import org.onap.so.bpmn.core.RollbackData
26 import org.onap.so.bpmn.core.WorkflowException
27 import org.onap.so.bpmn.core.UrnPropertiesReader
28 import org.onap.so.bpmn.core.json.JsonUtils
29 import org.onap.so.rest.APIResponse
30
31 import java.util.UUID;
32 import org.camunda.bpm.engine.delegate.BpmnError
33 import org.camunda.bpm.engine.delegate.DelegateExecution
34 import org.apache.commons.lang3.*
35 import org.springframework.web.util.UriUtils;
36 import static org.apache.commons.lang3.StringUtils.*
37
38 import org.onap.so.logger.MessageEnum
39 import org.onap.so.logger.MsoLogger
40 import org.onap.so.client.aai.AAIResourcesClient
41 import org.onap.so.client.aai.AAIObjectType
42 import org.onap.so.client.aai.entities.AAIResultWrapper
43 import org.onap.so.client.aai.entities.Relationships
44 import org.onap.so.client.aai.entities.uri.AAIResourceUri
45 import org.onap.so.client.aai.entities.uri.AAIUriFactory
46 import org.json.JSONObject
47 import javax.ws.rs.NotFoundException
48 /**
49  * This groovy class supports the <class>DoCreateAllottedResourceBRG.bpmn</class> process.
50  *
51  * @author
52  *
53  * Inputs:
54  * @param - msoRequestId
55  * @param - isDEbugLogEnabled
56  * @param - disableRollback
57  * @param - failExists  - O
58  * @param - serviceInstanceId
59  * @param - globalCustomerId - O
60  * @param - subscriptionServiceType - O
61  * @param - parentServiceInstanceId
62  * @param - allottedReourceId - O
63  * @param - allottedResourceModelInfo
64  * @param - allottedResourceRole
65  * @param - allottedResourceType
66  * @param - brgWanMacAddress
67  * @param - vni
68  * @param - vgmuxBearerIP
69  *
70  * Outputs:
71  * @param - rollbackData (localRB->null)
72  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
73  * @param - WorkflowException - O
74  * @param - allottedResourceId
75  * @param - allottedResourceName
76  *
77  */
78 public class DoCreateAllottedResourceBRG extends AbstractServiceTaskProcessor{
79         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateAllottedResourceBRG.class);
80
81         String Prefix="DCARBRG_"
82         ExceptionUtil exceptionUtil = new ExceptionUtil()
83         JsonUtils jsonUtil = new JsonUtils()
84
85         public void preProcessRequest (DelegateExecution execution) {
86
87
88                 String msg = ""
89                 msoLogger.trace("start preProcessRequest")
90
91                 try {
92                         execution.setVariable("prefix", Prefix)
93
94                         //Config Inputs
95                         String sdncCallbackUrl = UrnPropertiesReader.getVariable("mso.workflow.sdncadapter.callback",execution)
96                         if (isBlank(sdncCallbackUrl)) {
97                                 msg = "mso.workflow.sdncadapter.callback is null"
98                                 msoLogger.debug(msg)
99                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
100                         }
101                         execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
102                         msoLogger.debug("SDNC Callback URL: " + sdncCallbackUrl)
103
104                         String sdncReplDelay = UrnPropertiesReader.getVariable("mso.workflow.sdnc.replication.delay",execution)
105                         if (isBlank(sdncReplDelay)) {
106                                 msg = "mso.workflow.sdnc.replication.delay is null"
107                                 msoLogger.debug(msg)
108                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
109                         }
110                         execution.setVariable("sdncReplDelay", sdncReplDelay)
111                         msoLogger.debug("SDNC replication delay: " + sdncReplDelay)
112
113                         //Request Inputs
114                         if (isBlank(execution.getVariable("serviceInstanceId"))){
115                                 msg = "Input serviceInstanceId is null"
116                                 msoLogger.debug(msg)
117                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
118                         }
119                         if (isBlank(execution.getVariable("parentServiceInstanceId"))) {
120                                 msg = "Input parentServiceInstanceId is null"
121                                 msoLogger.debug(msg)
122                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
123                         }
124                         if (isBlank(execution.getVariable("allottedResourceModelInfo"))) {
125                                 msg = "Input allottedResourceModelInfo is null"
126                                 msoLogger.debug(msg)
127                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
128                         }
129                         if (isBlank(execution.getVariable("vni"))) {
130                                 msg = "Input vni is null"
131                                 msoLogger.debug(msg)
132                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
133                         }
134                         if (isBlank(execution.getVariable("vgmuxBearerIP"))) {
135                                 msg = "Input vgmuxBearerIP is null"
136                                 msoLogger.debug(msg)
137                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
138                         }
139                         if (isBlank(execution.getVariable("brgWanMacAddress"))) {
140                                 msg = "Input brgWanMacAddress is null"
141                                 msoLogger.debug(msg)
142                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
143                         }
144                         if (isBlank(execution.getVariable("allottedResourceRole"))) {
145                                 msg = "Input allottedResourceRole is null"
146                                 msoLogger.debug(msg)
147                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
148                         }
149                         if (isBlank(execution.getVariable("allottedResourceType"))) {
150                                 msg = "Input allottedResourceType is null"
151                                 msoLogger.debug(msg)
152                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
153                         }
154                 }catch(BpmnError b){
155                         msoLogger.debug("Rethrowing MSOWorkflowException")
156                         throw b
157                 } catch (Exception ex){
158                         msg = "Exception in preProcessRequest " + ex.getMessage()
159                         msoLogger.debug(msg)
160                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
161                 }
162                 msoLogger.trace("end preProcessRequest")
163         }
164
165         /**
166          * Gets the service instance uri from aai
167          */
168         public void getServiceInstance(DelegateExecution execution) {
169                 msoLogger.trace("getServiceInstance ")
170                 try {
171                         String serviceInstanceId = execution.getVariable('serviceInstanceId')
172
173                         AAIResourcesClient resourceClient = new AAIResourcesClient()
174                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
175
176                         if(resourceClient.exists(uri)){
177                                 execution.setVariable("CSI_resourceLink", uri.build().toString())
178                         }else{
179                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai")
180                         }
181
182                 }catch(BpmnError e) {
183                         throw e;
184                 }catch (Exception ex){
185                         String msg = "Exception in getServiceInstance. " + ex.getMessage()
186                         msoLogger.debug(msg)
187                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
188                 }
189                 msoLogger.trace("Exit getServiceInstance ")
190         }
191
192         public void getAaiAR (DelegateExecution execution) {
193
194
195                 msoLogger.trace("start getAaiAR")
196
197                 String arType = execution.getVariable("allottedResourceType")
198                 String arRole = execution.getVariable("allottedResourceRole")
199
200                 AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
201                 String orchStatus = arUtils.getAROrchStatus(execution)
202
203                 String errorMsg = ""
204
205                 if (orchStatus != null) // AR was found
206                 {
207                         if ("true".equals(execution.getVariable("failExists")))
208                         {
209                                 errorMsg = "Allotted resource " + arType + " with Role " + arRole + " already exists"
210                         }
211                         else
212                         {
213                                 if ("Active".equals(orchStatus))
214                                 {
215                                         execution.setVariable("foundActiveAR", true)
216                                 }
217                                 else // blanks included
218                                 {
219                                         errorMsg = "Allotted Resource " + arType + " with Role " + arRole + " already exists in an incomplete state -"  +  orchStatus
220                                 }
221                         }
222                 }
223                 if (!isBlank(errorMsg)) {
224                         msoLogger.debug(errorMsg)
225                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, errorMsg)
226                 }
227                 msoLogger.trace("end getAaiAR")
228         }
229
230         public void getParentServiceInstance(DelegateExecution execution) {
231                 msoLogger.trace("getParentServiceInstance ")
232                 try {
233                         String serviceInstanceId = execution.getVariable('parentServiceInstanceId')
234
235                         AAIResourcesClient resourceClient = new AAIResourcesClient()
236                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.NODES_QUERY, "").queryParam("search-node-type", "service-instance").queryParam("filter", "service-instance-id:EQUALS:" + serviceInstanceId)
237                         String json = resourceClient.get(uri).getJson()
238
239                         JSONObject obj = new JSONObject(json)
240                         if(obj.has("result-data")){
241                                 JSONObject ob = obj.getJSONArray("result-data").getJSONObject(0)
242                                 String resourceLink = ob.getString("resource-link")
243                                 AAIResourceUri siUri = AAIUriFactory.createResourceFromExistingURI(AAIObjectType.SERVICE_INSTANCE, new URI(resourceLink))
244
245                                 execution.setVariable("PSI_resourceLink", siUri)
246                         }else{
247                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai")
248                         }
249
250                 }catch(BpmnError e) {
251                         throw e;
252                 }catch (Exception ex){
253                         String msg = "Exception in getParentServiceInstance. " + ex.getMessage()
254                         msoLogger.debug(msg)
255                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
256                 }
257                 msoLogger.trace("Exit getParentServiceInstance ")
258         }
259
260
261         public void createAaiAR(DelegateExecution execution) {
262
263
264                 msoLogger.trace("start createAaiAR")
265                 String msg = ""
266
267                 String allottedResourceId = execution.getVariable("allottedResourceId")
268                 if (isBlank(allottedResourceId))
269                 {
270                         allottedResourceId = UUID.randomUUID().toString()
271                         execution.setVariable("allottedResourceId", allottedResourceId)
272                 }
273                 String arUrl = ""
274                 try {
275
276                         //AAI PUT
277                         AaiUtil aaiUriUtil = new AaiUtil(this)
278                         AAIResourceUri siResourceLink= execution.getVariable("PSI_resourceLink")
279                         AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
280                         execution.setVariable("aaiARPath", arUtils.createARUrl(execution, siResourceLink, allottedResourceId))
281                         msoLogger.debug("GET AllottedResource AAI URL is:\n" + arUrl)
282
283                         String namespace = aaiUriUtil.getNamespaceFromUri(execution, arUrl)
284
285                         String arType = execution.getVariable("allottedResourceType")
286                         String arRole = execution.getVariable("allottedResourceRole")
287                         String CSI_resourceLink = execution.getVariable("CSI_resourceLink")
288                         String arModelInfo = execution.getVariable("allottedResourceModelInfo")
289                         String modelInvariantId = jsonUtil.getJsonValue(arModelInfo, "modelInvariantUuid")
290                         String modelVersionId = jsonUtil.getJsonValue(arModelInfo, "modelUuid")
291                         String modelCustomizationId = jsonUtil.getJsonValue(arModelInfo, "modelCustomizationUuid")
292
293                         if (modelInvariantId == null) {
294                                 modelInvariantId = ""
295                         }
296                         if (modelVersionId == null) {
297                                 modelVersionId = ""
298                         }
299                         if (modelCustomizationId == null) {
300                                 modelCustomizationId = ""
301                         }
302
303                         String payload =
304                         """<allotted-resource xmlns="${namespace}">
305                                 <id>${MsoUtils.xmlEscape(allottedResourceId)}</id>
306                                 <description></description>
307                                 <type>${MsoUtils.xmlEscape(arType)}</type>
308                                 <role>${MsoUtils.xmlEscape(arRole)}</role>
309                                 <selflink></selflink>
310                                 <model-invariant-id>${MsoUtils.xmlEscape(modelInvariantId)}</model-invariant-id>
311                                 <model-version-id>${MsoUtils.xmlEscape(modelVersionId)}</model-version-id>
312                                 <model-customization-id>${MsoUtils.xmlEscape(modelCustomizationId)}</model-customization-id>
313                                 <orchestration-status>PendingCreate</orchestration-status>
314                                 <operation-status></operation-status>
315                                 <relationship-list>
316                                         <relationship>
317                                 <related-to>service-instance</related-to>
318                                 <related-link>${MsoUtils.xmlEscape(CSI_resourceLink)}</related-link>
319                                         </relationship>
320                                 </relationship-list>
321                         </allotted-resource>""".trim()
322
323                         execution.setVariable("AaiARPayload", payload)
324                         msoLogger.debug(" payload to create AllottedResource in AAI:" + "\n" + payload)
325
326                         APIResponse response = aaiUriUtil.executeAAIPutCall(execution, arUrl, payload)
327                         int responseCode = response.getStatusCode()
328                         msoLogger.debug("AllottedResource AAI PUT responseCode:" + responseCode)
329
330                         String aaiResponse = response.getResponseBodyAsString()
331                         msoLogger.debug("AllottedResource AAI PUT responseStr:" + aaiResponse)
332
333                         //200 OK 201 CREATED 202 ACCEPTED
334                         if(responseCode == 200 || responseCode == 201 || responseCode == 202 )
335                         {
336                                 msoLogger.debug("AAI PUT AllottedResource received a Good Response")
337                         }
338                         else{
339                                 msoLogger.debug("AAI Put AllottedResouce received a Bad Response Code: " + responseCode)
340                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
341                                 throw new BpmnError("MSOWorkflowException")
342                         }
343                 }catch(BpmnError b){
344                         msoLogger.debug("Rethrowing MSOWorkflowException")
345                         throw b
346                 } catch (Exception ex) {
347                         msg = "Exception in createAaiAR " + ex.getMessage()
348                         msoLogger.debug(msg)
349                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
350                 }
351
352                 //start rollback set up
353                 RollbackData rollbackData = new RollbackData()
354                 def disableRollback = execution.getVariable("disableRollback")
355                 rollbackData.put(Prefix, "disableRollback", disableRollback.toString())
356                 rollbackData.put(Prefix, "rollbackAAI", "true")
357                 rollbackData.put(Prefix, "allottedResourceId", allottedResourceId)
358                 rollbackData.put(Prefix, "serviceInstanceId", execution.getVariable("serviceInstanceId"))
359                 rollbackData.put(Prefix, "parentServiceInstanceId", execution.getVariable("parentServiceInstanceId"))
360                 rollbackData.put(Prefix, "aaiARPath", arUrl)
361                 execution.setVariable("rollbackData", rollbackData)
362                 msoLogger.trace("end createAaiAR")
363         }
364
365         public String buildSDNCRequest(DelegateExecution execution, String action, String sdncRequestId) {
366
367
368                 String msg = ""
369                 msoLogger.trace("start buildSDNCRequest")
370                 String sdncReq = null
371
372                 try {
373
374                         String allottedResourceId = execution.getVariable("allottedResourceId")
375                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
376                         String globalCustomerId = execution.getVariable("globalCustomerId")
377                         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
378                         String parentServiceInstanceId = execution.getVariable("parentServiceInstanceId")
379                         String callbackUrl = execution.getVariable("sdncCallbackUrl")
380                         String requestId = execution.getVariable("msoRequestId")
381
382                         String brgWanMacAddress = execution.getVariable("brgWanMacAddress")
383                         String vni = execution.getVariable("vni")
384                         String vgmuxBearerIP = execution.getVariable("vgmuxBearerIP")
385
386                         String arModelInfo = execution.getVariable("allottedResourceModelInfo")
387                         String modelInvariantId = jsonUtil.getJsonValue(arModelInfo, "modelInvariantUuid")
388                         String modelVersion = jsonUtil.getJsonValue(arModelInfo, "modelVersion")
389                         String modelUUId = jsonUtil.getJsonValue(arModelInfo, "modelUuid")
390                         String modelCustomizationId = jsonUtil.getJsonValue(arModelInfo, "modelCustomizationUuid")
391                         String modelName = jsonUtil.getJsonValue(arModelInfo, "modelName")
392
393                         if (modelInvariantId == null) {
394                                 modelInvariantId = ""
395                         }
396                         if (modelVersion == null) {
397                                 modelVersion = ""
398                         }
399                         if (modelUUId == null) {
400                                 modelUUId = ""
401                         }
402                         if (modelName == null) {
403                                 modelName = ""
404                         }
405                         if (modelCustomizationId == null) {
406                                 modelCustomizationId = ""
407                         }
408
409                         sdncReq =
410                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
411                                                                                                         xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
412                                                                                                         xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
413                                    <sdncadapter:RequestHeader>
414                                                         <sdncadapter:RequestId>${MsoUtils.xmlEscape(sdncRequestId)}</sdncadapter:RequestId>
415                                                         <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(serviceInstanceId)}</sdncadapter:SvcInstanceId>
416                                                         <sdncadapter:SvcAction>${MsoUtils.xmlEscape(action)}</sdncadapter:SvcAction>
417                                                         <sdncadapter:SvcOperation>brg-topology-operation</sdncadapter:SvcOperation>
418                                                         <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackUrl)}</sdncadapter:CallbackUrl>
419                                         </sdncadapter:RequestHeader>
420                                 <sdncadapterworkflow:SDNCRequestData>
421                                         <request-information>
422                                                 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
423                                                 <request-action>CreateBRGInstance</request-action>
424                                                 <source>MSO</source>
425                                                 <notification-url/>
426                                                 <order-number/>
427                                                 <order-version/>
428                                         </request-information>
429                                         <service-information>
430                                                 <service-id></service-id>
431                                                 <subscription-service-type>${MsoUtils.xmlEscape(subscriptionServiceType)}</subscription-service-type>
432                                                 <onap-model-information></onap-model-information>
433                                                 <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
434                                                 <subscriber-name/>
435                                                 <global-customer-id>${MsoUtils.xmlEscape(globalCustomerId)}</global-customer-id>
436                                         </service-information>
437                                         <allotted-resource-information>
438                                                 <allotted-resource-id>${MsoUtils.xmlEscape(allottedResourceId)}</allotted-resource-id>
439                                                 <allotted-resource-type>brg</allotted-resource-type>
440                                                 <parent-service-instance-id>${MsoUtils.xmlEscape(parentServiceInstanceId)}</parent-service-instance-id>
441                                                 <onap-model-information>
442                                                         <model-invariant-uuid>${MsoUtils.xmlEscape(modelInvariantId)}</model-invariant-uuid>
443                                                         <model-uuid>${MsoUtils.xmlEscape(modelUUId)}</model-uuid>
444                                                         <model-customization-uuid>${MsoUtils.xmlEscape(modelCustomizationId)}</model-customization-uuid>
445                                                         <model-version>${MsoUtils.xmlEscape(modelVersion)}</model-version>
446                                                         <model-name>${MsoUtils.xmlEscape(modelName)}</model-name>
447                                                 </onap-model-information>
448                                         </allotted-resource-information>
449                                         <brg-request-input>
450                                                         <brg-wan-mac-address>${MsoUtils.xmlEscape(brgWanMacAddress)}</brg-wan-mac-address>
451                                                         <vni>${MsoUtils.xmlEscape(vni)}</vni>
452                                                         <vgmux-bearer-ip>${MsoUtils.xmlEscape(vgmuxBearerIP)}</vgmux-bearer-ip>
453                                         </brg-request-input>
454                                 </sdncadapterworkflow:SDNCRequestData>
455                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
456
457                         msoLogger.debug("sdncRequest:\n" + sdncReq)
458                         sdncReq = utils.formatXml(sdncReq)
459
460                 } catch(Exception ex) {
461                         msg = "Exception in buildSDNCRequest. " + ex.getMessage()
462                         msoLogger.debug(msg)
463                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
464                 }
465                 msoLogger.trace("end buildSDNCRequest")
466                 return sdncReq
467         }
468
469         public void preProcessSDNCAssign(DelegateExecution execution) {
470
471
472                 String msg = ""
473                 msoLogger.trace("start preProcessSDNCAssign")
474
475                 try {
476                         String sdncRequestId = UUID.randomUUID().toString()
477                         String sdncAssignReq = buildSDNCRequest(execution, "assign", sdncRequestId)
478                         execution.setVariable("sdncAssignRequest", sdncAssignReq)
479                         msoLogger.debug("sdncAssignRequest:  " + sdncAssignReq)
480                         def sdncRequestId2 = UUID.randomUUID().toString()
481                         String sdncAssignRollbackReq = sdncAssignReq.replace(">assign<", ">unassign<").replace(">CreateBRGInstance<", ">DeleteBRGInstance<").replace(">${sdncRequestId}<", ">${sdncRequestId2}<")
482                         def rollbackData = execution.getVariable("rollbackData")
483                         rollbackData.put(Prefix, "sdncAssignRollbackReq", sdncAssignRollbackReq)
484                         execution.setVariable("rollbackData", rollbackData)
485
486                         msoLogger.debug("sdncAssignRollbackReq:\n" + sdncAssignRollbackReq)
487                         msoLogger.debug("rollbackData:\n" + rollbackData.toString())
488
489                 } catch (BpmnError e) {
490                         throw e;
491                 } catch(Exception ex) {
492                         msg = "Exception in preProcessSDNCAssign. " + ex.getMessage()
493                         msoLogger.debug(msg)
494                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
495                 }
496                 msoLogger.trace("end preProcessSDNCAssign")
497         }
498
499         public void preProcessSDNCCreate(DelegateExecution execution) {
500
501
502                 String msg = ""
503                 msoLogger.trace("start preProcessSDNCCreate")
504
505                 try {
506                         String sdncRequestId = UUID.randomUUID().toString()
507                         String sdncCreateReq = buildSDNCRequest(execution, "create", sdncRequestId)
508                         execution.setVariable("sdncCreateRequest", sdncCreateReq)
509                         msoLogger.debug("sdncCreateReq:  " + sdncCreateReq)
510                         def sdncRequestId2 = UUID.randomUUID().toString()
511                         String sdncCreateRollbackReq = sdncCreateReq.replace(">create<", ">delete<").replace(">CreateBRGInstance<", ">DeleteBRGInstance<").replace(">${sdncRequestId}<", ">${sdncRequestId2}<")
512                         def rollbackData = execution.getVariable("rollbackData")
513                         rollbackData.put(Prefix, "sdncCreateRollbackReq", sdncCreateRollbackReq)
514                         execution.setVariable("rollbackData", rollbackData)
515
516                         msoLogger.debug("sdncCreateRollbackReq:\n" + sdncCreateRollbackReq)
517                         msoLogger.debug("rollbackData:\n" + rollbackData.toString())
518
519                 } catch (BpmnError e) {
520                         throw e;
521                 } catch(Exception ex) {
522                         msg = "Exception in preProcessSDNCCreate. " + ex.getMessage()
523                         msoLogger.debug(msg)
524                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
525                 }
526                 msoLogger.trace("end preProcessSDNCCreate")
527         }
528
529         public void preProcessSDNCActivate(DelegateExecution execution) {
530
531
532                 String msg = ""
533                 msoLogger.trace("start preProcessSDNCActivate")
534
535                 try {
536                         String sdncRequestId = UUID.randomUUID().toString()
537                         String sdncActivateReq = buildSDNCRequest(execution, "activate", sdncRequestId)
538                         execution.setVariable("sdncActivateRequest", sdncActivateReq)
539                         msoLogger.debug("sdncActivateReq:  " + sdncActivateReq)
540                         def sdncRequestId2 = UUID.randomUUID().toString()
541                         String sdncActivateRollbackReq = sdncActivateReq.replace(">activate<", ">deactivate<").replace(">CreateBRGInstance<", ">DeleteBRGInstance<").replace(">${sdncRequestId}<", ">${sdncRequestId2}<")
542                         def rollbackData = execution.getVariable("rollbackData")
543                         rollbackData.put(Prefix, "sdncActivateRollbackReq", sdncActivateRollbackReq)
544                         execution.setVariable("rollbackData", rollbackData)
545
546                         msoLogger.debug("sdncActivateRollbackReq:\n" + sdncActivateRollbackReq)
547                         msoLogger.debug("rollbackData:\n" + rollbackData.toString())
548
549                 } catch (BpmnError e) {
550                         throw e;
551                 } catch(Exception ex) {
552                         msg = "Exception in preProcessSDNCActivate. " + ex.getMessage()
553                         msoLogger.debug(msg)
554                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
555                 }
556                 msoLogger.trace("end preProcessSDNCActivate")
557         }
558
559         public void validateSDNCResp(DelegateExecution execution, String response, String method){
560
561
562                 msoLogger.trace("ValidateSDNCResponse Process")
563                 String msg = ""
564
565                 try {
566                         WorkflowException workflowException = execution.getVariable("WorkflowException")
567                         msoLogger.debug("workflowException: " + workflowException)
568
569                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
570                         msoLogger.debug("SDNCResponse: " + response)
571
572                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
573                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
574
575                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
576                                 msoLogger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + response)
577
578                                 if (!"get".equals(method))
579                                 {
580                                         def rollbackData = execution.getVariable("rollbackData")
581                                         rollbackData.put(Prefix, "rollback" +  "SDNC" + method, "true")
582                                         execution.setVariable("rollbackData", rollbackData)
583                                 }
584
585                         }else{
586                                 msoLogger.debug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.")
587                                 throw new BpmnError("MSOWorkflowException")
588                         }
589                 } catch (BpmnError e) {
590                         throw e;
591                 } catch(Exception ex) {
592                         msg = "Exception in validateSDNCResp. " + ex.getMessage()
593                         msoLogger.debug(msg)
594                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
595                 }
596                 msoLogger.trace("End ValidateSDNCResp Process")
597         }
598
599         public void preProcessSDNCGet(DelegateExecution execution){
600
601                 msoLogger.trace("start preProcessSDNCGet")
602                 try{
603
604                         def callbackUrl = execution.getVariable("sdncCallbackUrl")
605                         // serviceOperation (URI for topology GET) will be retrieved from "selflink" from AAI if active AR exists in AAI
606                         // or from  "object-path" in SDNC response for assign when AR does not exist in AA
607
608                         String serviceOperation = ""
609
610                         if (execution.getVariable("foundActiveAR")) {
611                                 def aaiQueryResponse = execution.getVariable("aaiARGetResponse")
612                                 serviceOperation = utils.getNodeText(aaiQueryResponse, "selflink")
613                                 msoLogger.debug("AR service operation/aaiARSelfLink: " + serviceOperation)
614                         }
615                         else
616                         {
617                                 String response = execution.getVariable("sdncAssignResponse")
618                                 String data = utils.getNodeXml(response, "response-data")
619                                 msoLogger.debug("Assign responseData: " + data)
620                                 serviceOperation = utils.getNodeText(data, "object-path")
621                                 msoLogger.debug("AR service operation:" + serviceOperation)
622                         }
623
624                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
625                         String sdncRequestId = UUID.randomUUID().toString()
626
627                         //neeed the same url as used by vfmodules
628                         String SDNCGetRequest =
629                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
630                                                                                         xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
631                                                                                         xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
632                                         <sdncadapter:RequestHeader>
633                                         <sdncadapter:RequestId>${MsoUtils.xmlEscape(sdncRequestId)}</sdncadapter:RequestId>
634                                         <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(serviceInstanceId)}</sdncadapter:SvcInstanceId>
635                                         <sdncadapter:SvcAction>query</sdncadapter:SvcAction>
636                                         <sdncadapter:SvcOperation>${MsoUtils.xmlEscape(serviceOperation)}</sdncadapter:SvcOperation>
637                                         <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackUrl)}</sdncadapter:CallbackUrl>
638                                         <sdncadapter:MsoAction>vfmodule</sdncadapter:MsoAction>
639                                 </sdncadapter:RequestHeader>
640                                         <sdncadapterworkflow:SDNCRequestData></sdncadapterworkflow:SDNCRequestData>
641                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
642
643                         execution.setVariable("sdncGetRequest", SDNCGetRequest)
644
645                 }catch(Exception e){
646                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occurred Processing preProcessSDNCGetRequest.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
647                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during SDNC GET Method:\n" + e.getMessage())
648                 }
649                 msoLogger.trace("end preProcessSDNCGet")
650         }
651
652         public void updateAaiAROrchStatus(DelegateExecution execution, String status){
653
654                 msoLogger.trace("start updateAaiAROrchStatus")
655                 String aaiARPath = execution.getVariable("aaiARPath") //set during query (existing AR) or create
656                 AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
657                 String orchStatus = arUtils.updateAROrchStatus(execution, status, aaiARPath)
658                 msoLogger.trace("end updateAaiAROrchStatus")
659         }
660
661         public void generateOutputs(DelegateExecution execution)
662         {
663
664                 msoLogger.trace("start generateOutputs")
665                 try {
666                         String sdncGetResponse = execution.getVariable("enhancedCallbackRequestData") //unescaped
667                         msoLogger.debug("resp:" + sdncGetResponse)
668                         String arData = utils.getNodeXml(sdncGetResponse, "brg-topology")
669                         arData = utils.removeXmlNamespaces(arData)
670
671                         String brga = utils.getNodeXml(arData, "brg-assignments")
672                         String ari = utils.getNodeXml(arData, "allotted-resource-identifiers")
673                         execution.setVariable("allotedResourceName", utils.getNodeText(ari, "allotted-resource-name"))
674                 } catch (BpmnError e) {
675                         msoLogger.debug("BPMN Error in generateOutputs ")
676                 } catch(Exception ex) {
677                         String msg = "Exception in generateOutputs " + ex.getMessage()
678                         msoLogger.debug(msg)
679                 }
680                 msoLogger.trace("end generateOutputs")
681
682         }
683
684         public void preProcessRollback (DelegateExecution execution) {
685
686                 msoLogger.trace("start preProcessRollback")
687                 try {
688
689                         Object workflowException = execution.getVariable("WorkflowException");
690
691                         if (workflowException instanceof WorkflowException) {
692                                 msoLogger.debug("Prev workflowException: " + workflowException.getErrorMessage())
693                                 execution.setVariable("prevWorkflowException", workflowException);
694                                 //execution.setVariable("WorkflowException", null);
695                         }
696                 } catch (BpmnError e) {
697                         msoLogger.debug("BPMN Error during preProcessRollback")
698                 } catch(Exception ex) {
699                         String msg = "Exception in preProcessRollback. " + ex.getMessage()
700                         msoLogger.debug(msg)
701                 }
702                 msoLogger.trace("end preProcessRollback")
703         }
704
705         public void postProcessRollback (DelegateExecution execution) {
706
707                 msoLogger.trace("start postProcessRollback")
708                 String msg = ""
709                 try {
710                         Object workflowException = execution.getVariable("prevWorkflowException");
711                         if (workflowException instanceof WorkflowException) {
712                                 msoLogger.debug("Setting prevException to WorkflowException: ")
713                                 execution.setVariable("WorkflowException", workflowException);
714                         }
715                         execution.setVariable("rollbackData", null)
716                 } catch (BpmnError b) {
717                         msoLogger.debug("BPMN Error during postProcessRollback")
718                         throw b;
719                 } catch(Exception ex) {
720                         msg = "Exception in postProcessRollback. " + ex.getMessage()
721                         msoLogger.debug(msg)
722                 }
723                 msoLogger.trace("end postProcessRollback")
724         }
725
726 }