a53540ac89fa17407b16d59b76ead09a4d2f4b86
[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.openecomp.mso.bpmn.infrastructure.scripts
22
23 import java.util.ArrayList
24 import java.util.Iterator
25 import java.util.List
26 import org.apache.commons.lang3.StringUtils
27 import org.apache.http.HttpResponse
28 import org.camunda.bpm.engine.delegate.DelegateExecution
29 import org.codehaus.groovy.runtime.ArrayUtil
30 import org.codehaus.groovy.runtime.ScriptBytecodeAdapter
31 import org.codehaus.groovy.runtime.callsite.CallSite
32 import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation
33 import org.codehaus.groovy.runtime.typehandling.ShortTypeHandling
34 import org.json.JSONArray
35 import org.json.JSONObject
36 import org.openecomp.mso.bpmn.common.recipe.BpmnRestClient
37 import org.openecomp.mso.bpmn.common.recipe.ResourceInput
38 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
39 import org.openecomp.mso.bpmn.common.scripts.CatalogDbUtils
40 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
41 import org.openecomp.mso.bpmn.core.domain.AllottedResource
42 import org.openecomp.mso.bpmn.core.domain.NetworkResource
43 import org.openecomp.mso.bpmn.core.domain.Resource
44 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition
45 import org.openecomp.mso.bpmn.core.domain.VnfResource
46 import org.openecomp.mso.bpmn.core.json.JsonUtils
47 import org.openecomp.mso.bpmn.common.resource.ResourceRequestBuilder
48
49 /**
50  * This groovy class supports the <class>DoCreateResources.bpmn</class> process.
51  * 
52  * Inputs:
53  * @param - msoRequestId
54  * @param - globalSubscriberId - O
55  * @param - subscriptionServiceType - O
56  * @param - serviceInstanceId
57  * @param - serviceInstanceName - O
58  * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
59  * @param - sdncVersion 
60  *
61  * @param - addResourceList
62  *
63  * Outputs:
64  * @param - WorkflowException
65
66  */
67 public class DoCreateResources extends AbstractServiceTaskProcessor
68 {
69         ExceptionUtil exceptionUtil = new ExceptionUtil()
70         JsonUtils jsonUtil = new JsonUtils()
71         CatalogDbUtils cutils = new CatalogDbUtils()
72
73     public void preProcessRequest(DelegateExecution execution)
74     {
75                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
76                 utils.log("INFO"," ***** preProcessRequest *****",    isDebugEnabled)
77                 String msg = ""
78                 
79         List addResourceList = execution.getVariable("addResourceList")
80         if (addResourceList == null)
81         {
82             msg = "Input addResourceList is null"
83             utils.log("INFO", msg, isDebugEnabled)
84             exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)  
85         }
86         else if (addResourceList.size() == 0)
87         {
88             msg = "No resource in addResourceList"
89             utils.log("INFO", msg, isDebugEnabled)
90         }
91         utils.log("INFO", " ***** Exit preProcessRequest *****", isDebugEnabled)
92     }
93     
94     public void sequenceResoure(DelegateExecution execution)
95     {
96         def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
97         utils.log("INFO", "======== Start sequenceResoure Process ======== ", isDebugEnabled)
98         
99         String serviceModelUUID = execution.getVariable("modelUuid")      
100                
101         List<Resource> addResourceList = execution.getVariable("addResourceList")        
102
103         //we use VF to define a network service
104         List<VnfResource> vnfResourceList = new ArrayList<VnfResource>()
105         //here wan is defined as a network resource
106         List<NetworkResource> networkResourceList = new ArrayList<NetworkResource>()
107         //allotted resource      
108         List<AllottedResource> arResourceList = new ArrayList<AllottedResource>()
109
110         //define sequenced resource list, we deploy vf first and then network and then ar 
111         //this is defaule sequence
112         List<Resource> sequencedResourceList = new ArrayList<Resource>()
113         for (Resource rc : addResourceList){
114         if (rc instanceof VnfResource) {
115                 vnfResourceList.add(rc)
116             } else if (rc instanceof NetworkResource) {
117                 networkResourceList.add(rc)
118             } else if (rc instanceof AllottedResource) {
119                 arResourceList.add(rc)
120             }
121         }        
122         sequencedResourceList.addAll(vnfResourceList)
123         sequencedResourceList.addAll(networkResourceList)
124         sequencedResourceList.addAll(arResourceList)
125         
126         String isContainsWanResource = networkResourceList.isEmpty() ? "false" : "true"
127         execution.setVariable("isContainsWanResource", isContainsWanResource)
128         execution.setVariable("currentResourceIndex", 0)
129         execution.setVariable("sequencedResourceList", sequencedResourceList)
130         utils.log("INFO", "sequencedResourceList: " + sequencedResourceList, isDebugEnabled) 
131         utils.log("INFO", "======== COMPLETED sequenceResoure Process ======== ", isDebugEnabled)
132     }   
133    
134     public void getCurrentResoure(DelegateExecution execution){
135             def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   
136         utils.log("INFO", "======== Start getCurrentResoure Process ======== ", isDebugEnabled)    
137             def currentIndex = execution.getVariable("currentResourceIndex")
138             List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")  
139             Resource currentResource = sequencedResourceList.get(currentIndex)
140             utils.log("INFO", "Now we deal with resouce:" + currentResource.getModelInfo().getModelName(), isDebugEnabled)  
141         utils.log("INFO", "======== COMPLETED getCurrentResoure Process ======== ", isDebugEnabled)  
142     }
143     
144     public void parseNextResource(DelegateExecution execution){
145         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
146         utils.log("INFO", "======== Start parseNextResource Process ======== ", isDebugEnabled)    
147         def currentIndex = execution.getVariable("currentResourceIndex")
148         def nextIndex =  currentIndex + 1
149         execution.setVariable("currentResourceIndex", nextIndex)
150         List<String> sequencedResourceList = execution.getVariable("sequencedResourceList")    
151         if(nextIndex >= sequencedResourceList.size()){
152             execution.setVariable("allResourceFinished", "true")
153         }else{
154             execution.setVariable("allResourceFinished", "false")
155         }
156         utils.log("INFO", "======== COMPLETED parseNextResource Process ======== ", isDebugEnabled)       
157     }    
158
159          public void prepareResourceRecipeRequest(DelegateExecution execution){
160                  def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
161                  utils.log("INFO", "======== Start prepareResourceRecipeRequest Process ======== ", isDebugEnabled)
162                  ResourceInput resourceInput = new ResourceInput()
163                  String serviceInstanceName = execution.getVariable("serviceInstanceName")
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                  
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(DelegateExecution 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.getResourceModelInfo().getModelUuid(), 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(DelegateExecution execution){
211          //now do noting
212      }
213 }