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.bpmn.core.UrnPropertiesReader
37 import javax.ws.rs.core.Response
38 import org.onap.so.utils.TargetEntity
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);
47 // String vfcUrl = "/vfc/rest/v1/vfcadapter"
49 // String host = "http://mso.mso.testlab.openecomp.org:8080"
51 ExceptionUtil exceptionUtil = new ExceptionUtil()
53 JsonUtils jsonUtil = new JsonUtils()
57 * Pre Process the BPMN Flow Request
59 * generate the nsOperationKey
60 * generate the nsParameters
62 public void preProcessRequest (DelegateExecution execution) {
63 JsonUtils jsonUtil = new JsonUtils()
66 msoLogger.trace("preProcessRequest() ")
68 //deal with nsName and Description
69 String resourceInput = execution.getVariable("resourceInput")
71 String resourceParameters = jsonUtil.getJsonValue(resourceInput, "resourceParameters")
73 String resourceName = jsonUtil.getJsonValue(resourceInput, "resourceInstanceName")
74 execution.setVariable("nsServiceName", resourceName)
76 String nsServiceDescription = execution.getVariable("nsServiceDescription")
77 msoLogger.info("nsServiceName:" + resourceName + " nsServiceDescription:" + nsServiceDescription)
78 //deal with operation key
79 String globalSubscriberId = jsonUtil.getJsonValue(resourceInput, "globalSubscriberId")
80 msoLogger.info("globalSubscriberId:" + globalSubscriberId)
81 //set local globalSubscriberId variable
82 execution.setVariable("globalSubscriberId", globalSubscriberId);
83 String serviceType = execution.getVariable("serviceType")
84 msoLogger.info("serviceType:" + serviceType)
86 String serviceId = execution.getVariable("serviceInstanceId")
87 msoLogger.info("serviceId:" + serviceId)
89 String operationId = jsonUtil.getJsonValue(resourceInput, "operationId")
90 msoLogger.info("serviceType:" + serviceType)
92 String nodeTemplateUUID = jsonUtil.getJsonValue(resourceInput, "resourceModelInfo.modelCustomizationUuid")
93 String nsServiceModelUUID = jsonUtil.getJsonValue(resourceParameters, "requestInputs.nsd0_providing_service_uuid")
94 msoLogger.info("nodeTemplateUUID:" + nodeTemplateUUID)
96 * segmentInformation needed as a object of segment
99 * "nodeTemplateName":"",
102 * //this is the nsParameters sent to VF-C
106 String nsParameters = jsonUtil.getJsonValue(resourceInput, "resourceParameters")
107 msoLogger.info("nsParameters:" + nsParameters)
108 String nsOperationKey = """{
109 "globalSubscriberId":"${globalSubscriberId}",
110 "serviceType":"${serviceType}",
111 "serviceId":"${serviceId}",
112 "operationId":"${operationId}",
113 "nodeTemplateUUID":"${nodeTemplateUUID}"
115 execution.setVariable("nsOperationKey", nsOperationKey);
116 execution.setVariable("nsParameters", nsParameters)
117 execution.setVariable("nsServiceModelUUID", nsServiceModelUUID);
119 String vfcAdapterUrl = UrnPropertiesReader.getVariable("mso.adapters.vfc.rest.endpoint", execution)
121 if (vfcAdapterUrl == null || vfcAdapterUrl.isEmpty()) {
122 msg = getProcessKey(execution) + ': mso:adapters:vfcc:rest:endpoint URN mapping is not defined'
126 while (vfcAdapterUrl.endsWith('/')) {
127 vfcAdapterUrl = vfcAdapterUrl.substring(0, vfcAdapterUrl.length()-1)
130 execution.setVariable("vfcAdapterUrl", vfcAdapterUrl)
132 } catch (BpmnError e) {
134 } catch (Exception ex){
135 msg = "Exception in preProcessRequest " + ex.getMessage()
137 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
139 msoLogger.trace("Exit preProcessRequest ")
145 public void createNetworkService(DelegateExecution execution) {
146 msoLogger.trace("createNetworkService ")
147 String vfcAdapterUrl = execution.setVariable("vfcAdapterUrl")
148 String nsOperationKey = execution.getVariable("nsOperationKey");
149 String nsServiceModelUUID = execution.getVariable("nsServiceModelUUID");
150 String nsParameters = execution.getVariable("nsParameters");
151 String nsServiceName = execution.getVariable("nsServiceName")
152 String nsServiceDescription = execution.getVariable("nsServiceDescription")
153 String locationConstraints = jsonUtil.getJsonValue(nsParameters, "locationConstraints")
154 String requestInputs = jsonUtil.getJsonValue(nsParameters, "requestInputs")
156 "nsServiceName":"${nsServiceName}",
157 "nsServiceDescription":"${nsServiceDescription}",
158 "nsServiceModelUUID":"${nsServiceModelUUID}",
159 "nsOperationKey":${nsOperationKey},
161 "locationConstraints":${locationConstraints},
162 "additionalParamForNs":${requestInputs}
165 Response apiResponse = postRequest(execution, vfcAdapterUrl + "/ns", reqBody)
166 String returnCode = apiResponse.getStatus()
167 String aaiResponseAsString = apiResponse.readEntity(String.class)
168 String nsInstanceId = "";
169 if(returnCode== "200" || returnCode == "201"){
170 nsInstanceId = jsonUtil.getJsonValue(aaiResponseAsString, "nsInstanceId")
172 execution.setVariable("nsInstanceId", nsInstanceId)
173 msoLogger.info(" *****Exit createNetworkService *****")
177 * instantiate NS task
179 public void instantiateNetworkService(DelegateExecution execution) {
180 msoLogger.trace("instantiateNetworkService ")
181 String vfcAdapterUrl = execution.setVariable("vfcAdapterUrl")
182 String nsOperationKey = execution.getVariable("nsOperationKey");
183 String nsParameters = execution.getVariable("nsParameters");
184 String nsServiceName = execution.getVariable("nsServiceName")
185 String nsServiceDescription = execution.getVariable("nsServiceDescription")
187 "nsServiceName":"${nsServiceName}",
188 "nsServiceDescription":"${nsServiceDescription}",
189 "nsOperationKey":${nsOperationKey},
190 "nsParameters":${nsParameters}
192 String nsInstanceId = execution.getVariable("nsInstanceId")
193 String url = vfcAdapterUrl + "/ns/" +nsInstanceId + "/instantiate"
194 Response apiResponse = postRequest(execution, url, reqBody)
195 String returnCode = apiResponse.getStatus()
196 String aaiResponseAsString = apiResponse.readEntity(String.class)
198 if(returnCode== "200"|| returnCode == "201"){
199 jobId = jsonUtil.getJsonValue(aaiResponseAsString, "jobId")
201 execution.setVariable("jobId", jobId)
202 msoLogger.info(" *****Exit instantiateNetworkService *****")
208 public void queryNSProgress(DelegateExecution execution) {
209 msoLogger.trace("queryNSProgress ")
210 String vfcAdapterUrl = execution.setVariable("vfcAdapterUrl")
211 String jobId = execution.getVariable("jobId")
212 String nsOperationKey = execution.getVariable("nsOperationKey");
213 String url = vfcAdapterUrl + "/jobs/" + jobId
214 Response apiResponse = postRequest(execution, url, nsOperationKey)
215 String returnCode = apiResponse.getStatus()
216 String aaiResponseAsString = apiResponse.readEntity(String.class)
217 String operationStatus = "error"
218 if(returnCode== "200"|| returnCode == "201"){
219 operationStatus = jsonUtil.getJsonValue(aaiResponseAsString, "responseDescriptor.status")
221 execution.setVariable("operationStatus", operationStatus)
222 msoLogger.info(" *****Exit queryNSProgress *****")
228 public void timeDelay(DelegateExecution execution) {
231 } catch(InterruptedException e) {
232 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Time Delay exception" + e , "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
239 public void addNSRelationship(DelegateExecution execution) {
240 msoLogger.trace("addNSRelationship ")
241 String nsInstanceId = execution.getVariable("nsInstanceId")
242 if(nsInstanceId == null || nsInstanceId == ""){
243 msoLogger.info(" create NS failed, so do not need to add relationship")
246 String globalSubscriberId = execution.getVariable("globalSubscriberId")
247 String serviceType = execution.getVariable("serviceType")
248 String serviceId = execution.getVariable("serviceInstanceId")
250 AAIResourceUri nsUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,globalSubscriberId,serviceType,nsInstanceId)
251 AAIResourceUri relatedServiceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,globalSubscriberId,serviceType,serviceId)
254 getAAIClient().connect(nsUri,relatedServiceUri)
255 msoLogger.info("NS relationship to Service added successfully")
257 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception occured while Creating NS relationship.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.getMessage(),e);
258 throw new BpmnError("MSOWorkflowException")
264 * url: the url of the request
265 * requestBody: the body of the request
267 private Response postRequest(DelegateExecution execution, String urlString, String requestBody){
268 msoLogger.trace("Started Execute VFC adapter Post Process ")
269 msoLogger.info("url:" + urlString +"\nrequestBody:"+ requestBody)
270 Response apiResponse = null
273 URL url = new URL(urlString);
275 HttpClient httpClient = new HttpClient(url, "application/json", TargetEntity.VNF_ADAPTER)
276 httpClient.addAdditionalHeader("Accept", "application/json")
277 httpClient.addAdditionalHeader("Authorization", "Basic YnBlbDpwYXNzd29yZDEk")
279 apiResponse = httpClient.post(requestBody)
281 msoLogger.info("response code:"+ apiResponse.getStatus() +"\nresponse body:"+ apiResponse.readEntity(String.class))
282 msoLogger.trace("Completed Execute VF-C adapter Post Process ")
284 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception occured while executing AAI Post Call.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e);
285 throw new BpmnError("MSOWorkflowException")
290 public void sendSyncResponse (DelegateExecution execution) {
291 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
292 utils.log("DEBUG", " *** sendSyncResponse *** ", isDebugEnabled)
295 String operationStatus = execution.getVariable("operationStatus")
296 // RESTResponse for main flow
297 String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
298 utils.log("DEBUG", " sendSyncResponse to APIH:" + "\n" + resourceOperationResp, isDebugEnabled)
299 sendWorkflowResponse(execution, 202, resourceOperationResp)
300 execution.setVariable("sentSyncResponse", true)
302 } catch (Exception ex) {
303 String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
304 utils.log("DEBUG", msg, isDebugEnabled)
305 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
307 utils.log("DEBUG"," ***** Exit sendSyncResopnse *****", isDebugEnabled)