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