fc80a9f65848ac07020f2b40570553a3735ddbc5
[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         String globalSubscriberId = execution.getVariable("globalSubscriberId")
105         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
106
107         Map<String, Object> serviceProfile = sliceParams.getServiceProfile()
108         String ssInstanceId = execution.getVariable("serviceInstanceId")
109         try {
110             ServiceInstance ss = new ServiceInstance()
111             ss.setServiceInstanceId(ssInstanceId)
112             String sliceInstanceName = execution.getVariable("serviceInstanceName")
113             ss.setServiceInstanceName(sliceInstanceName)
114             ss.setServiceType(serviceType)
115             String serviceStatus = "deactivated"
116             ss.setOrchestrationStatus(serviceStatus)
117             String modelInvariantUuid = modelInfo.getModelInvariantUuid()
118             String modelUuid = modelInfo.getModelUuid()
119             ss.setModelInvariantId(modelInvariantUuid)
120             ss.setModelVersionId(modelUuid)
121             String serviceInstanceLocationid = serviceProfile.get("plmnIdList")
122             ss.setServiceInstanceLocationId(serviceInstanceLocationid)
123             String snssai = serviceProfile.get("sNSSAI")
124             ss.setEnvironmentContext(snssai)
125             ss.setServiceRole(serviceRole)
126
127             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
128                     .customer(globalSubscriberId)
129                     .serviceSubscription(subscriptionServiceType)
130                     .serviceInstance(ssInstanceId))
131             client.create(uri, ss)
132         } catch (BpmnError e) {
133             throw e
134         } catch (Exception ex) {
135             String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
136             logger.info(msg)
137             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
138         }
139
140
141         RollbackData rollbackData = execution.getVariable("RollbackData")
142         if (rollbackData == null) {
143             rollbackData = new RollbackData()
144         }
145         //rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
146         rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
147         rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", ssInstanceId)
148         rollbackData.put("SERVICEINSTANCE", "subscriptionServiceType", subscriptionServiceType)
149         rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", globalSubscriberId)
150         execution.setVariable("rollbackData", rollbackData)
151         execution.setVariable("RollbackData", rollbackData)
152         logger.debug("RollbackData:" + rollbackData)
153
154     }
155
156     /**
157      * create service profile in aai
158      * @param execution
159      */
160     void createServiceProfile(DelegateExecution execution) {
161
162         /**
163          * todo: ServiceProfile params changed
164          */
165         String globalSubscriberId = execution.getVariable("globalSubscriberId")
166         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
167         SliceTaskParamsAdapter sliceParams =
168                 execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
169         Map<String, Object> serviceProfileMap = sliceParams.getServiceProfile()
170
171         String serviceProfileInstanceId = execution.getVariable("serviceInstanceId")
172         String serviceProfileId = UUID.randomUUID().toString()
173         sliceParams.serviceProfile.put("profileId", serviceProfileId)
174
175         ServiceProfile serviceProfile = new ServiceProfile()
176         serviceProfile.setProfileId(serviceProfileId)
177         serviceProfile.setLatency(Integer.parseInt(serviceProfileMap.get("latency").toString()))
178         serviceProfile.setMaxNumberOfUEs(Integer.parseInt(serviceProfileMap.get("maxNumberofUEs").toString()))
179         serviceProfile.setCoverageAreaTAList(serviceProfileMap.get("coverageAreaTAList").toString())
180         serviceProfile.setUeMobilityLevel(serviceProfileMap.get("uEMobilityLevel").toString())
181         serviceProfile.setResourceSharingLevel(serviceProfileMap.get("resourceSharingLevel").toString())
182         serviceProfile.setDlThptPerSlice(Integer.parseInt(serviceProfileMap.get("dLThptPerSlice").toString()))
183         serviceProfile.setDlThptPerUE(Integer.parseInt(serviceProfileMap.get("dLThptPerUE").toString()))
184         serviceProfile.setUlThptPerSlice(Integer.parseInt(serviceProfileMap.get("uLThptPerSlice").toString()))
185         serviceProfile.setUlThptPerUE(Integer.parseInt(serviceProfileMap.get("uLThptPerUE").toString()))
186         serviceProfile.setActivityFactor(Integer.parseInt(serviceProfileMap.get("activityFactor").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             }
250
251         }catch (Exception ex) {
252             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception in createAaiAR " + ex.getMessage())
253         }
254     }
255
256 }