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.IOException;
20 import java.util.HashMap;
22 import org.apache.http.client.ClientProtocolException;
23 import org.apache.log4j.Logger;
24 import org.json.JSONException;
25 import org.json.JSONObject;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfRequest;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfResponse;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfRequest;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfResponse;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryOperExecutionResponse;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryVnfResponse;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfRequest;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfResponse;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfRequest;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfResponse;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.util.CommonUtil;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpResult;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.http.MediaType;
46 import org.springframework.stereotype.Component;
47 import org.springframework.web.bind.annotation.RequestMethod;
49 import com.google.gson.Gson;
52 public class CbamMgmrImpl implements CbamMgmrInf {
53 private static final Logger logger = Logger.getLogger(CbamMgmrImpl.class);
54 private Gson gson = new Gson();
57 private AdaptorEnv adaptorEnv;
60 HttpClientProcessorInf httpClientProcessor;
62 public String retrieveToken() throws ClientProtocolException, IOException, JSONException {
64 String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.CbamRetrieveTokenPath;
65 HashMap<String, String> map = new HashMap<>();
66 map.put(CommonConstants.ACCEPT, "*/*");
67 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
69 String bodyPostStr = String.format(CommonConstants.CbamRetrieveTokenPostStr, adaptorEnv.getClientId(), adaptorEnv.getClientSecret(), adaptorEnv.getCbamUserName(), adaptorEnv.getCbamPassword());
71 logger.debug("CbamMgmrImpl -> retrieveToken, url is " + url);
72 logger.debug("CbamMgmrImpl -> retrieveToken, bodyPostStr is " + bodyPostStr);
74 String responseStr = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr).getContent();
76 logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
78 JSONObject tokenJsonObject = new JSONObject(responseStr);
80 result = tokenJsonObject.getString(CommonConstants.CBAM_TOKEN_KEY);
85 public CBAMCreateVnfResponse createVnf(CBAMCreateVnfRequest cbamRequest) throws ClientProtocolException, IOException {
86 String httpPath = CommonConstants.CbamCreateVnfPath;
87 RequestMethod method = RequestMethod.POST;
89 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
90 String responseStr = httpResult.getContent();
92 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
93 int code = httpResult.getStatusCode();
95 logger.info("CbamMgmrImpl -> createVnf success");
97 logger.error("CbamMgmrImpl -> createVnf error ");
99 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
105 * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
107 public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
108 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
109 RequestMethod method = RequestMethod.POST;
111 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
112 String responseStr = httpResult.getContent();
114 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
115 int code = httpResult.getStatusCode();
117 logger.info("CbamMgmrImpl -> instantiateVnf success " );
119 logger.error("CbamMgmrImpl -> instantiateVnf error " );
121 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
126 public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
127 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
128 RequestMethod method = RequestMethod.POST;
130 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
131 String responseStr = httpResult.getContent();
133 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
134 int code = httpResult.getStatusCode();
136 logger.info("CbamMgmrImpl -> terminateVnf sucess " );
138 logger.error("CbamMgmrImpl -> terminateVnf error " );
140 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
145 public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
146 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
147 RequestMethod method = RequestMethod.DELETE;
148 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
150 int code = httpResult.getStatusCode();
152 logger.info("CbamMgmrImpl -> deleteVnf success.");
154 logger.error("CbamMgmrImpl -> deleteVnf error. detail info is " + httpResult.getContent());
159 public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
160 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
161 RequestMethod method = RequestMethod.POST;
163 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
164 String responseStr = httpResult.getContent();
165 int code = httpResult.getStatusCode();
167 logger.info("CbamMgmrImpl -> scaleVnf success.");
169 logger.error("CbamMgmrImpl -> scaleVnf error. " );
171 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
176 public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
177 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
178 RequestMethod method = RequestMethod.POST;
180 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
181 String responseStr = httpResult.getContent();
183 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
184 int code = httpResult.getStatusCode();
186 logger.info("CbamMgmrImpl -> healVnf success.");
188 logger.error("CbamMgmrImpl -> healVnf error. " );
190 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
195 public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
196 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
197 RequestMethod method = RequestMethod.GET;
199 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
200 String responseStr = httpResult.getContent();
202 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
203 int code = httpResult.getStatusCode();
205 logger.info("CbamMgmrImpl -> queryVnf success.");
207 logger.error("CbamMgmrImpl -> queryVnf error. " );
210 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
215 public HttpResult operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
218 token = retrieveToken();
219 } catch (JSONException e) {
220 logger.error("retrieveTokenError ", e);
223 String url= adaptorEnv.getCbamApiUriFront() + httpPath;
225 HashMap<String, String> map = new HashMap<>();
226 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
227 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
229 return httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj));
232 public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException{
233 String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
234 RequestMethod method = RequestMethod.GET;
236 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
237 String responseStr = httpResult.getContent();
239 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
241 int code = httpResult.getStatusCode();
243 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, success" );
244 }else if(code == 202) {
245 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, ongoing" );
247 logger.error("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, error" );
251 CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
256 public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
257 this.adaptorEnv = adaptorEnv;
261 public void uploadVnfPackage(String cbamPackageFilePath) throws ClientProtocolException, IOException {
262 String httpPath = CommonConstants.CbamUploadVnfPackagePath;
263 RequestMethod method = RequestMethod.POST;
265 HttpResult httpResult = operateCbamHttpUploadTask(cbamPackageFilePath, httpPath, method);
266 String responseStr = httpResult.getContent();
268 logger.info("CbamMgmrImpl -> uploadVnfPackage, statusCode is " + httpResult.getStatusCode() + ", cause is " + httpResult.getStatusCause() + ". responseStr is " + responseStr);
270 int code = httpResult.getStatusCode();
272 logger.info("CbamMgmrImpl -> uploadVnfPackage, success" );
273 logger.info("Upload vnf package " + cbamPackageFilePath + " to CBAM is successful.");
275 logger.error("CbamMgmrImpl -> uploadVnfPackage, error" );
279 public HttpResult operateCbamHttpUploadTask(String filePath, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
282 token = retrieveToken();
283 } catch (JSONException e) {
284 logger.error("retrieveTokenError ", e);
287 String url = adaptorEnv.getCbamApiUriFront() + httpPath;
289 HashMap<String, String> map = new HashMap<>();
290 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
291 map.put(CommonConstants.CONTENT_TYPE, "multipart/form-data, boundary=--fsgdsfgjgjdsgdfjgjgj");
292 byte[] fileBytes = CommonUtil.getBytes(filePath);
293 logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, url is " + url);
294 logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, token is " + token);
295 logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, bodyPostStr byte lenth is " + fileBytes.length);
297 return httpClientProcessor.processBytes(url, method, map, fileBytes);
300 public HttpClientProcessorInf getHttpClientProcessor() {
301 return httpClientProcessor;
304 public void setHttpClientProcessor(HttpClientProcessorInf httpClientProcessor) {
305 this.httpClientProcessor = httpClientProcessor;