2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.infrastructure.scripts
25 import org.onap.so.client.HttpClientFactory
26 import org.onap.so.client.aai.AAIObjectType
27 import org.onap.so.client.aai.entities.uri.AAIResourceUri
28 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
29 import org.camunda.bpm.engine.delegate.BpmnError
30 import org.camunda.bpm.engine.delegate.DelegateExecution
31 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
32 import org.onap.so.bpmn.common.scripts.ExceptionUtil
33 import org.onap.so.bpmn.core.json.JsonUtils
34 import org.onap.so.client.HttpClient
35 import org.onap.so.logger.MessageEnum
36 import org.onap.so.logger.MsoLogger
37 import org.slf4j.Logger
38 import org.slf4j.LoggerFactory
39 import org.onap.so.bpmn.core.UrnPropertiesReader
41 import org.onap.so.utils.TargetEntity
43 import javax.ws.rs.core.Response
46 * This groovy class supports the <class>DoCreateVFCNetworkServiceInstance.bpmn</class> process.
47 * flow for VFC Network Service Create
49 public class DoCreateVFCNetworkServiceInstance extends AbstractServiceTaskProcessor {
50 private static final Logger logger = LoggerFactory.getLogger( DoCreateVFCNetworkServiceInstance.class);
53 ExceptionUtil exceptionUtil = new ExceptionUtil()
55 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) {
65 logger.trace("preProcessRequest()")
67 //deal with nsName and Description
68 String nsServiceName = execution.getVariable("nsServiceName")
69 String nsServiceDescription = execution.getVariable("nsServiceDescription")
70 logger.debug("nsServiceName:" + nsServiceName + " nsServiceDescription:" + nsServiceDescription)
71 //deal with operation key
72 String globalSubscriberId = execution.getVariable("globalSubscriberId")
73 logger.debug("globalSubscriberId:" + globalSubscriberId)
74 String serviceType = execution.getVariable("serviceType")
75 logger.debug("serviceType:" + serviceType)
76 String serviceId = execution.getVariable("serviceId")
77 logger.debug("serviceId:" + serviceId)
78 String operationId = execution.getVariable("operationId")
79 logger.debug("serviceType:" + serviceType)
80 String nodeTemplateUUID = execution.getVariable("resourceUUID")
81 logger.debug("nodeTemplateUUID:" + nodeTemplateUUID)
83 * segmentInformation needed as a object of segment
86 * "nodeTemplateName":"",
89 * //this is the nsParameters sent to VF-C
93 String nsParameters = execution.getVariable("resourceParameters")
94 logger.debug("nsParameters:" + nsParameters)
95 String nsOperationKey = """{
96 "globalSubscriberId":"${globalSubscriberId}",
97 "serviceType":"${serviceType}",
98 "serviceId":"${serviceId}",
99 "operationId":"${operationId}",
100 "nodeTemplateUUID":"${nodeTemplateUUID}"
102 execution.setVariable("nsOperationKey", nsOperationKey);
103 execution.setVariable("nsParameters", nsParameters)
105 String vfcAdapterUrl = UrnPropertiesReader.getVariable("mso.adapters.vfc.rest.endpoint", execution)
107 if (vfcAdapterUrl == null || vfcAdapterUrl.isEmpty()) {
108 msg = getProcessKey(execution) + ': mso:adapters:vfcc:rest:endpoint URN mapping is not defined'
112 while (vfcAdapterUrl.endsWith('/')) {
113 vfcAdapterUrl = vfcAdapterUrl.substring(0, vfcAdapterUrl.length()-1)
116 execution.setVariable("vfcAdapterUrl", vfcAdapterUrl)
119 } catch (BpmnError e) {
121 } catch (Exception ex){
122 msg = "Exception in preProcessRequest " + ex.getMessage()
124 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
126 logger.trace("Exit preProcessRequest")
132 public void createNetworkService(DelegateExecution execution) {
133 logger.trace("createNetworkService")
134 String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
135 String nsOperationKey = execution.getVariable("nsOperationKey");
136 String nsParameters = execution.getVariable("nsParameters");
137 String nsServiceName = execution.getVariable("nsServiceName")
138 String nsServiceDescription = execution.getVariable("nsServiceDescription")
140 "nsServiceName":"${nsServiceName}",
141 "nsServiceDescription":"${nsServiceDescription}",
142 "nsOperationKey":${nsOperationKey},
143 "nsParameters":${nsParameters}
145 Response apiResponse = postRequest(execution, vfcAdapterUrl + "/ns", reqBody)
146 String returnCode = apiResponse.getStatus()
147 String aaiResponseAsString = apiResponse.readEntity(String.class)
148 String nsInstanceId = "";
149 if(returnCode== "200" || returnCode == "201"){
150 nsInstanceId = jsonUtil.getJsonValue(aaiResponseAsString, "nsInstanceId")
152 execution.setVariable("nsInstanceId", nsInstanceId)
153 logger.trace("Exit createNetworkService")
157 * instantiate NS task
159 public void instantiateNetworkService(DelegateExecution execution) {
160 logger.trace("instantiateNetworkService")
161 String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
162 String nsOperationKey = execution.getVariable("nsOperationKey");
163 String nsParameters = execution.getVariable("nsParameters");
164 String nsServiceName = execution.getVariable("nsServiceName")
165 String nsServiceDescription = execution.getVariable("nsServiceDescription")
167 "nsServiceName":"${nsServiceName}",
168 "nsServiceDescription":"${nsServiceDescription}",
169 "nsOperationKey":${nsOperationKey},
170 "nsParameters":${nsParameters}
172 String nsInstanceId = execution.getVariable("nsInstanceId")
173 String url = vfcAdapterUrl + "/ns/" +nsInstanceId + "/instantiate"
174 Response apiResponse = postRequest(execution, url, reqBody)
175 String returnCode = apiResponse.getStatus()
176 String aaiResponseAsString = apiResponse.readEntity(String.class)
178 if(returnCode== "200"|| returnCode == "201"){
179 jobId = jsonUtil.getJsonValue(aaiResponseAsString, "jobId")
181 execution.setVariable("jobId", jobId)
182 logger.trace("Exit instantiateNetworkService")
188 public void queryNSProgress(DelegateExecution execution) {
189 logger.trace("queryNSProgress")
190 String vfcAdapterUrl = execution.getVariable("vfcAdapterUrl")
191 String jobId = execution.getVariable("jobId")
192 String nsOperationKey = execution.getVariable("nsOperationKey");
193 String url = vfcAdapterUrl + "/jobs/" + jobId
194 Response apiResponse = postRequest(execution, url, nsOperationKey)
195 String returnCode = apiResponse.getStatus()
196 String aaiResponseAsString = apiResponse.readEntity(String.class)
197 String operationStatus = "error"
198 if(returnCode== "200"|| returnCode == "201"){
199 operationStatus = jsonUtil.getJsonValue(aaiResponseAsString, "responseDescriptor.status")
201 execution.setVariable("operationStatus", operationStatus)
202 logger.trace("Exit queryNSProgress")
208 public void timeDelay(DelegateExecution execution) {
211 } catch(InterruptedException e) {
212 logger.debug("Time Delay exception" + e )
219 public void addNSRelationship(DelegateExecution execution) {
220 logger.trace("addNSRelationship")
221 String nsInstanceId = execution.getVariable("nsInstanceId")
222 if(nsInstanceId == null || nsInstanceId == ""){
223 logger.debug(" create NS failed, so do not need to add relationship")
226 String globalSubscriberId = execution.getVariable("globalSubscriberId")
227 String serviceType = execution.getVariable("serviceType")
228 String serviceId = execution.getVariable("serviceId")
230 AAIResourceUri nsUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,globalSubscriberId,serviceType,nsInstanceId)
231 AAIResourceUri relatedServiceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,globalSubscriberId,serviceType,serviceId)
234 getAAIClient().connect(nsUri,relatedServiceUri)
235 logger.info("NS relationship to Service added successfully")
237 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
238 "Exception occured while executing AAI Put Call", "BPMN", MsoLogger.getServiceName(),
239 MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
240 throw new BpmnError("MSOWorkflowException")
246 * url: the url of the request
247 * requestBody: the body of the request
249 private Response postRequest(DelegateExecution execution, String urlString, String requestBody){
250 logger.trace("Started Execute VFC adapter Post Process")
251 logger.debug("url:"+urlString +"\nrequestBody:"+ requestBody)
252 Response apiResponse = null
255 URL url = new URL(urlString);
257 // Get the Basic Auth credentials for the VFCAdapter, username is 'bpel', auth is '07a7159d3bf51a0e53be7a8f89699be7'
258 // user 'bepl' authHeader is the same with mso.db.auth
259 String basicAuthValuedb = UrnPropertiesReader.getVariable("mso.db.auth", execution)
260 HttpClient httpClient = new HttpClientFactory().newJsonClient(url, TargetEntity.VNF_ADAPTER)
261 httpClient.addAdditionalHeader("Accept", "application/json")
262 httpClient.addAdditionalHeader("Authorization", basicAuthValuedb)
264 apiResponse = httpClient.post(requestBody)
266 logger.debug("response code:"+ apiResponse.getStatus() +"\nresponse body:"+ apiResponse.readEntity(String.class))
267 logger.trace("Completed Execute VF-C adapter Post Process")
269 logger.error("Exception occured while executing VFC Adapter Post Call" + e.getMessage ());
270 throw new BpmnError("MSOWorkflowException")