aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/DoCreateResources.groovy
blob: b49f00a2473ae90839e3acd5df28f1e49a1bea24 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * 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.onap.so.bpmn.common.scripts.CatalogDbUtilsFactory
import org.onap.so.bpmn.infrastructure.properties.BPMNProperties
import org.apache.commons.lang3.StringUtils
import org.apache.http.HttpResponse
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.json.JSONObject
import org.onap.so.bpmn.common.recipe.BpmnRestClient
import org.onap.so.bpmn.common.recipe.ResourceInput
import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
import org.onap.so.bpmn.common.scripts.CatalogDbUtils
import org.onap.so.bpmn.common.scripts.ExceptionUtil
import org.onap.so.bpmn.core.domain.AllottedResource
import org.onap.so.bpmn.core.domain.NetworkResource
import org.onap.so.bpmn.core.domain.Resource
import org.onap.so.bpmn.core.domain.ServiceDecomposition
import org.onap.so.bpmn.core.domain.VnfResource
import org.onap.so.bpmn.core.json.JsonUtils
import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
import org.slf4j.Logger
import org.slf4j.LoggerFactory



/**
 * This groovy class supports the <class>DoCreateResources.bpmn</class> process.
 * 
 * Inputs:
 * @param - msoRequestId
 * @param - globalSubscriberId - O
 * @param - subscriptionServiceType - O
 * @param - serviceInstanceId
 * @param - serviceInstanceName - O
 * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
 * @param - sdncVersion 
 *
 * @param - addResourceList
 *
 * Outputs:
 * @param - WorkflowException
 */
public class DoCreateResources extends AbstractServiceTaskProcessor{
    private static final Logger logger = LoggerFactory.getLogger( DoCreateResources.class);

    ExceptionUtil exceptionUtil = new ExceptionUtil()
    JsonUtils jsonUtil = new JsonUtils()
    CatalogDbUtils catalogDbUtils = new CatalogDbUtilsFactory().create()

    public void preProcessRequest(DelegateExecution execution) {
        logger.trace("preProcessRequest ")
        String msg = ""

        List addResourceList = execution.getVariable("addResourceList")
        if (addResourceList == null) {
            msg = "Input addResourceList is null"
            logger.info(msg)
            exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
        }
        else if (addResourceList.size() == 0) {
            msg = "No resource in addResourceList"
            logger.info(msg)
        }
        logger.trace("Exit preProcessRequest ")
    }

    public void sequenceResoure(DelegateExecution execution) {
        logger.trace("Start sequenceResoure Process ")
        
        String incomingRequest = execution.getVariable("uuiRequest")
        String serviceModelUuid = jsonUtil.getJsonValue(incomingRequest,"service.serviceUuid")

        List<Resource> addResourceList = execution.getVariable("addResourceList")

        List<NetworkResource> networkResourceList = new ArrayList<NetworkResource>()

        List<Resource> sequencedResourceList = new ArrayList<Resource>()

        String serviceDecompose = execution.getVariable("serviceDecomposition")
        String serviceModelName = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelName")

        // get Sequence from properties        
        def resourceSequence = BPMNProperties.getResourceSequenceProp(serviceModelName)

        // get Sequence from csar(model)  
        if(resourceSequence == null) {
            resourceSequence = ResourceRequestBuilder.getResourceSequence(serviceModelUuid)
            logger.info("Get Sequence from csar : " + resourceSequence)
        }

        if(resourceSequence != null) {
            for (resourceType in resourceSequence) {
                for (resource in addResourceList) {
                    if (StringUtils.containsIgnoreCase(resource.getModelInfo().getModelName(), resourceType)) {
                        sequencedResourceList.add(resource)

                        if (resource instanceof NetworkResource) {
                            networkResourceList.add(resource)
                        }
                    }
                }
            }
        } else {

            //define sequenced resource list, we deploy vf first and then network and then ar
            //this is defaule sequence
            List<VnfResource> vnfResourceList = new ArrayList<VnfResource>()
            List<AllottedResource> arResourceList = new ArrayList<AllottedResource>()

            for (Resource rc : addResourceList){
                if (rc instanceof VnfResource) {
                    vnfResourceList.add(rc)
                } else if (rc instanceof NetworkResource) {
                    networkResourceList.add(rc)
                } else if (rc instanceof AllottedResource) {
                    arResourceList.add(rc)
                }
            }
            sequencedResourceList.addAll(vnfResourceList)
            sequencedResourceList.addAll(networkResourceList)
            sequencedResourceList.addAll(arResourceList)
        }

        String isContainsWanResource = networkResourceList.isEmpty() ? "false" : "true"
        //if no networkResource, get SDNC config from properties file
        if( "false".equals(isContainsWanResource)) {
            String serviceNeedSDNC = "mso.workflow.custom." + serviceModelName + ".sdnc.need";
            isContainsWanResource = BPMNProperties.getProperty(serviceNeedSDNC, isContainsWanResource)
        }

        execution.setVariable("isContainsWanResource", isContainsWanResource)
        execution.setVariable("currentResourceIndex", 0)
        execution.setVariable("sequencedResourceList", sequencedResourceList)
        logger.info("sequencedResourceList: " + sequencedResourceList)
        logger.trace("COMPLETED sequenceResoure Process ")
    }

    public prepareServiceTopologyRequest(DelegateExecution execution) {

        logger.trace("======== Start prepareServiceTopologyRequest Process ======== ")

        String serviceDecompose = execution.getVariable("serviceDecomposition")

        execution.setVariable("operationType", "create")
        execution.setVariable("resourceType", "")

        String serviceInvariantUuid = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelInvariantUuid")
        String serviceUuid = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelUuid")
        String serviceModelName = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelName")

        execution.setVariable("modelInvariantUuid", serviceInvariantUuid)
        execution.setVariable("modelUuid", serviceUuid)
        execution.setVariable("serviceModelName", serviceModelName)

        logger.trace("======== End prepareServiceTopologyRequest Process ======== ")
    }

    public void getCurrentResoure(DelegateExecution execution){
        logger.trace("Start getCurrentResoure Process ")
        def currentIndex = execution.getVariable("currentResourceIndex")
        List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")
        Resource currentResource = sequencedResourceList.get(currentIndex)
        execution.setVariable("resourceType", currentResource.getModelInfo().getModelName())
        logger.info("Now we deal with resouce:" + currentResource.getModelInfo().getModelName())
        logger.trace("COMPLETED getCurrentResoure Process ")
    }

    public void parseNextResource(DelegateExecution execution){
        logger.trace("Start parseNextResource Process ")
        def currentIndex = execution.getVariable("currentResourceIndex")
        def nextIndex =  currentIndex + 1
        execution.setVariable("currentResourceIndex", nextIndex)
        List<String> sequencedResourceList = execution.getVariable("sequencedResourceList")
        if(nextIndex >= sequencedResourceList.size()){
            execution.setVariable("allResourceFinished", "true")
        }else{
            execution.setVariable("allResourceFinished", "false")
        }
        logger.trace("COMPLETED parseNextResource Process ")
    }

    public void prepareResourceRecipeRequest(DelegateExecution execution){
        logger.trace("Start prepareResourceRecipeRequest Process ")
        ResourceInput resourceInput = new ResourceInput()
        String serviceInstanceName = execution.getVariable("serviceInstanceName")
        String resourceType = execution.getVariable("resourceType")
        String resourceInstanceName = resourceType + "_" + serviceInstanceName
        resourceInput.setResourceInstanceName(resourceInstanceName)
        logger.info("Prepare Resource Request resourceInstanceName:" + resourceInstanceName)
        String globalSubscriberId = execution.getVariable("globalSubscriberId")
        String serviceType = execution.getVariable("serviceType")
        String serviceInstanceId = execution.getVariable("serviceInstanceId")
        String operationId = execution.getVariable("operationId")
        String operationType = "createInstance"
        resourceInput.setGlobalSubscriberId(globalSubscriberId)
        resourceInput.setServiceType(serviceType)
        resourceInput.setServiceInstanceId(serviceInstanceId)
        resourceInput.setOperationId(operationId)
        resourceInput.setOperationType(operationType);
        def currentIndex = execution.getVariable("currentResourceIndex")
        List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")
        Resource currentResource = sequencedResourceList.get(currentIndex)
        resourceInput.setResourceModelInfo(currentResource.getModelInfo());
        ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
        resourceInput.setServiceModelInfo(serviceDecomposition.getModelInfo());
        def String resourceCustomizationUuid = currentResource.getModelInfo().getModelCustomizationUuid();

        String incomingRequest = execution.getVariable("uuiRequest")
        //set the requestInputs from tempalte  To Be Done
        String serviceModelUuid = jsonUtil.getJsonValue(incomingRequest,"service.serviceUuid")
        String serviceParameters = jsonUtil.getJsonValue(incomingRequest, "service.parameters")
        String resourceParameters = ResourceRequestBuilder.buildResourceRequestParameters(execution, serviceModelUuid, resourceCustomizationUuid, serviceParameters)
        resourceInput.setResourceParameters(resourceParameters)
        resourceInput.setRequestsInputs(incomingRequest)
        execution.setVariable("resourceInput", resourceInput.toString())
        execution.setVariable("resourceModelUUID", resourceInput.getResourceModelInfo().getModelUuid())
        logger.trace("COMPLETED prepareResourceRecipeRequest Process ")
    }

    public void executeResourceRecipe(DelegateExecution execution){
        logger.trace("Start executeResourceRecipe Process ")

        try {
            String requestId = execution.getVariable("msoRequestId")
            String serviceInstanceId = execution.getVariable("serviceInstanceId")
            String serviceType = execution.getVariable("serviceType")
            String resourceInput = execution.getVariable("resourceInput")
            String resourceModelUUID = execution.getVariable("resourceModelUUID")

            // requestAction is action, not opertiontype
            //String requestAction = resourceInput.getOperationType()
            String requestAction = "createInstance"
            JSONObject resourceRecipe = catalogDbUtils.getResourceRecipe(execution, resourceModelUUID, requestAction)

            if (resourceRecipe != null) {
                String recipeURL = BPMNProperties.getProperty("bpelURL", "http://so-bpmn-infra.onap:8081") + resourceRecipe.getString("orchestrationUri")
                int recipeTimeOut = resourceRecipe.getInt("recipeTimeout")
                String recipeParamXsd = resourceRecipe.get("paramXSD")

                BpmnRestClient bpmnRestClient = new BpmnRestClient()
                HttpResponse resp = bpmnRestClient.post(recipeURL, requestId, recipeTimeOut, requestAction, serviceInstanceId, serviceType, resourceInput, recipeParamXsd)
            } else {
                String exceptionMessage = "Resource receipe is not found for resource modeluuid: " + resourceModelUUID
                logger.trace(exceptionMessage)
                exceptionUtil.buildAndThrowWorkflowException(execution, 500, exceptionMessage)
            }

            logger.trace("======== end executeResourceRecipe Process ======== ")
        }catch(BpmnError b){
            logger.debug("Rethrowing MSOWorkflowException")
            throw b
        }catch(Exception e){
            logger.debug("Error occured within DoCreateResources executeResourceRecipe method: " + e)
            exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured during DoCreateResources executeResourceRecipe Catalog")
        }
    }

    public void postConfigRequest(DelegateExecution execution){
        //now do noting
        ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
        for (VnfResource resource : serviceDecomposition.vnfResources) {
            resource.setOrchestrationStatus("Active")
        }
        execution.setVariable("serviceDecomposition", serviceDecomposition)
    }
}