Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / 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.openecomp.mso.bpmn.infrastructure.scripts
22
23 import org.codehaus.jackson.map.ObjectMapper
24 import org.openecomp.mso.bpmn.infrastructure.properties.BPMNProperties
25
26 import java.util.ArrayList
27 import java.util.Iterator
28 import java.util.List
29 import org.apache.commons.lang3.StringUtils
30 import org.apache.http.HttpResponse
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.openecomp.mso.bpmn.common.recipe.BpmnRestClient
40 import org.openecomp.mso.bpmn.common.recipe.ResourceInput
41 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
42 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils
43 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
44 import org.openecomp.mso.bpmn.core.domain.AllottedResource
45 import org.openecomp.mso.bpmn.core.domain.NetworkResource
46 import org.openecomp.mso.bpmn.core.domain.Resource
47 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
48 import org.openecomp.mso.bpmn.core.domain.VnfResource
49 import org.openecomp.mso.bpmn.core.json.JsonUtils
50 import org.openecomp.mso.bpmn.common.resource.ResourceRequestBuilder
51
52 /**
53  * This groovy class supports the <class>DoCreateResources.bpmn</class> process.
54  * 
55  * Inputs:
56  * @param - msoRequestId
57  * @param - globalSubscriberId - O
58  * @param - subscriptionServiceType - O
59  * @param - serviceInstanceId
60  * @param - serviceInstanceName - O
61  * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
62  * @param - sdncVersion 
63  *
64  * @param - addResourceList
65  *
66  * Outputs:
67  * @param - WorkflowException
68
69  */
70 public class DoCreateResources extends AbstractServiceTaskProcessor
71 {
72         ExceptionUtil exceptionUtil = new ExceptionUtil()
73         JsonUtils jsonUtil = new JsonUtils()
74         CatalogDbUtils cutils = new CatalogDbUtils()
75
76     public void preProcessRequest(DelegateExecution execution)
77     {
78                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
79                 utils.log("INFO"," ***** preProcessRequest *****",    isDebugEnabled)
80                 String msg = ""
81                 
82         List addResourceList = execution.getVariable("addResourceList")
83         if (addResourceList == null)
84         {
85             msg = "Input addResourceList is null"
86             utils.log("INFO", msg, isDebugEnabled)
87             exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)  
88         }
89         else if (addResourceList.size() == 0)
90         {
91             msg = "No resource in addResourceList"
92             utils.log("INFO", msg, isDebugEnabled)
93         }
94         utils.log("INFO", " ***** Exit preProcessRequest *****", isDebugEnabled)
95     }
96     
97     public void sequenceResoure(DelegateExecution execution)
98     {
99         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
100         utils.log("INFO", "======== Start sequenceResoure Process ======== ", isDebugEnabled)
101         
102         String serviceModelUUID = execution.getVariable("modelUuid")      
103                
104         List<Resource> addResourceList = execution.getVariable("addResourceList")
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         def resourceSequence = BPMNProperties.getResourceSequenceProp(serviceModelName)
112
113         if(resourceSequence != null) {
114             // sequence is defined in config file
115             for (resourceType in resourceSequence) {
116                 for (resource in addResourceList) {
117                     if (StringUtils.containsIgnoreCase(resource.getModelInfo().getModelName(), resourceType)) {
118                         sequencedResourceList.add(resource)
119
120                         if (resource instanceof NetworkResource) {
121                             networkResourceList.add(resource)
122                         }
123                     }
124                 }
125             }
126         } else {
127
128         //define sequenced resource list, we deploy vf first and then network and then ar
129         //this is defaule sequence
130         List<VnfResource> vnfResourceList = new ArrayList<VnfResource>()
131         List<AllottedResource> arResourceList = new ArrayList<AllottedResource>()
132
133         for (Resource rc : addResourceList){
134             if (rc instanceof VnfResource) {
135                 vnfResourceList.add(rc)
136             } else if (rc instanceof NetworkResource) {
137                 networkResourceList.add(rc)
138             } else if (rc instanceof AllottedResource) {
139                 arResourceList.add(rc)
140             }
141         }
142         sequencedResourceList.addAll(vnfResourceList)
143         sequencedResourceList.addAll(networkResourceList)
144         sequencedResourceList.addAll(arResourceList)
145         }
146
147         String isContainsWanResource = networkResourceList.isEmpty() ? "false" : "true"
148         execution.setVariable("isContainsWanResource", isContainsWanResource)
149         execution.setVariable("currentResourceIndex", 0)
150         execution.setVariable("sequencedResourceList", sequencedResourceList)
151         utils.log("INFO", "sequencedResourceList: " + sequencedResourceList, isDebugEnabled) 
152         utils.log("INFO", "======== COMPLETED sequenceResoure Process ======== ", isDebugEnabled)
153     }
154
155     public prepareServiceTopologyRequest(DelegateExecution execution) {
156
157         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
158         utils.log("INFO", "======== Start prepareServiceTopologyRequest Process ======== ", isDebugEnabled)
159
160         String serviceDecompose = execution.getVariable("serviceDecomposition")
161
162         execution.setVariable("operationType", "create")
163         execution.setVariable("resourceType", "")
164
165         String serviceInvariantUuid = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelInvariantUuid")
166         String serviceUuid = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelUuid")
167         String serviceModelName = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelName")
168
169         execution.setVariable("modelInvariantUuid", serviceInvariantUuid)
170         execution.setVariable("modelUuid", serviceUuid)
171         execution.setVariable("serviceModelName", serviceModelName)
172
173         utils.log("INFO", "======== End prepareServiceTopologyRequest Process ======== ", isDebugEnabled)
174     }
175    
176     public void getCurrentResoure(DelegateExecution execution){
177             def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   
178         utils.log("INFO", "======== Start getCurrentResoure Process ======== ", isDebugEnabled)    
179             def currentIndex = execution.getVariable("currentResourceIndex")
180             List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")  
181             Resource currentResource = sequencedResourceList.get(currentIndex)
182         execution.setVariable("resourceType", currentResource.getModelInfo().getModelName())
183             utils.log("INFO", "Now we deal with resouce:" + currentResource.getModelInfo().getModelName(), isDebugEnabled)  
184         utils.log("INFO", "======== COMPLETED getCurrentResoure Process ======== ", isDebugEnabled)  
185     }
186     
187     public void parseNextResource(DelegateExecution execution){
188         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
189         utils.log("INFO", "======== Start parseNextResource Process ======== ", isDebugEnabled)    
190         def currentIndex = execution.getVariable("currentResourceIndex")
191         def nextIndex =  currentIndex + 1
192         execution.setVariable("currentResourceIndex", nextIndex)
193         List<String> sequencedResourceList = execution.getVariable("sequencedResourceList")    
194         if(nextIndex >= sequencedResourceList.size()){
195             execution.setVariable("allResourceFinished", "true")
196         }else{
197             execution.setVariable("allResourceFinished", "false")
198         }
199         utils.log("INFO", "======== COMPLETED parseNextResource Process ======== ", isDebugEnabled)       
200     }    
201
202          public void prepareResourceRecipeRequest(DelegateExecution execution){
203                  def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
204                  utils.log("INFO", "======== Start prepareResourceRecipeRequest Process ======== ", isDebugEnabled)
205                  ResourceInput resourceInput = new ResourceInput()
206                  String serviceInstanceName = execution.getVariable("serviceInstanceName")
207          String resourceType = execution.getVariable("resourceType")
208                  String resourceInstanceName = resourceType + "_" + serviceInstanceName
209                  resourceInput.setResourceInstanceName(resourceInstanceName)
210                  utils.log("INFO", "Prepare Resource Request resourceInstanceName:" + resourceInstanceName, isDebugEnabled)
211                  String globalSubscriberId = execution.getVariable("globalSubscriberId")
212                  String serviceType = execution.getVariable("serviceType")
213                  String serviceInstanceId = execution.getVariable("serviceInstanceId")
214                  String operationId = execution.getVariable("operationId")
215                  String operationType = "createInstance"
216                  resourceInput.setGlobalSubscriberId(globalSubscriberId)
217                  resourceInput.setServiceType(serviceType)
218                  resourceInput.setServiceInstanceId(serviceInstanceId)
219                  resourceInput.setOperationId(operationId)
220                  resourceInput.setOperationType(operationType);
221                  def currentIndex = execution.getVariable("currentResourceIndex")
222                  List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")
223                  Resource currentResource = sequencedResourceList.get(currentIndex)
224                  resourceInput.setResourceModelInfo(currentResource.getModelInfo());
225                  ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
226                  resourceInput.setServiceModelInfo(serviceDecomposition.getModelInfo());
227          def String resourceCustomizationUuid = currentResource.getModelInfo().getModelCustomizationUuid();
228                  
229                  String incomingRequest = execution.getVariable("uuiRequest")
230                  //set the requestInputs from tempalte  To Be Done
231                  String serviceModelUuid = jsonUtil.getJsonValue(incomingRequest,"service.serviceUuid")
232          String serviceParameters = jsonUtil.getJsonValue(incomingRequest, "service.parameters")
233                  String resourceParameters = ResourceRequestBuilder.buildResourceRequestParameters(execution, serviceModelUuid, resourceCustomizationUuid, serviceParameters)
234                  resourceInput.setResourceParameters(resourceParameters)
235                  execution.setVariable("resourceInput", resourceInput)
236                  utils.log("INFO", "======== COMPLETED prepareResourceRecipeRequest Process ======== ", isDebugEnabled)
237          }
238          
239          public void executeResourceRecipe(DelegateExecution execution){
240                  def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
241                  utils.log("INFO", "======== Start executeResourceRecipe Process ======== ", isDebugEnabled)
242                  String requestId = execution.getVariable("msoRequestId")
243                  String serviceInstanceId = execution.getVariable("serviceInstanceId")
244                  String serviceType = execution.getVariable("serviceType")
245                  ResourceInput resourceInput = execution.getVariable("resourceInput")
246                  
247                  // requestAction is action, not opertiontype
248                  //String requestAction = resourceInput.getOperationType()
249                  String requestAction = "createInstance"
250                  JSONObject resourceRecipe = cutils.getResourceRecipe(execution, resourceInput.getResourceModelInfo().getModelUuid(), requestAction)
251
252          if (resourceRecipe != null) {
253              String recipeURL = BPMNProperties.getProperty("bpelURL", "http://mso:8080") + resourceRecipe.getString("orchestrationUri")
254              int recipeTimeOut = resourceRecipe.getInt("recipeTimeout")
255              String recipeParamXsd = resourceRecipe.get("paramXSD")
256              HttpResponse resp = BpmnRestClient.post(recipeURL, requestId, recipeTimeOut, requestAction, serviceInstanceId, serviceType, resourceInput.toString(), recipeParamXsd)
257          } else {
258              String exceptionMessage = "Resource receipe is not found for resource modeluuid: " +
259                      resourceInput.getResourceModelInfo().getModelUuid()
260              utils.log("ERROR", exceptionMessage, isDebugEnabled)
261              exceptionUtil.buildAndThrowWorkflowException(execution, 500, exceptionMessage)
262          }
263
264          utils.log("INFO", "======== end executeResourceRecipe Process ======== ", isDebugEnabled)
265          }
266     
267      public void postConfigRequest(DelegateExecution execution){
268          //now do noting
269      }
270 }