2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.bpmn.infrastructure.scripts
23 import org.onap.so.client.aai.AAIObjectType
24 import org.onap.so.client.aai.entities.uri.AAIResourceUri
25 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
29 import org.onap.so.bpmn.common.scripts.ExceptionUtil
30 import org.onap.so.bpmn.core.json.JsonUtils
31 import org.onap.so.client.HttpClient
32 import org.onap.so.logger.MessageEnum
33 import org.onap.so.logger.MsoLogger
34 import org.onap.so.rest.APIResponse
35 import org.onap.so.rest.RESTClient
36 import org.onap.so.rest.RESTConfig
41 * This groovy class supports the <class>DoCreateVFCNetworkServiceInstance.bpmn</class> process.
42 * flow for VFC Network Service Create
44 public class CreateVFCNSResource extends AbstractServiceTaskProcessor {
45 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CreateVFCNSResource.class);
48 String vfcUrl = "/vfc/rest/v1/vfcadapter"
50 String host = "http://mso.mso.testlab.openecomp.org:8080"
52 ExceptionUtil exceptionUtil = new ExceptionUtil()
54 JsonUtils jsonUtil = new JsonUtils()
58 * Pre Process the BPMN Flow Request
60 * generate the nsOperationKey
61 * generate the nsParameters
63 public void preProcessRequest (DelegateExecution execution) {
64 JsonUtils jsonUtil = new JsonUtils()
67 msoLogger.trace("preProcessRequest() ")
69 //deal with nsName and Description
70 String resourceInput = execution.getVariable("resourceInput")
72 String resourceParameters = jsonUtil.getJsonValue(resourceInput, "resourceParameters")
74 String resourceName = jsonUtil.getJsonValue(resourceInput, "resourceInstanceName")
75 execution.setVariable("nsServiceName", resourceName)
77 String nsServiceDescription = execution.getVariable("nsServiceDescription")
78 msoLogger.info("nsServiceName:" + resourceName + " nsServiceDescription:" + nsServiceDescription)
79 //deal with operation key
80 String globalSubscriberId = jsonUtil.getJsonValue(resourceInput, "globalSubscriberId")
81 msoLogger.info("globalSubscriberId:" + globalSubscriberId)
82 //set local globalSubscriberId variable
83 execution.setVariable("globalSubscriberId", globalSubscriberId);
84 String serviceType = execution.getVariable("serviceType")
85 msoLogger.info("serviceType:" + serviceType)
87 String serviceId = execution.getVariable("serviceInstanceId")
88 msoLogger.info("serviceId:" + serviceId)
90 String operationId = jsonUtil.getJsonValue(resourceInput, "operationId")
91 msoLogger.info("serviceType:" + serviceType)
93 String nodeTemplateUUID = jsonUtil.getJsonValue(resourceInput, "resourceModelInfo.modelCustomizationUuid")
94 String nsServiceModelUUID = jsonUtil.getJsonValue(resourceParameters, "requestInputs.nsd0_providing_service_uuid")
95 msoLogger.info("nodeTemplateUUID:" + nodeTemplateUUID)
97 * segmentInformation needed as a object of segment
100 * "nodeTemplateName":"",
103 * //this is the nsParameters sent to VF-C
107 String nsParameters = jsonUtil.getJsonValue(resourceInput, "resourceParameters")
108 msoLogger.info("nsParameters:" + nsParameters)
109 String nsOperationKey = """{
110 "globalSubscriberId":"${globalSubscriberId}",
111 "serviceType":"${serviceType}",
112 "serviceId":"${serviceId}",
113 "operationId":"${operationId}",
114 "nodeTemplateUUID":"${nodeTemplateUUID}"
116 execution.setVariable("nsOperationKey", nsOperationKey);
117 execution.setVariable("nsParameters", nsParameters)
118 execution.setVariable("nsServiceModelUUID", nsServiceModelUUID);
121 } catch (BpmnError e) {
123 } catch (Exception ex){
124 msg = "Exception in preProcessRequest " + ex.getMessage()
126 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
128 msoLogger.trace("Exit preProcessRequest ")
134 public void createNetworkService(DelegateExecution execution) {
135 msoLogger.trace("createNetworkService ")
136 String nsOperationKey = execution.getVariable("nsOperationKey");
137 String nsServiceModelUUID = execution.getVariable("nsServiceModelUUID");
138 String nsParameters = execution.getVariable("nsParameters");
139 String nsServiceName = execution.getVariable("nsServiceName")
140 String nsServiceDescription = execution.getVariable("nsServiceDescription")
141 String locationConstraints = jsonUtil.getJsonValue(nsParameters, "locationConstraints")
142 String requestInputs = jsonUtil.getJsonValue(nsParameters, "requestInputs")
144 "nsServiceName":"${nsServiceName}",
145 "nsServiceDescription":"${nsServiceDescription}",
146 "nsServiceModelUUID":"${nsServiceModelUUID}",
147 "nsOperationKey":${nsOperationKey},
149 "locationConstraints":${locationConstraints},
150 "additionalParamForNs":${requestInputs}
153 APIResponse apiResponse = postRequest(execution, host + vfcUrl + "/ns", reqBody)
154 String returnCode = apiResponse.getStatusCode()
155 String aaiResponseAsString = apiResponse.getResponseBodyAsString()
156 String nsInstanceId = "";
157 if(returnCode== "200" || returnCode == "201"){
158 nsInstanceId = jsonUtil.getJsonValue(aaiResponseAsString, "nsInstanceId")
160 execution.setVariable("nsInstanceId", nsInstanceId)
161 msoLogger.info(" *****Exit createNetworkService *****")
165 * instantiate NS task
167 public void instantiateNetworkService(DelegateExecution execution) {
168 msoLogger.trace("instantiateNetworkService ")
169 String nsOperationKey = execution.getVariable("nsOperationKey");
170 String nsParameters = execution.getVariable("nsParameters");
171 String nsServiceName = execution.getVariable("nsServiceName")
172 String nsServiceDescription = execution.getVariable("nsServiceDescription")
174 "nsServiceName":"${nsServiceName}",
175 "nsServiceDescription":"${nsServiceDescription}",
176 "nsOperationKey":${nsOperationKey},
177 "nsParameters":${nsParameters}
179 String nsInstanceId = execution.getVariable("nsInstanceId")
180 String url = host + vfcUrl + "/ns/" +nsInstanceId + "/instantiate"
181 APIResponse apiResponse = postRequest(execution, url, reqBody)
182 String returnCode = apiResponse.getStatusCode()
183 String aaiResponseAsString = apiResponse.getResponseBodyAsString()
185 if(returnCode== "200"|| returnCode == "201"){
186 jobId = jsonUtil.getJsonValue(aaiResponseAsString, "jobId")
188 execution.setVariable("jobId", jobId)
189 msoLogger.info(" *****Exit instantiateNetworkService *****")
195 public void queryNSProgress(DelegateExecution execution) {
196 msoLogger.trace("queryNSProgress ")
197 String jobId = execution.getVariable("jobId")
198 String nsOperationKey = execution.getVariable("nsOperationKey");
199 String url = host + vfcUrl + "/jobs/" + jobId
200 APIResponse apiResponse = postRequest(execution, url, nsOperationKey)
201 String returnCode = apiResponse.getStatusCode()
202 String aaiResponseAsString = apiResponse.getResponseBodyAsString()
203 String operationStatus = "error"
204 if(returnCode== "200"|| returnCode == "201"){
205 operationStatus = jsonUtil.getJsonValue(aaiResponseAsString, "responseDescriptor.status")
207 execution.setVariable("operationStatus", operationStatus)
208 msoLogger.info(" *****Exit queryNSProgress *****")
214 public void timeDelay(DelegateExecution execution) {
217 } catch(InterruptedException e) {
218 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Time Delay exception" + e , "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
225 public void addNSRelationship(DelegateExecution execution) {
226 msoLogger.trace("addNSRelationship ")
227 String nsInstanceId = execution.getVariable("nsInstanceId")
228 if(nsInstanceId == null || nsInstanceId == ""){
229 msoLogger.info(" create NS failed, so do not need to add relationship")
232 String globalSubscriberId = execution.getVariable("globalSubscriberId")
233 String serviceType = execution.getVariable("serviceType")
234 String serviceId = execution.getVariable("serviceInstanceId")
236 AAIResourceUri nsUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,globalSubscriberId,serviceType,nsInstanceId)
237 AAIResourceUri relatedServiceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,globalSubscriberId,serviceType,serviceId)
240 getAAIClient().connect(nsUri,relatedServiceUri)
241 msoLogger.info("NS relationship to Service added successfully")
243 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception occured while Creating NS relationship.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.getMessage(),e);
244 throw new BpmnError("MSOWorkflowException")
250 * url: the url of the request
251 * requestBody: the body of the request
253 private APIResponse postRequest(DelegateExecution execution, String url, String requestBody){
254 msoLogger.trace("Started Execute VFC adapter Post Process ")
255 msoLogger.info("url:"+url +"\nrequestBody:"+ requestBody)
256 APIResponse apiResponse = null
258 RESTConfig config = new RESTConfig(url);
259 RESTClient client = new RESTClient(config).addHeader("Content-Type", "application/json").addHeader("Accept","application/json").addHeader("Authorization","Basic QlBFTENsaWVudDpwYXNzd29yZDEk");
260 apiResponse = client.httpPost(requestBody)
261 msoLogger.info("response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString())
262 msoLogger.trace("Completed Execute VF-C adapter Post Process ")
264 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception occured while executing AAI Post Call.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e);
265 throw new BpmnError("MSOWorkflowException")
270 public void sendSyncResponse (DelegateExecution execution) {
271 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
272 utils.log("DEBUG", " *** sendSyncResponse *** ", isDebugEnabled)
275 String operationStatus = execution.getVariable("operationStatus")
276 // RESTResponse for main flow
277 String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
278 utils.log("DEBUG", " sendSyncResponse to APIH:" + "\n" + resourceOperationResp, isDebugEnabled)
279 sendWorkflowResponse(execution, 202, resourceOperationResp)
280 execution.setVariable("sentSyncResponse", true)
282 } catch (Exception ex) {
283 String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
284 utils.log("DEBUG", msg, isDebugEnabled)
285 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
287 utils.log("DEBUG"," ***** Exit sendSyncResopnse *****", isDebugEnabled)