Add feature to Support NSMF based TN slices
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateSliceServiceInstance.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  # Copyright (c) 2020, CMCC Technologies Co., Ltd.
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 package org.onap.so.bpmn.infrastructure.scripts
21
22
23 import org.camunda.bpm.engine.delegate.BpmnError
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.aai.domain.yang.AllottedResource
26 import org.onap.aai.domain.yang.ServiceInstance
27 import org.onap.aai.domain.yang.ServiceProfile;
28 import org.onap.aaiclient.client.aai.AAIResourcesClient
29 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
30 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
31 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
32 import org.onap.so.beans.nsmf.SliceTaskParamsAdapter
33 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
34 import org.onap.so.bpmn.common.scripts.ExceptionUtil
35 import org.onap.so.bpmn.core.RollbackData
36 import org.onap.so.bpmn.core.domain.ModelInfo
37 import org.onap.so.bpmn.core.domain.ServiceDecomposition
38 import org.onap.so.bpmn.core.json.JsonUtils
39 import org.slf4j.Logger
40 import org.slf4j.LoggerFactory
41
42
43
44 class DoCreateSliceServiceInstance extends AbstractServiceTaskProcessor{
45
46     private static final Logger logger = LoggerFactory.getLogger( DoCreateSliceServiceInstance.class)
47
48     JsonUtils jsonUtil = new JsonUtils()
49
50     ExceptionUtil exceptionUtil = new ExceptionUtil()
51
52     AAIResourcesClient client = getAAIClient()
53     /**
54      * Pre Process the BPMN Flow Request
55      * Inclouds:
56      * generate the nsOperationKey
57      * generate the nsParameters
58      */
59     void preProcessRequest (DelegateExecution execution) {
60         logger.trace("Enter preProcessRequest()")
61         //here modelVersion is not set, we use modelUuid to decompose the service.
62         def isDebugLogEnabled = true
63         execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
64
65         logger.trace("Exit preProcessRequest")
66     }
67
68     /**
69      * prepare decompose service profile instance template
70      * @param execution
71      */
72     public void prepareDecomposeService(DelegateExecution execution) {
73
74         String uuiRequest = execution.getVariable("uuiRequest")
75         String modelInvariantUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceInvariantUuid")
76         String modelUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceUuid")
77         String serviceModelInfo = """{
78             "modelInvariantUuid":"${modelInvariantUuid}",
79             "modelUuid":"${modelUuid}",
80             "modelVersion":""
81              }"""
82         execution.setVariable("serviceModelInfo", serviceModelInfo)
83     }
84
85     /**
86      * create service-profile instance in aai
87      * @param execution
88      */
89     void createServiceProfileInstance(DelegateExecution execution) {
90
91         SliceTaskParamsAdapter sliceParams =
92                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
93
94         ServiceDecomposition serviceDecomposition =
95                 execution.getVariable("serviceProfileDecomposition") as ServiceDecomposition
96         ModelInfo modelInfo = serviceDecomposition.getModelInfo()
97         //String serviceRole = "e2eslice-service"
98         /**
99          * todo: ServiceProfile params changed
100          * todo: role
101          */
102         String serviceRole = "service-profile"
103         String globalSubscriberId = execution.getVariable("globalSubscriberId")
104         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
105
106         Map<String, Object> serviceProfile = sliceParams.getServiceProfile()
107         String ssInstanceId = execution.getVariable("serviceInstanceId")
108         try {
109             ServiceInstance ss = new ServiceInstance()
110             ss.setServiceInstanceId(ssInstanceId)
111             String sliceInstanceName = execution.getVariable("serviceInstanceName")
112             ss.setServiceInstanceName(sliceInstanceName)
113             ss.setServiceType(serviceProfile.get("sST"))
114             String serviceStatus = "deactivated"
115             ss.setOrchestrationStatus(serviceStatus)
116             String modelInvariantUuid = modelInfo.getModelInvariantUuid()
117             String modelUuid = modelInfo.getModelUuid()
118             ss.setModelInvariantId(modelInvariantUuid)
119             ss.setModelVersionId(modelUuid)
120             String serviceInstanceLocationid = serviceProfile.get("pLMNIdList")
121             ss.setServiceInstanceLocationId(serviceInstanceLocationid)
122             String snssai = serviceProfile.get("sNSSAI")
123             ss.setEnvironmentContext(snssai)
124             ss.setServiceRole(serviceRole)
125
126             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
127                     .customer(globalSubscriberId)
128                     .serviceSubscription(subscriptionServiceType)
129                     .serviceInstance(ssInstanceId))
130             client.create(uri, ss)
131         } catch (BpmnError e) {
132             throw e
133         } catch (Exception ex) {
134             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
135             logger.info(msg)
136             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
137         }
138
139
140         RollbackData rollbackData = execution.getVariable("RollbackData")
141         if (rollbackData == null) {
142             rollbackData = new RollbackData()
143         }
144         //rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
145         rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
146         rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", ssInstanceId)
147         rollbackData.put("SERVICEINSTANCE", "subscriptionServiceType", subscriptionServiceType)
148         rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", globalSubscriberId)
149         execution.setVariable("rollbackData", rollbackData)
150         execution.setVariable("RollbackData", rollbackData)
151         logger.debug("RollbackData:" + rollbackData)
152
153     }
154
155     /**
156      * create service profile in aai
157      * @param execution
158      */
159     void createServiceProfile(DelegateExecution execution) {
160
161         /**
162          * todo: ServiceProfile params changed
163          */
164         String globalSubscriberId = execution.getVariable("globalSubscriberId")
165         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
166         SliceTaskParamsAdapter sliceParams =
167                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
168         Map<String, Object> serviceProfileMap = sliceParams.getServiceProfile()
169
170         String serviceProfileInstanceId = execution.getVariable("serviceInstanceId")
171         String serviceProfileId = UUID.randomUUID().toString()
172         sliceParams.serviceProfile.put("profileId", serviceProfileId)
173
174         ServiceProfile serviceProfile = new ServiceProfile()
175         serviceProfile.setProfileId(serviceProfileId)
176         serviceProfile.setLatency(Integer.parseInt(serviceProfileMap.get("latency").toString()))
177         serviceProfile.setMaxNumberOfUEs(Integer.parseInt(serviceProfileMap.get("maxNumberofUEs").toString()))
178         serviceProfile.setCoverageAreaTAList(serviceProfileMap.get("coverageAreaTAList").toString())
179         serviceProfile.setUeMobilityLevel(serviceProfileMap.get("uEMobilityLevel").toString())
180         serviceProfile.setResourceSharingLevel(serviceProfileMap.get("resourceSharingLevel").toString())
181         serviceProfile.setDlThptPerSlice(Integer.parseInt(serviceProfileMap.get("dLThptPerSlice").toString()))
182         serviceProfile.setDlThptPerUE(Integer.parseInt(serviceProfileMap.get("dLThptPerUE").toString()))
183         serviceProfile.setUlThptPerSlice(Integer.parseInt(serviceProfileMap.get("uLThptPerSlice").toString()))
184         serviceProfile.setUlThptPerUE(Integer.parseInt(serviceProfileMap.get("uLThptPerUE").toString()))
185         serviceProfile.setActivityFactor(Integer.parseInt(serviceProfileMap.get("activityFactor").toString()))
186         serviceProfile.setMaxNumberOfConns(Integer.parseInt(serviceProfileMap.get("maxNumberofConns").toString()))
187
188         serviceProfile.setJitter(Integer.parseInt(serviceProfileMap.get("jitter").toString()))
189         serviceProfile.setSurvivalTime("0")
190         serviceProfile.setReliability("")
191         try {
192             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
193                     .customer(globalSubscriberId)
194                     .serviceSubscription(subscriptionServiceType)
195                     .serviceInstance(serviceProfileInstanceId)
196                     .serviceProfile(serviceProfileId))
197             client.create(uri, serviceProfile)
198             execution.setVariable("sliceTaskParams", sliceParams)
199
200         } catch (BpmnError e) {
201             throw e
202         } catch (Exception ex) {
203             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
204             logger.info(msg)
205             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
206         }
207     }
208
209     /**
210      * create allotted resource
211      * todo: unfinished
212      * @param execution
213      */
214     public void createAllottedResource(DelegateExecution execution) {
215
216         try {
217             String globalSubscriberId = execution.getVariable("globalSubscriberId")
218             String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
219             ServiceDecomposition serviceDecomposition =
220                     execution.getVariable("serviceProfileDecomposition") as ServiceDecomposition
221             String serviceInstanceId = execution.getVariable("serviceInstanceId")
222
223             List<org.onap.so.bpmn.core.domain.AllottedResource> allottedResourceList = serviceDecomposition.getAllottedResources()
224             for(org.onap.so.bpmn.core.domain.AllottedResource allottedResource : allottedResourceList) {
225                 String allottedResourceId = UUID.randomUUID().toString()
226
227                 AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
228                         .customer(globalSubscriberId)
229                         .serviceSubscription(subscriptionServiceType)
230                         .serviceInstance(serviceInstanceId)
231                         .allottedResource(allottedResourceId))
232
233                 execution.setVariable("allottedResourceUri", allottedResourceUri)
234                 String arType = allottedResource.getAllottedResourceType()
235                 String arRole = allottedResource.getAllottedResourceRole()
236                 String modelInvariantId = allottedResource.getModelInfo().getModelInvariantUuid()
237                 String modelVersionId = allottedResource.getModelInfo().getModelUuid()
238
239                 AllottedResource resource = new AllottedResource()
240                 resource.setId(allottedResourceId)
241                 resource.setType(arType)
242                 resource.setAllottedResourceName("Allotted_"+ execution.getVariable("serviceInstanceName"))
243                 resource.setRole(arRole)
244                 resource.setModelInvariantId(modelInvariantId)
245                 resource.setModelVersionId(modelVersionId)
246
247                 client.create(allottedResourceUri, resource)
248
249                 execution.setVariable("allottedResourceId", allottedResourceId)
250             }
251
252         }catch (Exception ex) {
253             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception in createAaiAR " + ex.getMessage())
254         }
255     }
256
257 }