SO changes for CCVPN enhancement in Jakarta
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCloudLeasedLineCreate.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Huawei Technologies Co., Ltd. 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.infrastructure.scripts
22
23 import org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.aai.domain.yang.NetworkPolicy
26 import org.onap.aai.domain.yang.ServiceInstance
27 import org.onap.aaiclient.client.aai.AAIResourcesClient
28 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
29 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
30 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
31 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
32 import org.onap.so.bpmn.common.scripts.ExceptionUtil
33 import org.onap.so.bpmn.common.scripts.RequestDBUtil
34 import org.onap.so.bpmn.core.json.JsonUtils
35 import org.onap.so.db.request.beans.ResourceOperationStatus
36 import org.slf4j.Logger
37 import org.slf4j.LoggerFactory
38 import org.springframework.web.util.UriUtils
39
40 import static org.apache.commons.lang3.StringUtils.isBlank
41 import static org.apache.commons.lang3.StringUtils.isNotBlank
42
43 class DoCloudLeasedLineCreate extends AbstractServiceTaskProcessor {
44
45     private static final Logger logger = LoggerFactory.getLogger(DoCloudLeasedLineCreate.class);
46     JsonUtils jsonUtil = new JsonUtils()
47     RequestDBUtil requestDBUtil = new RequestDBUtil()
48     ServiceIntentUtils serviceIntentUtils = new ServiceIntentUtils()
49     ExceptionUtil exceptionUtil = new ExceptionUtil()
50     String Prefix = "CLLC_"
51
52
53     void preProcessRequest(DelegateExecution execution) {
54         logger.debug("Start preProcessRequest")
55         execution.setVariable("prefix", Prefix)
56         String msg = ""
57
58         try {
59             execution.setVariable("startTime", System.currentTimeMillis())
60
61             msg = serviceIntentUtils.getExecutionInputParams(execution)
62             logger.debug("Create CLL input parameters: " + msg)
63
64             serviceIntentUtils.setSdncCallbackUrl(execution, true)
65             logger.debug("SDNC Callback URL: " + execution.getVariable("sdncCallbackUrl"))
66
67             String additionalPropJsonStr = execution.getVariable("serviceIntentParams")
68
69             String cllId = jsonUtil.getJsonValue(additionalPropJsonStr, "serviceInstanceID") //for debug
70             if (isBlank(cllId)) {
71                 cllId = UUID.randomUUID().toString()
72             }
73
74             String operationId = UUID.randomUUID().toString()
75             execution.setVariable("operationId", operationId)
76
77             logger.debug("Generate new CLL ID:" + cllId)
78             cllId = UriUtils.encode(cllId, "UTF-8")
79             execution.setVariable("cllId", cllId)
80
81             String cllName = execution.getVariable("servicename")
82             execution.setVariable("cllName", cllName)
83
84             String sst = execution.getVariable("sst")
85             execution.setVariable("sst", sst)
86
87             String transportNetworks = jsonUtil.getJsonValue(additionalPropJsonStr, "transportNetworks")
88             if (isBlank(transportNetworks)) {
89                 msg = "ERROR: preProcessRequest: Input transportNetworks is null"
90                 logger.error(msg)
91                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
92             } else {
93                 execution.setVariable("transportNetworks", transportNetworks)
94             }
95             logger.debug("transportNetworks: " + transportNetworks)
96
97             if (isBlank(serviceIntentUtils.setExecVarFromJsonIfExists(execution, additionalPropJsonStr,
98                     "enableSdnc", "enableSdnc"))) {
99                 serviceIntentUtils.setEnableSdncConfig(execution)
100             }
101         } catch (BpmnError e) {
102             throw e
103         } catch (Exception ex) {
104             msg = "Exception in preProcessRequest " + ex.getMessage()
105             logger.debug(msg)
106             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
107         }
108         logger.debug("Finish preProcessRequest")
109     }
110
111     void updateAAIOrchStatus(DelegateExecution execution) {
112         logger.debug("Start updateAAIOrchStatus")
113         String cllId = execution.getVariable("cllId")
114         String orchStatus = execution.getVariable("orchestrationStatus")
115
116         try {
117             ServiceInstance si = new ServiceInstance()
118             si.setOrchestrationStatus(orchStatus)
119             AAIResourcesClient client = new AAIResourcesClient()
120             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.Types.SERVICE_INSTANCE.getFragment(cllId))
121             client.update(uri, si)
122         } catch (BpmnError e) {
123             throw e
124         } catch (Exception ex) {
125             String msg = "Exception in CreateSliceService.updateAAIOrchStatus " + ex.getMessage()
126             logger.info(msg)
127             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
128         }
129
130         logger.debug("Finish updateAAIOrchStatus")
131     }
132
133     void prepareUpdateJobStatus(DelegateExecution execution,
134                                 String status,
135                                 String progress,
136                                 String statusDescription) {
137         String cllId = execution.getVariable("cllId")
138         String modelUuid = execution.getVariable("modelUuid")
139         String jobId = execution.getVariable("jobId")
140         String nsiId = cllId
141         String operType = "CREATE"
142
143         ResourceOperationStatus roStatus = serviceIntentUtils.buildRoStatus(modelUuid, cllId,
144                 jobId, nsiId, operType, status, progress, statusDescription)
145
146         logger.debug("prepareUpdateJobStatus: roStatus={}", roStatus)
147         requestDBUtil.prepareUpdateResourceOperationStatus(execution, roStatus)
148     }
149
150
151     void createServiceInstance(DelegateExecution execution) {
152
153         String serviceRole = "cll"
154         String serviceType = execution.getVariable("serviceType")
155         String cllId = execution.getVariable("cllId")
156         try {
157             org.onap.aai.domain.yang.ServiceInstance ss = new org.onap.aai.domain.yang.ServiceInstance()
158             ss.setServiceInstanceId(cllId)
159             String cllName = execution.getVariable("cllName")
160             if (isBlank(cllName)) {
161                 logger.error("ERROR: createServiceInstance: cllName is null")
162                 cllName = cllId
163             }
164             ss.setServiceInstanceName(cllName)
165             ss.setServiceType(serviceType)
166             String serviceStatus = "created"
167             ss.setOrchestrationStatus(serviceStatus)
168             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
169             String modelUuid = execution.getVariable("modelUuid")
170             ss.setModelInvariantId(modelInvariantUuid)
171             ss.setModelVersionId(modelUuid)
172             ss.setEnvironmentContext("cll")
173             ss.setServiceRole(serviceRole)
174
175             AAIResourcesClient client = getAAIClient()
176             AAIResourceUri uri =
177                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
178                             .customer(execution.getVariable("globalSubscriberId"))
179                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
180                             .serviceInstance(cllId))
181             client.create(uri, ss)
182         } catch (BpmnError e) {
183             throw e
184         } catch (Exception ex) {
185             String msg = "Exception in DoCloudLeasedLineCreate.createServiceInstance: " + ex.getMessage()
186             logger.error(msg)
187             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
188         }
189     }
190
191
192     void createAllottedResource(DelegateExecution execution) {
193         String cllId = execution.getVariable('cllId')
194
195         try {
196             List<String> networkStrList = jsonUtil.StringArrayToList(execution.getVariable("transportNetworks"))
197
198             for (String networkStr : networkStrList) {
199                 String networkId = jsonUtil.getJsonValue(networkStr, "id")
200                 String allottedResourceId = isBlank(networkId) ? UUID.randomUUID().toString() : networkId
201
202                 AAIResourceUri allottedResourceUri =
203                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
204                                 .customer(execution.getVariable("globalSubscriberId"))
205                                 .serviceSubscription(execution.getVariable("subscriptionServiceType"))
206                                 .serviceInstance(execution.getVariable("cllId"))
207                                 .allottedResource(allottedResourceId))
208                 execution.setVariable("allottedResourceUri", allottedResourceUri)
209                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
210                 String modelVersionId = execution.getVariable("modelUuid")
211
212                 String slaStr = jsonUtil.getJsonValue(networkStr, "sla")
213                 if (slaStr == null || slaStr.isEmpty()) {
214                     String msg = "ERROR: createNetworkPolicy: SLA is null"
215                     logger.error(msg)
216                     exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
217                 }
218
219                 org.onap.aai.domain.yang.AllottedResource resource = new org.onap.aai.domain.yang.AllottedResource()
220                 resource.setId(allottedResourceId)
221                 resource.setType("TsciNetwork")
222                 resource.setAllottedResourceName("network_" + allottedResourceId)
223                 getAAIClient().create(allottedResourceUri, resource)
224
225                 createNetworkPolicyForAllocatedResource(execution, cllId, allottedResourceId, slaStr)
226
227                 String linkArrayStr = jsonUtil.getJsonValue(networkStr, "connectionLinks")
228                 createLogicalLinksForAllocatedResource(execution, linkArrayStr, cllId, allottedResourceId)
229             }
230         } catch (BpmnError e) {
231             throw e
232         } catch (Exception ex) {
233             String msg = "Exception in DoCloudLeasedLineCreate.createAllottedResource: " + ex.getMessage()
234             logger.error(msg)
235             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
236         }
237     }
238
239     void createNetworkPolicy(DelegateExecution execution, String cllId, String networkPolicyId, String slaStr) {
240         try {
241
242
243             NetworkPolicy networkPolicy = new NetworkPolicy();
244             networkPolicy.setNetworkPolicyId(networkPolicyId)
245             networkPolicy.setName("TSCi policy")
246             networkPolicy.setType("SLA")
247             networkPolicy.setNetworkPolicyFqdn(cllId)
248
249             String latencyStr = jsonUtil.getJsonValue(slaStr, "latency")
250             if (latencyStr != null && !latencyStr.isEmpty()) {
251                 networkPolicy.setLatency(Integer.parseInt(latencyStr))
252             }
253
254             String bwStr = jsonUtil.getJsonValue(slaStr, "maxBandwidth")
255             if (bwStr != null && !bwStr.isEmpty()) {
256                 networkPolicy.setMaxBandwidth(Integer.parseInt(bwStr))
257             } else {
258                 logger.debug("ERROR: createNetworkPolicy: maxBandwidth is null")
259             }
260
261             //networkPolicy.setReliability(new Object())
262
263             AAIResourceUri networkPolicyUri =
264                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().networkPolicy(networkPolicyId))
265             getAAIClient().create(networkPolicyUri, networkPolicy)
266
267         } catch (BpmnError e) {
268             throw e
269         } catch (Exception ex) {
270             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
271             logger.error(msg)
272             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
273         }
274     }
275
276     void createNetworkPolicyForAllocatedResource(DelegateExecution execution,
277                                                  String cllId,
278                                                  String allottedResourceId, String slaStr) {
279         try {
280             AAIResourceUri allottedResourceUri =
281                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
282                             .customer(execution.getVariable("globalSubscriberId"))
283                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
284                             .serviceInstance(cllId)
285                             .allottedResource(allottedResourceId))
286
287             if (!getAAIClient().exists(allottedResourceUri)) {
288                 logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
289                         allottedResourceUri)
290                 return
291             }
292
293             String networkPolicyId = UUID.randomUUID().toString()
294             createNetworkPolicy(execution, cllId, networkPolicyId, slaStr)
295
296             serviceIntentUtils.attachNetworkPolicyToAllottedResource(execution, serviceIntentUtils.AAI_VERSION,
297                     allottedResourceUri,
298                     networkPolicyId);
299
300         } catch (BpmnError e) {
301             throw e
302         } catch (Exception ex) {
303             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
304             logger.error(msg)
305             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
306         }
307     }
308
309     void createLogicalLinksForAllocatedResource(DelegateExecution execution,
310                                                 String linkArrayStr, String cllId,
311                                                 String allottedResourceId) {
312         try {
313             AAIResourceUri allottedResourceUri =
314                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
315                             .customer(execution.getVariable("globalSubscriberId"))
316                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
317                             .serviceInstance(cllId)
318                             .allottedResource(allottedResourceId))
319
320             if (!getAAIClient().exists(allottedResourceUri)) {
321                 logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
322                         allottedResourceUri)
323                 return
324             }
325
326             List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayStr)
327
328             for (String linkStr : linkStrList) {
329                 String linkId = jsonUtil.getJsonValue(linkStr, "name")
330                 if (isBlank(linkId)) {
331                     linkId = "cll-" + UUID.randomUUID().toString()
332                 }
333                 logger.debug("createLogicalLinksForAllocatedResource: linkId=" + linkId)
334
335                 String epA = jsonUtil.getJsonValue(linkStr, "transportEndpointA")
336                 String epB = jsonUtil.getJsonValue(linkStr, "transportEndpointB")
337                 String epBProtect = jsonUtil.getJsonValue(linkStr, "transportEndpointBProtection")
338                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
339                 String modelVersionId = execution.getVariable("modelUuid")
340
341                 org.onap.aai.domain.yang.LogicalLink resource = new org.onap.aai.domain.yang.LogicalLink()
342                 resource.setLinkId(linkId)
343                 resource.setLinkName(epA)
344                 resource.setLinkName2(epB)
345                 if (isNotBlank(epBProtect)) {
346                     resource.setSegmentId(epBProtect)
347                 }
348                 resource.setLinkType("TsciConnectionLink")
349                 resource.setInMaint(false)
350
351                 //epA is link-name
352                 AAIResourceUri logicalLinkUri =
353                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().logicalLink(epA))
354                 getAAIClient().create(logicalLinkUri, resource)
355
356                 serviceIntentUtils.attachLogicalLinkToAllottedResource(execution, serviceIntentUtils.AAI_VERSION,
357                         allottedResourceUri, epA);
358             }
359         } catch (BpmnError e) {
360             throw e
361         } catch (Exception ex) {
362             String msg = "Exception in DoCloudLeasedLineCreate.createLogicalLinksForAllocatedResource: " + ex.getMessage()
363             logger.error(msg)
364             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
365         }
366     }
367
368     void preprocessSdncCreateCllRequest(DelegateExecution execution) {
369         def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
370                 'execution=' + execution.getId() +
371                 ')'
372         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
373         logger.trace('Entered ' + method)
374
375         logger.trace("STARTED preProcessSDNCActivateRequest Process")
376         try {
377             String serviceInstanceId = execution.getVariable("cllId")
378
379             String createSDNCRequest = serviceIntentUtils.buildSDNCRequest(execution, serviceInstanceId, "create")
380
381             execution.setVariable("CLL_SDNCRequest", createSDNCRequest)
382             logger.debug("Outgoing SDNCRequest is: \n" + createSDNCRequest)
383
384         } catch (Exception e) {
385             logger.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e)
386             exceptionUtil.buildAndThrowWorkflowException(execution, 1002,
387                     "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
388         }
389         logger.trace("COMPLETED  preProcessSDNCActivateRequest Process")
390     }
391
392
393     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
394         serviceIntentUtils.validateSDNCResponse(execution, response, method)
395     }
396 }