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