a7d453d56db53544291ce8f170d94cd4c0b273a8
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.scripts
24
25 import org.onap.so.bpmn.common.scripts.CatalogDbUtilsFactory
26 import org.onap.so.bpmn.core.domain.GroupResource
27 import org.onap.so.bpmn.infrastructure.properties.BPMNProperties
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.json.JSONObject
33 import org.onap.so.bpmn.common.recipe.BpmnRestClient
34 import org.onap.so.bpmn.common.recipe.ResourceInput
35 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
36 import org.onap.so.bpmn.common.scripts.CatalogDbUtils
37 import org.onap.so.bpmn.common.scripts.ExceptionUtil
38 import org.onap.so.bpmn.core.domain.AllottedResource
39 import org.onap.so.bpmn.core.domain.NetworkResource
40 import org.onap.so.bpmn.core.domain.Resource
41 import org.onap.so.bpmn.core.domain.ServiceDecomposition
42 import org.onap.so.bpmn.core.domain.VnfResource
43 import org.onap.so.bpmn.core.json.JsonUtils
44 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
45 import org.slf4j.Logger
46 import org.slf4j.LoggerFactory
47
48
49
50 /**
51  * This groovy class supports the <class>DoCreateResources.bpmn</class> process.
52  * 
53  * Inputs:
54  * @param - msoRequestId
55  * @param - globalSubscriberId - O
56  * @param - subscriptionServiceType - O
57  * @param - serviceInstanceId
58  * @param - serviceInstanceName - O
59  * @param - serviceInputParams (should contain aic_zone for serviceTypes TRANSPORT,ATM)
60  * @param - sdncVersion 
61  *
62  * @param - addResourceList
63  *
64  * Outputs:
65  * @param - WorkflowException
66  */
67 public class DoCreateResources extends AbstractServiceTaskProcessor{
68     private static final Logger logger = LoggerFactory.getLogger( DoCreateResources.class);
69
70     ExceptionUtil exceptionUtil = new ExceptionUtil()
71     JsonUtils jsonUtil = new JsonUtils()
72     CatalogDbUtils catalogDbUtils = new CatalogDbUtilsFactory().create()
73
74     public void preProcessRequest(DelegateExecution execution) {
75         logger.trace("preProcessRequest ")
76         String msg = ""
77
78         List addResourceList = execution.getVariable("addResourceList")
79         if (addResourceList == null) {
80             msg = "Input addResourceList is null"
81             logger.info(msg)
82             exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
83         }
84         else if (addResourceList.size() == 0) {
85             msg = "No resource in addResourceList"
86             logger.info(msg)
87         }
88         logger.trace("Exit preProcessRequest ")
89     }
90
91     public void sequenceResoure(DelegateExecution execution) {
92         logger.trace("Start sequenceResoure Process ")
93         
94         String incomingRequest = execution.getVariable("uuiRequest")
95         String serviceModelUuid = jsonUtil.getJsonValue(incomingRequest,"service.serviceUuid")
96
97         List<Resource> addResourceList = execution.getVariable("addResourceList")
98
99         List<NetworkResource> networkResourceList = new ArrayList<NetworkResource>()
100
101         List<Resource> sequencedResourceList = new ArrayList<Resource>()
102
103         String serviceDecompose = execution.getVariable("serviceDecomposition")
104         String serviceModelName = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelName")
105
106         // get Sequence from properties        
107         def resourceSequence = BPMNProperties.getResourceSequenceProp(serviceModelName)
108
109         // get Sequence from csar(model)  
110         if(resourceSequence == null) {
111             resourceSequence = ResourceRequestBuilder.getResourceSequence(serviceModelUuid)
112             logger.info("Get Sequence from csar : " + resourceSequence)
113         }
114
115         if(resourceSequence != null) {
116             for (resourceType in resourceSequence) {
117                 for (resource in addResourceList) {
118                     if (StringUtils.containsIgnoreCase(resource.getModelInfo().getModelName(), resourceType)) {
119                         sequencedResourceList.add(resource)
120
121                         // if resource type is vnfResource then check for groups also
122                         // Did not use continue because if same model type is used twice
123                         // then we would like to add it twice for processing
124                         // e.g.  S{ V1{G1, G2, G1}} --> S{ V1{G1, G1, G2}}
125                         if (resource instanceof VnfResource) {
126                             if (resource.getGroups() != null) {
127                                 String[] grpSequence = resource.getGroupOrder().split(",")
128                                 for (String grpType in grpSequence) {
129                                     for (GroupResource gResource in resource.getGroups()) {
130                                         if (StringUtils.containsIgnoreCase(gResource.getModelInfo().getModelName(), grpType)) {
131                                             sequencedResourceList.add(gResource)
132                                         }
133                                     }
134                                 }
135                             }
136                         }
137                         if (resource instanceof NetworkResource) {
138                             networkResourceList.add(resource)
139                         }
140                     }
141                 }
142             }
143         } else {
144
145             //define sequenced resource list, we deploy vf first and then network and then ar
146             //this is default sequence
147             List<VnfResource> vnfResourceList = new ArrayList<VnfResource>()
148             List<AllottedResource> arResourceList = new ArrayList<AllottedResource>()
149
150             for (Resource rc : addResourceList){
151                 if (rc instanceof VnfResource) {
152                     vnfResourceList.add(rc)
153                 } else if (rc instanceof NetworkResource) {
154                     networkResourceList.add(rc)
155                 } else if (rc instanceof AllottedResource) {
156                     arResourceList.add(rc)
157                 }
158             }
159             sequencedResourceList.addAll(vnfResourceList)
160             sequencedResourceList.addAll(networkResourceList)
161             sequencedResourceList.addAll(arResourceList)
162         }
163
164         String isContainsWanResource = networkResourceList.isEmpty() ? "false" : "true"
165         //if no networkResource, get SDNC config from properties file
166         if( "false".equals(isContainsWanResource)) {
167             String serviceNeedSDNC = "mso.workflow.custom." + serviceModelName + ".sdnc.need";
168             isContainsWanResource = BPMNProperties.getProperty(serviceNeedSDNC, isContainsWanResource)
169         }
170
171         execution.setVariable("isContainsWanResource", isContainsWanResource)
172         execution.setVariable("currentResourceIndex", 0)
173         execution.setVariable("sequencedResourceList", sequencedResourceList)
174         logger.info("sequencedResourceList: " + sequencedResourceList)
175         logger.trace("COMPLETED sequenceResoure Process ")
176     }
177
178     public prepareServiceTopologyRequest(DelegateExecution execution) {
179
180         logger.trace("======== Start prepareServiceTopologyRequest Process ======== ")
181
182         String serviceDecompose = execution.getVariable("serviceDecomposition")
183
184         execution.setVariable("operationType", "create")
185         execution.setVariable("resourceType", "")
186
187         String serviceInvariantUuid = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelInvariantUuid")
188         String serviceUuid = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelUuid")
189         String serviceModelName = jsonUtil.getJsonValue(serviceDecompose, "serviceResources.modelInfo.modelName")
190
191         execution.setVariable("modelInvariantUuid", serviceInvariantUuid)
192         execution.setVariable("modelUuid", serviceUuid)
193         execution.setVariable("serviceModelName", serviceModelName)
194
195         logger.trace("======== End prepareServiceTopologyRequest Process ======== ")
196     }
197
198     public void getCurrentResoure(DelegateExecution execution){
199         logger.trace("Start getCurrentResoure Process ")
200         def currentIndex = execution.getVariable("currentResourceIndex")
201         List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")
202         Resource currentResource = sequencedResourceList.get(currentIndex)
203         execution.setVariable("resourceType", currentResource.getModelInfo().getModelName())
204         logger.info("Now we deal with resouce:" + currentResource.getModelInfo().getModelName())
205         logger.trace("COMPLETED getCurrentResoure Process ")
206     }
207
208     public void parseNextResource(DelegateExecution execution){
209         logger.trace("Start parseNextResource Process ")
210         def currentIndex = execution.getVariable("currentResourceIndex")
211         def nextIndex =  currentIndex + 1
212         execution.setVariable("currentResourceIndex", nextIndex)
213         List<String> sequencedResourceList = execution.getVariable("sequencedResourceList")
214         if(nextIndex >= sequencedResourceList.size()){
215             execution.setVariable("allResourceFinished", "true")
216         }else{
217             execution.setVariable("allResourceFinished", "false")
218         }
219         logger.trace("COMPLETED parseNextResource Process ")
220     }
221
222     public void prepareResourceRecipeRequest(DelegateExecution execution){
223         logger.trace("Start prepareResourceRecipeRequest Process ")
224         ResourceInput resourceInput = new ResourceInput()
225         String serviceInstanceName = execution.getVariable("serviceInstanceName")
226         String resourceType = execution.getVariable("resourceType")
227         String resourceInstanceName = resourceType + "_" + serviceInstanceName
228         resourceInput.setResourceInstanceName(resourceInstanceName)
229         logger.info("Prepare Resource Request resourceInstanceName:" + resourceInstanceName)
230         String globalSubscriberId = execution.getVariable("globalSubscriberId")
231         String serviceType = execution.getVariable("serviceType")
232         String serviceInstanceId = execution.getVariable("serviceInstanceId")
233         String operationId = execution.getVariable("operationId")
234         String operationType = "createInstance"
235         resourceInput.setGlobalSubscriberId(globalSubscriberId)
236         resourceInput.setServiceType(serviceType)
237         resourceInput.setServiceInstanceId(serviceInstanceId)
238         resourceInput.setOperationId(operationId)
239         resourceInput.setOperationType(operationType);
240         def currentIndex = execution.getVariable("currentResourceIndex")
241         List<Resource> sequencedResourceList = execution.getVariable("sequencedResourceList")
242         Resource currentResource = sequencedResourceList.get(currentIndex)
243         resourceInput.setResourceModelInfo(currentResource.getModelInfo());
244         ServiceDecomposition serviceDecomposition = execution.getVariable("serviceDecomposition")
245         resourceInput.setServiceModelInfo(serviceDecomposition.getModelInfo());
246         def String resourceCustomizationUuid = currentResource.getModelInfo().getModelCustomizationUuid();
247
248         String incomingRequest = execution.getVariable("uuiRequest")
249         //set the requestInputs from tempalte  To Be Done
250         String serviceModelUuid = jsonUtil.getJsonValue(incomingRequest,"service.serviceUuid")
251         String serviceParameters = jsonUtil.getJsonValue(incomingRequest, "service.parameters")
252         String resourceParameters = ResourceRequestBuilder.buildResourceRequestParameters(execution, serviceModelUuid, resourceCustomizationUuid, serviceParameters)
253         resourceInput.setResourceParameters(resourceParameters)
254         resourceInput.setRequestsInputs(incomingRequest)
255         execution.setVariable("resourceInput", resourceInput.toString())
256         execution.setVariable("resourceModelUUID", resourceInput.getResourceModelInfo().getModelUuid())
257         logger.trace("COMPLETED prepareResourceRecipeRequest Process ")
258     }
259
260     public void executeResourceRecipe(DelegateExecution execution){
261         logger.trace("Start executeResourceRecipe Process ")
262
263         try {
264             String requestId = execution.getVariable("msoRequestId")
265             String serviceInstanceId = execution.getVariable("serviceInstanceId")
266             String serviceType = execution.getVariable("serviceType")
267             String resourceInput = execution.getVariable("resourceInput")
268             String resourceModelUUID = execution.getVariable("resourceModelUUID")
269
270             // requestAction is action, not opertiontype
271             //String requestAction = resourceInput.getOperationType()
272             String requestAction = "createInstance"
273             JSONObject resourceRecipe = catalogDbUtils.getResourceRecipe(execution, resourceModelUUID, requestAction)
274
275             if (resourceRecipe != null) {
276                 String recipeURL = BPMNProperties.getProperty("bpelURL", "http://so-bpmn-infra.onap:8081") + resourceRecipe.getString("orchestrationUri")
277                 int recipeTimeOut = resourceRecipe.getInt("recipeTimeout")
278                 String recipeParamXsd = resourceRecipe.get("paramXSD")
279
280                 BpmnRestClient bpmnRestClient = new BpmnRestClient()
281                 HttpResponse resp = bpmnRestClient.post(recipeURL, requestId, recipeTimeOut, requestAction, serviceInstanceId, serviceType, resourceInput, recipeParamXsd)
282             } else {
283                 String exceptionMessage = "Resource receipe is not found for resource modeluuid: " + resourceModelUUID
284                 logger.trace(exceptionMessage)
285                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, exceptionMessage)
286             }
287
288             logger.trace("======== end executeResourceRecipe Process ======== ")
289         }catch(BpmnError b){
290             logger.debug("Rethrowing MSOWorkflowException")
291             throw b
292         }catch(Exception e){
293             logger.debug("Error occured within DoCreateResources executeResourceRecipe method: " + e)
294             exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured during DoCreateResources executeResourceRecipe Catalog")
295         }
296     }
297
298     public void postConfigRequest(DelegateExecution execution){
299         //now do noting
300     }
301 }