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.constant.CommonConstants;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpResult;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.MediaType;
45 import org.springframework.stereotype.Component;
46 import org.springframework.web.bind.annotation.RequestMethod;
48 import com.google.gson.Gson;
51 public class CbamMgmrImpl implements CbamMgmrInf {
52 private static final Logger logger = Logger.getLogger(CbamMgmrImpl.class);
53 private Gson gson = new Gson();
56 private AdaptorEnv adaptorEnv;
59 HttpClientProcessorInf httpClientProcessor;
61 private String retrieveToken() throws ClientProtocolException, IOException, JSONException {
63 String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.RetrieveCbamTokenPath;
64 HashMap<String, String> map = new HashMap<>();
65 map.put(CommonConstants.ACCEPT, "*/*");
66 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
68 String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret());
70 String responseStr = httpClientProcessor.process(url, RequestMethod.GET, map, bodyPostStr).getContent();
72 logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
74 JSONObject tokenJsonObject = new JSONObject(responseStr);
76 result = tokenJsonObject.getString(CommonConstants.CBAM_TOKEN_KEY);
81 public CBAMCreateVnfResponse createVnf(CBAMCreateVnfRequest cbamRequest) throws ClientProtocolException, IOException {
82 String httpPath = CommonConstants.CbamCreateVnfPath;
83 RequestMethod method = RequestMethod.POST;
85 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
86 String responseStr = httpResult.getContent();
88 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
89 int code = httpResult.getStatusCode();
91 logger.info("CbamMgmrImpl -> createVnf success");
93 logger.error("CbamMgmrImpl -> createVnf error ");
95 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
101 * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
103 public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
104 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
105 RequestMethod method = RequestMethod.POST;
107 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
108 String responseStr = httpResult.getContent();
110 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
111 int code = httpResult.getStatusCode();
113 logger.info("CbamMgmrImpl -> instantiateVnf success " );
115 logger.error("CbamMgmrImpl -> instantiateVnf error " );
117 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
122 public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
123 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
124 RequestMethod method = RequestMethod.POST;
126 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
127 String responseStr = httpResult.getContent();
129 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
130 int code = httpResult.getStatusCode();
132 logger.info("CbamMgmrImpl -> terminateVnf sucess " );
134 logger.error("CbamMgmrImpl -> terminateVnf error " );
136 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
141 public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
142 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
143 RequestMethod method = RequestMethod.DELETE;
144 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
146 int code = httpResult.getStatusCode();
148 logger.info("CbamMgmrImpl -> deleteVnf success.");
150 logger.error("CbamMgmrImpl -> deleteVnf error. detail info is " + httpResult.getContent());
155 public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
156 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
157 RequestMethod method = RequestMethod.POST;
159 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
160 String responseStr = httpResult.getContent();
161 int code = httpResult.getStatusCode();
163 logger.info("CbamMgmrImpl -> scaleVnf success.");
165 logger.error("CbamMgmrImpl -> scaleVnf error. " );
167 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
172 public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
173 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
174 RequestMethod method = RequestMethod.POST;
176 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
177 String responseStr = httpResult.getContent();
179 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
180 int code = httpResult.getStatusCode();
182 logger.info("CbamMgmrImpl -> healVnf success.");
184 logger.error("CbamMgmrImpl -> healVnf error. " );
186 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
191 public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
192 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
193 RequestMethod method = RequestMethod.GET;
195 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
196 String responseStr = httpResult.getContent();
198 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
199 int code = httpResult.getStatusCode();
201 logger.info("CbamMgmrImpl -> queryVnf success.");
203 logger.error("CbamMgmrImpl -> queryVnf error. " );
206 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
211 public HttpResult operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
214 token = retrieveToken();
215 } catch (JSONException e) {
216 logger.error("retrieveTokenError ", e);
219 String url= adaptorEnv.getCbamApiUriFront() + httpPath;
221 HashMap<String, String> map = new HashMap<>();
222 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
223 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
225 return httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj));
228 public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException{
229 String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
230 RequestMethod method = RequestMethod.GET;
232 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
233 String responseStr = httpResult.getContent();
235 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
237 int code = httpResult.getStatusCode();
239 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, success" );
240 }else if(code == 202) {
241 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, ongoing" );
243 logger.error("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, error" );
247 CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
252 public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
253 this.adaptorEnv = adaptorEnv;