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