aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DoCreateSliceServiceInstance.groovy
blob: 5476cb5afa6a9369bb4b4ca9e56e3333a4682769 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 # Copyright (c) 2020, CMCC Technologies Co., Ltd.
 #
 # Licensed under the Apache License, Version 2.0 (the "License")
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
 #       http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.so.bpmn.infrastructure.scripts


import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.onap.aai.domain.yang.AllottedResource
import org.onap.aai.domain.yang.ServiceInstance
import org.onap.aai.domain.yang.ServiceProfile;
import org.onap.aaiclient.client.aai.AAIResourcesClient
import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
import org.onap.so.beans.nsmf.SliceTaskParamsAdapter
import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
import org.onap.so.bpmn.common.scripts.ExceptionUtil
import org.onap.so.bpmn.core.RollbackData
import org.onap.so.bpmn.core.domain.ModelInfo
import org.onap.so.bpmn.core.domain.ServiceDecomposition
import org.onap.so.bpmn.core.json.JsonUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory



class DoCreateSliceServiceInstance extends AbstractServiceTaskProcessor{

    private static final Logger logger = LoggerFactory.getLogger( DoCreateSliceServiceInstance.class)

    JsonUtils jsonUtil = new JsonUtils()

    ExceptionUtil exceptionUtil = new ExceptionUtil()

    AAIResourcesClient client = getAAIClient()
    /**
     * Pre Process the BPMN Flow Request
     * Inclouds:
     * generate the nsOperationKey
     * generate the nsParameters
     */
    void preProcessRequest (DelegateExecution execution) {
        logger.trace("Enter preProcessRequest()")
        //here modelVersion is not set, we use modelUuid to decompose the service.
        def isDebugLogEnabled = true
        execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)

        logger.trace("Exit preProcessRequest")
    }

    /**
     * prepare decompose service profile instance template
     * @param execution
     */
    public void prepareDecomposeService(DelegateExecution execution) {

        String uuiRequest = execution.getVariable("uuiRequest")
        String modelInvariantUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceInvariantUuid")
        String modelUuid = jsonUtil.getJsonValue(uuiRequest, "service.serviceUuid")
        String serviceModelInfo = """{
            "modelInvariantUuid":"${modelInvariantUuid}",
            "modelUuid":"${modelUuid}",
            "modelVersion":""
             }"""
        execution.setVariable("serviceModelInfo", serviceModelInfo)
    }

    /**
     * create service-profile instance in aai
     * @param execution
     */
    void createServiceProfileInstance(DelegateExecution execution) {

        SliceTaskParamsAdapter sliceParams =
                execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter

        ServiceDecomposition serviceDecomposition =
                execution.getVariable("serviceProfileDecomposition") as ServiceDecomposition
        ModelInfo modelInfo = serviceDecomposition.getModelInfo()
        //String serviceRole = "e2eslice-service"
        /**
         * todo: ServiceProfile params changed
         * todo: role
         */
        String serviceRole = "service-profile"
        String globalSubscriberId = execution.getVariable("globalSubscriberId")
        String subscriptionServiceType = execution.getVariable("subscriptionServiceType")

        Map<String, Object> serviceProfile = sliceParams.getServiceProfile()
        String ssInstanceId = execution.getVariable("serviceInstanceId")
        try {
            ServiceInstance ss = new ServiceInstance()
            ss.setServiceInstanceId(ssInstanceId)
            String sliceInstanceName = execution.getVariable("serviceInstanceName")
            ss.setServiceInstanceName(sliceInstanceName)
            ss.setServiceType(serviceProfile.get("sST"))
            String serviceStatus = "deactivated"
            ss.setOrchestrationStatus(serviceStatus)
            String modelInvariantUuid = modelInfo.getModelInvariantUuid()
            String modelUuid = modelInfo.getModelUuid()
            ss.setModelInvariantId(modelInvariantUuid)
            ss.setModelVersionId(modelUuid)
            String serviceInstanceLocationid = serviceProfile.get("plmnIdList")
            ss.setServiceInstanceLocationId(serviceInstanceLocationid)
            String snssai = serviceProfile.get("sNSSAI")
            ss.setEnvironmentContext(snssai)
            ss.setServiceRole(serviceRole)

            AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
                    .customer(globalSubscriberId)
                    .serviceSubscription(subscriptionServiceType)
                    .serviceInstance(ssInstanceId))
            client.create(uri, ss)
        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
            logger.info(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }


        RollbackData rollbackData = execution.getVariable("RollbackData")
        if (rollbackData == null) {
            rollbackData = new RollbackData()
        }
        //rollbackData.put("SERVICEINSTANCE", "disableRollback", disableRollback.toString())
        rollbackData.put("SERVICEINSTANCE", "rollbackAAI", "true")
        rollbackData.put("SERVICEINSTANCE", "serviceInstanceId", ssInstanceId)
        rollbackData.put("SERVICEINSTANCE", "subscriptionServiceType", subscriptionServiceType)
        rollbackData.put("SERVICEINSTANCE", "globalSubscriberId", globalSubscriberId)
        execution.setVariable("rollbackData", rollbackData)
        execution.setVariable("RollbackData", rollbackData)
        logger.debug("RollbackData:" + rollbackData)

    }

    /**
     * create service profile in aai
     * @param execution
     */
    void createServiceProfile(DelegateExecution execution) {

        /**
         * todo: ServiceProfile params changed
         */
        String globalSubscriberId = execution.getVariable("globalSubscriberId")
        String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
        SliceTaskParamsAdapter sliceParams =
                execution.getVariable("sliceTaskParams") as SliceTaskParamsAdapter
        Map<String, Object> serviceProfileMap = sliceParams.getServiceProfile()

        String serviceProfileInstanceId = execution.getVariable("serviceInstanceId")
        String serviceProfileId = UUID.randomUUID().toString()
        sliceParams.serviceProfile.put("profileId", serviceProfileId)

        ServiceProfile serviceProfile = new ServiceProfile()
        serviceProfile.setProfileId(serviceProfileId)
        serviceProfile.setLatency(Integer.parseInt(serviceProfileMap.get("latency").toString()))
        serviceProfile.setMaxNumberOfUEs(Integer.parseInt(serviceProfileMap.get("maxNumberofUEs").toString()))
        serviceProfile.setCoverageAreaTAList(serviceProfileMap.get("coverageAreaTAList").toString())
        serviceProfile.setUeMobilityLevel(serviceProfileMap.get("uEMobilityLevel").toString())
        serviceProfile.setResourceSharingLevel(serviceProfileMap.get("resourceSharingLevel").toString())
        serviceProfile.setDlThptPerSlice(Integer.parseInt(serviceProfileMap.get("dLThptPerSlice").toString()))
        serviceProfile.setDlThptPerUE(Integer.parseInt(serviceProfileMap.get("dLThptPerUE").toString()))
        serviceProfile.setUlThptPerSlice(Integer.parseInt(serviceProfileMap.get("uLThptPerSlice").toString()))
        serviceProfile.setUlThptPerUE(Integer.parseInt(serviceProfileMap.get("uLThptPerUE").toString()))
        serviceProfile.setActivityFactor(Integer.parseInt(serviceProfileMap.get("activityFactor").toString()))
        serviceProfile.setMaxNumberOfConns(Integer.parseInt(serviceProfileMap.get("maxNumberofConns").toString()))

        serviceProfile.setJitter(Integer.parseInt(serviceProfileMap.get("jitter").toString()))
        serviceProfile.setSurvivalTime("0")
        serviceProfile.setReliability("")
        try {
            AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
                    .customer(globalSubscriberId)
                    .serviceSubscription(subscriptionServiceType)
                    .serviceInstance(serviceProfileInstanceId)
                    .serviceProfile(serviceProfileId))
            client.create(uri, serviceProfile)
            execution.setVariable("sliceTaskParams", sliceParams)

        } catch (BpmnError e) {
            throw e
        } catch (Exception ex) {
            String msg = "Exception in DoCreateSliceServiceInstance.instantiateSliceService. " + ex.getMessage()
            logger.info(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
        }
    }

    /**
     * create allotted resource
     * todo: unfinished
     * @param execution
     */
    public void createAllottedResource(DelegateExecution execution) {

        try {
            String globalSubscriberId = execution.getVariable("globalSubscriberId")
            String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
            ServiceDecomposition serviceDecomposition =
                    execution.getVariable("serviceProfileDecomposition") as ServiceDecomposition
            String serviceInstanceId = execution.getVariable("serviceInstanceId")

            List<org.onap.so.bpmn.core.domain.AllottedResource> allottedResourceList = serviceDecomposition.getAllottedResources()
            for(org.onap.so.bpmn.core.domain.AllottedResource allottedResource : allottedResourceList) {
                String allottedResourceId = UUID.randomUUID().toString()

                AAIResourceUri allottedResourceUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business()
                        .customer(globalSubscriberId)
                        .serviceSubscription(subscriptionServiceType)
                        .serviceInstance(serviceInstanceId)
                        .allottedResource(allottedResourceId))

                execution.setVariable("allottedResourceUri", allottedResourceUri)
                String arType = allottedResource.getAllottedResourceType()
                String arRole = allottedResource.getAllottedResourceRole()
                String modelInvariantId = allottedResource.getModelInfo().getModelInvariantUuid()
                String modelVersionId = allottedResource.getModelInfo().getModelUuid()

                AllottedResource resource = new AllottedResource()
                resource.setId(allottedResourceId)
                resource.setType(arType)
                resource.setAllottedResourceName("Allotted_"+ execution.getVariable("serviceInstanceName"))
                resource.setRole(arRole)
                resource.setModelInvariantId(modelInvariantId)
                resource.setModelVersionId(modelVersionId)

                client.create(allottedResourceUri, resource)

                execution.setVariable("allottedResourceId", allottedResourceId)
            }

        }catch (Exception ex) {
            exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception in createAaiAR " + ex.getMessage())
        }
    }

}