2c88320e8bd81cd3d259a09082a97ab630c743cc
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / cbam / impl / CbamMgmrImpl.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.impl;
18
19 import java.io.IOException;
20 import java.util.HashMap;
21
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;
48
49 import com.google.gson.Gson;
50
51 @Component
52 public class CbamMgmrImpl implements CbamMgmrInf {
53         private static final Logger logger = Logger.getLogger(CbamMgmrImpl.class);
54         private Gson gson = new Gson();
55         
56         @Autowired 
57         private AdaptorEnv adaptorEnv;
58         
59         @Autowired
60         HttpClientProcessorInf httpClientProcessor;
61         
62         private String retrieveToken() throws ClientProtocolException, IOException, JSONException {
63                 String result = null;
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);
68                 
69                 String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret());
70                 
71                 String responseStr = httpClientProcessor.process(url, RequestMethod.GET, map, bodyPostStr);
72                 
73                 logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
74                 
75                 JSONObject tokenJsonObject = new JSONObject(responseStr);
76                 
77                 result = tokenJsonObject.getString(CommonConstants.CBAM_TOKEN_KEY);
78                 
79                 return result;
80         }
81         
82         public CBAMCreateVnfResponse createVnf(CBAMCreateVnfRequest cbamRequest) throws ClientProtocolException, IOException {
83                 String httpPath = CommonConstants.CbamCreateVnfPath;
84                 RequestMethod method = RequestMethod.POST;
85                         
86                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
87                 
88                 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
89                 
90                 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
91                 
92                 return response;
93         }
94         
95         /* (non-Javadoc)
96          * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
97          */
98         public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
99                 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
100                 RequestMethod method = RequestMethod.POST;
101                         
102                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
103                 
104                 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
105                 
106                 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
107                 
108                 return response;
109         }
110         
111         public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
112                 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
113                 RequestMethod method = RequestMethod.POST;
114                 
115                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
116                 
117                 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
118                 
119                 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
120                 
121                 return response;
122         }
123         
124         public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
125                 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
126                 RequestMethod method = RequestMethod.DELETE;
127                 
128                 operateCbamHttpTask(null, httpPath, method);
129                 
130                 logger.info("CbamMgmrImpl -> deleteVnf.");
131         }
132         
133         public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
134                 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
135                 RequestMethod method = RequestMethod.POST;
136                         
137                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
138                 
139                 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
140                 
141                 return response;
142         }
143
144         public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
145                 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
146                 RequestMethod method = RequestMethod.POST;
147                         
148                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
149                 
150                 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
151                 
152                 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
153                 
154                 return response;
155         }
156         
157         public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
158                 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
159                 RequestMethod method = RequestMethod.GET;
160                 
161                 String responseStr = operateCbamHttpTask(null, httpPath, method);
162                 
163                 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
164                 
165                 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
166                 
167                 return response;
168         }
169
170         public String operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
171                 String token = null;
172                 try {
173                         token = retrieveToken();
174                 } catch (JSONException e) {
175                         logger.error("retrieveTokenError ", e);
176                 }
177         
178                 String url= adaptorEnv.getCbamApiUriFront() + httpPath;
179                 
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);
183                 
184                 String responseStr = httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj));
185                 
186                 return responseStr;
187         }
188
189         public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException{
190                 String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
191                 RequestMethod method = RequestMethod.GET;
192                 
193                 String responseStr = operateCbamHttpTask(null, httpPath, method);
194                 
195                 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
196                 
197                 CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
198                 
199                 return response;
200         }
201
202         public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
203                 this.adaptorEnv = adaptorEnv;
204         }
205         
206 }