Remove duplicate parts.
[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.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         HttpClientProcessorInf httpClientProcessor;
59         
60         private String retrieveToken() throws ClientProtocolException, IOException, JSONException {
61                 String result = null;
62                 String url= adaptorEnv.getCbamApiUriFront() + CommonConstants.RetrieveCbamTokenPath;
63                 HashMap<String, String> map = new HashMap<String, String>();
64                 map.put(CommonConstants.ACCEPT, "*/*");
65                 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
66                 
67                 String bodyPostStr = String.format(CommonConstants.RetrieveCbamTokenPostStr, adaptorEnv.getGrantType(), adaptorEnv.getClientId(), adaptorEnv.getClientSecret());
68                 
69                 String responseStr = httpClientProcessor.process(url, RequestMethod.GET, map, bodyPostStr).getContent();
70                 
71                 logger.info("CbamMgmrImpl -> retrieveToken, responseStr is " + responseStr);
72                 
73                 JSONObject tokenJsonObject = new JSONObject(responseStr);
74                 
75                 result = tokenJsonObject.getString(CommonConstants.CBAM_TOKEN_KEY);
76                 
77                 return result;
78         }
79         
80         public CBAMCreateVnfResponse createVnf(CBAMCreateVnfRequest cbamRequest) throws ClientProtocolException, IOException {
81                 String httpPath = CommonConstants.CbamCreateVnfPath;
82                 RequestMethod method = RequestMethod.POST;
83                         
84                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
85                 
86                 logger.info("CbamMgmrImpl -> createVnf, responseStr is " + responseStr);
87                 
88                 CBAMCreateVnfResponse response = gson.fromJson(responseStr, CBAMCreateVnfResponse.class);
89                 
90                 return response;
91         }
92         
93         /* (non-Javadoc)
94          * @see com.nokia.vfcadaptor.cbam.impl.CbamMgmrInf#instantiateVnf(com.nokia.vfcadaptor.cbam.bo.CBAMInstantiateVnfRequest, java.lang.String)
95          */
96         public CBAMInstantiateVnfResponse instantiateVnf(CBAMInstantiateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
97                 String httpPath = String.format(CommonConstants.CbamInstantiateVnfPath, vnfInstanceId);
98                 RequestMethod method = RequestMethod.POST;
99                         
100                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
101                 
102                 logger.info("CbamMgmrImpl -> instantiateVnf, responseStr is " + responseStr);
103                 
104                 CBAMInstantiateVnfResponse response = gson.fromJson(responseStr, CBAMInstantiateVnfResponse.class);
105                 
106                 return response;
107         }
108         
109         public CBAMTerminateVnfResponse terminateVnf(CBAMTerminateVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
110                 String httpPath = String.format(CommonConstants.CbamTerminateVnfPath, vnfInstanceId);
111                 RequestMethod method = RequestMethod.POST;
112                 
113                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
114                 
115                 logger.info("CbamMgmrImpl -> terminateVnf, responseStr is " + responseStr);
116                 
117                 CBAMTerminateVnfResponse response = gson.fromJson(responseStr, CBAMTerminateVnfResponse.class);
118                 
119                 return response;
120         }
121         
122         public void deleteVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
123                 String httpPath = String.format(CommonConstants.CbamDeleteVnfPath, vnfInstanceId);
124                 RequestMethod method = RequestMethod.DELETE;
125                 
126                 operateCbamHttpTask(null, httpPath, method);
127                 
128                 logger.info("CbamMgmrImpl -> deleteVnf.");
129         }
130         
131         public CBAMScaleVnfResponse scaleVnf(CBAMScaleVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
132                 String httpPath = String.format(CommonConstants.CbamScaleVnfPath, vnfInstanceId);
133                 RequestMethod method = RequestMethod.POST;
134                         
135                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
136                 
137                 CBAMScaleVnfResponse response = gson.fromJson(responseStr, CBAMScaleVnfResponse.class);
138                 
139                 return response;
140         }
141
142         public CBAMHealVnfResponse healVnf(CBAMHealVnfRequest cbamRequest, String vnfInstanceId) throws ClientProtocolException, IOException {
143                 String httpPath = String.format(CommonConstants.CbamHealVnfPath, vnfInstanceId);
144                 RequestMethod method = RequestMethod.POST;
145                         
146                 String responseStr = operateCbamHttpTask(cbamRequest, httpPath, method);
147                 
148                 logger.info("CbamMgmrImpl -> healVnf, responseStr is " + responseStr);
149                 
150                 CBAMHealVnfResponse response = gson.fromJson(responseStr, CBAMHealVnfResponse.class);
151                 
152                 return response;
153         }
154         
155         public CBAMQueryVnfResponse queryVnf(String vnfInstanceId) throws ClientProtocolException, IOException {
156                 String httpPath = String.format(CommonConstants.CbamQueryVnfPath, vnfInstanceId);
157                 RequestMethod method = RequestMethod.GET;
158                 
159                 String responseStr = operateCbamHttpTask(null, httpPath, method);
160                 
161                 logger.info("CbamMgmrImpl -> queryVnf, responseStr is " + responseStr);
162                 
163                 CBAMQueryVnfResponse response = gson.fromJson(responseStr, CBAMQueryVnfResponse.class);
164                 
165                 return response;
166         }
167
168         public String operateCbamHttpTask(Object httpBodyObj, String httpPath, RequestMethod method) throws ClientProtocolException, IOException {
169                 String token = null;
170                 try {
171                         token = retrieveToken();
172                 } catch (JSONException e) {
173                         logger.error("retrieveTokenError ", e);
174                 }
175         
176                 String url= adaptorEnv.getCbamApiUriFront() + httpPath;
177                 
178                 HashMap<String, String> map = new HashMap<String, String>();
179                 map.put(CommonConstants.AUTHORIZATION, "bearer " + token);
180                 map.put(CommonConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
181                 
182                 String responseStr = httpClientProcessor.process(url, method, map, gson.toJson(httpBodyObj)).getContent();
183                 
184                 return responseStr;
185         }
186
187         public CBAMQueryOperExecutionResponse queryOperExecution(String execId) throws ClientProtocolException, IOException{
188                 String httpPath = String.format(CommonConstants.CbamGetOperStatusPath, execId);
189                 RequestMethod method = RequestMethod.GET;
190                 
191                 String responseStr = operateCbamHttpTask(null, httpPath, method);
192                 
193                 logger.info("CbamMgmrImpl -> CBAMQueryOperExecutionResponse, responseStr is " + responseStr);
194                 
195                 CBAMQueryOperExecutionResponse response = gson.fromJson(responseStr, CBAMQueryOperExecutionResponse.class);
196                 
197                 return response;
198         }
199
200         public void setAdaptorEnv(AdaptorEnv adaptorEnv) {
201                 this.adaptorEnv = adaptorEnv;
202         }
203         
204 }