Modify POM parent oparent version as 0.1.1
[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.constant.CommonConstants;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpClientProcessorInf;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.http.client.HttpResult;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.MediaType;
45 import org.springframework.stereotype.Component;
46 import org.springframework.web.bind.annotation.RequestMethod;
47
48 import com.google.gson.Gson;
49
50 @Component
51 public class CbamMgmrImpl implements CbamMgmrInf {
52         private static final Logger logger = Logger.getLogger(CbamMgmrImpl.class);
53         private Gson gson = new Gson();
54         
55         @Autowired 
56         private AdaptorEnv adaptorEnv;
57         
58         @Autowired
59         HttpClientProcessorInf httpClientProcessor;
60         
61         private String retrieveToken() throws ClientProtocolException, IOException, JSONException {
62                 String result = null;
63                 String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.RetrieveCbamTokenPath;
64                 HashMap<String, String> map = new HashMap<>();
65                 map.put(CommonConstants.ACCEPT, "*/*");
66                 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
67                 
68                 String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret());
69                 
70                 String responseStr = httpClientProcessor.process(url, RequestMethod.GET, map, bodyPostStr).getContent();
71                 
72                 logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
73                 
74                 JSONObject tokenJsonObject = new JSONObject(responseStr);
75                 
76                 result = tokenJsonObject.getString(CommonConstants.CBAM_TOKEN_KEY);
77                 
78                 return result;
79         }
80         
81         public CBAMCreateVnfResponse createVnf(CBAMCreateVnfRequest cbamRequest) throws ClientProtocolException, IOException {
82                 String httpPath = CommonConstants.CbamCreateVnfPath;
83                 RequestMethod method = RequestMethod.POST;
84                         
85                 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
86                 String responseStr = httpResult.getContent();
87                 
88                 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
89                 int code = httpResult.getStatusCode();
90                 if(code == 201) {
91                         logger.info("CbamMgmrImpl -> createVnf success");
92                 }else {
93                         logger.error("CbamMgmrImpl -> createVnf error ");
94                 }
95                 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
96                 
97                 return response;
98         }
99         
100         /* (non-Javadoc)
101          * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
102          */
103         public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
104                 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
105                 RequestMethod method = RequestMethod.POST;
106                         
107                 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
108                 String responseStr = httpResult.getContent();
109                 
110                 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
111                 int code = httpResult.getStatusCode();
112                 if(code == 202) {
113                         logger.info("CbamMgmrImpl -> instantiateVnf success " );
114                 }else {
115                         logger.error("CbamMgmrImpl -> instantiateVnf error " );
116                 }
117                 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
118                 
119                 return response;
120         }
121         
122         public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
123                 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
124                 RequestMethod method = RequestMethod.POST;
125                 
126                 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
127                 String responseStr = httpResult.getContent();
128                 
129                 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
130                 int code = httpResult.getStatusCode();
131                 if(code == 202) {
132                         logger.info("CbamMgmrImpl -> terminateVnf  sucess " );
133                 }else {
134                         logger.error("CbamMgmrImpl -> terminateVnf error " );
135                 }
136                 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
137                 
138                 return response;
139         }
140         
141         public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
142                 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
143                 RequestMethod method = RequestMethod.DELETE;
144                 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
145                 
146                 int code = httpResult.getStatusCode();
147                 if(code == 204) {
148                         logger.info("CbamMgmrImpl -> deleteVnf success.");
149                 }else {
150                     logger.error("CbamMgmrImpl -> deleteVnf error. detail info is " + httpResult.getContent());
151                 }
152                 
153         }
154         
155         public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
156                 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
157                 RequestMethod method = RequestMethod.POST;
158                         
159                 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
160                 String responseStr = httpResult.getContent();
161                 int code = httpResult.getStatusCode();
162                 if(code == 202) {
163                         logger.info("CbamMgmrImpl -> scaleVnf success.");
164                 }else {
165                     logger.error("CbamMgmrImpl -> scaleVnf error. " );
166                 }
167                 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
168                 
169                 return response;
170         }
171
172         public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
173                 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
174                 RequestMethod method = RequestMethod.POST;
175                         
176                 HttpResult httpResult = operateCbamHttpTask(cbamRequest, httpPath, method);
177                 String responseStr = httpResult.getContent();
178                 
179                 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
180                 int code = httpResult.getStatusCode();
181                 if(code == 202) {
182                         logger.info("CbamMgmrImpl -> healVnf success.");
183                 }else {
184                     logger.error("CbamMgmrImpl -> healVnf error. " );
185                 }
186                 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
187                 
188                 return response;
189         }
190         
191         public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
192                 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
193                 RequestMethod method = RequestMethod.GET;
194                 
195                 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
196                 String responseStr = httpResult.getContent();
197                 
198                 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
199                 int code = httpResult.getStatusCode();
200                 if(code == 200) {
201                         logger.info("CbamMgmrImpl -> queryVnf success.");
202                 }else {
203                     logger.error("CbamMgmrImpl -> queryVnf error. " );
204                 }
205                 
206                 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
207                 
208                 return response;
209         }
210
211         public HttpResult operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
212                 String token = null;
213                 try {
214                         token = retrieveToken();
215                 } catch (JSONException e) {
216                         logger.error("retrieveTokenError ", e);
217                 }
218         
219                 String url= adaptorEnv.getCbamApiUriFront() + httpPath;
220                 
221                 HashMap<String, String> map = new HashMap<>();
222                 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
223                 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
224                 
225                 return httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj));
226         }
227
228         public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException{
229                 String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
230                 RequestMethod method = RequestMethod.GET;
231                 
232                 HttpResult httpResult = operateCbamHttpTask(null, httpPath, method);
233                 String responseStr = httpResult.getContent();
234                 
235                 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
236                 
237                 int code = httpResult.getStatusCode();
238                 if(code == 200) {
239                         logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, success" );
240                 }else if(code == 202) {
241                         logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, ongoing" );
242                 }else {
243                         logger.error("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, error" );
244                 }
245                 
246                 
247                 CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
248                 
249                 return response;
250         }
251
252         public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
253                 this.adaptorEnv = adaptorEnv;
254         }
255         
256 }