2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 CMCC. 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.openecomp.mso.bpmn.infrastructure.scripts
23 import org.camunda.bpm.engine.delegate.DelegateExecution
24 import org.json.JSONArray
25 import org.json.JSONObject;
27 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
28 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
29 import org.openecomp.mso.bpmn.core.json.JsonUtils
31 import org.camunda.bpm.engine.delegate.BpmnError
32 import org.camunda.bpm.engine.runtime.Execution
33 import org.codehaus.jackson.map.ObjectMapper
35 import org.openecomp.mso.rest.RESTClient
36 import org.openecomp.mso.rest.RESTConfig
37 import org.openecomp.mso.rest.APIResponse;
39 import org.openecomp.mso.bpmn.infrastructure.vfcmodel.ScaleResource
40 import org.openecomp.mso.bpmn.infrastructure.vfcmodel.ScaleNsByStepsData
41 import org.openecomp.mso.bpmn.infrastructure.vfcmodel.ScaleNsData
43 import org.openecomp.mso.bpmn.infrastructure.vfcmodel.NSResourceInputParameter
44 import org.openecomp.mso.bpmn.infrastructure.vfcmodel.NsOperationKey
45 import org.openecomp.mso.bpmn.infrastructure.vfcmodel.NsScaleParameters
46 import org.openecomp.mso.bpmn.infrastructure.vfcmodel.NsParameters
47 import org.openecomp.mso.bpmn.infrastructure.vfcmodel.LocationConstraint
51 * This groovy class supports the <class>DoScaleVFCNetworkServiceInstance.bpmn</class> process.
52 * flow for VFC Network Service Scale
54 public class DoScaleVFCNetworkServiceInstance extends AbstractServiceTaskProcessor {
56 String host = "http://mso.mso.testlab.openecomp.org:8080"
58 String scaleUrl = "/vfc/rest/v1/vfcadapter/ns/{nsInstanceId}/scale"
60 String queryJobUrl = "/vfc/rest/v1/vfcadapter/jobs/{jobId}"
62 ExceptionUtil exceptionUtil = new ExceptionUtil()
64 JsonUtils jsonUtil = new JsonUtils()
67 * Pre Process the BPMN Flow Request
69 * generate the nsOperationKey
70 * generate the nsParameters
72 public void preProcessRequest(DelegateExecution execution) {
73 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
74 utils.log("DEBUG", " *** preProcessRequest() *** ", isDebugEnabled)
76 List<NSResourceInputParameter> nsRIPList = convertScaleNsReq2NSResInputParamList(execution)
77 String requestJsonStr = ""
78 int size = nsRIPList.size()
79 for (int i = 0; i < size; i++) {
80 NSResourceInputParameter nsRIP = nsRIPList.get(i)
83 requestJsonStr += objectToJsonStr(nsRIP)
85 requestJsonStr += objectToJsonStr(nsRIP) + "|"
89 execution.setVariable("reqBody", requestJsonStr)
91 utils.log("DEBUG", " ***** Exit preProcessRequest *****", isDebugEnabled)
97 public void scaleNetworkService(DelegateExecution execution) {
98 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
100 String saleNsRequest = execution.getVariable("reqBody")
101 String[] nsReqStr = saleNsRequest.split("\\|")
103 for (int i = 0; i < nsReqStr.length; i++) {
104 JSONObject reqBodyJsonObj = new JSONObject(nsReqStr[i])
105 String nsInstanceId = reqBodyJsonObj.getJSONObject("nsScaleParameters").getString("nsInstanceId")
106 reqBodyJsonObj.getJSONObject("nsScaleParameters").remove("nsInstanceId")
107 String reqBody = reqBodyJsonObj.toString()
109 String url = host + scaleUrl.replaceAll("\\{nsInstanceId\\}", nsInstanceId)
111 APIResponse apiResponse = postRequest(execution, url, reqBody)
113 String returnCode = apiResponse.getStatusCode()
114 String aaiResponseAsString = apiResponse.getResponseBodyAsString()
116 if (returnCode == "200" || returnCode == "202") {
117 jobId = jsonUtil.getJsonValue(aaiResponseAsString, "jobId")
119 utils.log("INFO", "scaleNetworkService get a ns scale job Id:" + jobId, isDebugEnabled)
120 execution.setVariable("jobId", jobId)
122 String isScaleFinished = ""
124 // query the requested network service scale status, if finished, then start the next one, otherwise, wait
125 while (isScaleFinished != "finished" && isScaleFinished != "error"){
127 queryNSProgress(execution)
128 isScaleFinished = execution.getVariable("operationStatus")
136 private void queryNSProgress(DelegateExecution execution) {
137 String jobId = execution.getVariable("jobId")
138 String url = host + queryJobUrl.replaceAll("\\{jobId\\}", jobId)
140 NsOperationKey nsOperationKey = new NsOperationKey()
141 // is this net work service ID or E2E service ID?
142 nsOperationKey.setServiceId(execution.getVariable("serviceId"))
143 nsOperationKey.setServiceType(execution.getVariable("serviceType"))
144 nsOperationKey.setGlobalSubscriberId(execution.getVariable("globalSubscriberId"))
145 nsOperationKey.setNodeTemplateUUID(execution.getVariable("nodeTemplateUUID"))
146 nsOperationKey.setOperationId(execution.getVariable("operationId"))
147 String queryReqBody = objectToJsonStr(nsOperationKey)
149 APIResponse apiResponse = postRequest(execution,url, queryReqBody)
151 String returnCode = apiResponse.getStatusCode()
152 String aaiResponseAsString = apiResponse.getResponseBodyAsString()
154 String operationStatus = "error"
156 if (returnCode == "200") {
157 operationStatus = jsonUtil.getJsonValue(aaiResponseAsString, "responseDescriptor.status")
160 execution.setVariable("operationStatus", operationStatus)
167 private void timeDelay() {
170 } catch (InterruptedException e) {
171 taskProcessor.utils.log("ERROR", "Time Delay exception" + e, isDebugEnabled)
178 public void finishNSScale(DelegateExecution execution) {
179 //no need to do anything util now
180 System.out.println("Scale finished.")
185 * url: the url of the request
186 * requestBody: the body of the request
188 private APIResponse postRequest(DelegateExecution execution, String url, String requestBody){
189 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
190 utils.log("INFO"," ***** Started Execute VFC adapter Post Process *****", isDebugEnabled)
191 utils.log("INFO","url:"+url +"\nrequestBody:"+ requestBody, isDebugEnabled)
192 APIResponse apiResponse = null
194 RESTConfig config = new RESTConfig(url)
195 RESTClient client = new RESTClient(config).addHeader("Content-Type", "application/json").addHeader("Authorization","Basic QlBFTENsaWVudDpwYXNzd29yZDEk")
196 // RESTClient client = new RESTClient(config).addHeader("Content-Type", "application/json").addHeader("Accept","application/json").addHeader("Authorization","Basic QlBFTENsaWVudDpwYXNzd29yZDEk")
197 apiResponse = client.httpPost(requestBody)
198 utils.log("INFO","response code:"+ apiResponse.getStatusCode() +"\nresponse body:"+ apiResponse.getResponseBodyAsString(), isDebugEnabled)
199 utils.log("INFO","======== Completed Execute VF-C adapter Post Process ======== ", isDebugEnabled)
201 utils.log("ERROR","Exception occured while executing VFC Post Call. Exception is: \n" + e, isDebugEnabled)
202 throw new BpmnError("MSOWorkflowException")
208 * create a Scale Resource object list from a NSScaleRequestJso nString
209 * This method is for the specific request from Scale Network Service BPMN workflow
210 * @param nsScaleRequestJsonString , a specific request Json string which conform to ?? class
211 * @return List < ScaleResource >
213 private List<ScaleResource> jsonGetNsResourceList(String nsScaleRequestJsonString) {
214 List<ScaleResource> list = new ArrayList<ScaleResource>()
215 JSONObject jsonObject = new JSONObject(nsScaleRequestJsonString)
217 JSONObject jsonResource = jsonObject.getJSONObject("service")
218 JSONArray arr = jsonResource.getJSONArray("resources")
220 for (int i = 0; i < arr.length(); i++) {
221 JSONObject tempResource = arr.getJSONObject(i)
222 ScaleResource resource = new ScaleResource()
223 resource.setResourceInstanceId(tempResource.getString("resourceInstanceId"))
224 resource.setScaleType(tempResource.getString("scaleType"))
226 JSONObject jsonScaleNsData = tempResource.getJSONObject("scaleNsData")
227 JSONObject jsonScaleNsByStepData = jsonScaleNsData.getJSONObject("scaleNsByStepsData")
229 ScaleNsData scaleNsData = new ScaleNsData()
230 ScaleNsByStepsData stepsData = new ScaleNsByStepsData()
232 stepsData.setAspectId(jsonScaleNsByStepData.getString("aspectId"))
233 stepsData.setScalingDirection(jsonScaleNsByStepData.getString("scalingDirection"))
234 stepsData.setNumberOfSteps(Integer.parseInt(jsonScaleNsByStepData.getString("numberOfSteps")))
236 scaleNsData.setScaleNsByStepsData(stepsData)
237 resource.setScaleNsData(scaleNsData)
245 * Convert a java class to JSON string
249 private String objectToJsonStr(Object obj) {
250 ObjectMapper mapper = new ObjectMapper()
251 String jsonStr = null
253 jsonStr = mapper.writeValueAsString(obj)
254 } catch (IOException ioe) {
255 System.out.println(ioe.getMessage())
262 * create a NSResourceInputParameter list from a Scale Network request Json string
265 private List<NSResourceInputParameter> convertScaleNsReq2NSResInputParamList(DelegateExecution execution) {
266 String saleNsRequest = execution.getVariable("bpmnRequest")
268 //String requestId = execution.getVariable("msoRequestId")
269 //String serviceInstanceId = execution.getVariable("serviceInstanceId")
270 String serviceInstanceName = execution.getVariable("serviceInstanceName")
271 //String nodeTemplateUUID = execution.getVariable("nodeTemplateUUID")
272 String serviceType = execution.getVariable("serviceType")
273 String globalSubscriberId = execution.getVariable("globalSubscriberId")
274 String operationId = execution.getVariable("operationId")
275 String serviceId = execution.getVariable("serviceId")
276 String nsServiceDescription = execution.getVariable("requestDescription")
278 String resource = JsonUtils.getJsonValue(saleNsRequest, "service.resources")
280 // set nsScaleParameters properties
281 List<ScaleResource> scaleResourcesList = jsonGetNsResourceList(saleNsRequest)
282 List<NSResourceInputParameter> nsResourceInputParameterList = new ArrayList<NSResourceInputParameter>()
284 for (ScaleResource sr : scaleResourcesList) {
285 NSResourceInputParameter nsResourceInputParameter = new NSResourceInputParameter()
286 NsOperationKey nsOperationKey = new NsOperationKey()
287 NsParameters nsParameters = new NsParameters()
288 NsScaleParameters nsScaleParameters = new NsScaleParameters()
289 nsParameters.setLocationConstraints(new ArrayList<LocationConstraint>())
290 nsParameters.setAdditionalParamForNs(new HashMap<String, Object>())
292 // set NsOperationKey properties
293 nsOperationKey.setGlobalSubscriberId(globalSubscriberId)
294 nsOperationKey.setServiceId(serviceId)
295 nsOperationKey.setServiceType(serviceType)
296 // for ns scale the resourceInstanceId is the nodeTemplateUUID
297 nsOperationKey.setNodeTemplateUUID(sr.getResourceInstanceId())
298 nsOperationKey.setOperationId(operationId)
300 nsScaleParameters.setScaleType(sr.getScaleType())
301 nsScaleParameters.setNsInstanceId(sr.getResourceInstanceId())
303 ScaleNsByStepsData scaleNsByStepsData = new ScaleNsByStepsData()
304 scaleNsByStepsData.setScalingDirection(sr.getScaleNsData().getScaleNsByStepsData().getScalingDirection())
305 scaleNsByStepsData.setNumberOfSteps(sr.getScaleNsData().getScaleNsByStepsData().getNumberOfSteps())
306 scaleNsByStepsData.setAspectId(sr.getScaleNsData().getScaleNsByStepsData().getAspectId())
308 List<ScaleNsByStepsData> scaleNsByStepsDataList = new ArrayList<ScaleNsByStepsData>()
309 scaleNsByStepsDataList.add(scaleNsByStepsData)
310 nsScaleParameters.setScaleNsByStepsData(scaleNsByStepsDataList)
312 nsResourceInputParameter.setNsOperationKey(nsOperationKey)
313 nsResourceInputParameter.setNsServiceName(serviceInstanceName)
314 nsResourceInputParameter.setNsServiceDescription(nsServiceDescription)
315 nsResourceInputParameter.setNsParameters(nsParameters)
316 nsResourceInputParameter.setNsScaleParameters(nsScaleParameters)
318 nsResourceInputParameterList.add(nsResourceInputParameter)
320 return nsResourceInputParameterList