Merge "move comparemodel before syncresponse updateE2E"
[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         def resourceSequence = BPMNProperties.getResourceSequenceProp()
109
110         if(resourceSequence != null) {
111             // sequence is defined in config file
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         } else {
124
125         //define sequenced resource list, we deploy vf first and then network and then ar
126         //this is defaule sequence
127         List<VnfResource> vnfResourceList = new ArrayList<VnfResource>()
128         List<AllottedResource> arResourceList = new ArrayList<AllottedResource>()
129
130         for (Resource rc : addResourceList){
131             if (rc instanceof VnfResource) {
132                 vnfResourceList.add(rc)
133             } else if (rc instanceof NetworkResource) {
134                 networkResourceList.add(rc)
135             } else if (rc instanceof AllottedResource) {
136                 arResourceList.add(rc)
137             }
138         }
139         sequencedResourceList.addAll(vnfResourceList)
140         sequencedResourceList.addAll(networkResourceList)
141         sequencedResourceList.addAll(arResourceList)
142         }
143
144         String isContainsWanResource = networkResourceList.isEmpty() ? "false" : "true"
145         execution.setVariable("isContainsWanResource", isContainsWanResource)
146         execution.setVariable("currentResourceIndex", 0)
147         execution.setVariable("sequencedResourceList", sequencedResourceList)
148         utils.log("INFO", "sequencedResourceList: " + sequencedResourceList, isDebugEnabled) 
149         utils.log("INFO", "======== COMPLETED sequenceResoure Process ======== ", isDebugEnabled)
150     }
151
152     public prepareServiceTopologyRequest(DelegateExecution execution) {
153
154         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
155         utils.log("INFO", "======== Start prepareServiceTopologyRequest Process ======== ", isDebugEnabled)
156
157         String serviceDecompose = execution.getVariable("serviceDecomposition")
158
159         execution.setVariable("operationType", "create")
160         execution.setVariable("resourceType", "")
161
162         String serviceInvariantUuid = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelInvariantUuid")
163         String serviceUuid = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelUuid")
164         String serviceModelName = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelName")
165
166         execution.setVariable("modelInvariantUuid", serviceInvariantUuid)
167         execution.setVariable("modelUuid", serviceUuid)
168         execution.setVariable("serviceModelName", serviceModelName)
169
170         utils.log("INFO", "======== End prepareServiceTopologyRequest Process ======== ", isDebugEnabled)
171     }
172    
173     public void getCurrentResoure(DelegateExecution execution){
174             def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   
175         utils.log("INFO", "======== Start getCurrentResoure Process ======== ", isDebugEnabled)    
176             def currentIndex = execution.getVariable("currentResourceIndex")
177             List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")  
178             Resource currentResource = sequencedResourceList.get(currentIndex)
179         execution.setVariable("resourceType", currentResource.getModelInfo().getModelName())
180             utils.log("INFO", "Now we deal with resouce:" + currentResource.getModelInfo().getModelName(), isDebugEnabled)  
181         utils.log("INFO", "======== COMPLETED getCurrentResoure Process ======== ", isDebugEnabled)  
182     }
183     
184     public void parseNextResource(DelegateExecution execution){
185         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
186         utils.log("INFO", "======== Start parseNextResource Process ======== ", isDebugEnabled)    
187         def currentIndex = execution.getVariable("currentResourceIndex")
188         def nextIndex =  currentIndex + 1
189         execution.setVariable("currentResourceIndex", nextIndex)
190         List<String> sequencedResourceList = execution.getVariable("sequencedResourceList")    
191         if(nextIndex >= sequencedResourceList.size()){
192             execution.setVariable("allResourceFinished", "true")
193         }else{
194             execution.setVariable("allResourceFinished", "false")
195         }
196         utils.log("INFO", "======== COMPLETED parseNextResource Process ======== ", isDebugEnabled)       
197     }    
198
199          public void prepareResourceRecipeRequest(DelegateExecution execution){
200                  def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
201                  utils.log("INFO", "======== Start prepareResourceRecipeRequest Process ======== ", isDebugEnabled)
202                  ResourceInput resourceInput = new ResourceInput()
203                  String serviceInstanceName = execution.getVariable("serviceInstanceName")
204          String resourceType = execution.getVariable("resourceType")
205                  String resourceInstanceName = resourceType + "_" + serviceInstanceName
206                  resourceInput.setResourceInstanceName(resourceInstanceName)
207                  utils.log("INFO", "Prepare Resource Request resourceInstanceName:" + resourceInstanceName, isDebugEnabled)
208                  String globalSubscriberId = execution.getVariable("globalSubscriberId")
209                  String serviceType = execution.getVariable("serviceType")
210                  String serviceInstanceId = execution.getVariable("serviceInstanceId")
211                  String operationId = execution.getVariable("operationId")
212                  String operationType = "createInstance"
213                  resourceInput.setGlobalSubscriberId(globalSubscriberId)
214                  resourceInput.setServiceType(serviceType)
215                  resourceInput.setServiceInstanceId(serviceInstanceId)
216                  resourceInput.setOperationId(operationId)
217                  resourceInput.setOperationType(operationType);
218                  def currentIndex = execution.getVariable("currentResourceIndex")
219                  List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")
220                  Resource currentResource = sequencedResourceList.get(currentIndex)
221                  resourceInput.setResourceModelInfo(currentResource.getModelInfo());
222                  ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
223                  resourceInput.setServiceModelInfo(serviceDecomposition.getModelInfo());
224          def String resourceCustomizationUuid = currentResource.getModelInfo().getModelCustomizationUuid();
225                  
226                  String incomingRequest = execution.getVariable("uuiRequest")
227                  //set the requestInputs from tempalte  To Be Done
228                  String serviceModelUuid = jsonUtil.getJsonValue(incomingRequest,"service.serviceUuid")
229          String serviceParameters = jsonUtil.getJsonValue(incomingRequest, "service.parameters")
230                  String resourceParameters = ResourceRequestBuilder.buildResourceRequestParameters(execution, serviceModelUuid, resourceCustomizationUuid, serviceParameters)
231                  resourceInput.setResourceParameters(resourceParameters)
232                  execution.setVariable("resourceInput", resourceInput)
233                  utils.log("INFO", "======== COMPLETED prepareResourceRecipeRequest Process ======== ", isDebugEnabled)
234          }
235          
236          public void executeResourceRecipe(DelegateExecution execution){
237                  def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
238                  utils.log("INFO", "======== Start executeResourceRecipe Process ======== ", isDebugEnabled)
239                  String requestId = execution.getVariable("msoRequestId")
240                  String serviceInstanceId = execution.getVariable("serviceInstanceId")
241                  String serviceType = execution.getVariable("serviceType")
242                  ResourceInput resourceInput = execution.getVariable("resourceInput")
243                  
244                  // requestAction is action, not opertiontype
245                  //String requestAction = resourceInput.getOperationType()
246                  String requestAction = "createInstance"
247                  JSONObject resourceRecipe = cutils.getResourceRecipe(execution, resourceInput.getResourceModelInfo().getModelUuid(), requestAction)
248          String recipeURL = BPMNProperties.getProperty("bpelURL", "http://mso:8080") + resourceRecipe.getString("orchestrationUri")
249                  int recipeTimeOut = resourceRecipe.getInt("recipeTimeout")
250                  String recipeParamXsd = resourceRecipe.get("paramXSD")
251                  HttpResponse resp = BpmnRestClient.post(recipeURL, requestId, recipeTimeOut, requestAction, serviceInstanceId, serviceType, resourceInput.toString(), recipeParamXsd)
252          utils.log("INFO", "======== end executeResourceRecipe Process ======== ", isDebugEnabled)
253          }
254     
255      public void postConfigRequest(DelegateExecution execution){
256          //now do noting
257      }
258 }