[SO] Release so 1.13.0 image
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateResources.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
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 org.onap.so.bpmn.infrastructure.properties.BPMNProperties
24
25 import java.util.ArrayList
26 import java.util.Iterator
27 import java.util.List
28 import org.apache.commons.lang3.StringUtils
29 import org.apache.http.HttpResponse
30 import org.camunda.bpm.engine.delegate.BpmnError
31 import org.camunda.bpm.engine.delegate.DelegateExecution
32 import org.codehaus.groovy.runtime.ArrayUtil
33 import org.codehaus.groovy.runtime.ScriptBytecodeAdapter
34 import org.codehaus.groovy.runtime.callsite.CallSite
35 import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation
36 import org.codehaus.groovy.runtime.typehandling.ShortTypeHandling
37 import org.json.JSONArray
38 import org.json.JSONObject
39 import org.onap.so.bpmn.common.recipe.BpmnRestClient
40 import org.onap.so.bpmn.common.recipe.ResourceInput
41 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
42 import org.onap.so.bpmn.common.scripts.CatalogDbUtils
43 import org.onap.so.bpmn.common.scripts.ExceptionUtil
44 import org.onap.so.bpmn.core.domain.AllottedResource
45 import org.onap.so.bpmn.core.domain.NetworkResource
46 import org.onap.so.bpmn.core.domain.Resource
47 import org.onap.so.bpmn.core.domain.ServiceDecomposition
48 import org.onap.so.bpmn.core.domain.VnfResource
49 import org.onap.so.bpmn.core.json.JsonUtils
50 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
51 import org.onap.so.logger.MessageEnum
52 import org.onap.so.logger.MsoLogger
53
54
55
56 /**
57  * This groovy class supports the <class>DoCreateResources.bpmn</class> process.
58  * 
59  * Inputs:
60  * @param - msoRequestId
61  * @param - globalSubscriberId - O
62  * @param - subscriptionServiceType - O
63  * @param - serviceInstanceId
64  * @param - serviceInstanceName - O
65  * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
66  * @param - sdncVersion 
67  *
68  * @param - addResourceList
69  *
70  * Outputs:
71  * @param - WorkflowException
72  */
73 public class DoCreateResources extends AbstractServiceTaskProcessor{
74     private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateResources.class);
75
76     ExceptionUtil exceptionUtil = new ExceptionUtil()
77     JsonUtils jsonUtil = new JsonUtils()
78     CatalogDbUtils cutils = new CatalogDbUtils()
79
80     public void preProcessRequest(DelegateExecution execution) {
81         msoLogger.trace("preProcessRequest ")
82         String msg = ""
83
84         List addResourceList = execution.getVariable("addResourceList")
85         if (addResourceList == null) {
86             msg = "Input addResourceList is null"
87             msoLogger.info(msg)
88             exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
89         }
90         else if (addResourceList.size() == 0) {
91             msg = "No resource in addResourceList"
92             msoLogger.info(msg)
93         }
94         msoLogger.trace("Exit preProcessRequest ")
95     }
96
97     public void sequenceResoure(DelegateExecution execution) {
98         msoLogger.trace("Start sequenceResoure Process ")
99         
100         String incomingRequest = execution.getVariable("uuiRequest")
101         String serviceModelUuid = jsonUtil.getJsonValue(incomingRequest,"service.serviceUuid")
102
103         List<Resource> addResourceList = execution.getVariable("addResourceList")
104
105         List<NetworkResource> networkResourceList = new ArrayList<NetworkResource>()
106
107         List<Resource> sequencedResourceList = new ArrayList<Resource>()
108
109         String serviceDecompose = execution.getVariable("serviceDecomposition")
110         String serviceModelName = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelName")
111
112         // get Sequence from properties        
113         def resourceSequence = BPMNProperties.getResourceSequenceProp(serviceModelName)
114
115         // get Sequence from csar(model)  
116         if(resourceSequence == null) {
117             resourceSequence = ResourceRequestBuilder.getResourceSequence(execution, serviceModelUuid)
118             msoLogger.info("Get Sequence from csar : " + resourceSequence)
119         }
120
121         if(resourceSequence != null) {
122             for (resourceType in resourceSequence) {
123                 for (resource in addResourceList) {
124                     if (StringUtils.containsIgnoreCase(resource.getModelInfo().getModelName(), resourceType)) {
125                         sequencedResourceList.add(resource)
126
127                         if (resource instanceof NetworkResource) {
128                             networkResourceList.add(resource)
129                         }
130                     }
131                 }
132             }
133         } else {
134
135             //define sequenced resource list, we deploy vf first and then network and then ar
136             //this is defaule sequence
137             List<VnfResource> vnfResourceList = new ArrayList<VnfResource>()
138             List<AllottedResource> arResourceList = new ArrayList<AllottedResource>()
139
140             for (Resource rc : addResourceList){
141                 if (rc instanceof VnfResource) {
142                     vnfResourceList.add(rc)
143                 } else if (rc instanceof NetworkResource) {
144                     networkResourceList.add(rc)
145                 } else if (rc instanceof AllottedResource) {
146                     arResourceList.add(rc)
147                 }
148             }
149             sequencedResourceList.addAll(vnfResourceList)
150             sequencedResourceList.addAll(networkResourceList)
151             sequencedResourceList.addAll(arResourceList)
152         }
153
154         String isContainsWanResource = networkResourceList.isEmpty() ? "false" : "true"
155         //if no networkResource, get SDNC config from properties file
156         if( "false".equals(isContainsWanResource)) {
157             String serviceNeedSDNC = "mso.workflow.custom." + serviceModelName + ".sdnc.need";
158             isContainsWanResource = BPMNProperties.getProperty(serviceNeedSDNC, isContainsWanResource)
159         }
160
161         execution.setVariable("isContainsWanResource", isContainsWanResource)
162         execution.setVariable("currentResourceIndex", 0)
163         execution.setVariable("sequencedResourceList", sequencedResourceList)
164         msoLogger.info("sequencedResourceList: " + sequencedResourceList)
165         msoLogger.trace("COMPLETED sequenceResoure Process ")
166     }
167
168     public prepareServiceTopologyRequest(DelegateExecution execution) {
169
170         msoLogger.trace("======== Start prepareServiceTopologyRequest Process ======== ")
171
172         String serviceDecompose = execution.getVariable("serviceDecomposition")
173
174         execution.setVariable("operationType", "create")
175         execution.setVariable("resourceType", "")
176
177         String serviceInvariantUuid = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelInvariantUuid")
178         String serviceUuid = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelUuid")
179         String serviceModelName = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelName")
180
181         execution.setVariable("modelInvariantUuid", serviceInvariantUuid)
182         execution.setVariable("modelUuid", serviceUuid)
183         execution.setVariable("serviceModelName", serviceModelName)
184
185         msoLogger.trace("======== End prepareServiceTopologyRequest Process ======== ")
186     }
187
188     public void getCurrentResoure(DelegateExecution execution){
189         msoLogger.trace("Start getCurrentResoure Process ")
190         def currentIndex = execution.getVariable("currentResourceIndex")
191         List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")
192         Resource currentResource = sequencedResourceList.get(currentIndex)
193         execution.setVariable("resourceType", currentResource.getModelInfo().getModelName())
194         msoLogger.info("Now we deal with resouce:" + currentResource.getModelInfo().getModelName())
195         msoLogger.trace("COMPLETED getCurrentResoure Process ")
196     }
197
198     public void parseNextResource(DelegateExecution execution){
199         msoLogger.trace("Start parseNextResource Process ")
200         def currentIndex = execution.getVariable("currentResourceIndex")
201         def nextIndex =  currentIndex + 1
202         execution.setVariable("currentResourceIndex", nextIndex)
203         List<String> sequencedResourceList = execution.getVariable("sequencedResourceList")
204         if(nextIndex >= sequencedResourceList.size()){
205             execution.setVariable("allResourceFinished", "true")
206         }else{
207             execution.setVariable("allResourceFinished", "false")
208         }
209         msoLogger.trace("COMPLETED parseNextResource Process ")
210     }
211
212     public void prepareResourceRecipeRequest(DelegateExecution execution){
213         msoLogger.trace("Start prepareResourceRecipeRequest Process ")
214         ResourceInput resourceInput = new ResourceInput()
215         String serviceInstanceName = execution.getVariable("serviceInstanceName")
216         String resourceType = execution.getVariable("resourceType")
217         String resourceInstanceName = resourceType + "_" + serviceInstanceName
218         resourceInput.setResourceInstanceName(resourceInstanceName)
219         msoLogger.info("Prepare Resource Request resourceInstanceName:" + resourceInstanceName)
220         String globalSubscriberId = execution.getVariable("globalSubscriberId")
221         String serviceType = execution.getVariable("serviceType")
222         String serviceInstanceId = execution.getVariable("serviceInstanceId")
223         String operationId = execution.getVariable("operationId")
224         String operationType = "createInstance"
225         resourceInput.setGlobalSubscriberId(globalSubscriberId)
226         resourceInput.setServiceType(serviceType)
227         resourceInput.setServiceInstanceId(serviceInstanceId)
228         resourceInput.setOperationId(operationId)
229         resourceInput.setOperationType(operationType);
230         def currentIndex = execution.getVariable("currentResourceIndex")
231         List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")
232         Resource currentResource = sequencedResourceList.get(currentIndex)
233         resourceInput.setResourceModelInfo(currentResource.getModelInfo());
234         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
235         resourceInput.setServiceModelInfo(serviceDecomposition.getModelInfo());
236         def String resourceCustomizationUuid = currentResource.getModelInfo().getModelCustomizationUuid();
237
238         String incomingRequest = execution.getVariable("uuiRequest")
239         //set the requestInputs from tempalte  To Be Done
240         String serviceModelUuid = jsonUtil.getJsonValue(incomingRequest,"service.serviceUuid")
241         String serviceParameters = jsonUtil.getJsonValue(incomingRequest, "service.parameters")
242         String resourceParameters = ResourceRequestBuilder.buildResourceRequestParameters(execution, serviceModelUuid, resourceCustomizationUuid, serviceParameters)
243         resourceInput.setResourceParameters(resourceParameters)
244         resourceInput.setRequestsInputs(incomingRequest)
245         execution.setVariable("resourceInput", resourceInput)
246         msoLogger.trace("COMPLETED prepareResourceRecipeRequest Process ")
247     }
248
249     public void executeResourceRecipe(DelegateExecution execution){
250         msoLogger.trace("Start executeResourceRecipe Process ")
251
252         try {
253             String requestId = execution.getVariable("msoRequestId")
254             String serviceInstanceId = execution.getVariable("serviceInstanceId")
255             String serviceType = execution.getVariable("serviceType")
256             ResourceInput resourceInput = execution.getVariable("resourceInput")
257
258             // requestAction is action, not opertiontype
259             //String requestAction = resourceInput.getOperationType()
260             String requestAction = "createInstance"
261             JSONObject resourceRecipe = cutils.getResourceRecipe(execution, resourceInput.getResourceModelInfo().getModelUuid(), requestAction)
262
263             if (resourceRecipe != null) {
264                 String recipeURL = BPMNProperties.getProperty("bpelURL", "http://mso:8080") + resourceRecipe.getString("orchestrationUri")
265                 int recipeTimeOut = resourceRecipe.getInt("recipeTimeout")
266                 String recipeParamXsd = resourceRecipe.get("paramXSD")
267                 HttpResponse resp = BpmnRestClient.post(recipeURL, requestId, recipeTimeOut, requestAction, serviceInstanceId, serviceType, resourceInput.toString(), recipeParamXsd)
268             } else {
269                 String exceptionMessage = "Resource receipe is not found for resource modeluuid: " +
270                         resourceInput.getResourceModelInfo().getModelUuid()
271                 msoLogger.trace(exceptionMessage)
272                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, exceptionMessage)
273             }
274
275             msoLogger.trace("======== end executeResourceRecipe Process ======== ")
276         }catch(BpmnError b){
277             msoLogger.debug("Rethrowing MSOWorkflowException")
278             throw b
279         }catch(Exception e){
280             msoLogger.debug("Error occured within DoCreateResources executeResourceRecipe method: " + e)
281             exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured during DoCreateResources executeResourceRecipe Catalog")
282         }
283     }
284
285     public void postConfigRequest(DelegateExecution execution){
286         //now do noting
287     }
288 }