2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 CMCC. 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.logger.LoggingAnchor
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.json.JSONArray
28 import org.json.JSONObject;
30 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
31 import org.onap.so.bpmn.common.scripts.ExceptionUtil
32 import org.onap.so.bpmn.core.json.JsonUtils
34 import org.camunda.bpm.engine.delegate.BpmnError
35 import com.fasterxml.jackson.databind.ObjectMapper
36 import org.onap.so.client.HttpClientFactory
37 import org.onap.so.logger.ErrorCode
39 import javax.ws.rs.core.Response
41 import org.onap.so.bpmn.infrastructure.vfcmodel.ScaleResource
42 import org.onap.so.client.HttpClient
43 import org.onap.so.bpmn.infrastructure.vfcmodel.ScaleNsByStepsData
44 import org.onap.so.bpmn.infrastructure.vfcmodel.ScaleNsData
46 import org.onap.so.bpmn.infrastructure.vfcmodel.NSResourceInputParameter
47 import org.onap.so.bpmn.infrastructure.vfcmodel.NsOperationKey
48 import org.onap.so.bpmn.infrastructure.vfcmodel.NsScaleParameters
49 import org.onap.so.bpmn.infrastructure.vfcmodel.NsParameters
50 import org.onap.so.bpmn.infrastructure.vfcmodel.LocationConstraint
51 import org.onap.so.logger.MessageEnum
52 import org.slf4j.Logger
53 import org.slf4j.LoggerFactory
54 import org.onap.logging.filter.base.ONAPComponents;
60 * This groovy class supports the <class>DoScaleVFCNetworkServiceInstance.bpmn</class> process.
61 * flow for VFC Network Service Scale
63 public class DoScaleVFCNetworkServiceInstance extends AbstractServiceTaskProcessor {
64 private static final Logger logger = LoggerFactory.getLogger( DoScaleVFCNetworkServiceInstance.class);
67 String host = "http://mso.mso.testlab.openecomp.org:8080"
69 String scaleUrl = "/vfc/rest/v1/vfcadapter/ns/{nsInstanceId}/scale"
71 String queryJobUrl = "/vfc/rest/v1/vfcadapter/jobs/{jobId}"
73 ExceptionUtil exceptionUtil = new ExceptionUtil()
75 JsonUtils jsonUtil = new JsonUtils()
78 * Pre Process the BPMN Flow Request
80 * generate the nsOperationKey
81 * generate the nsParameters
83 public void preProcessRequest(DelegateExecution execution) {
84 logger.trace("preProcessRequest() ")
86 List<NSResourceInputParameter> nsRIPList = convertScaleNsReq2NSResInputParamList(execution)
87 String requestJsonStr = ""
88 int size = nsRIPList.size()
89 for (int i = 0; i < size; i++) {
90 NSResourceInputParameter nsRIP = nsRIPList.get(i)
93 requestJsonStr += objectToJsonStr(nsRIP)
95 requestJsonStr += objectToJsonStr(nsRIP) + "|"
99 execution.setVariable("reqBody", requestJsonStr)
101 logger.trace("Exit preProcessRequest ")
107 public void scaleNetworkService(DelegateExecution execution) {
108 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
110 String saleNsRequest = execution.getVariable("reqBody")
111 String[] nsReqStr = saleNsRequest.split("\\|")
113 for (int i = 0; i < nsReqStr.length; i++) {
114 JSONObject reqBodyJsonObj = new JSONObject(nsReqStr[i])
115 String nsInstanceId = reqBodyJsonObj.getJSONObject("nsScaleParameters").getString("nsInstanceId")
116 String nodeTemplateUUID = reqBodyJsonObj.getJSONObject("nsOperationKey").getString("nodeTemplateUUID")
117 reqBodyJsonObj.getJSONObject("nsScaleParameters").remove("nsInstanceId")
118 String reqBody = reqBodyJsonObj.toString()
120 String url = host + scaleUrl.replaceAll("\\{nsInstanceId\\}", nsInstanceId)
122 Response apiResponse = postRequest(execution, url, reqBody)
124 String returnCode = apiResponse.getStatus()
125 String aaiResponseAsString = apiResponse.readEntity(String.class)
127 if (returnCode == "200" || returnCode == "202") {
128 jobId = jsonUtil.getJsonValue(aaiResponseAsString, "jobId")
130 logger.info( "scaleNetworkService get a ns scale job Id:" + jobId)
131 execution.setVariable("jobId", jobId)
132 execution.setVariable("nodeTemplateUUID", nodeTemplateUUID)
134 String isScaleFinished = ""
136 if(jobId =="" || jobId == null){
139 // query the requested network service scale status, if finished, then start the next one, otherwise, wait
140 while (isScaleFinished != "finished" && isScaleFinished != "error"){
142 queryNSProgress(execution)
143 isScaleFinished = execution.getVariable("operationStatus")
151 private void queryNSProgress(DelegateExecution execution) {
152 String jobId = execution.getVariable("jobId")
153 String url = host + queryJobUrl.replaceAll("\\{jobId\\}", jobId)
155 NsOperationKey nsOperationKey = new NsOperationKey()
156 // is this net work service ID or E2E service ID?
157 nsOperationKey.setServiceId(execution.getVariable("serviceId"))
158 nsOperationKey.setServiceType(execution.getVariable("serviceType"))
159 nsOperationKey.setGlobalSubscriberId(execution.getVariable("globalSubscriberId"))
160 nsOperationKey.setNodeTemplateUUID(execution.getVariable("nodeTemplateUUID"))
161 nsOperationKey.setOperationId(execution.getVariable("operationId"))
162 String queryReqBody = objectToJsonStr(nsOperationKey)
164 Response apiResponse = postRequest(execution,url, queryReqBody)
166 String returnCode = apiResponse.getStatus()
167 String aaiResponseAsString = apiResponse.readEntity(String.class)
169 String operationStatus = "error"
171 if (returnCode == "200") {
172 operationStatus = jsonUtil.getJsonValue(aaiResponseAsString, "responseDescriptor.status")
175 execution.setVariable("operationStatus", operationStatus)
182 private void timeDelay() {
185 } catch (InterruptedException e) {
186 logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
187 "Time Delay exception" + e, "BPMN",
188 ErrorCode.UnknownError.getValue());
195 public void finishNSScale(DelegateExecution execution) {
196 //no need to do anything util now
197 System.out.println("Scale finished.")
202 * url: the url of the request
203 * requestBody: the body of the request
205 private Response postRequest(DelegateExecution execution, String urlString, String requestBody){
206 logger.trace("Started Execute VFC adapter Post Process ")
207 logger.info("url:"+urlString +"\nrequestBody:"+ requestBody)
208 Response apiResponse = null
210 URL url = new URL(urlString);
212 HttpClient httpClient = new HttpClientFactory().newJsonClient(url, ONAPComponents.VNF_ADAPTER)
213 httpClient.addAdditionalHeader("Authorization", "Basic QlBFTENsaWVudDpwYXNzd29yZDEk")
215 apiResponse = httpClient.post(requestBody)
217 logger.info("response code:"+ apiResponse.getStatus() +"\nresponse body:"+ apiResponse.readEntity(String.class))
218 logger.trace("Completed Execute VF-C adapter Post Process ")
220 logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), "Exception occured " +
221 "while executing VFC Post Call.", "BPMN", ErrorCode.UnknownError.getValue(), e);
222 throw new BpmnError("MSOWorkflowException")
228 * create a Scale Resource object list from a NSScaleRequestJso nString
229 * This method is for the specific request from Scale Network Service BPMN workflow
230 * @param nsScaleRequestJsonString , a specific request Json string which conform to ?? class
231 * @return List < ScaleResource >
233 private List<ScaleResource> jsonGetNsResourceList(String nsScaleRequestJsonString) {
234 List<ScaleResource> list = new ArrayList<ScaleResource>()
235 JSONObject jsonObject = new JSONObject(nsScaleRequestJsonString)
237 JSONObject jsonResource = jsonObject.getJSONObject("service")
238 JSONArray arr = jsonResource.getJSONArray("resources")
240 for (int i = 0; i < arr.length(); i++) {
241 JSONObject tempResource = arr.getJSONObject(i)
242 ScaleResource resource = new ScaleResource()
243 resource.setResourceInstanceId(tempResource.getString("resourceInstanceId"))
244 resource.setScaleType(tempResource.getString("scaleType"))
246 JSONObject jsonScaleNsData = tempResource.getJSONObject("scaleNsData")
247 JSONObject jsonScaleNsByStepData = jsonScaleNsData.getJSONObject("scaleNsByStepsData")
249 ScaleNsData scaleNsData = new ScaleNsData()
250 ScaleNsByStepsData stepsData = new ScaleNsByStepsData()
252 stepsData.setAspectId(jsonScaleNsByStepData.getString("aspectId"))
253 stepsData.setScalingDirection(jsonScaleNsByStepData.getString("scalingDirection"))
254 stepsData.setNumberOfSteps(Integer.parseInt(jsonScaleNsByStepData.getString("numberOfSteps")))
256 scaleNsData.setScaleNsByStepsData(stepsData)
257 resource.setScaleNsData(scaleNsData)
265 * Convert a java class to JSON string
269 private String objectToJsonStr(Object obj) {
270 ObjectMapper mapper = new ObjectMapper()
271 String jsonStr = null
273 jsonStr = mapper.writeValueAsString(obj)
274 } catch (IOException ioe) {
275 System.out.println(ioe.getMessage())
282 * create a NSResourceInputParameter list from a Scale Network request Json string
285 private List<NSResourceInputParameter> convertScaleNsReq2NSResInputParamList(DelegateExecution execution) {
286 String saleNsRequest = execution.getVariable("bpmnRequest")
288 //String requestId = execution.getVariable("msoRequestId")
289 //String serviceInstanceId = execution.getVariable("serviceInstanceId")
290 String serviceInstanceName = execution.getVariable("serviceInstanceName")
291 //String nodeTemplateUUID = execution.getVariable("nodeTemplateUUID")
292 String serviceType = execution.getVariable("serviceType")
293 String globalSubscriberId = execution.getVariable("globalSubscriberId")
294 String operationId = execution.getVariable("operationId")
295 String serviceId = execution.getVariable("serviceId")
296 String nsServiceDescription = execution.getVariable("requestDescription")
298 String resource = JsonUtils.getJsonValue(saleNsRequest, "service.resources")
300 // set nsScaleParameters properties
301 List<ScaleResource> scaleResourcesList = jsonGetNsResourceList(saleNsRequest)
302 List<NSResourceInputParameter> nsResourceInputParameterList = new ArrayList<NSResourceInputParameter>()
304 for (ScaleResource sr : scaleResourcesList) {
305 NSResourceInputParameter nsResourceInputParameter = new NSResourceInputParameter()
306 NsOperationKey nsOperationKey = new NsOperationKey()
307 NsParameters nsParameters = new NsParameters()
308 NsScaleParameters nsScaleParameters = new NsScaleParameters()
309 nsParameters.setLocationConstraints(new ArrayList<LocationConstraint>())
310 nsParameters.setAdditionalParamForNs(new HashMap<String, Object>())
312 // set NsOperationKey properties
313 nsOperationKey.setGlobalSubscriberId(globalSubscriberId)
314 nsOperationKey.setServiceId(serviceId)
315 nsOperationKey.setServiceType(serviceType)
316 // for ns scale the resourceInstanceId is the nodeTemplateUUID
317 nsOperationKey.setNodeTemplateUUID(sr.getResourceInstanceId())
318 nsOperationKey.setOperationId(operationId)
320 nsScaleParameters.setScaleType(sr.getScaleType())
321 nsScaleParameters.setNsInstanceId(sr.getResourceInstanceId())
323 ScaleNsByStepsData scaleNsByStepsData = new ScaleNsByStepsData()
324 scaleNsByStepsData.setScalingDirection(sr.getScaleNsData().getScaleNsByStepsData().getScalingDirection())
325 scaleNsByStepsData.setNumberOfSteps(sr.getScaleNsData().getScaleNsByStepsData().getNumberOfSteps())
326 scaleNsByStepsData.setAspectId(sr.getScaleNsData().getScaleNsByStepsData().getAspectId())
328 List<ScaleNsByStepsData> scaleNsByStepsDataList = new ArrayList<ScaleNsByStepsData>()
329 scaleNsByStepsDataList.add(scaleNsByStepsData)
330 nsScaleParameters.setScaleNsByStepsData(scaleNsByStepsDataList)
332 nsResourceInputParameter.setNsOperationKey(nsOperationKey)
333 nsResourceInputParameter.setNsServiceName(serviceInstanceName)
334 nsResourceInputParameter.setNsServiceDescription(nsServiceDescription)
335 nsResourceInputParameter.setNsParameters(nsParameters)
336 nsResourceInputParameter.setNsScaleParameters(nsScaleParameters)
338 nsResourceInputParameterList.add(nsResourceInputParameter)
340 return nsResourceInputParameterList