48e1acd52395be59cf5d5219a1fb75cca2ebad84
[so.git] /
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 serviceType = execution.getVariable("serviceType")
104         Map<String, Object> serviceProfile = sliceParams.getServiceProfile()
105         String ssInstanceId = execution.getVariable("serviceInstanceId")
106         try {
107             ServiceInstance ss = new ServiceInstance()
108             ss.setServiceInstanceId(ssInstanceId)
109             String sliceInstanceName = execution.getVariable("serviceInstanceName")
110             ss.setServiceInstanceName(sliceInstanceName)
111             ss.setServiceType(serviceType)
112             String serviceStatus = "deactivated"
113             ss.setOrchestrationStatus(serviceStatus)
114             String modelInvariantUuid = modelInfo.getModelInvariantUuid()
115             String modelUuid = modelInfo.getModelUuid()
116             ss.setModelInvariantId(modelInvariantUuid)
117             ss.setModelVersionId(modelUuid)
118             String serviceInstanceLocationid = serviceProfile.get("plmnIdList")
119             ss.setServiceInstanceLocationId(serviceInstanceLocationid)
120             String snssai = serviceProfile.get("sNSSAI")
121             ss.setEnvironmentContext(snssai)
122             ss.setServiceRole(serviceRole)
123
124             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(ssInstanceId))
125             client.create(uri, ss)
126         } catch (BpmnError e) {
127             throw e
128         } catch (Exception ex) {
129             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
130             logger.info(msg)
131             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
132         }
133
134
135         RollbackData rollbackData = execution.getVariable("RollbackData")
136         if (rollbackData == null) {
137             rollbackData = new RollbackData()
138         }
139         //rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
140         rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
141         rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", ssInstanceId)
142         rollbackData.put("SERVICEINSTANCE", "subscriptionServiceType", execution.getVariable("subscriptionServiceType"))
143         rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", execution.getVariable("globalSubscriberId"))
144         execution.setVariable("rollbackData", rollbackData)
145         execution.setVariable("RollbackData", rollbackData)
146         logger.debug("RollbackData:" + rollbackData)
147
148     }
149
150     /**
151      * create service profile in aai
152      * @param execution
153      */
154     void createServiceProfile(DelegateExecution execution) {
155
156         /**
157          * todo: ServiceProfile params changed
158          */
159         SliceTaskParamsAdapter sliceParams =
160                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
161         Map<String, Object> serviceProfileMap = sliceParams.getServiceProfile()
162
163         String serviceProfileInstanceId = execution.getVariable("serviceInstanceId")
164         String serviceProfileId = UUID.randomUUID().toString()
165         sliceParams.serviceProfile.put("profileId", serviceProfileId)
166
167         ServiceProfile serviceProfile = new ServiceProfile()
168         serviceProfile.setProfileId(serviceProfileId)
169         serviceProfile.setLatency(Integer.parseInt(serviceProfileMap.get("latency").toString()))
170         serviceProfile.setMaxNumberOfUEs(Integer.parseInt(serviceProfileMap.get("maxNumberofUEs").toString()))
171         serviceProfile.setCoverageAreaTAList(serviceProfileMap.get("coverageAreaTAList").toString())
172         serviceProfile.setUeMobilityLevel(serviceProfileMap.get("uEMobilityLevel").toString())
173         serviceProfile.setResourceSharingLevel(serviceProfileMap.get("resourceSharingLevel").toString())
174         serviceProfile.setDlThptPerSlice(Integer.parseInt(serviceProfileMap.get("dLThptPerSlice").toString()))
175         serviceProfile.setDlThptPerUE(Integer.parseInt(serviceProfileMap.get("dLThptPerUE").toString()))
176         serviceProfile.setUlThptPerSlice(Integer.parseInt(serviceProfileMap.get("uLThptPerSlice").toString()))
177         serviceProfile.setUlThptPerUE(Integer.parseInt(serviceProfileMap.get("uLThptPerUE").toString()))
178         serviceProfile.setActivityFactor(Integer.parseInt(serviceProfileMap.get("activityFactor").toString()))
179
180         serviceProfile.setJitter(Integer.parseInt(serviceProfileMap.get("jitter").toString()))
181         serviceProfile.setSurvivalTime("0")
182         serviceProfile.setReliability("")
183         try {
184             AAIResourceUri uri = AAIUriFactory.createResourceUri(
185                 AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId"))
186                 .serviceSubscription(execution.getVariable("subscriptionServiceType"))
187                 .serviceInstance(serviceProfileInstanceId)
188                 .serviceProfile(serviceProfileId))
189             client.create(uri, serviceProfile)
190             execution.setVariable("sliceTaskParams", sliceParams)
191
192         } catch (BpmnError e) {
193             throw e
194         } catch (Exception ex) {
195             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
196             logger.info(msg)
197             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
198         }
199     }
200
201     /**
202      * create allotted resource
203      * todo: unfinished
204      * @param execution
205      */
206     public void createAllottedResource(DelegateExecution execution) {
207
208         try {
209
210             ServiceDecomposition serviceDecomposition =
211                     execution.getVariable("serviceProfileDecomposition") as ServiceDecomposition
212
213             List<org.onap.so.bpmn.core.domain.AllottedResource> allottedResourceList = serviceDecomposition.getAllottedResources()
214             for(org.onap.so.bpmn.core.domain.AllottedResource allottedResource : allottedResourceList) {
215                 String allottedResourceId = UUID.randomUUID().toString()
216
217                 AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(execution.getVariable("globalSubscriberId")).serviceSubscription(execution.getVariable("subscriptionServiceType")).serviceInstance(execution.getVariable("serviceInstanceId")).allottedResource(allottedResourceId))
218
219                 execution.setVariable("allottedResourceUri", allottedResourceUri)
220                 String arType = allottedResource.getAllottedResourceType()
221                 String arRole = allottedResource.getAllottedResourceRole()
222                 String modelInvariantId = allottedResource.getModelInfo().getModelInvariantUuid()
223                 String modelVersionId = allottedResource.getModelInfo().getModelUuid()
224
225                 AllottedResource resource = new AllottedResource()
226                 resource.setId(allottedResourceId)
227                 resource.setType(arType)
228                 resource.setAllottedResourceName("Allotted_"+ execution.getVariable("serviceInstanceName"))
229                 resource.setRole(arRole)
230                 resource.setModelInvariantId(modelInvariantId)
231                 resource.setModelVersionId(modelVersionId)
232
233                 client.create(allottedResourceUri, resource)
234
235             }
236
237         }catch (Exception ex) {
238             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception in createAaiAR " + ex.getMessage())
239         }
240     }
241
242 }