482da23b33df23198de59a0ce9c51e8cc3b7d64e
[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         //if no networkResource, get SDNC config from properties file
153         if( "false".equals(isContainsWanResource)) {
154             String serviceNeedSDNC = "mso.workflow.custom." + serviceModelName + ".sdnc.need";
155             isContainsWanResource = BPMNProperties.getProperty(serviceNeedSDNC, isContainsWanResource)
156         }
157                                 
158         execution.setVariable("isContainsWanResource", isContainsWanResource)
159         execution.setVariable("currentResourceIndex", 0)
160         execution.setVariable("sequencedResourceList", sequencedResourceList)
161         msoLogger.info("sequencedResourceList: " + sequencedResourceList) 
162         msoLogger.trace("COMPLETED sequenceResoure Process ")
163     }
164
165     public prepareServiceTopologyRequest(DelegateExecution execution) {
166
167         msoLogger.trace("======== Start prepareServiceTopologyRequest Process ======== ")
168
169         String serviceDecompose = execution.getVariable("serviceDecomposition")
170
171         execution.setVariable("operationType", "create")
172         execution.setVariable("resourceType", "")
173
174         String serviceInvariantUuid = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelInvariantUuid")
175         String serviceUuid = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelUuid")
176         String serviceModelName = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelName")
177
178         execution.setVariable("modelInvariantUuid", serviceInvariantUuid)
179         execution.setVariable("modelUuid", serviceUuid)
180         execution.setVariable("serviceModelName", serviceModelName)
181
182         msoLogger.trace("======== End prepareServiceTopologyRequest Process ======== ")
183     }
184    
185     public void getCurrentResoure(DelegateExecution execution){
186             msoLogger.trace("Start getCurrentResoure Process ")    
187             def currentIndex = execution.getVariable("currentResourceIndex")
188             List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")  
189             Resource currentResource = sequencedResourceList.get(currentIndex)
190             execution.setVariable("resourceType", currentResource.getModelInfo().getModelName())
191             msoLogger.info("Now we deal with resouce:" + currentResource.getModelInfo().getModelName())  
192             msoLogger.trace("COMPLETED getCurrentResoure Process ")  
193     }
194     
195     public void parseNextResource(DelegateExecution execution){
196         msoLogger.trace("Start parseNextResource Process ")    
197         def currentIndex = execution.getVariable("currentResourceIndex")
198         def nextIndex =  currentIndex + 1
199         execution.setVariable("currentResourceIndex", nextIndex)
200         List<String> sequencedResourceList = execution.getVariable("sequencedResourceList")    
201         if(nextIndex >= sequencedResourceList.size()){
202             execution.setVariable("allResourceFinished", "true")
203         }else{
204             execution.setVariable("allResourceFinished", "false")
205         }
206         msoLogger.trace("COMPLETED parseNextResource Process ")       
207     }    
208
209          public void prepareResourceRecipeRequest(DelegateExecution execution){
210                  msoLogger.trace("Start prepareResourceRecipeRequest Process ")
211                  ResourceInput resourceInput = new ResourceInput()
212                  String serviceInstanceName = execution.getVariable("serviceInstanceName")
213                  String resourceType = execution.getVariable("resourceType")
214                  String resourceInstanceName = resourceType + "_" + serviceInstanceName
215                  resourceInput.setResourceInstanceName(resourceInstanceName)
216                  msoLogger.info("Prepare Resource Request resourceInstanceName:" + resourceInstanceName)
217                  String globalSubscriberId = execution.getVariable("globalSubscriberId")
218                  String serviceType = execution.getVariable("serviceType")
219                  String serviceInstanceId = execution.getVariable("serviceInstanceId")
220                  String operationId = execution.getVariable("operationId")
221                  String operationType = "createInstance"
222                  resourceInput.setGlobalSubscriberId(globalSubscriberId)
223                  resourceInput.setServiceType(serviceType)
224                  resourceInput.setServiceInstanceId(serviceInstanceId)
225                  resourceInput.setOperationId(operationId)
226                  resourceInput.setOperationType(operationType);
227                  def currentIndex = execution.getVariable("currentResourceIndex")
228                  List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")
229                  Resource currentResource = sequencedResourceList.get(currentIndex)
230                  resourceInput.setResourceModelInfo(currentResource.getModelInfo());
231                  ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
232                  resourceInput.setServiceModelInfo(serviceDecomposition.getModelInfo());
233                  def String resourceCustomizationUuid = currentResource.getModelInfo().getModelCustomizationUuid();
234                  
235                  String incomingRequest = execution.getVariable("uuiRequest")
236                  //set the requestInputs from tempalte  To Be Done
237                  String serviceModelUuid = jsonUtil.getJsonValue(incomingRequest,"service.serviceUuid")
238                  String serviceParameters = jsonUtil.getJsonValue(incomingRequest, "service.parameters")
239                  String resourceParameters = ResourceRequestBuilder.buildResourceRequestParameters(execution, serviceModelUuid, resourceCustomizationUuid, serviceParameters)
240                  resourceInput.setResourceParameters(resourceParameters)
241                  resourceInput.setRequestsInputs(incomingRequest)
242                  execution.setVariable("resourceInput", resourceInput)
243                  msoLogger.trace("COMPLETED prepareResourceRecipeRequest Process ")
244          }
245          
246          public void executeResourceRecipe(DelegateExecution execution){
247                  msoLogger.trace("Start executeResourceRecipe Process ")
248                  
249                  try {
250                          String requestId = execution.getVariable("msoRequestId")
251                          String serviceInstanceId = execution.getVariable("serviceInstanceId")
252                          String serviceType = execution.getVariable("serviceType")
253                          ResourceInput resourceInput = execution.getVariable("resourceInput")
254                          
255                          // requestAction is action, not opertiontype
256                          //String requestAction = resourceInput.getOperationType()
257                          String requestAction = "createInstance"
258                          JSONObject resourceRecipe = cutils.getResourceRecipe(execution, resourceInput.getResourceModelInfo().getModelUuid(), requestAction)
259
260                          if (resourceRecipe != null) {
261                              String recipeURL = BPMNProperties.getProperty("bpelURL", "http://mso:8080") + resourceRecipe.getString("orchestrationUri")
262                              int recipeTimeOut = resourceRecipe.getInt("recipeTimeout")
263                              String recipeParamXsd = resourceRecipe.get("paramXSD")
264                              HttpResponse resp = BpmnRestClient.post(recipeURL, requestId, recipeTimeOut, requestAction, serviceInstanceId, serviceType, resourceInput.toString(), recipeParamXsd)
265                          } else {
266                              String exceptionMessage = "Resource receipe is not found for resource modeluuid: " +
267                              resourceInput.getResourceModelInfo().getModelUuid()
268                              msoLogger.trace(exceptionMessage)
269                              exceptionUtil.buildAndThrowWorkflowException(execution, 500, exceptionMessage)
270                          }
271
272                          msoLogger.trace("======== end executeResourceRecipe Process ======== ")
273                  }catch(BpmnError b){
274                          msoLogger.debug("Rethrowing MSOWorkflowException")
275                          throw b
276                  }catch(Exception e){
277                          msoLogger.debug("Error occured within DoCreateResources executeResourceRecipe method: " + e)
278                          exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured during DoCreateResources executeResourceRecipe Catalog")
279                  }
280          }
281
282      public void postConfigRequest(DelegateExecution execution){
283          //now do noting
284      }
285 }