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.client.RestRequest
33 import org.onap.so.logger.MessageEnum
34 import org.onap.so.logger.MsoLogger
35 import org.onap.so.rest.APIResponse
36 import org.onap.so.rest.RESTClient
37 import org.onap.so.rest.RESTConfig
38 import org.onap.so.bpmn.core.UrnPropertiesReader
41 //import javax.ws.rs.core.Response
42 import org.onap.so.utils.TargetEntity
45 * This groovy class supports the <class>DoCreateVFCNetworkServiceInstance.bpmn</class> process.
46 * flow for VFC Network Service Create
48 public class CreateVFCNSResource extends AbstractServiceTaskProcessor {
49 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CreateVFCNSResource.class);
51 // String vfcUrl = "/vfc/rest/v1/vfcadapter"
53 // String host = "http://mso.mso.testlab.openecomp.org:8080"
55 ExceptionUtil exceptionUtil = new ExceptionUtil()
57 JsonUtils jsonUtil = new JsonUtils()
61 * Pre Process the BPMN Flow Request
63 * generate the nsOperationKey
64 * generate the nsParameters
66 public void preProcessRequest (DelegateExecution execution) {
67 JsonUtils jsonUtil = new JsonUtils()
70 msoLogger.trace("preProcessRequest() ")
72 //deal with nsName and Description
73 String resourceInput = execution.getVariable("resourceInput")
75 String resourceParameters = jsonUtil.getJsonValue(resourceInput, "resourceParameters")
77 String resourceName = jsonUtil.getJsonValue(resourceInput, "resourceInstanceName")
78 execution.setVariable("nsServiceName", resourceName)
80 String nsServiceDescription = execution.getVariable("nsServiceDescription")
81 msoLogger.info("nsServiceName:" + resourceName + " nsServiceDescription:" + nsServiceDescription)
82 //deal with operation key
83 String globalSubscriberId = jsonUtil.getJsonValue(resourceInput, "globalSubscriberId")
84 msoLogger.info("globalSubscriberId:" + globalSubscriberId)
85 //set local globalSubscriberId variable
86 execution.setVariable("globalSubscriberId", globalSubscriberId);
87 String serviceType = execution.getVariable("serviceType")
88 msoLogger.info("serviceType:" + serviceType)
90 String serviceId = execution.getVariable("serviceInstanceId")
91 msoLogger.info("serviceId:" + serviceId)
93 String operationId = jsonUtil.getJsonValue(resourceInput, "operationId")
94 msoLogger.info("serviceType:" + serviceType)
96 String nodeTemplateUUID = jsonUtil.getJsonValue(resourceInput, "resourceModelInfo.modelCustomizationUuid")
97 String nsServiceModelUUID = jsonUtil.getJsonValue(resourceParameters, "requestInputs.nsd0_providing_service_uuid")
98 msoLogger.info("nodeTemplateUUID:" + nodeTemplateUUID)
100 * segmentInformation needed as a object of segment
103 * "nodeTemplateName":"",
106 * //this is the nsParameters sent to VF-C
110 String nsParameters = jsonUtil.getJsonValue(resourceInput, "resourceParameters")
111 msoLogger.info("nsParameters:" + nsParameters)
112 String nsOperationKey = """{
113 "globalSubscriberId":"${globalSubscriberId}",
114 "serviceType":"${serviceType}",
115 "serviceId":"${serviceId}",
116 "operationId":"${operationId}",
117 "nodeTemplateUUID":"${nodeTemplateUUID}"
119 execution.setVariable("nsOperationKey", nsOperationKey);
120 execution.setVariable("nsParameters", nsParameters)
121 execution.setVariable("nsServiceModelUUID", nsServiceModelUUID);
123 String vfcAdapterUrl = UrnPropertiesReader.getVariable("mso.adapters.vfc.rest.endpoint", execution)
125 if (vfcAdapterUrl == null || vfcAdapterUrl.isEmpty()) {
126 msg = getProcessKey(execution) + ': mso:adapters:vfcc:rest:endpoint URN mapping is not defined'
130 while (vfcAdapterUrl.endsWith('/')) {
131 vfcAdapterUrl = vfcAdapterUrl.substring(0, vfcAdapterUrl.length()-1)
134 execution.setVariable("vfcAdapterUrl", vfcAdapterUrl)
136 } catch (BpmnError e) {
138 } catch (Exception ex){
139 msg = "Exception in preProcessRequest " + ex.getMessage()
141 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
143 msoLogger.trace("Exit preProcessRequest ")
149 public void createNetworkService(DelegateExecution execution) {
150 msoLogger.trace("createNetworkService ")
151 String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
152 String nsOperationKey = execution.getVariable("nsOperationKey");
153 String nsServiceModelUUID = execution.getVariable("nsServiceModelUUID");
154 String nsParameters = execution.getVariable("nsParameters");
155 String nsServiceName = execution.getVariable("nsServiceName")
156 String nsServiceDescription = execution.getVariable("nsServiceDescription")
157 String locationConstraints = jsonUtil.getJsonValue(nsParameters, "locationConstraints")
158 String requestInputs = jsonUtil.getJsonValue(nsParameters, "requestInputs")
160 "nsServiceName":"${nsServiceName}",
161 "nsServiceDescription":"${nsServiceDescription}",
162 "nsServiceModelUUID":"${nsServiceModelUUID}",
163 "nsOperationKey":${nsOperationKey},
165 "locationConstraints":${locationConstraints},
166 "additionalParamForNs":${requestInputs}
169 APIResponse apiResponse = postRequest(execution, vfcAdapterUrl + "/ns", reqBody)
170 String returnCode = apiResponse.getStatusCode()
171 String aaiResponseAsString = apiResponse.getResponseBodyAsString()
172 String nsInstanceId = "";
173 if(returnCode== "200" || returnCode == "201"){
174 nsInstanceId = jsonUtil.getJsonValue(aaiResponseAsString, "nsInstanceId")
176 execution.setVariable("nsInstanceId", nsInstanceId)
177 msoLogger.info(" *****Exit createNetworkService *****")
181 * instantiate NS task
183 public void instantiateNetworkService(DelegateExecution execution) {
184 msoLogger.trace("instantiateNetworkService ")
185 String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
186 String nsOperationKey = execution.getVariable("nsOperationKey");
187 String nsParameters = execution.getVariable("nsParameters");
188 String nsServiceName = execution.getVariable("nsServiceName")
189 String nsServiceDescription = execution.getVariable("nsServiceDescription")
191 "nsServiceName":"${nsServiceName}",
192 "nsServiceDescription":"${nsServiceDescription}",
193 "nsOperationKey":${nsOperationKey},
194 "nsParameters":${nsParameters}
196 String nsInstanceId = execution.getVariable("nsInstanceId")
197 String url = vfcAdapterUrl + "/ns/" +nsInstanceId + "/instantiate"
198 APIResponse apiResponse = postRequest(execution, url, reqBody)
199 String returnCode = apiResponse.getStatusCode()
200 String aaiResponseAsString = apiResponse.getResponseBodyAsString()
202 if(returnCode== "200"|| returnCode == "201"){
203 jobId = jsonUtil.getJsonValue(aaiResponseAsString, "jobId")
205 execution.setVariable("jobId", jobId)
206 msoLogger.info(" *****Exit instantiateNetworkService *****")
212 public void queryNSProgress(DelegateExecution execution) {
213 msoLogger.trace("queryNSProgress ")
214 String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
215 String jobId = execution.getVariable("jobId")
216 String nsOperationKey = execution.getVariable("nsOperationKey");
217 String url = vfcAdapterUrl + "/jobs/" + jobId
218 APIResponse apiResponse = postRequest(execution, url, nsOperationKey)
219 String returnCode = apiResponse.getStatusCode()
220 String aaiResponseAsString = apiResponse.getResponseBodyAsString()
221 String operationStatus = "error"
222 if(returnCode== "200"|| returnCode == "201"){
223 operationStatus = jsonUtil.getJsonValue(aaiResponseAsString, "responseDescriptor.status")
225 execution.setVariable("operationStatus", operationStatus)
226 msoLogger.info(" *****Exit queryNSProgress *****")
232 public void timeDelay(DelegateExecution execution) {
235 } catch(InterruptedException e) {
236 msoLogger.error( "Time Delay exception" + e.getMessage());
243 public void addNSRelationship(DelegateExecution execution) {
244 msoLogger.trace("addNSRelationship ")
245 String nsInstanceId = execution.getVariable("nsInstanceId")
246 if(nsInstanceId == null || nsInstanceId == ""){
247 msoLogger.info(" create NS failed, so do not need to add relationship")
250 String globalSubscriberId = execution.getVariable("globalSubscriberId")
251 String serviceType = execution.getVariable("serviceType")
252 String serviceId = execution.getVariable("serviceInstanceId")
254 AAIResourceUri nsUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,globalSubscriberId,serviceType,nsInstanceId)
255 AAIResourceUri relatedServiceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,globalSubscriberId,serviceType,serviceId)
258 getAAIClient().connect(nsUri,relatedServiceUri)
259 msoLogger.info("NS relationship to Service added successfully")
261 msoLogger.error("Exception occured while Creating NS relationship."+ e.getMessage());
262 throw new BpmnError("MSOWorkflowException")
268 * url: the url of the request
269 * requestBody: the body of the request
271 private APIResponse postRequest(DelegateExecution execution, String urlString, String requestBody){
272 msoLogger.trace("Started Execute VFC adapter Post Process ")
273 msoLogger.info("url:" + urlString +"\nrequestBody:"+ requestBody)
274 APIResponse apiResponse = null
277 // URL url = new URL(urlString);
279 // HttpClient httpClient = new HttpClient(url, "application/json", TargetEntity.VNF_ADAPTER)
280 // httpClient.addAdditionalHeader("Accept", "application/json")
281 // httpClient.addAdditionalHeader("Authorization", "Basic YnBlbDpwYXNzd29yZDEk")
283 // apiResponse = httpClient.post(requestBody)
284 // msoLogger.info("response code:"+ apiResponse.getStatus() +"\nresponse body:"+ apiResponse.readEntity(String.class))
286 // Get the Basic Auth credentials for the VFCAdapter (yes... we ARE using the PO adapters credentials)
287 String basicAuthValuePO = UrnPropertiesReader.getVariable("mso.adapters.po.auth", execution)
289 msoLogger.debug("basicAuthValuePO: " + basicAuthValuePO)
290 if (basicAuthValuePO == null || basicAuthValuePO.isEmpty()) {
291 msoLogger.debug("mso:adapters:po:auth URN mapping is not defined")
294 RESTConfig config = new RESTConfig(urlString)
295 RESTClient client = null;
298 client = new RESTClient(config).addHeader("Accept", "application/json").addAuthorizationHeader(basicAuthValuePO)
300 apiResponse = client.httpPost(requestBody)
302 statusCode = apiResponse.getStatusCode()
304 if(statusCode == 200 || statusCode == 201) {
308 msoLogger.error("VFC Aatpter Post Call Exception using mso.adapters.po.auth:" + e.getMessage());
311 msoLogger.debug("response code:"+ statusCode +"\nresponse body:"+ apiResponse.getResponseBodyAsString())
313 msoLogger.debug("VFC Aatpter Post Call using mso.msoKey")
314 String basicAuthValue = UrnPropertiesReader.getVariable("mso.msoKey", execution)
315 msoLogger.debug("basicAuthValue: " + basicAuthValue)
316 if (basicAuthValue == null || basicAuthValue.isEmpty()) {
317 msoLogger.debug("mso:msoKey URN mapping is not defined")
320 client = new RESTClient(config).addHeader("Accept", "application/json").addAuthorizationHeader(basicAuthValue)
322 apiResponse = client.httpPost(requestBody)
324 statusCode = apiResponse.getStatusCode()
326 if(statusCode == 200 || statusCode == 201) {
330 msoLogger.error("VFC Aatpter Post Call Exception using mso.msoKey:" + e.getMessage());
334 msoLogger.debug("response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString())
336 msoLogger.debug("VFC Aatpter Post Call using mso.db.auth")
337 String basicAuthValuedb = UrnPropertiesReader.getVariable("mso.db.auth", execution)
338 msoLogger.debug("basicAuthValuedb: " + basicAuthValuedb)
339 if (basicAuthValuedb == null || basicAuthValuedb.isEmpty()) {
340 msoLogger.debug("mso:db.auth URN mapping is not defined")
343 client = new RESTClient(config).addHeader("Accept", "application/json").addAuthorizationHeader(basicAuthValuedb)
345 apiResponse = client.httpPost(requestBody)
346 statusCode = apiResponse.getStatusCode()
348 if(statusCode == 200 || statusCode == 201) {
352 msoLogger.error("VFC Aatpter Post Call Exception using mso.msoKey:" + e.getMessage());
353 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "VFC Aatpter Post Call Exception by using mso.msoKey or mso.adapters.po.auth")
357 msoLogger.debug("response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString())
358 String auth = "Basic QlBFTENsaWVudDpwYXNzd29yZDEk"
359 msoLogger.debug("auth: " + basicAuthValuedb)
360 client = new RESTClient(config).addHeader("Accept", "application/json").addAuthorizationHeader(auth)
362 apiResponse = client.httpPost(requestBody)
364 msoLogger.debug("response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString())
366 msoLogger.trace("Completed Execute VF-C adapter Post Process ")
371 public void sendSyncResponse (DelegateExecution execution) {
372 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
373 utils.log("DEBUG", " *** sendSyncResponse *** ", isDebugEnabled)
376 String operationStatus = execution.getVariable("operationStatus")
377 // RESTResponse for main flow
378 String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
379 utils.log("DEBUG", " sendSyncResponse to APIH:" + "\n" + resourceOperationResp, isDebugEnabled)
380 sendWorkflowResponse(execution, 202, resourceOperationResp)
381 execution.setVariable("sentSyncResponse", true)
383 } catch (Exception ex) {
384 String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
385 utils.log("DEBUG", msg, isDebugEnabled)
386 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
388 utils.log("DEBUG"," ***** Exit sendSyncResopnse *****", isDebugEnabled)