Merge "Cloudify REST Client unit tests"
[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 java.util.ArrayList
24 import java.util.Iterator
25 import java.util.List
26 import org.apache.commons.lang3.StringUtils
27 import org.apache.http.HttpResponse
28 import org.camunda.bpm.engine.delegate.DelegateExecution
29 import org.codehaus.groovy.runtime.ArrayUtil
30 import org.codehaus.groovy.runtime.ScriptBytecodeAdapter
31 import org.codehaus.groovy.runtime.callsite.CallSite
32 import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation
33 import org.codehaus.groovy.runtime.typehandling.ShortTypeHandling
34 import org.json.JSONArray
35 import org.json.JSONObject
36 import org.openecomp.mso.bpmn.common.recipe.BpmnRestClient
37 import org.openecomp.mso.bpmn.common.recipe.ResourceInput
38 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
39 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils
40 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
41 import org.openecomp.mso.bpmn.core.domain.AllottedResource
42 import org.openecomp.mso.bpmn.core.domain.NetworkResource
43 import org.openecomp.mso.bpmn.core.domain.Resource
44 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
45 import org.openecomp.mso.bpmn.core.domain.VnfResource
46 import org.openecomp.mso.bpmn.core.json.JsonUtils
47 import org.openecomp.mso.bpmn.common.resource.ResourceRequestBuilder
48
49 /**
50  * This groovy class supports the <class>DoCreateResources.bpmn</class> process.
51  * 
52  * Inputs:
53  * @param - msoRequestId
54  * @param - globalSubscriberId - O
55  * @param - subscriptionServiceType - O
56  * @param - serviceInstanceId
57  * @param - serviceInstanceName - O
58  * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
59  * @param - sdncVersion 
60  *
61  * @param - addResourceList
62  *
63  * Outputs:
64  * @param - WorkflowException
65
66  */
67 public class DoCreateResources extends AbstractServiceTaskProcessor
68 {
69         ExceptionUtil exceptionUtil = new ExceptionUtil()
70         JsonUtils jsonUtil = new JsonUtils()
71         CatalogDbUtils cutils = new CatalogDbUtils()
72
73     public void preProcessRequest(DelegateExecution execution)
74     {
75                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
76                 utils.log("INFO"," ***** preProcessRequest *****",    isDebugEnabled)
77                 String msg = ""
78                 
79         List addResourceList = execution.getVariable("addResourceList")
80         if (addResourceList == null)
81         {
82             msg = "Input addResourceList is null"
83             utils.log("INFO", msg, isDebugEnabled)
84             exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)  
85         }
86         else if (addResourceList.size() == 0)
87         {
88             msg = "No resource in addResourceList"
89             utils.log("INFO", msg, isDebugEnabled)
90         }
91         utils.log("INFO", " ***** Exit preProcessRequest *****", isDebugEnabled)
92     }
93     
94     public void sequenceResoure(DelegateExecution execution)
95     {
96         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
97         utils.log("INFO", "======== Start sequenceResoure Process ======== ", isDebugEnabled)
98         
99         String serviceModelUUID = execution.getVariable("modelUuid")      
100                
101         List<Resource> addResourceList = execution.getVariable("addResourceList")        
102
103         //we use VF to define a network service
104         List<VnfResource> vnfResourceList = new ArrayList<VnfResource>()
105         //here wan is defined as a network resource
106         List<NetworkResource> networkResourceList = new ArrayList<NetworkResource>()
107         //allotted resource      
108         List<AllottedResource> arResourceList = new ArrayList<AllottedResource>()
109
110         //define sequenced resource list, we deploy vf first and then network and then ar 
111         //this is defaule sequence
112         List<Resource> sequencedResourceList = new ArrayList<Resource>()
113         for (Resource rc : addResourceList){
114         if (rc instanceof VnfResource) {
115                 vnfResourceList.add(rc)
116             } else if (rc instanceof NetworkResource) {
117                 networkResourceList.add(rc)
118             } else if (rc instanceof AllottedResource) {
119                 arResourceList.add(rc)
120             }
121         }        
122         sequencedResourceList.addAll(vnfResourceList)
123         sequencedResourceList.addAll(networkResourceList)
124         sequencedResourceList.addAll(arResourceList)
125         
126         String isContainsWanResource = networkResourceList.isEmpty() ? "false" : "true"
127         execution.setVariable("isContainsWanResource", isContainsWanResource)
128         execution.setVariable("currentResourceIndex", 0)
129         execution.setVariable("sequencedResourceList", sequencedResourceList)
130         utils.log("INFO", "sequencedResourceList: " + sequencedResourceList, isDebugEnabled) 
131         utils.log("INFO", "======== COMPLETED sequenceResoure Process ======== ", isDebugEnabled)
132     }   
133    
134     public void getCurrentResoure(DelegateExecution execution){
135             def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   
136         utils.log("INFO", "======== Start getCurrentResoure Process ======== ", isDebugEnabled)    
137             def currentIndex = execution.getVariable("currentResourceIndex")
138             List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")  
139             Resource currentResource = sequencedResourceList.get(currentIndex)
140         execution.setVariable("resourceType", currentResource.getModelInfo().getModelName())
141             utils.log("INFO", "Now we deal with resouce:" + currentResource.getModelInfo().getModelName(), isDebugEnabled)  
142         utils.log("INFO", "======== COMPLETED getCurrentResoure Process ======== ", isDebugEnabled)  
143     }
144     
145     public void parseNextResource(DelegateExecution execution){
146         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
147         utils.log("INFO", "======== Start parseNextResource Process ======== ", isDebugEnabled)    
148         def currentIndex = execution.getVariable("currentResourceIndex")
149         def nextIndex =  currentIndex + 1
150         execution.setVariable("currentResourceIndex", nextIndex)
151         List<String> sequencedResourceList = execution.getVariable("sequencedResourceList")    
152         if(nextIndex >= sequencedResourceList.size()){
153             execution.setVariable("allResourceFinished", "true")
154         }else{
155             execution.setVariable("allResourceFinished", "false")
156         }
157         utils.log("INFO", "======== COMPLETED parseNextResource Process ======== ", isDebugEnabled)       
158     }    
159
160          public void prepareResourceRecipeRequest(DelegateExecution execution){
161                  def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
162                  utils.log("INFO", "======== Start prepareResourceRecipeRequest Process ======== ", isDebugEnabled)
163                  ResourceInput resourceInput = new ResourceInput()
164                  String serviceInstanceName = execution.getVariable("serviceInstanceName")
165          String resourceType = execution.getVariable("resourceType")
166                  String resourceInstanceName = resourceType + "_" + serviceInstanceName
167                  resourceInput.setResourceInstanceName(resourceInstanceName)
168                  utils.log("INFO", "Prepare Resource Request resourceInstanceName:" + resourceInstanceName, isDebugEnabled)
169                  String globalSubscriberId = execution.getVariable("globalSubscriberId")
170                  String serviceType = execution.getVariable("serviceType")
171                  String serviceInstanceId = execution.getVariable("serviceInstanceId")
172                  String operationId = execution.getVariable("operationId")
173                  String operationType = execution.getVariable("operationType")
174                  resourceInput.setGlobalSubscriberId(globalSubscriberId)
175                  resourceInput.setServiceType(serviceType)
176                  resourceInput.setServiceInstanceId(serviceInstanceId)
177                  resourceInput.setOperationId(operationId)
178                  resourceInput.setOperationType(operationType);
179                  def currentIndex = execution.getVariable("currentResourceIndex")
180                  List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")
181                  Resource currentResource = sequencedResourceList.get(currentIndex)
182                  resourceInput.setResourceModelInfo(currentResource.getModelInfo());
183                  ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
184                  resourceInput.setServiceModelInfo(serviceDecomposition.getModelInfo());
185                  
186                  String incomingRequest = execution.getVariable("uuiRequest")
187                  //set the requestInputs from tempalte  To Be Done
188                  String serviceModelUuid = jsonUtil.getJsonValue(incomingRequest,"service.serviceUuid")
189          String serviceParameters = jsonUtil.getJsonValue(incomingRequest, "service.parameters")
190                  String resourceParameters = ResourceRequestBuilder.buildResourceRequestParameters(execution, serviceModelUuid, resourceCustomizationUuid, serviceParameters)
191                  resourceInput.setResourceParameters(resourceParameters)
192                  execution.setVariable("resourceInput", resourceInput)
193                  utils.log("INFO", "======== COMPLETED prepareResourceRecipeRequest Process ======== ", isDebugEnabled)
194          }
195          
196          public void executeResourceRecipe(DelegateExecution execution){
197                  def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
198                  utils.log("INFO", "======== Start executeResourceRecipe Process ======== ", isDebugEnabled)
199                  String requestId = execution.getVariable("msoRequestId")
200                  String serviceInstanceId = execution.getVariable("serviceInstanceId")
201                  String serviceType = execution.getVariable("serviceType")
202                  ResourceInput resourceInput = execution.getVariable("resourceInput")
203                  String requestAction = resourceInput.getOperationType()
204                  JSONObject resourceRecipe = cutils.getResourceRecipe(execution, resourceInput.getResourceModelInfo().getModelUuid(), requestAction)
205                  String recipeUri = resourceRecipe.getString("orchestrationUri")
206                  String recipeTimeOut = resourceRecipe.getString("recipeTimeout")
207                  String recipeParamXsd = resourceRecipe.get("paramXSD")
208                  HttpResponse resp = BpmnRestClient.post(recipeUri, requestId, recipeTimeOut, requestAction, serviceInstanceId, serviceType, resourceInput.toString(), recipeParamXsd)
209                  
210          }
211     
212      public void postConfigRequest(DelegateExecution execution){
213          //now do noting
214      }
215 }