ff16184f0296acaa36c5bd081c3cc78e8a621ebd
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020  Telecom Italia
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 com.fasterxml.jackson.databind.ObjectMapper
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.aai.domain.yang.v19.AllottedResource
26 import org.onap.aai.domain.yang.v19.AllottedResources
27 import org.onap.aai.domain.yang.v19.ServiceInstance
28 import org.onap.aai.domain.yang.v19.SliceProfile
29 import org.onap.aai.domain.yang.v19.SliceProfiles
30 import org.onap.aaiclient.client.aai.AAIResourcesClient
31 import org.onap.aaiclient.client.aai.entities.AAIEdgeLabel
32 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
33 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
34 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
35 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
36 import org.onap.so.bpmn.common.scripts.ExceptionUtil
37 import org.onap.so.bpmn.common.scripts.MsoUtils
38 import org.onap.so.bpmn.common.scripts.RequestDBUtil
39 import org.onap.so.bpmn.core.json.JsonUtils
40 import org.slf4j.Logger
41 import org.slf4j.LoggerFactory
42
43 import static org.apache.commons.lang3.StringUtils.isAllLowerCase
44 import static org.apache.commons.lang3.StringUtils.isBlank
45
46 class DoModifyCoreNSSI extends DoCommonCoreNSSI {
47
48     private final String PREFIX ="DoModifyCoreNSSI"
49     private final String ACTION = "Modify"
50
51     private ExceptionUtil exceptionUtil = new ExceptionUtil()
52     private RequestDBUtil requestDBUtil = new RequestDBUtil()
53     private MsoUtils utils = new MsoUtils()
54     private JsonUtils jsonUtil = new JsonUtils()
55
56     private static final Logger LOGGER = LoggerFactory.getLogger( DoModifyCoreNSSI.class)
57
58
59     /**
60      * Prepares Slice Profile
61      * @param execution
62      * @return SLice Profile
63      */
64     SliceProfile prepareSliceProfile(DelegateExecution execution) {
65         def currentNSSI = execution.getVariable("currentNSSI")
66
67         String sliceProfileID = currentNSSI['sliceProfileId']
68         Map<String,Object> sliceProfileMap = new ObjectMapper().readValue(currentNSSI['sliceProfile'], Map.class)
69
70         SliceProfile sliceProfile = new SliceProfile()
71         sliceProfile.setServiceAreaDimension("")
72         sliceProfile.setPayloadSize(0)
73         sliceProfile.setJitter(0)
74         sliceProfile.setSurvivalTime(0)
75         sliceProfile.setExpDataRate(0)
76         sliceProfile.setTrafficDensity(0)
77         sliceProfile.setConnDensity(0)
78         sliceProfile.setSNssai(currentNSSI['S-NSSAI'])
79
80         if(!isBlank(sliceProfileMap.get("expDataRateUL"))) {
81             sliceProfile.setExpDataRateUL(Integer.parseInt(sliceProfileMap.get("expDataRateUL").toString()))
82         }
83
84         if(!isBlank(sliceProfileMap.get("expDataRateDL"))) {
85             sliceProfile.setExpDataRateDL(Integer.parseInt(sliceProfileMap.get("expDataRateDL").toString()))
86         }
87
88         if(!isBlank(sliceProfileMap.get("activityFactor"))) {
89             sliceProfile.setActivityFactor(Integer.parseInt(sliceProfileMap.get("activityFactor").toString()))
90         }
91
92         if(!isBlank(sliceProfileMap.get("resourceSharingLevel"))) {
93             sliceProfile.setResourceSharingLevel(sliceProfileMap.get("resourceSharingLevel").toString())
94         }
95
96         if(!isBlank(sliceProfileMap.get("uEMobilityLevel"))) {
97             sliceProfile.setUeMobilityLevel(sliceProfileMap.get("uEMobilityLevel").toString())
98         }
99
100         if(!isBlank(sliceProfileMap.get("coverageAreaTAList"))) {
101             sliceProfile.setCoverageAreaTAList(sliceProfileMap.get("coverageAreaTAList").toString())
102         }
103
104         if(!isBlank(sliceProfileMap.get("maxNumberofUEs"))) {
105             sliceProfile.setMaxNumberOfUEs(Integer.parseInt(sliceProfileMap.get("maxNumberofUEs").toString()))
106         }
107
108         if(!isBlank(sliceProfileMap.get("latency"))) {
109             sliceProfile.setLatency(Integer.parseInt(sliceProfileMap.get("latency").toString()))
110         }
111
112         sliceProfile.setProfileId(sliceProfileID)
113         sliceProfile.setE2ELatency(0)
114
115         return sliceProfile
116     }
117
118
119     /**
120      * Prepares Slice Profile Instance
121      * @param execution
122      * @return Slice Profile Instance
123      */
124     ServiceInstance prepareSliceProfileInstance(DelegateExecution execution) {
125
126         def currentNSSI = execution.getVariable("currentNSSI")
127
128         ServiceInstance sliceProfileInstance = new ServiceInstance()
129         String sliceProfileInstanceId = UUID.randomUUID().toString()
130         sliceProfileInstance.setServiceInstanceId(sliceProfileInstanceId)
131
132
133         String sliceInstanceName = "sliceprofile_" + sliceProfileInstanceId
134         sliceProfileInstance.setServiceInstanceName(sliceInstanceName)
135
136         String serviceType = jsonUtil.getJsonValue(currentNSSI['sliceProfile'], "sST")
137         sliceProfileInstance.setServiceType(serviceType)
138
139         String serviceStatus = "deactivated"
140         sliceProfileInstance.setOrchestrationStatus(serviceStatus)
141
142         String serviceInstanceLocationid = jsonUtil.getJsonValue(currentNSSI['sliceProfile'], "plmnIdList")
143         sliceProfileInstance.setServiceInstanceLocationId(serviceInstanceLocationid)
144
145         String serviceRole = "slice-profile-instance"
146         sliceProfileInstance.setServiceRole(serviceRole)
147         List<String> snssaiList = (List<String>)currentNSSI['S-NSSAIs']
148         String snssai = snssaiList.get(0)
149
150         sliceProfileInstance.setEnvironmentContext(snssai)
151         sliceProfileInstance.setWorkloadContext("CN-NF")
152
153         // TO DO: Model info
154
155         return sliceProfileInstance
156     }
157
158
159
160     /**
161      * Creates Slice Profile Instance
162      * @param execution
163      */
164     void createSliceProfileInstance(DelegateExecution execution) {
165         LOGGER.trace("${PREFIX} Start createSliceProfileInstance")
166
167         def currentNSSI = execution.getVariable("currentNSSI")
168
169         String globalSubscriberId = execution.getVariable("globalSubscriberId")
170         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
171
172         SliceProfile sliceProfile = prepareSliceProfile(execution)
173
174         ServiceInstance sliceProfileInstance = prepareSliceProfileInstance(execution)
175
176         SliceProfiles sliceProfiles = new SliceProfiles()
177         sliceProfiles.getSliceProfile().add(sliceProfile)
178         sliceProfileInstance.setSliceProfiles(sliceProfiles)
179
180         try {
181             AAIResourcesClient client = getAAIClient()
182             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).
183                                                                 serviceInstance(sliceProfileInstance.getServiceInstanceId()))
184             client.create(uri, sliceProfileInstance)
185
186             currentNSSI['createdSliceProfileInstanceId'] = sliceProfileInstance.getServiceInstanceId()
187         } catch (Exception ex) {
188             exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occurred while Slice Profile create call:" + ex.getMessage())
189         }
190
191         LOGGER.trace("${PREFIX} Exit createSliceProfileInstance")
192     }
193
194
195     /**
196      * Creates Allotted Resource
197      * @param execution
198      * @return AllottedResource
199      */
200     AllottedResource createAllottedResource(DelegateExecution execution) {
201         def currentNSSI = execution.getVariable("currentNSSI")
202
203         String globalSubscriberId = execution.getVariable("globalSubscriberId")
204         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
205         String sliceProfileInstanceId = currentNSSI['createdSliceProfileInstanceId']
206
207         AllottedResource allottedResource = new AllottedResource()
208
209         String allottedResourceId = UUID.randomUUID().toString()
210
211         allottedResource.setId(allottedResourceId)
212
213         // TO DO: No other info
214
215         try {
216             AAIResourcesClient client = getAAIClient()
217             AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(globalSubscriberId).serviceSubscription(subscriptionServiceType).serviceInstance(sliceProfileInstanceId).allottedResource(allottedResourceId))
218
219             client.create(allottedResourceUri, allottedResource)
220
221             currentNSSI['allottedResourceUri'] = allottedResourceUri
222         } catch (Exception ex) {
223             exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occurred while Allotted Resource create call:" + ex.getMessage())
224         }
225
226         return allottedResource
227     }
228
229
230
231     /**
232      * Creates Slice Profile association with NSSI
233      * @param execution
234      */
235     void associateSliceProfileInstanceWithNSSI(DelegateExecution execution) {
236         LOGGER.trace("${PREFIX} Start associateSliceProfileInstanceWithNSSI")
237
238         def currentNSSI = execution.getVariable("currentNSSI")
239
240         String nssiId = currentNSSI['nssiId']
241
242         String sliceProfileInstanceId = currentNSSI['createdSliceProfileInstanceId']
243
244         AAIResourcesClient client = getAAIClient()
245
246         // Creates Allotted Resource
247         AllottedResource allottedResource = createAllottedResource(execution)
248         AAIResourceUri allottedResourceUri = (AAIResourceUri)currentNSSI['allottedResourceUri']
249
250         // Updates Slice Profile Instance with Allotted Resource
251         try {
252             AAIResourceUri sliceProfileInstanceUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(sliceProfileInstanceId))
253             Optional<ServiceInstance> sliceProfileInstanceOpt = client.get(ServiceInstance.class, sliceProfileInstanceUri)
254             if (sliceProfileInstanceOpt.isPresent()) {
255                 ServiceInstance sliceProfileInstance = sliceProfileInstanceOpt.get()
256
257                 AllottedResources allottedResources = sliceProfileInstance.getAllottedResources()
258                 if(allottedResources == null) {
259                     allottedResources = new AllottedResources()
260                 }
261
262                 allottedResources.getAllottedResource().add(allottedResource)
263                 sliceProfileInstance.setAllottedResources(allottedResources)
264
265                 client.update(sliceProfileInstanceUri, sliceProfileInstance)
266             }
267             else {
268                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "No slice profile instance found with id = " + sliceProfileInstanceId)
269             }
270
271         } catch(Exception e){
272             exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occured while Slice Profile Instance update call: " + e.getMessage())
273         }
274
275
276         // Associates NSSI with Allotted Resource
277         try {
278             AAIResourceUri nssiUri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(nssiId))
279             client.connect(nssiUri, allottedResourceUri, AAIEdgeLabel.USES)
280         } catch(Exception e){
281             exceptionUtil.buildAndThrowWorkflowException(execution, 25000, "Exception occured while NSSI with Allotted Resource connect call: " + e.getMessage())
282         }
283
284         LOGGER.trace("${PREFIX} Exit associateSliceProfileInstanceWithNSSI")
285     }
286
287
288     @Override
289     String getPrefix() {
290         return PREFIX
291     }
292
293
294     @Override
295     String getAction() {
296         return ACTION
297     }
298 }