package org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.impl;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
import java.util.HashMap;
import org.apache.http.client.ClientProtocolException;
-import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf;
-import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpRequestProcessor;
+import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
@Autowired
HttpClientProcessorInf httpClientProcessor;
- private String retrieveToken() throws ClientProtocolException, IOException, JSONException {
+ public String retrieveToken() throws ClientProtocolException, IOException, JSONException {
String result = null;
- String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.RetrieveCbamTokenPath;
- HashMap<String, String> map = new HashMap<String, String>();
+ String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.CbamRetrieveTokenPath;
+ HashMap<String, String> map = new HashMap<>();
map.put(CommonConstants.ACCEPT, "*/*");
map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
- String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret());
+ String bodyPostStr = String.format(CommonConstants.CbamRetrieveTokenPostStr, adaptorEnv.getClientId(), adaptorEnv.getClientSecret(), adaptorEnv.getCbamUserName(), adaptorEnv.getCbamPassword());
- String responseStr = httpClientProcessor.process(url, RequestMethod.GET, map, bodyPostStr);
+ logger.debug("CbamMgmrImpl -> retrieveToken, url is " + url);
+ logger.debug("CbamMgmrImpl -> retrieveToken, bodyPostStr is " + bodyPostStr);
+
+ String responseStr = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr).getContent();
logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
String httpPath = CommonConstants.CbamCreateVnfPath;
RequestMethod method = RequestMethod.POST;
- String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
+ HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
+ String responseStr = httpResult.getContent();
logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
-
+ int code = httpResult.getStatusCode();
+ if(code == 201) {
+ logger.info("CbamMgmrImpl -> createVnf success");
+ }else {
+ logger.error("CbamMgmrImpl -> createVnf error ");
+ }
CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
return response;
String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
RequestMethod method = RequestMethod.POST;
- String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
+ HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
+ String responseStr = httpResult.getContent();
logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
-
+ int code = httpResult.getStatusCode();
+ if(code == 202) {
+ logger.info("CbamMgmrImpl -> instantiateVnf success " );
+ }else {
+ logger.error("CbamMgmrImpl -> instantiateVnf error " );
+ }
CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
return response;
String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
RequestMethod method = RequestMethod.POST;
- String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
+ HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
+ String responseStr = httpResult.getContent();
logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
-
+ int code = httpResult.getStatusCode();
+ if(code == 202) {
+ logger.info("CbamMgmrImpl -> terminateVnf sucess " );
+ }else {
+ logger.error("CbamMgmrImpl -> terminateVnf error " );
+ }
CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
return response;
public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
RequestMethod method = RequestMethod.DELETE;
+ HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
- operateCbamHttpTask(null, httpPath, method);
+ int code = httpResult.getStatusCode();
+ if(code == 204) {
+ logger.info("CbamMgmrImpl -> deleteVnf success.");
+ }else {
+ logger.error("CbamMgmrImpl -> deleteVnf error. detail info is " + httpResult.getContent());
+ }
- logger.info("CbamMgmrImpl -> deleteVnf.");
}
public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
RequestMethod method = RequestMethod.POST;
- String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
-
+ HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
+ String responseStr = httpResult.getContent();
+ int code = httpResult.getStatusCode();
+ if(code == 202) {
+ logger.info("CbamMgmrImpl -> scaleVnf success.");
+ }else {
+ logger.error("CbamMgmrImpl -> scaleVnf error. " );
+ }
CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
return response;
String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
RequestMethod method = RequestMethod.POST;
- String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
+ HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
+ String responseStr = httpResult.getContent();
logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
-
+ int code = httpResult.getStatusCode();
+ if(code == 202) {
+ logger.info("CbamMgmrImpl -> healVnf success.");
+ }else {
+ logger.error("CbamMgmrImpl -> healVnf error. " );
+ }
CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
return response;
String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
RequestMethod method = RequestMethod.GET;
- String responseStr = operateCbamHttpTask(null, httpPath, method);
+ HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
+ String responseStr = httpResult.getContent();
logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
+ int code = httpResult.getStatusCode();
+ if(code == 200) {
+ logger.info("CbamMgmrImpl -> queryVnf success.");
+ }else {
+ logger.error("CbamMgmrImpl -> queryVnf error. " );
+ }
CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
return response;
}
- public String operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
+ public HttpResult operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
String token = null;
try {
token = retrieveToken();
String url= adaptorEnv.getCbamApiUriFront() + httpPath;
- HashMap<String, String> map = new HashMap<String, String>();
+ HashMap<String, String> map = new HashMap<>();
map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
- String responseStr = httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj));
-
- return responseStr;
+ return httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj));
}
public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException{
String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
RequestMethod method = RequestMethod.GET;
- String responseStr = operateCbamHttpTask(null, httpPath, method);
+ HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
+ String responseStr = httpResult.getContent();
logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
+ int code = httpResult.getStatusCode();
+ if(code == 200) {
+ logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, success" );
+ }else if(code == 202) {
+ logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, ongoing" );
+ }else {
+ logger.error("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, error" );
+ }
+
+
CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
return response;
public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
this.adaptorEnv = adaptorEnv;
}
+
+ @Override
+ public void uploadVnfPackage(String cbamPackageFilePath) throws ClientProtocolException, IOException {
+ String httpPath = CommonConstants.CbamUploadVnfPackagePath;
+ RequestMethod method = RequestMethod.POST;
+
+ HttpResult httpResult = operateCbamHttpUploadTask(cbamPackageFilePath, httpPath, method);
+ String responseStr = httpResult.getContent();
+
+ logger.info("CbamMgmrImpl -> uploadVnfPackage, statusCode is " + httpResult.getStatusCode() + ", cause is " + httpResult.getStatusCause() + ". responseStr is " + responseStr);
+
+ int code = httpResult.getStatusCode();
+ if(code == 200) {
+ logger.info("CbamMgmrImpl -> uploadVnfPackage, success" );
+ logger.info("Upload vnf package " + cbamPackageFilePath + " to CBAM is successful.");
+ }else {
+ logger.error("CbamMgmrImpl -> uploadVnfPackage, error" );
+ }
+ }
+
+ public HttpResult operateCbamHttpUploadTask(String filePath, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
+ String token = null;
+ try {
+ token = retrieveToken();
+ } catch (JSONException e) {
+ logger.error("retrieveTokenError ", e);
+ }
+ String url = adaptorEnv.getCbamApiUriFront() + httpPath;
+ String command = "curl --insecure -X POST -H \"Authorization: bearer " + token + "\" --form content=@" + filePath + " " + url;
+ StringBuffer respStr = new StringBuffer();
+ try {
+ logger.info("start to upload file.");
+ String os = System.getProperty("os.name");
+ String[] cmd = {"cmd", "/C", command};
+ if(!os.toLowerCase().startsWith("win")){
+ cmd = new String[]{"/bin/sh"," -c ", command};
+ }
+ Process process = Runtime.getRuntime().exec(cmd);
+ InputStream fis=process.getInputStream();
+ InputStreamReader isr=new InputStreamReader(fis);
+ BufferedReader br=new BufferedReader(isr);
+ String line=null;
+ while((line = br.readLine())!=null)
+ {
+ respStr.append(line);
+ }
+
+ } catch (Exception e) {
+ logger.error("operateCbamHttpUploadTask error", e);
+ }
+
+// HashMap<String, String> map = new HashMap<>();
+// map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
+// map.put(CommonConstants.CONTENT_TYPE, "multipart/form-data, boundary=---CFSGSSGGSGdssdfsdhd---");
+// byte[] fileBytes = CommonUtil.getBytes(filePath);
+// logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, url is " + url);
+// logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, token is " + token);
+// logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, bodyPostStr byte lenth is " + fileBytes.length);
+
+// return httpClientProcessor.processBytes(url, method, map, fileBytes);
+
+ HttpResult hResult = new HttpResult();
+ hResult.setContent(respStr.toString());
+ hResult.setStatusCode(200);
+ return hResult;
+
+// String charset = "UTF-8";
+// File uploadFile1 = new File(filePath);
+// String requestURL = url;
+// HttpResult result = new HttpResult();
+//
+// try {
+// MultipartUtility multipart = new MultipartUtility(requestURL, charset);
+//
+// multipart.addHeaderField("User-Agent", "CodeJava");
+// multipart.addHeaderField(CommonConstants.AUTHORIZATION, "bearer " + token);
+//
+// multipart.addFilePart("fileUpload", uploadFile1);
+//
+// List<String> response = multipart.finish();
+//
+// result.setContent(Arrays.deepToString(response.toArray(new String[0])));
+// result.setStatusCode(200);
+// } catch (Exception ex) {
+// logger.error("CbamMgmrImpl -> operateCbamHttpUploadTask, error ", ex);
+// result.setStatusCode(500);
+// }
+//
+// return result;
+ }
+// public static String postByHttps(String url, String body, Object contentType) {
+// String result = "";
+// Protocol https = new Protocol("https", new HTTPSSecureProtocolSocketFactory(), 443);
+// Protocol.registerProtocol("https", https);
+// PostMethod post = new PostMethod(url);
+// HttpClient client = new HttpClient();
+// try {
+// post.setRequestHeader("Content-Type", contentType);
+// post.setRequestBody(body);
+// client.executeMethod(post);
+// result = post.getResponseBodyAsString();
+// Protocol.unregisterProtocol("https");
+// return result;
+// } catch (HttpException e) {
+// e.printStackTrace();
+// } catch (IOException e) {
+// e.printStackTrace();
+// } catch(Exception e) {
+// e.printStackTrace();
+// }
+//
+// return "error";
+// }
+
+ public HttpClientProcessorInf getHttpClientProcessor() {
+ return httpClientProcessor;
+ }
+
+ public void setHttpClientProcessor(HttpClientProcessorInf httpClientProcessor) {
+ this.httpClientProcessor = httpClientProcessor;
+ }
}