Merge "Adding Helm type Issue-ID: SO-3080"
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateTnNssiInstance.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.SliceProfile
26 import org.onap.aaiclient.client.aai.AAIResourcesClient
27 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
28 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
29 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
30 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
31 import org.onap.so.bpmn.common.scripts.ExceptionUtil
32 import org.onap.so.bpmn.core.json.JsonUtils
33 import org.slf4j.Logger
34 import org.slf4j.LoggerFactory
35
36 class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
37
38     private static final Logger logger = LoggerFactory.getLogger(DoCreateTnNssiInstance.class);
39     final String AAI_VERSION = "v21"
40     JsonUtils jsonUtil = new JsonUtils()
41     TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
42     ExceptionUtil exceptionUtil = new ExceptionUtil()
43     String Prefix = "DCTN_"
44
45     void preProcessRequest(DelegateExecution execution) {
46         String msg = ""
47         logger.trace("Enter preProcessRequest()")
48
49         execution.setVariable("prefix", Prefix)
50
51         String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
52         String modelUuid = execution.getVariable("modelUuid")
53         //here modelVersion is not set, we use modelUuid to decompose the service.
54         def isDebugLogEnabled = true
55         execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
56         String serviceModelInfo = """{
57             "modelInvariantUuid":"${modelInvariantUuid}",
58             "modelUuid":"${modelUuid}",
59             "modelVersion":""
60              }"""
61         execution.setVariable("serviceModelInfo", serviceModelInfo)
62
63         logger.trace("Exit preProcessRequest")
64     }
65
66
67     void createSliceProfile(DelegateExecution execution) {
68
69         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
70         String sliceProfileStr = execution.getVariable("sliceProfile")
71         String sliceProfileId = UUID.randomUUID().toString()
72         SliceProfile sliceProfile = new SliceProfile();
73         sliceProfile.setProfileId(sliceProfileId)
74         sliceProfile.setLatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
75         sliceProfile.setResourceSharingLevel(jsonUtil.getJsonValue(sliceProfileStr, "resourceSharingLevel"))
76         //sliceProfile.setSNssai(tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr))
77
78         sliceProfile.setMaxBandwidth(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")))
79
80         //sliceProfile.setReliability(new Object())
81         try {
82             AAIResourcesClient client = getAAIClient()
83             AAIResourceUri uri =
84                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
85                             .customer(execution.getVariable("globalSubscriberId"))
86                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
87                             .serviceInstance(ssInstanceId)
88                             .sliceProfile(sliceProfileId))
89             client.create(uri, sliceProfile)
90
91         } catch (BpmnError e) {
92             throw e
93         } catch (Exception ex) {
94             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
95             logger.info(msg)
96             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
97         }
98     }
99
100
101     void createServiceInstance(DelegateExecution execution) {
102
103         String serviceRole = "TN"
104         String serviceType = execution.getVariable("subscriptionServiceType")
105         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
106         String sliceProfileStr = execution.getVariable("sliceProfile")
107         try {
108             org.onap.aai.domain.yang.ServiceInstance ss = new org.onap.aai.domain.yang.ServiceInstance()
109             ss.setServiceInstanceId(ssInstanceId)
110             String sliceInstanceName = execution.getVariable("sliceServiceInstanceName")
111             ss.setServiceInstanceName(sliceInstanceName)
112             ss.setServiceType(serviceType)
113             String serviceStatus = "allocated"
114             ss.setOrchestrationStatus(serviceStatus)
115             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
116             String modelUuid = execution.getVariable("modelUuid")
117             //TODO: need valid model ID from the caller, as AAI does not accept invalid IDs
118             //ss.setModelInvariantId(modelInvariantUuid)
119             //ss.setModelVersionId(modelUuid)
120             String serviceInstanceLocationid = tnNssmfUtils.getFirstPlmnIdFromSliceProfile(sliceProfileStr)
121             ss.setServiceInstanceLocationId(serviceInstanceLocationid)
122             String snssai = tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr)
123             ss.setEnvironmentContext(snssai)
124             ss.setServiceRole(serviceRole)
125             AAIResourcesClient client = getAAIClient()
126             AAIResourceUri uri =
127                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
128                             .customer(execution.getVariable("globalSubscriberId"))
129                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
130                             .serviceInstance(ssInstanceId))
131             client.create(uri, ss)
132         } catch (BpmnError e) {
133             throw e
134         } catch (Exception ex) {
135             String msg = "Exception in DoCreateTnNssiInstance.createServiceInstance: " + ex.getMessage()
136             logger.info(msg)
137             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
138         }
139     }
140
141
142     void createAllottedResource(DelegateExecution execution) {
143         String ssInstanceId = execution.getVariable('sliceServiceInstanceId')
144
145         try {
146             List<String> networkStrList = jsonUtil.StringArrayToList(execution.getVariable("transportSliceNetworks"))
147
148             for (String networkStr : networkStrList) {
149                 String allottedResourceId = UUID.randomUUID().toString()
150                 AAIResourceUri allottedResourceUri =
151                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
152                                 .customer(execution.getVariable("globalSubscriberId"))
153                                 .serviceSubscription(execution.getVariable("subscriptionServiceType"))
154                                 .serviceInstance(execution.getVariable("sliceServiceInstanceId"))
155                                 .allottedResource(allottedResourceId))
156                 execution.setVariable("allottedResourceUri", allottedResourceUri)
157                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
158                 String modelVersionId = execution.getVariable("modelUuid")
159
160                 org.onap.aai.domain.yang.AllottedResource resource = new org.onap.aai.domain.yang.AllottedResource()
161                 resource.setId(allottedResourceId)
162                 resource.setType("TsciNetwork")
163                 resource.setAllottedResourceName("network_" + execution.getVariable("sliceServiceInstanceName"))
164                 getAAIClient().create(allottedResourceUri, resource)
165
166                 String linkArrayStr = jsonUtil.getJsonValue(networkStr, "connectionLinks")
167                 createLogicalLinksForAllocatedResource(execution, linkArrayStr, ssInstanceId, allottedResourceId)
168             }
169         } catch (BpmnError e) {
170             throw e
171         } catch (Exception ex) {
172             String msg = "Exception in DoCreateTnNssiInstance.createAllottedResource: " + ex.getMessage()
173             logger.info(msg)
174             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
175         }
176     }
177
178     void createLogicalLinksForAllocatedResource(DelegateExecution execution,
179                                                 String linkArrayStr, String ssInstanceId,
180                                                 String allottedResourceId) {
181         try {
182             AAIResourceUri allottedResourceUri =
183                     AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
184                             .customer(execution.getVariable("globalSubscriberId"))
185                             .serviceSubscription(execution.getVariable("subscriptionServiceType"))
186                             .serviceInstance(ssInstanceId)
187                             .allottedResource(allottedResourceId))
188
189             if (!getAAIClient().exists(allottedResourceUri)) {
190                 logger.info("ERROR: createLogicalLinksForAllocatedResource: allottedResource not exist: uri={}",
191                         allottedResourceUri)
192                 return
193             }
194
195             List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayStr)
196
197             for (String linkStr : linkStrList) {
198                 String logicalLinkId = UUID.randomUUID().toString()
199                 String epA = jsonUtil.getJsonValue(linkStr, "transportEndpointA")
200                 String epB = jsonUtil.getJsonValue(linkStr, "transportEndpointB")
201                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
202                 String modelVersionId = execution.getVariable("modelUuid")
203
204                 org.onap.aai.domain.yang.LogicalLink resource = new org.onap.aai.domain.yang.LogicalLink()
205                 resource.setLinkId(logicalLinkId)
206                 resource.setLinkName(epA)
207                 resource.setLinkName2(epB)
208                 resource.setLinkType("TsciConnectionLink")
209                 resource.setInMaint(false)
210
211                 //epA is link-name
212                 AAIResourceUri logicalLinkUri =
213                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().logicalLink(epA))
214                 getAAIClient().create(logicalLinkUri, resource)
215
216                 tnNssmfUtils.attachLogicalLinkToAllottedResource(execution, AAI_VERSION, allottedResourceUri, epA);
217             }
218         } catch (BpmnError e) {
219             throw e
220         } catch (Exception ex) {
221             String msg = "Exception in DoCreateTnNssiInstance.createLogicalLinksForAllocatedResource: " + ex.getMessage()
222             logger.info(msg)
223             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
224         }
225     }
226
227     void preprocessSdncAllocateTnNssiRequest(DelegateExecution execution) {
228         def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
229                 'execution=' + execution.getId() +
230                 ')'
231         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
232         logger.trace('Entered ' + method)
233
234         logger.trace("STARTED preProcessSDNCActivateRequest Process")
235         try {
236             String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")
237
238             String createSDNCRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, "create")
239
240             execution.setVariable("TNNSSMF_SDNCRequest", createSDNCRequest)
241             logger.debug("Outgoing SDNCRequest is: \n" + createSDNCRequest)
242
243         } catch (Exception e) {
244             logger.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e)
245             exceptionUtil.buildAndThrowWorkflowException(execution, 1002,
246                     "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
247         }
248         logger.trace("COMPLETED  preProcessSDNCActivateRequest Process")
249     }
250
251
252     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
253         tnNssmfUtils.validateSDNCResponse(execution, response, method)
254     }
255 }