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;
25 import org.apache.http.client.ClientProtocolException;
26 import org.apache.log4j.Logger;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfRequest;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfResponse;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfRequest;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfResponse;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMModifyVnfRequest;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMModifyVnfResponse;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryOperExecutionResponse;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryVnfResponse;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfRequest;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfResponse;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfRequest;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfResponse;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
44 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
45 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
46 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf;
47 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpResult;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.http.MediaType;
50 import org.springframework.stereotype.Component;
51 import org.springframework.web.bind.annotation.RequestMethod;
53 import com.google.gson.Gson;
56 public class CbamMgmrImpl implements CbamMgmrInf {
57 private static final Logger logger = Logger.getLogger(CbamMgmrImpl.class);
58 private Gson gson = new Gson();
61 private AdaptorEnv adaptorEnv;
64 HttpClientProcessorInf httpClientProcessor;
66 public String retrieveToken() throws ClientProtocolException, IOException, JSONException {
68 String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.CbamRetrieveTokenPath;
69 HashMap<String, String> map = new HashMap<>();
70 map.put(CommonConstants.ACCEPT, "*/*");
71 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
73 String bodyPostStr = String.format(CommonConstants.CbamRetrieveTokenPostStr, adaptorEnv.getClientId(), adaptorEnv.getClientSecret(), adaptorEnv.getCbamUserName(), adaptorEnv.getCbamPassword());
75 logger.debug("CbamMgmrImpl -> retrieveToken, url is " + url);
76 logger.debug("CbamMgmrImpl -> retrieveToken, bodyPostStr is " + bodyPostStr);
78 String responseStr = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr).getContent();
80 logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
82 JSONObject tokenJsonObject = new JSONObject(responseStr);
84 result = tokenJsonObject.getString(CommonConstants.CBAM_TOKEN_KEY);
89 public CBAMCreateVnfResponse createVnf(CBAMCreateVnfRequest cbamRequest) throws ClientProtocolException, IOException {
90 String httpPath = CommonConstants.CbamCreateVnfPath;
91 RequestMethod method = RequestMethod.POST;
93 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
94 String responseStr = httpResult.getContent();
96 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
97 int code = httpResult.getStatusCode();
99 logger.info("CbamMgmrImpl -> createVnf success");
101 logger.error("CbamMgmrImpl -> createVnf error ");
103 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
108 public CBAMModifyVnfResponse modifyVnf(CBAMModifyVnfRequest cbamRequest, String vnfInstanceId)
109 throws ClientProtocolException, IOException {
110 String httpPath = String.format(CommonConstants.CbamModifyVnfPath, vnfInstanceId);
112 RequestMethod method = RequestMethod.PATCH;
114 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
115 String responseStr = httpResult.getContent();
117 logger.info("CbamMgmrImpl -> modifyVnf, responseStr is " + responseStr);
118 int code = httpResult.getStatusCode();
120 logger.info("CbamMgmrImpl -> modifyVnf success");
122 logger.error("CbamMgmrImpl -> modifyVnf error ");
124 CBAMModifyVnfResponse response = gson.fromJson(responseStr, CBAMModifyVnfResponse.class);
129 * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
131 public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
132 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
133 RequestMethod method = RequestMethod.POST;
135 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
136 String responseStr = httpResult.getContent();
138 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
139 int code = httpResult.getStatusCode();
141 logger.info("CbamMgmrImpl -> instantiateVnf success " );
143 logger.error("CbamMgmrImpl -> instantiateVnf error " );
145 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
150 public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
151 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
152 RequestMethod method = RequestMethod.POST;
154 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
155 String responseStr = httpResult.getContent();
157 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
158 int code = httpResult.getStatusCode();
160 logger.info("CbamMgmrImpl -> terminateVnf sucess " );
162 logger.error("CbamMgmrImpl -> terminateVnf error " );
164 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
169 public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
170 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
171 RequestMethod method = RequestMethod.DELETE;
172 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
174 int code = httpResult.getStatusCode();
176 logger.info("CbamMgmrImpl -> deleteVnf success.");
178 logger.error("CbamMgmrImpl -> deleteVnf error. detail info is " + httpResult.getContent());
183 public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
184 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
185 RequestMethod method = RequestMethod.POST;
187 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
188 String responseStr = httpResult.getContent();
189 int code = httpResult.getStatusCode();
191 logger.info("CbamMgmrImpl -> scaleVnf success.");
193 logger.error("CbamMgmrImpl -> scaleVnf error. " );
195 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
200 public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
201 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
202 RequestMethod method = RequestMethod.POST;
204 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
205 String responseStr = httpResult.getContent();
207 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
208 int code = httpResult.getStatusCode();
210 logger.info("CbamMgmrImpl -> healVnf success.");
212 logger.error("CbamMgmrImpl -> healVnf error. " );
214 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
219 public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
220 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
221 RequestMethod method = RequestMethod.GET;
223 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
224 String responseStr = httpResult.getContent();
226 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
227 int code = httpResult.getStatusCode();
229 logger.info("CbamMgmrImpl -> queryVnf success.");
231 logger.error("CbamMgmrImpl -> queryVnf error. " );
234 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
239 public HttpResult operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
242 token = retrieveToken();
243 } catch (JSONException e) {
244 logger.error("retrieveTokenError ", e);
247 String url= adaptorEnv.getCbamApiUriFront() + httpPath;
249 HashMap<String, String> map = new HashMap<>();
250 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
251 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
253 return httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj));
256 public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException{
257 String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
258 RequestMethod method = RequestMethod.GET;
260 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
261 String responseStr = httpResult.getContent();
263 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
265 int code = httpResult.getStatusCode();
267 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, success" );
268 }else if(code == 202) {
269 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, ongoing" );
271 logger.error("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, error" );
274 CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
279 public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
280 this.adaptorEnv = adaptorEnv;
284 public void uploadVnfPackage(String cbamPackageFilePath) throws ClientProtocolException, IOException {
285 String httpPath = CommonConstants.CbamUploadVnfPackagePath;
286 RequestMethod method = RequestMethod.POST;
288 HttpResult httpResult = operateCbamHttpUploadTask(cbamPackageFilePath, httpPath, method);
289 String responseStr = httpResult.getContent();
291 logger.info("CbamMgmrImpl -> uploadVnfPackage, statusCode is " + httpResult.getStatusCode() + ", cause is " + httpResult.getStatusCause() + ". responseStr is " + responseStr);
293 int code = httpResult.getStatusCode();
295 logger.info("CbamMgmrImpl -> uploadVnfPackage, success" );
296 logger.info("Upload vnf package " + cbamPackageFilePath + " to CBAM is successful.");
298 logger.error("CbamMgmrImpl -> uploadVnfPackage, error" );
302 public HttpResult operateCbamHttpUploadTask(String filePath, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
305 token = retrieveToken();
306 } catch (JSONException e) {
307 logger.error("retrieveTokenError ", e);
309 String url = adaptorEnv.getCbamApiUriFront() + httpPath;
310 logger.info("start to upload file.");
311 String command = "/usr/bin/curl --insecure -X POST -H \"Authorization: bearer " + token + "\" --form content=@" + filePath + " " + url;
312 StringBuffer respStr = execCommand(command);
314 // HashMap<String, String> map = new HashMap<>();
315 // map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
316 // map.put(CommonConstants.CONTENT_TYPE, "multipart/form-data, boundary=---CFSGSSGGSGdssdfsdhd---");
317 // byte[] fileBytes = CommonUtil.getBytes(filePath);
318 // logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, url is " + url);
319 // logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, token is " + token);
320 // logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, bodyPostStr byte lenth is " + fileBytes.length);
322 // return httpClientProcessor.processBytes(url, method, map, fileBytes);
324 HttpResult hResult = new HttpResult();
325 hResult.setStatusCause(respStr.toString());
326 hResult.setContent(respStr.toString());
327 hResult.setStatusCode(200);
330 // String charset = "UTF-8";
331 // File uploadFile1 = new File(filePath);
332 // String requestURL = url;
333 // HttpResult result = new HttpResult();
336 // MultipartUtility multipart = new MultipartUtility(requestURL, charset);
338 // multipart.addHeaderField("User-Agent", "CodeJava");
339 // multipart.addHeaderField(CommonConstants.AUTHORIZATION, "bearer " + token);
341 // multipart.addFilePart("fileUpload", uploadFile1);
343 // List<String> response = multipart.finish();
345 // result.setContent(Arrays.deepToString(response.toArray(new String[0])));
346 // result.setStatusCode(200);
347 // } catch (Exception ex) {
348 // logger.error("CbamMgmrImpl -> operateCbamHttpUploadTask, error ", ex);
349 // result.setStatusCode(500);
355 private StringBuffer execCommand(String command) {
356 logger.info("CbamMgmrImpl -> execCommand, command is " + command);
357 StringBuffer respStr = new StringBuffer("\r\n");
359 String os = System.getProperty("os.name");
360 String[] cmd = {"cmd", "/c", command};
361 if(!os.toLowerCase().startsWith("win")){
362 cmd = new String[]{"/bin/sh","-c", command};
364 Process process = Runtime.getRuntime().exec(cmd);
365 Thread t=new Thread(new InputStreamRunnable(process.getErrorStream(),"ErrorStream"));
368 InputStream fis=process.getInputStream();
369 InputStreamReader isr=new InputStreamReader(fis);
371 BufferedReader br=new BufferedReader(isr);
373 while((line = br.readLine())!=null)
375 respStr.append(line + "\r\n");
377 respStr.append("\r\n");
382 logger.info("operateCbamHttpUploadTask respStr is: " + respStr);
383 } catch (Exception e) {
384 logger.error("operateCbamHttpUploadTask error", e);
389 // public static String postByHttps(String url, String body, Object contentType) {
390 // String result = "";
391 // Protocol https = new Protocol("https", new HTTPSSecureProtocolSocketFactory(), 443);
392 // Protocol.registerProtocol("https", https);
393 // PostMethod post = new PostMethod(url);
394 // HttpClient client = new HttpClient();
396 // post.setRequestHeader("Content-Type", contentType);
397 // post.setRequestBody(body);
398 // client.executeMethod(post);
399 // result = post.getResponseBodyAsString();
400 // Protocol.unregisterProtocol("https");
402 // } catch (HttpException e) {
403 // e.printStackTrace();
404 // } catch (IOException e) {
405 // e.printStackTrace();
406 // } catch(Exception e) {
407 // e.printStackTrace();
413 public HttpClientProcessorInf getHttpClientProcessor() {
414 return httpClientProcessor;
417 public void setHttpClientProcessor(HttpClientProcessorInf httpClientProcessor) {
418 this.httpClientProcessor = httpClientProcessor;