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 com.nokia.vfcadaptor.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.JSONObject;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.http.MediaType;
27 import org.springframework.stereotype.Component;
28 import org.springframework.web.bind.annotation.RequestMethod;
30 import com.google.gson.Gson;
31 import com.nokia.vfcadaptor.cbam.bo.CBAMCreateVnfRequest;
32 import com.nokia.vfcadaptor.cbam.bo.CBAMCreateVnfResponse;
33 import com.nokia.vfcadaptor.cbam.bo.CBAMHealVnfRequest;
34 import com.nokia.vfcadaptor.cbam.bo.CBAMHealVnfResponse;
35 import com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest;
36 import com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfResponse;
37 import com.nokia.vfcadaptor.cbam.bo.CBAMQueryVnfResponse;
38 import com.nokia.vfcadaptor.cbam.bo.CBAMScaleVnfRequest;
39 import com.nokia.vfcadaptor.cbam.bo.CBAMScaleVnfResponse;
40 import com.nokia.vfcadaptor.cbam.bo.CBAMTerminateVnfRequest;
41 import com.nokia.vfcadaptor.cbam.bo.CBAMTerminateVnfResponse;
42 import com.nokia.vfcadaptor.cbam.inf.CbamMgmrInf;
43 import com.nokia.vfcadaptor.common.bo.AdaptorEnv;
44 import com.nokia.vfcadaptor.constant.CommonConstants;
45 import com.nokia.vfcadaptor.http.client.HttpRequestProcessor;
48 public class CbamMgmrImpl implements CbamMgmrInf {
49 private Logger logger = Logger.getLogger(CbamMgmrImpl.class);
51 private Gson gson = new Gson();
54 private AdaptorEnv adaptorEnv;
57 private HttpClientBuilder httpClientBuilder;
59 private String retrieveToken() throws ClientProtocolException, IOException {
61 String url="https://" + adaptorEnv.getCbamIp() + CommonConstants.RetrieveCbamTokenPath;
62 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, RequestMethod.POST);
63 processor.addHdeader(CommonConstants.ACCEPT, "*/*");
64 processor.addHdeader(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
66 String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret());
67 processor.addPostEntity(bodyPostStr);
69 String responseStr = processor.process(url);
71 logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
73 JSONObject tokenJsonObject = new JSONObject(responseStr);
75 result = tokenJsonObject.getString(CommonConstants.CBAM_TOKEN_KEY);
80 public CBAMCreateVnfResponse createVnf(CBAMCreateVnfRequest cbamRequest) throws ClientProtocolException, IOException {
81 String httpPath = CommonConstants.CbamCreateVnfPath;
82 RequestMethod method = RequestMethod.POST;
84 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
86 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
88 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
94 * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
96 public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
97 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
98 RequestMethod method = RequestMethod.POST;
100 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
102 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
104 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
109 public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
110 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
111 RequestMethod method = RequestMethod.POST;
113 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
115 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
117 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
122 public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
123 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
124 RequestMethod method = RequestMethod.DELETE;
126 operateCbamHttpTask(null, httpPath, method);
128 logger.info("CbamMgmrImpl -> deleteVnf.");
132 * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#scaleVnf(com.nokia.vfcadaptor.cbam.bo.CBAMScaleVnfRequest, java.lang.String)
134 public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
135 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
136 RequestMethod method = RequestMethod.POST;
138 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
140 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
145 public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
146 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
147 RequestMethod method = RequestMethod.POST;
149 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
151 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
153 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
158 public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
159 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
160 RequestMethod method = RequestMethod.GET;
162 String responseStr = operateCbamHttpTask(null, httpPath, method);
164 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
166 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
171 public String operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
172 String token = retrieveToken();
173 String url="https://" + adaptorEnv.getCbamIp() + httpPath;
174 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, method);
175 processor.addHdeader(CommonConstants.AUTHORIZATION, "bearer " + token);
176 processor.addHdeader(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
178 processor.addPostEntity(gson.toJson(httpBodyObj));
180 String responseStr = processor.process(url);