Modify database part in the deployment
[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
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.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.HttpRequestProcessor;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.http.MediaType;
44 import org.springframework.stereotype.Component;
45 import org.springframework.web.bind.annotation.RequestMethod;
46
47 import com.google.gson.Gson;
48
49 @Component
50 public class CbamMgmrImpl implements CbamMgmrInf {
51         private static final Logger logger = Logger.getLogger(CbamMgmrImpl.class);
52         private Gson gson = new Gson();
53         
54         @Autowired 
55         private AdaptorEnv adaptorEnv;
56         
57         @Autowired
58         private HttpClientBuilder httpClientBuilder;// = HttpClientUtils.createHttpClientBuilder();
59         
60         private String retrieveToken() throws ClientProtocolException, IOException, JSONException {
61                 String result = null;
62                 String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.RetrieveCbamTokenPath;
63                 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, RequestMethod.POST);
64                 processor.addHdeader(CommonConstants.ACCEPT, "*/*");
65                 processor.addHdeader(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
66                 
67                 String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret());
68                 processor.addPostEntity(bodyPostStr);
69                 
70                 String responseStr = processor.process(url);
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                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
86                 
87                 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
88                 
89                 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
90                 
91                 return response;
92         }
93         
94         /* (non-Javadoc)
95          * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
96          */
97         public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
98                 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
99                 RequestMethod method = RequestMethod.POST;
100                         
101                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
102                 
103                 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
104                 
105                 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
106                 
107                 return response;
108         }
109         
110         public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
111                 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
112                 RequestMethod method = RequestMethod.POST;
113                 
114                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
115                 
116                 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
117                 
118                 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
119                 
120                 return response;
121         }
122         
123         public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
124                 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
125                 RequestMethod method = RequestMethod.DELETE;
126                 
127                 operateCbamHttpTask(null, httpPath, method);
128                 
129                 logger.info("CbamMgmrImpl -> deleteVnf.");
130         }
131         
132         public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
133                 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
134                 RequestMethod method = RequestMethod.POST;
135                         
136                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
137                 
138                 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
139                 
140                 return response;
141         }
142
143         public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
144                 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
145                 RequestMethod method = RequestMethod.POST;
146                         
147                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
148                 
149                 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
150                 
151                 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
152                 
153                 return response;
154         }
155         
156         public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
157                 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
158                 RequestMethod method = RequestMethod.GET;
159                 
160                 String responseStr = operateCbamHttpTask(null, httpPath, method);
161                 
162                 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
163                 
164                 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
165                 
166                 return response;
167         }
168
169         public String operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
170                 String token = null;
171                 try {
172                         token = retrieveToken();
173                 } catch (JSONException e) {
174                         logger.error("retrieveTokenError ", e);
175                 }
176         
177                 String url= adaptorEnv.getCbamApiUriFront() + httpPath;
178                 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, method);
179                 processor.addHdeader(CommonConstants.AUTHORIZATION, "bearer " + token);
180                 processor.addHdeader(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
181                 
182                 processor.addPostEntity(gson.toJson(httpBodyObj));
183                 
184                 String responseStr = processor.process(url);
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 }