Merge "Changed the object mapper to ignore unknown fields"
[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.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
31 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
32 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
33 import org.onap.so.bpmn.common.scripts.ExceptionUtil
34 import org.onap.so.bpmn.core.json.JsonUtils
35 import org.slf4j.Logger
36 import org.slf4j.LoggerFactory
37
38 class DoCreateTnNssiInstance extends AbstractServiceTaskProcessor {
39
40     private static final Logger logger = LoggerFactory.getLogger(DoCreateTnNssiInstance.class);
41     JsonUtils jsonUtil = new JsonUtils()
42     TnNssmfUtils tnNssmfUtils = new TnNssmfUtils()
43     ExceptionUtil exceptionUtil = new ExceptionUtil()
44     String Prefix = "DCTN_"
45
46     void preProcessRequest(DelegateExecution execution) {
47         String msg = ""
48         logger.trace("Enter preProcessRequest()")
49
50         execution.setVariable("prefix", Prefix)
51
52         String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
53         String modelUuid = execution.getVariable("modelUuid")
54         //here modelVersion is not set, we use modelUuid to decompose the service.
55         def isDebugLogEnabled = true
56         execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
57         String serviceModelInfo = """{
58             "modelInvariantUuid":"${modelInvariantUuid}",
59             "modelUuid":"${modelUuid}",
60             "modelVersion":""
61              }"""
62         execution.setVariable("serviceModelInfo", serviceModelInfo)
63
64         logger.trace("Exit preProcessRequest")
65     }
66
67
68     void createSliceProfile(DelegateExecution execution) {
69
70         String sliceserviceInstanceId = execution.getVariable("sliceServiceInstanceId")
71         String sliceProfileStr = execution.getVariable("sliceProfile")
72         String sliceProfileId = UUID.randomUUID().toString()
73         SliceProfile sliceProfile = new SliceProfile();
74         sliceProfile.setProfileId(sliceProfileId)
75         sliceProfile.setLatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
76         sliceProfile.setResourceSharingLevel(jsonUtil.getJsonValue(sliceProfileStr, "resourceSharingLevel"))
77         sliceProfile.setSNssai(tnNssmfUtils.getFirstSnssaiFromSliceProfile(sliceProfileStr))    //TODO: should be list
78
79         sliceProfile.setE2ELatency(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "latency")))
80         sliceProfile.setMaxBandwidth(Integer.parseInt(jsonUtil.getJsonValue(sliceProfileStr, "maxBandwidth")))
81
82         //TODO: new API
83         sliceProfile.setReliability(new Object())
84         try {
85             AAIResourcesClient client = getAAIClient()
86             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(sliceserviceInstanceId).sliceProfile(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(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(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(Types.SERVICE_INSTANCE.getFragment(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(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(execution.getVariable("sliceserviceInstanceId")).allottedResource(allottedResourceId))
147                 execution.setVariable("allottedResourceUri", allottedResourceUri)
148                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
149                 String modelVersionId = execution.getVariable("modelUuid")
150
151                 org.onap.aai.domain.yang.AllottedResource resource = new org.onap.aai.domain.yang.AllottedResource()
152                 resource.setId(allottedResourceId)
153                 resource.setType("TsciNetwork")
154                 resource.setAllottedResourceName("network_" + execution.getVariable("sliceServiceInstanceName"))
155                 resource.setModelInvariantId(modelInvariantId)
156                 resource.setModelVersionId(modelVersionId)
157                 getAAIClient().create(allottedResourceUri, resource)
158                 //AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceFromExistingURI(Types.SERVICE_INSTANCE, UriBuilder.fromPath(ssServiceuri).build())
159                 //getAAIClient().connect(allottedResourceUri,ssServiceuri)
160                 //execution.setVariable("aaiARPath", allottedResourceUri.build().toString());
161
162                 String linkArrayStr = jsonUtil.getJsonValue(networkStr, "connectionLinks")
163                 createLogicalLinksForAllocatedResource(execution, linkArrayStr, serviceInstanceId, allottedResourceId)
164             }
165
166         } catch (Exception ex) {
167             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception in createAaiAR " + ex.getMessage())
168         }
169     }
170
171     void createLogicalLinksForAllocatedResource(DelegateExecution execution,
172                                                 String linkArrayStr, String serviceInstanceId,
173                                                 String allottedResourceId) {
174
175         try {
176             List<String> linkStrList = jsonUtil.StringArrayToList(linkArrayStr)
177
178             for (String linkStr : linkStrList) {
179                 String logicalLinkId = UUID.randomUUID().toString()
180                 String epA = jsonUtil.getJsonValue(linkStr, "transportEndpointA")
181                 String epB = jsonUtil.getJsonValue(linkStr, "transportEndpointB")
182                 String modelInvariantId = execution.getVariable("modelInvariantUuid")
183                 String modelVersionId = execution.getVariable("modelUuid")
184
185                 org.onap.aai.domain.yang.LogicalLink resource = new org.onap.aai.domain.yang.LogicalLink()
186                 resource.setLinkId(logicalLinkId)
187                 resource.setLinkName(epA)
188                 resource.setLinkName2(epB)
189                 resource.setModelInvariantId(modelInvariantId)
190                 resource.setModelVersionId(modelVersionId)
191
192                 AAIResourceUri logicalLinkUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().logicalLink(logicalLinkId))
193                 getAAIClient().create(logicalLinkUri, resource)
194             }
195         } catch (Exception ex) {
196             exceptionUtil.buildAndThrowWorkflowException(execution, 7000,
197                     "Exception in createLogicalLinksForAllocatedResource" + ex.getMessage())
198         }
199     }
200
201     void preprocessSdncAllocateTnNssiRequest(DelegateExecution execution) {
202         def method = getClass().getSimpleName() + '.preProcessSDNCActivateRequest(' +
203                 'execution=' + execution.getId() +
204                 ')'
205         def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
206         logger.trace('Entered ' + method)
207
208         logger.trace("STARTED preProcessSDNCActivateRequest Process")
209         try {
210             String serviceInstanceId = execution.getVariable("sliceServiceInstanceId")
211
212             String createSDNCRequest = tnNssmfUtils.buildSDNCRequest(execution, serviceInstanceId, "create")
213
214             execution.setVariable("TNNSSMF_SDNCRequest", createSDNCRequest)
215             logger.debug("Outgoing SDNCRequest is: \n" + createSDNCRequest)
216
217         } catch (Exception e) {
218             logger.debug("Exception Occured Processing preProcessSDNCActivateRequest. Exception is:\n" + e)
219             exceptionUtil.buildAndThrowWorkflowException(execution, 1002,
220                     "Error Occured during  preProcessSDNCActivateRequest Method:\n" + e.getMessage())
221         }
222         logger.trace("COMPLETED  preProcessSDNCActivateRequest Process")
223     }
224
225
226     void validateSDNCResponse(DelegateExecution execution, String response, String method) {
227         tnNssmfUtils.validateSDNCResponse(execution, response, method)
228     }
229 }