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;
21 import org.apache.http.client.ClientProtocolException;
22 import org.apache.http.impl.client.HttpClientBuilder;
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.HttpRequestProcessor;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.http.MediaType;
44 import org.springframework.stereotype.Component;
45 import org.springframework.web.bind.annotation.RequestMethod;
47 import com.google.gson.Gson;
50 public class CbamMgmrImpl implements CbamMgmrInf {
51 private static final Logger logger = Logger.getLogger(CbamMgmrImpl.class);
52 private Gson gson = new Gson();
55 private AdaptorEnv adaptorEnv;
58 private HttpClientBuilder httpClientBuilder;// = HttpClientUtils.createHttpClientBuilder();
60 private String retrieveToken() throws ClientProtocolException, IOException, JSONException {
62 String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.RetrieveCbamTokenPath;
63 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, RequestMethod.POST);
64 processor.addHdeader(CommonConstants.ACCEPT, "*/*");
65 processor.addHdeader(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
67 String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret());
68 processor.addPostEntity(bodyPostStr);
70 String responseStr = processor.process(url);
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 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
87 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
89 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
95 * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
97 public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
98 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
99 RequestMethod method = RequestMethod.POST;
101 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
103 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
105 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
110 public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
111 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
112 RequestMethod method = RequestMethod.POST;
114 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
116 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
118 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
123 public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
124 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
125 RequestMethod method = RequestMethod.DELETE;
127 operateCbamHttpTask(null, httpPath, method);
129 logger.info("CbamMgmrImpl -> deleteVnf.");
132 public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
133 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
134 RequestMethod method = RequestMethod.POST;
136 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
138 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
143 public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
144 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
145 RequestMethod method = RequestMethod.POST;
147 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
149 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
151 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
156 public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
157 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
158 RequestMethod method = RequestMethod.GET;
160 String responseStr = operateCbamHttpTask(null, httpPath, method);
162 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
164 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
169 public String operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
172 token = retrieveToken();
173 } catch (JSONException e) {
174 logger.error("retrieveTokenError ", e);
177 String url= adaptorEnv.getCbamApiUriFront() + httpPath;
178 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, method);
179 processor.addHdeader(CommonConstants.AUTHORIZATION, "bearer " + token);
180 processor.addHdeader(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
182 processor.addPostEntity(gson.toJson(httpBodyObj));
184 String responseStr = processor.process(url);
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);