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.http.impl.client.HttpClientBuilder;
24 import org.apache.log4j.Logger;
25 import org.json.JSONException;
26 import org.json.JSONObject;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfRequest;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMCreateVnfResponse;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfRequest;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMHealVnfResponse;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfRequest;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMInstantiateVnfResponse;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryOperExecutionResponse;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMQueryVnfResponse;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfRequest;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMScaleVnfResponse;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfRequest;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.bo.CBAMTerminateVnfResponse;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.common.bo.AdaptorEnv;
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.HttpRequestProcessor;
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.RetrieveCbamTokenPath;
65 HashMap<String, String> map = new HashMap<String, String>();
66 map.put(CommonConstants.ACCEPT, "*/*");
67 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
69 String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret());
71 String responseStr = httpClientProcessor.process(url, RequestMethod.GET, map, bodyPostStr);
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 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
88 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
90 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
96 * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
98 public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
99 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
100 RequestMethod method = RequestMethod.POST;
102 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
104 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
106 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
111 public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
112 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
113 RequestMethod method = RequestMethod.POST;
115 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
117 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
119 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
124 public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
125 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
126 RequestMethod method = RequestMethod.DELETE;
128 operateCbamHttpTask(null, httpPath, method);
130 logger.info("CbamMgmrImpl -> deleteVnf.");
133 public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
134 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
135 RequestMethod method = RequestMethod.POST;
137 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
139 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
144 public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
145 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
146 RequestMethod method = RequestMethod.POST;
148 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
150 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
152 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
157 public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
158 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
159 RequestMethod method = RequestMethod.GET;
161 String responseStr = operateCbamHttpTask(null, httpPath, method);
163 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
165 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
170 public String operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
173 token = retrieveToken();
174 } catch (JSONException e) {
175 logger.error("retrieveTokenError ", e);
178 String url= adaptorEnv.getCbamApiUriFront() + httpPath;
180 HashMap<String, String> map = new HashMap<String, String>();
181 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
182 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
184 String responseStr = httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj));
189 public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException{
190 String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
191 RequestMethod method = RequestMethod.GET;
193 String responseStr = operateCbamHttpTask(null, httpPath, method);
195 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
197 CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
202 public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
203 this.adaptorEnv = adaptorEnv;