Merge "Implement Deallocate Core NSSMF workflow"
[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.AAIObjectType
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.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     JsonUtils jsonUtil = new JsonUtils()
40     TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
41     ExceptionUtil exceptionUtil = new ExceptionUtil()
42     String Prefix = "DCTN_"
43
44     void preProcessRequest(DelegateExecution execution) {
45         String msg = ""
46         logger.trace("Enter preProcessRequest()")
47
48         execution.setVariable("prefix", Prefix)
49
50         String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
51         String modelUuid = execution.getVariable("modelUuid")
52         //here modelVersion is not set, we use modelUuid to decompose the service.
53         def isDebugLogEnabled = true
54         execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
55         String serviceModelInfo = """{
56             "modelInvariantUuid":"${modelInvariantUuid}",
57             "modelUuid":"${modelUuid}",
58             "modelVersion":""
59              }"""
60         execution.setVariable("serviceModelInfo", serviceModelInfo)
61
62         logger.trace("Exit preProcessRequest")
63     }
64
65
66     void createSliceProfile(DelegateExecution execution) {
67
68         String sliceserviceInstanceId = execution.getVariable("sliceServiceInstanceId")
69         String sliceProfileStr = execution.getVariable("sliceProfile")
70         String sliceProfileId = UUID.randomUUID().toString()
71         SliceProfile sliceProfile = new SliceProfile();
72         sliceProfile.setProfileId(sliceProfileId)
73         sliceProfile.setLatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
74         sliceProfile.setResourceSharingLevel(jsonUtil.getJsonValue(sliceProfileStr, "resourceSharingLevel"))
75         sliceProfile.setSNssai(tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr))    //TODO: should be list
76
77         sliceProfile.setE2ELatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
78         sliceProfile.setMaxBandwidth(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")))
79
80         //TODO: new API
81         sliceProfile.setReliability(new Object())
82         try {
83             AAIResourcesClient client = getAAIClient()
84             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SLICE_PROFILE, execution.getVariable
85                     ("globalSubscriberId"),
86                     execution.getVariable("subscriptionServiceType"), sliceserviceInstanceId, sliceProfileId)
87             client.create(uri, sliceProfile)
88
89         } catch (BpmnError e) {
90             throw e
91         } catch (Exception ex) {
92             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
93             logger.info(msg)
94             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
95         }
96     }
97
98
99     void createServiceInstance(DelegateExecution execution) {
100
101         String serviceRole = "TN"
102         String serviceType = execution.getVariable("subscriptionServiceType")
103         String ssInstanceId = execution.getVariable("sliceServiceInstanceId")
104         String sliceProfileStr = execution.getVariable("sliceProfile")
105         try {
106             org.onap.aai.domain.yang.ServiceInstance ss = new org.onap.aai.domain.yang.ServiceInstance()
107             ss.setServiceInstanceId(ssInstanceId)
108             String sliceInstanceName = execution.getVariable("sliceServiceInstanceName")
109             ss.setServiceInstanceName(sliceInstanceName)
110             ss.setServiceType(serviceType)
111             String serviceStatus = "allocated"
112             ss.setOrchestrationStatus(serviceStatus)
113             String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
114             String modelUuid = execution.getVariable("modelUuid")
115             ss.setModelInvariantId(modelInvariantUuid)
116             ss.setModelVersionId(modelUuid)
117             String serviceInstanceLocationid = tnNssmfUtils.getFirstPlmnIdFromSliceProfile(sliceProfileStr)
118             ss.setServiceInstanceLocationId(serviceInstanceLocationid)
119             String snssai = tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr)
120             ss.setEnvironmentContext(snssai)
121             ss.setServiceRole(serviceRole)
122             AAIResourcesClient client = getAAIClient()
123             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, execution.getVariable("globalSubscriberId"), execution.getVariable("subscriptionServiceType"), ssInstanceId)
124             client.create(uri, ss)
125         } catch (BpmnError e) {
126             throw e
127         } catch (Exception ex) {
128             String msg = "Exception in DoCreateTnNssiInstance.createServiceInstance. " + ex.getMessage()
129             logger.info(msg)
130             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
131         }
132     }
133
134
135     void createAllottedResource(DelegateExecution execution) {
136         String serviceInstanceId = execution.getVariable('sliceServiceInstanceId')
137
138         AAIResourcesClient resourceClient = getAAIClient()
139         AAIResourceUri ssServiceuri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
140
141         try {
142             List<String> networkStrList = jsonUtil.StringArrayToList(execution.getVariable("transportSliceNetworks"))
143
144             for (String networkStr : networkStrList) {
145                 String allottedResourceId = UUID.randomUUID().toString()
146                 AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIObjectType.ALLOTTED_RESOURCE,
147                         execution.getVariable("globalSubscriberId"), execution.getVariable("subscriptionServiceType"),
148                         execution.getVariable("sliceserviceInstanceId"), allottedResourceId)
149                 execution.setVariable("allottedResourceUri", allottedResourceUri)
150                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
151                 String modelVersionId = execution.getVariable("modelUuid")
152
153                 org.onap.aai.domain.yang.AllottedResource resource = new org.onap.aai.domain.yang.AllottedResource()
154                 resource.setId(allottedResourceId)
155                 resource.setType("TsciNetwork")
156                 resource.setAllottedResourceName("network_" + execution.getVariable("sliceServiceInstanceName"))
157                 resource.setModelInvariantId(modelInvariantId)
158                 resource.setModelVersionId(modelVersionId)
159                 getAAIClient().create(allottedResourceUri, resource)
160                 //AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceFromExistingURI(AAIObjectType.SERVICE_INSTANCE, UriBuilder.fromPath(ssServiceuri).build())
161                 //getAAIClient().connect(allottedResourceUri,ssServiceuri)
162                 //execution.setVariable("aaiARPath", allottedResourceUri.build().toString());
163
164                 String linkArrayStr = jsonUtil.getJsonValue(networkStr, "connectionLinks")
165                 createLogicalLinksForAllocatedResource(execution, linkArrayStr, serviceInstanceId, allottedResourceId)
166             }
167
168         } catch (Exception ex) {
169             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception in createAaiAR " + ex.getMessage())
170         }
171     }
172
173     void createLogicalLinksForAllocatedResource(DelegateExecution execution,
174                                                 String linkArrayStr, String serviceInstanceId,
175                                                 String allottedResourceId) {
176
177         try {
178             List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayStr)
179
180             for (String linkStr : linkStrList) {
181                 String logicalLinkId = UUID.randomUUID().toString()
182                 String epA = jsonUtil.getJsonValue(linkStr, "transportEndpointA")
183                 String epB = jsonUtil.getJsonValue(linkStr, "transportEndpointB")
184                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
185                 String modelVersionId = execution.getVariable("modelUuid")
186
187                 org.onap.aai.domain.yang.LogicalLink resource = new org.onap.aai.domain.yang.LogicalLink()
188                 resource.setLinkId(logicalLinkId)
189                 resource.setLinkName(epA)
190                 resource.setLinkName2(epB)
191                 resource.setModelInvariantId(modelInvariantId)
192                 resource.setModelVersionId(modelVersionId)
193
194                 AAIResourceUri logicalLinkUri = AAIUriFactory.createResourceUri(AAIObjectType.LOGICAL_LINK, logicalLinkId)
195                 getAAIClient().create(logicalLinkUri, resource)
196             }
197         } catch (Exception ex) {
198             exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
199                     "Exception in createLogicalLinksForAllocatedResource" + ex.getMessage())
200         }
201     }
202
203     void preprocessSdncAllocateTnNssiRequest(DelegateExecution execution) {
204         def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
205                 'execution=' + execution.getId() +
206                 ')'
207         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
208         logger.trace('Entered ' + method)
209
210         logger.trace("STARTED preProcessSDNCActivateRequest Process")
211         try {
212             String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")
213
214             String createSDNCRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, "create")
215
216             execution.setVariable("TNNSSMF_SDNCRequest", createSDNCRequest)
217             logger.debug("Outgoing SDNCRequest is: \n" + createSDNCRequest)
218
219         } catch (Exception e) {
220             logger.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e)
221             exceptionUtil.buildAndThrowWorkflowException(execution, 1002,
222                     "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
223         }
224         logger.trace("COMPLETED  preProcessSDNCActivateRequest Process")
225     }
226
227
228     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
229         tnNssmfUtils.validateSDNCResponse(execution, response, method)
230     }
231 }