dc6a6189e42ba12473e319aa1f4e2af79b703f74
[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.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.common.util.CommonUtil;
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.HttpResult;
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         public String retrieveToken() throws ClientProtocolException, IOException, JSONException {
63                 String result = null;
64                 String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.CbamRetrieveTokenPath;
65                 HashMap<String, String> map = new HashMap<>();
66                 map.put(CommonConstants.ACCEPT, "*/*");
67                 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
68                 
69                 String bodyPostStr = String.format(CommonConstants.CbamRetrieveTokenPostStr, adaptorEnv.getClientId(), adaptorEnv.getClientSecret(), adaptorEnv.getCbamUserName(), adaptorEnv.getCbamPassword());
70                 
71                 logger.debug("CbamMgmrImpl -> retrieveToken, url is " + url);
72                 logger.debug("CbamMgmrImpl -> retrieveToken, bodyPostStr is " + bodyPostStr);
73                 
74                 String responseStr = httpClientProcessor.process(url, RequestMethod.POST, map, bodyPostStr).getContent();
75                 
76                 logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
77                 
78                 JSONObject tokenJsonObject = new JSONObject(responseStr);
79                 
80                 result = tokenJsonObject.getString(CommonConstants.CBAM_TOKEN_KEY);
81                 
82                 return result;
83         }
84         
85         public CBAMCreateVnfResponse createVnf(CBAMCreateVnfRequest cbamRequest) throws ClientProtocolException, IOException {
86                 String httpPath = CommonConstants.CbamCreateVnfPath;
87                 RequestMethod method = RequestMethod.POST;
88                         
89                 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
90                 String responseStr = httpResult.getContent();
91                 
92                 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
93                 int code = httpResult.getStatusCode();
94                 if(code == 201) {
95                         logger.info("CbamMgmrImpl -> createVnf success");
96                 }else {
97                         logger.error("CbamMgmrImpl -> createVnf error ");
98                 }
99                 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
100                 
101                 return response;
102         }
103         
104         /* (non-Javadoc)
105          * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
106          */
107         public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
108                 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
109                 RequestMethod method = RequestMethod.POST;
110                         
111                 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
112                 String responseStr = httpResult.getContent();
113                 
114                 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
115                 int code = httpResult.getStatusCode();
116                 if(code == 202) {
117                         logger.info("CbamMgmrImpl -> instantiateVnf success " );
118                 }else {
119                         logger.error("CbamMgmrImpl -> instantiateVnf error " );
120                 }
121                 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
122                 
123                 return response;
124         }
125         
126         public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
127                 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
128                 RequestMethod method = RequestMethod.POST;
129                 
130                 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
131                 String responseStr = httpResult.getContent();
132                 
133                 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
134                 int code = httpResult.getStatusCode();
135                 if(code == 202) {
136                         logger.info("CbamMgmrImpl -> terminateVnf  sucess " );
137                 }else {
138                         logger.error("CbamMgmrImpl -> terminateVnf error " );
139                 }
140                 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
141                 
142                 return response;
143         }
144         
145         public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
146                 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
147                 RequestMethod method = RequestMethod.DELETE;
148                 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
149                 
150                 int code = httpResult.getStatusCode();
151                 if(code == 204) {
152                         logger.info("CbamMgmrImpl -> deleteVnf success.");
153                 }else {
154                     logger.error("CbamMgmrImpl -> deleteVnf error. detail info is " + httpResult.getContent());
155                 }
156                 
157         }
158         
159         public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
160                 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
161                 RequestMethod method = RequestMethod.POST;
162                         
163                 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
164                 String responseStr = httpResult.getContent();
165                 int code = httpResult.getStatusCode();
166                 if(code == 202) {
167                         logger.info("CbamMgmrImpl -> scaleVnf success.");
168                 }else {
169                     logger.error("CbamMgmrImpl -> scaleVnf error. " );
170                 }
171                 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
172                 
173                 return response;
174         }
175
176         public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
177                 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
178                 RequestMethod method = RequestMethod.POST;
179                         
180                 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
181                 String responseStr = httpResult.getContent();
182                 
183                 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
184                 int code = httpResult.getStatusCode();
185                 if(code == 202) {
186                         logger.info("CbamMgmrImpl -> healVnf success.");
187                 }else {
188                     logger.error("CbamMgmrImpl -> healVnf error. " );
189                 }
190                 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
191                 
192                 return response;
193         }
194         
195         public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
196                 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
197                 RequestMethod method = RequestMethod.GET;
198                 
199                 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
200                 String responseStr = httpResult.getContent();
201                 
202                 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
203                 int code = httpResult.getStatusCode();
204                 if(code == 200) {
205                         logger.info("CbamMgmrImpl -> queryVnf success.");
206                 }else {
207                     logger.error("CbamMgmrImpl -> queryVnf error. " );
208                 }
209                 
210                 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
211                 
212                 return response;
213         }
214
215         public HttpResult operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
216                 String token = null;
217                 try {
218                         token = retrieveToken();
219                 } catch (JSONException e) {
220                         logger.error("retrieveTokenError ", e);
221                 }
222         
223                 String url= adaptorEnv.getCbamApiUriFront() + httpPath;
224                 
225                 HashMap<String, String> map = new HashMap<>();
226                 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
227                 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
228                 
229                 return httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj));
230         }
231
232         public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException{
233                 String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
234                 RequestMethod method = RequestMethod.GET;
235                 
236                 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
237                 String responseStr = httpResult.getContent();
238                 
239                 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
240                 
241                 int code = httpResult.getStatusCode();
242                 if(code == 200) {
243                         logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, success" );
244                 }else if(code == 202) {
245                         logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, ongoing" );
246                 }else {
247                         logger.error("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, error" );
248                 }
249                 
250                 
251                 CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
252                 
253                 return response;
254         }
255
256         public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
257                 this.adaptorEnv = adaptorEnv;
258         }
259
260         @Override
261         public void uploadVnfPackage(String cbamPackageFilePath) throws ClientProtocolException, IOException {
262                 String httpPath = CommonConstants.CbamUploadVnfPackagePath;
263                 RequestMethod method = RequestMethod.POST;
264                 
265                 HttpResult httpResult = operateCbamHttpUploadTask(cbamPackageFilePath, httpPath, method);
266                 String responseStr = httpResult.getContent();
267                 
268                 logger.info("CbamMgmrImpl -> uploadVnfPackage, statusCode is " + httpResult.getStatusCode() + ", cause is " + httpResult.getStatusCause() + ". responseStr is " + responseStr);
269                 
270                 int code = httpResult.getStatusCode();
271                 if(code == 200) {
272                         logger.info("CbamMgmrImpl -> uploadVnfPackage, success" );
273                         logger.info("Upload vnf package " + cbamPackageFilePath + " to CBAM is successful.");
274                 }else {
275                         logger.error("CbamMgmrImpl -> uploadVnfPackage, error" );
276                 }
277         }
278
279         public HttpResult operateCbamHttpUploadTask(String filePath, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
280                 String token = null;
281                 try {
282                         token = retrieveToken();
283                 } catch (JSONException e) {
284                         logger.error("retrieveTokenError ", e);
285                 }
286         
287                 String url = adaptorEnv.getCbamApiUriFront() + httpPath;
288                 
289                 HashMap<String, String> map = new HashMap<>();
290                 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
291                 map.put(CommonConstants.CONTENT_TYPE, "multipart/form-data, boundary=--fsgdsfgjgjdsgdfjgjgj");
292                 byte[] fileBytes = CommonUtil.getBytes(filePath);
293                 logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, url is " + url);
294                 logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, token is " + token);
295                 logger.info("CbamMgmrImpl -> operateCbamHttpUploadTask, bodyPostStr byte lenth is " + fileBytes.length);
296                 
297                 return httpClientProcessor.processBytes(url, method, map, fileBytes);
298         }
299
300         public HttpClientProcessorInf getHttpClientProcessor() {
301                 return httpClientProcessor;
302         }
303
304         public void setHttpClientProcessor(HttpClientProcessorInf httpClientProcessor) {
305                 this.httpClientProcessor = httpClientProcessor;
306         }
307 }