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