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