2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.impl;
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.util.HashMap;
24 import java.util.List;
26 import org.apache.http.client.ClientProtocolException;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateSubscriptionRequest;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateSubscriptionResponse;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfRequest;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfResponse;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfRequest;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfResponse;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMModifyVnfRequest;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMModifyVnfResponse;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryOperExecutionResponse;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryVnfResponse;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfRequest;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfResponse;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfRequest;
44 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfResponse;
45 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMVnfNotificationRequest;
46 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMVnfNotificationResponse;
47 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.entity.SubscriptionResponse;
48 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.entity.VnfcResourceInfo;
49 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
50 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
51 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
52 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf;
53 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpResult;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.http.MediaType;
58 import org.springframework.stereotype.Component;
59 import org.springframework.web.bind.annotation.RequestMethod;
61 import com.google.common.reflect.TypeToken;
62 import com.google.gson.Gson;
65 public class CbamMgmrImpl implements CbamMgmrInf {
66 private static final Logger logger = LoggerFactory.getLogger(CbamMgmrImpl.class);
67 private Gson gson = new Gson();
70 private AdaptorEnv adaptorEnv;
73 HttpClientProcessorInf httpClientProcessor;
75 public String retrieveToken() throws ClientProtocolException, IOException, JSONException {
77 String url = adaptorEnv.getCbamApiUriFront() + CommonConstants.CbamRetrieveTokenPath;
78 HashMap<String, String> map = new HashMap<>();
79 map.put(CommonConstants.ACCEPT, "*/*");
80 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
82 String bodyPostStr = String.format(CommonConstants.CbamRetrieveTokenPostStr, adaptorEnv.getClientId(),
83 adaptorEnv.getClientSecret(), adaptorEnv.getCbamUserName(), adaptorEnv.getCbamPassword());
85 logger.debug("CbamMgmrImpl -> retrieveToken, url is " + url);
86 logger.debug("CbamMgmrImpl -> retrieveToken, bodyPostStr is " + bodyPostStr);
88 String responseStr = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr).getContent();
90 logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
92 JSONObject tokenJsonObject = new JSONObject(responseStr);
94 result = tokenJsonObject.getString(CommonConstants.CBAM_TOKEN_KEY);
99 public CBAMCreateVnfResponse createVnf(CBAMCreateVnfRequest cbamRequest)
100 throws ClientProtocolException, IOException {
101 String httpPath = CommonConstants.CbamCreateVnfPath;
102 RequestMethod method = RequestMethod.POST;
104 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
105 String responseStr = httpResult.getContent();
107 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
108 int code = httpResult.getStatusCode();
110 logger.info("CbamMgmrImpl -> createVnf success");
112 logger.error("CbamMgmrImpl -> createVnf error ");
114 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
119 public CBAMModifyVnfResponse modifyVnf(CBAMModifyVnfRequest cbamRequest, String vnfInstanceId)
120 throws ClientProtocolException, IOException {
121 String httpPath = String.format(CommonConstants.CbamModifyVnfPath, vnfInstanceId);
123 RequestMethod method = RequestMethod.PATCH;
125 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
126 String responseStr = httpResult.getContent();
128 logger.info("CbamMgmrImpl -> modifyVnf, responseStr is " + responseStr);
129 int code = httpResult.getStatusCode();
132 logger.info("CbamMgmrImpl -> modifyVnf success");
134 logger.error("CbamMgmrImpl -> modifyVnf error, statusCode = " + code);
136 CBAMModifyVnfResponse response = gson.fromJson(responseStr, CBAMModifyVnfResponse.class);
143 * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.
144 * vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
146 public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId)
147 throws ClientProtocolException, IOException {
148 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
149 RequestMethod method = RequestMethod.POST;
151 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
152 String responseStr = httpResult.getContent();
154 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
155 int code = httpResult.getStatusCode();
157 logger.info("CbamMgmrImpl -> instantiateVnf success ");
159 logger.error("CbamMgmrImpl -> instantiateVnf error ");
161 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
166 public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId)
167 throws ClientProtocolException, IOException {
168 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
169 RequestMethod method = RequestMethod.POST;
171 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
172 String responseStr = httpResult.getContent();
174 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
175 int code = httpResult.getStatusCode();
177 logger.info("CbamMgmrImpl -> terminateVnf sucess ");
179 logger.error("CbamMgmrImpl -> terminateVnf error ");
181 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
186 public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
187 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
188 RequestMethod method = RequestMethod.DELETE;
189 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
191 int code = httpResult.getStatusCode();
193 logger.info("CbamMgmrImpl -> deleteVnf success.");
195 logger.error("CbamMgmrImpl -> deleteVnf error. detail info is " + httpResult.getContent());
200 public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId)
201 throws ClientProtocolException, IOException {
202 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
203 RequestMethod method = RequestMethod.POST;
205 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
206 String responseStr = httpResult.getContent();
207 int code = httpResult.getStatusCode();
209 logger.info("CbamMgmrImpl -> scaleVnf success.");
211 logger.error("CbamMgmrImpl -> scaleVnf error. detail info is " + httpResult.getContent());
213 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
218 public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId)
219 throws ClientProtocolException, IOException {
220 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
221 RequestMethod method = RequestMethod.POST;
223 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
224 String responseStr = httpResult.getContent();
226 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
227 int code = httpResult.getStatusCode();
229 logger.info("CbamMgmrImpl -> healVnf success.");
231 logger.error("CbamMgmrImpl -> healVnf error. " + httpResult.getContent());
233 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
238 public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
239 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
240 RequestMethod method = RequestMethod.GET;
242 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
243 String responseStr = httpResult.getContent();
245 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
246 int code = httpResult.getStatusCode();
248 logger.info("CbamMgmrImpl -> queryVnf success.");
250 logger.error("CbamMgmrImpl -> queryVnf error. " + httpResult.getContent());
253 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
258 public List<VnfcResourceInfo> queryVnfcResource(String vnfInstanceId) throws ClientProtocolException, IOException {
259 String httpPath = String.format(CommonConstants.CbamQueryVnfcResourcePath, vnfInstanceId);
260 RequestMethod method = RequestMethod.GET;
262 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
263 String responseStr = httpResult.getContent();
265 logger.info("CbamMgmrImpl -> queryVnfcResource, responseStr is " + responseStr);
266 int code = httpResult.getStatusCode();
268 logger.info("CbamMgmrImpl -> queryVnfcResource success.");
270 logger.error("CbamMgmrImpl -> queryVnfcResource error. ");
272 List<VnfcResourceInfo> response = gson.fromJson(responseStr, new TypeToken<List<VnfcResourceInfo>>() {
279 public HttpResult operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method)
280 throws ClientProtocolException, IOException {
283 token = retrieveToken();
284 } catch (JSONException e) {
285 logger.error("retrieveTokenError ", e);
288 String url = adaptorEnv.getCbamApiUriFront() + httpPath;
290 HashMap<String, String> map = new HashMap<>();
291 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
292 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
294 return httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj));
297 public CBAMQueryOperExecutionResponse queryOperExecution(String execId)
298 throws ClientProtocolException, IOException {
299 String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
300 RequestMethod method = RequestMethod.GET;
302 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
303 String responseStr = httpResult.getContent();
305 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
307 int code = httpResult.getStatusCode();
309 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, success");
310 } else if (code == 202) {
311 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, ongoing");
313 logger.error("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, error");
316 CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
321 public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
322 this.adaptorEnv = adaptorEnv;
326 public void uploadVnfPackage(String cbamPackageFilePath) throws ClientProtocolException, IOException {
327 String httpPath = CommonConstants.CbamUploadVnfPackagePath;
328 RequestMethod method = RequestMethod.POST;
330 HttpResult httpResult = operateCbamHttpUploadTask(cbamPackageFilePath, httpPath, method);
331 String responseStr = httpResult.getContent();
333 logger.info("CbamMgmrImpl -> uploadVnfPackage, statusCode is " + httpResult.getStatusCode() + ", cause is "
334 + httpResult.getStatusCause() + ". responseStr is " + responseStr);
336 int code = httpResult.getStatusCode();
338 logger.info("CbamMgmrImpl -> uploadVnfPackage, success");
339 logger.info("Upload vnf package " + cbamPackageFilePath + " to CBAM is successful.");
341 logger.error("CbamMgmrImpl -> uploadVnfPackage, error");
345 public HttpResult operateCbamHttpUploadTask(String filePath, String httpPath, RequestMethod method)
346 throws ClientProtocolException, IOException {
349 token = retrieveToken();
350 } catch (JSONException e) {
351 logger.error("retrieveTokenError ", e);
353 String url = adaptorEnv.getCbamApiUriFront() + httpPath;
354 logger.info("start to upload file.");
355 String command = "/usr/bin/curl --insecure -X POST -H \"Authorization: bearer " + token + "\" --form content=@"
356 + filePath + " " + url;
357 StringBuffer respStr = execCommand(command);
359 // HashMap<String, String> map = new HashMap<>();
360 // map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
361 // map.put(CommonConstants.CONTENT_TYPE, "multipart/form-data,
362 // boundary=---CFSGSSGGSGdssdfsdhd---");
363 // byte[] fileBytes = CommonUtil.getBytes(filePath);
364 // logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, url is " + url);
365 // logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, token is " + token);
366 // logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, bodyPostStr byte
367 // lenth is " + fileBytes.length);
369 // return httpClientProcessor.processBytes(url, method, map, fileBytes);
371 HttpResult hResult = new HttpResult();
372 hResult.setStatusCause(respStr.toString());
373 hResult.setContent(respStr.toString());
374 hResult.setStatusCode(200);
377 // String charset = "UTF-8";
378 // File uploadFile1 = new File(filePath);
379 // String requestURL = url;
380 // HttpResult result = new HttpResult();
383 // MultipartUtility multipart = new MultipartUtility(requestURL, charset);
385 // multipart.addHeaderField("User-Agent", "CodeJava");
386 // multipart.addHeaderField(CommonConstants.AUTHORIZATION, "bearer " + token);
388 // multipart.addFilePart("fileUpload", uploadFile1);
390 // List<String> response = multipart.finish();
392 // result.setContent(Arrays.deepToString(response.toArray(new String[0])));
393 // result.setStatusCode(200);
394 // } catch (Exception ex) {
395 // logger.error("CbamMgmrImpl -> operateCbamHttpUploadTask, error ", ex);
396 // result.setStatusCode(500);
402 private StringBuffer execCommand(String command) {
403 logger.info("CbamMgmrImpl -> execCommand, command is " + command);
404 StringBuffer respStr = new StringBuffer("\r\n");
406 String os = System.getProperty("os.name");
407 String[] cmd = { "cmd", "/c", command };
408 if (!os.toLowerCase().startsWith("win")) {
409 cmd = new String[] { "/bin/sh", "-c", command };
411 Process process = Runtime.getRuntime().exec(cmd);
412 Thread t = new Thread(new InputStreamRunnable(process.getErrorStream(), "ErrorStream"));
415 InputStream fis = process.getInputStream();
416 InputStreamReader isr = new InputStreamReader(fis);
418 BufferedReader br = new BufferedReader(isr);
420 while ((line = br.readLine()) != null) {
421 respStr.append(line + "\r\n");
423 respStr.append("\r\n");
428 logger.info("operateCbamHttpUploadTask respStr is: " + respStr);
429 } catch (Exception e) {
430 logger.error("operateCbamHttpUploadTask error", e);
435 // public static String postByHttps(String url, String body, Object contentType)
437 // String result = "";
438 // Protocol https = new Protocol("https", new
439 // HTTPSSecureProtocolSocketFactory(), 443);
440 // Protocol.registerProtocol("https", https);
441 // PostMethod post = new PostMethod(url);
442 // HttpClient client = new HttpClient();
444 // post.setRequestHeader("Content-Type", contentType);
445 // post.setRequestBody(body);
446 // client.executeMethod(post);
447 // result = post.getResponseBodyAsString();
448 // Protocol.unregisterProtocol("https");
450 // } catch (HttpException e) {
451 // e.printStackTrace();
452 // } catch (IOException e) {
453 // e.printStackTrace();
454 // } catch(Exception e) {
455 // e.printStackTrace();
461 public HttpClientProcessorInf getHttpClientProcessor() {
462 return httpClientProcessor;
465 public void setHttpClientProcessor(HttpClientProcessorInf httpClientProcessor) {
466 this.httpClientProcessor = httpClientProcessor;
470 public CBAMCreateSubscriptionResponse createSubscription(CBAMCreateSubscriptionRequest subscriptionRequest)
471 throws ClientProtocolException, IOException {
472 String httpPath = String.format(CommonConstants.CbamCreateSubscriptionPath);
473 RequestMethod method = RequestMethod.POST;
474 logger.info("CbamMgmrImpl -> createSubscription, requestStr is " + gson.toJson(subscriptionRequest));
475 HttpResult httpResult = operateCbamHttpTask(subscriptionRequest, httpPath, method);
476 String responseStr = httpResult.getContent();
478 CBAMCreateSubscriptionResponse response = null;
480 logger.info("CbamMgmrImpl -> createSubscription, responseStr is " + responseStr);
481 int code = httpResult.getStatusCode();
483 logger.info("CbamMgmrImpl -> createSubscription success ");
484 response = gson.fromJson(responseStr, CBAMCreateSubscriptionResponse.class);
486 logger.error("CbamMgmrImpl -> createSubscription error , statusCode = " + code);
493 public SubscriptionResponse getSubscription(String subscriptionId) throws ClientProtocolException, IOException {
494 String httpPath = String.format(CommonConstants.CbamGetSubscriptionPath, subscriptionId);
495 RequestMethod method = RequestMethod.GET;
497 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
498 String responseStr = httpResult.getContent();
500 logger.info("CbamMgmrImpl -> getSubscription, responseStr is " + responseStr);
501 int code = httpResult.getStatusCode();
503 logger.info("CbamMgmrImpl -> getSubscription success ");
505 logger.error("CbamMgmrImpl -> getSubscription error , statusCode = " + code);
507 SubscriptionResponse response = gson.fromJson(responseStr, SubscriptionResponse.class);
513 public CBAMVnfNotificationResponse getNotification(CBAMVnfNotificationRequest getNotificationRequest)
514 throws ClientProtocolException, IOException {
515 String httpPath = String.format(CommonConstants.CbamGetNotificationPath);
516 RequestMethod method = RequestMethod.GET;
518 HttpResult httpResult = operateCbamHttpTask(getNotificationRequest, httpPath, method);
519 String responseStr = httpResult.getContent();
521 logger.info("CbamMgmrImpl -> getNotification, responseStr is " + responseStr);
522 int code = httpResult.getStatusCode();
524 logger.info("CbamMgmrImpl -> getNotification success ");
526 logger.error("CbamMgmrImpl -> getNotification error , statusCode = " + code);
528 CBAMVnfNotificationResponse response = gson.fromJson(responseStr, CBAMVnfNotificationResponse.class);