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 private 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.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret(), adaptorEnv.getCbamUserName(), adaptorEnv.getCbamPassword());
71 String responseStr = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr).getContent();
73 logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
75 JSONObject tokenJsonObject = new JSONObject(responseStr);
77 result = tokenJsonObject.getString(CommonConstants.CBAM_TOKEN_KEY);
82 public CBAMCreateVnfResponse createVnf(CBAMCreateVnfRequest cbamRequest) throws ClientProtocolException, IOException {
83 String httpPath = CommonConstants.CbamCreateVnfPath;
84 RequestMethod method = RequestMethod.POST;
86 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
87 String responseStr = httpResult.getContent();
89 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
90 int code = httpResult.getStatusCode();
92 logger.info("CbamMgmrImpl -> createVnf success");
94 logger.error("CbamMgmrImpl -> createVnf error ");
96 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
102 * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
104 public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
105 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
106 RequestMethod method = RequestMethod.POST;
108 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
109 String responseStr = httpResult.getContent();
111 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
112 int code = httpResult.getStatusCode();
114 logger.info("CbamMgmrImpl -> instantiateVnf success " );
116 logger.error("CbamMgmrImpl -> instantiateVnf error " );
118 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
123 public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
124 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
125 RequestMethod method = RequestMethod.POST;
127 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
128 String responseStr = httpResult.getContent();
130 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
131 int code = httpResult.getStatusCode();
133 logger.info("CbamMgmrImpl -> terminateVnf sucess " );
135 logger.error("CbamMgmrImpl -> terminateVnf error " );
137 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
142 public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
143 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
144 RequestMethod method = RequestMethod.DELETE;
145 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
147 int code = httpResult.getStatusCode();
149 logger.info("CbamMgmrImpl -> deleteVnf success.");
151 logger.error("CbamMgmrImpl -> deleteVnf error. detail info is " + httpResult.getContent());
156 public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
157 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
158 RequestMethod method = RequestMethod.POST;
160 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
161 String responseStr = httpResult.getContent();
162 int code = httpResult.getStatusCode();
164 logger.info("CbamMgmrImpl -> scaleVnf success.");
166 logger.error("CbamMgmrImpl -> scaleVnf error. " );
168 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
173 public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
174 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
175 RequestMethod method = RequestMethod.POST;
177 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
178 String responseStr = httpResult.getContent();
180 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
181 int code = httpResult.getStatusCode();
183 logger.info("CbamMgmrImpl -> healVnf success.");
185 logger.error("CbamMgmrImpl -> healVnf error. " );
187 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
192 public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
193 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
194 RequestMethod method = RequestMethod.GET;
196 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
197 String responseStr = httpResult.getContent();
199 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
200 int code = httpResult.getStatusCode();
202 logger.info("CbamMgmrImpl -> queryVnf success.");
204 logger.error("CbamMgmrImpl -> queryVnf error. " );
207 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
212 public HttpResult operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
215 token = retrieveToken();
216 } catch (JSONException e) {
217 logger.error("retrieveTokenError ", e);
220 String url= adaptorEnv.getCbamApiUriFront() + httpPath;
222 HashMap<String, String> map = new HashMap<>();
223 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
224 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
226 return httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj));
229 public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException{
230 String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
231 RequestMethod method = RequestMethod.GET;
233 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
234 String responseStr = httpResult.getContent();
236 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
238 int code = httpResult.getStatusCode();
240 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, success" );
241 }else if(code == 202) {
242 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, ongoing" );
244 logger.error("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, error" );
248 CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
253 public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
254 this.adaptorEnv = adaptorEnv;
258 public void uploadVnfPackage(String cbamPackageFilePath) throws ClientProtocolException, IOException {
259 String httpPath = CommonConstants.CbamUploadVnfPackagePath;
260 RequestMethod method = RequestMethod.POST;
262 HttpResult httpResult = operateCbamHttpUploadTask(cbamPackageFilePath, httpPath, method);
263 String responseStr = httpResult.getContent();
265 logger.info("CbamMgmrImpl -> uploadVnfPackage, responseStr is " + responseStr);
267 int code = httpResult.getStatusCode();
269 logger.info("CbamMgmrImpl -> uploadVnfPackage, success" );
270 logger.info("Upload vnf package " + cbamPackageFilePath + " to CBAM is successful.");
272 logger.error("CbamMgmrImpl -> uploadVnfPackage, error" );
276 private HttpResult operateCbamHttpUploadTask(String filePath, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
279 token = retrieveToken();
280 } catch (JSONException e) {
281 logger.error("retrieveTokenError ", e);
284 String url = adaptorEnv.getCbamApiUriFront() + httpPath;
286 HashMap<String, String> map = new HashMap<>();
287 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
288 map.put(CommonConstants.CONTENT_TYPE, "multipart/form-data, boundary=--fsgdsfgjgjdsgdfjgjgj");
289 byte[] fileBytes = CommonUtil.getBytes(filePath);
290 return httpClientProcessor.processBytes(url, method, map, fileBytes);