2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.vid.aai.util;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URLEncoder;
25 import java.security.KeyManagementException;
26 import java.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.Base64;
29 import java.util.Date;
30 import java.util.UUID;
32 import javax.ws.rs.client.Client;
33 import javax.ws.rs.client.Entity;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
37 import com.att.eelf.configuration.EELFLogger;
38 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
39 import org.openecomp.portalsdk.core.util.SystemProperties;
40 import org.eclipse.jetty.util.security.Password;
41 import org.onap.vid.utils.Logging;
42 import org.springframework.http.HttpMethod;
43 import static org.onap.vid.utils.Logging.getHttpServletRequest;
44 import static org.onap.vid.utils.Logging.requestIdHeaderKey;
48 * The Class AAIRestInterface.
50 public class AAIRestInterface {
53 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AAIRestInterface.class);
55 final private EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("aai");
57 /** The Constant dateFormat. */
58 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
61 private static Client client = null;
63 /** The rest srvr base URL. */
64 private String restSrvrBaseURL;
66 /** The certificate path. */
67 public String certificatePath = "";
69 private String START_STRING = " start";
71 private String TRANSACTION_ID_HEADER = "X-TransactionId";
72 private String FROM_APP_ID_HEADER = "X-FromAppId";
73 private String SUCCESSFUL_API_MESSAGE=" REST api POST was successful!";
74 private String URL_DECLERATION = ", url=";
82 * Instantiates a new AAI rest interface.
84 * @param certPath the cert path
86 public AAIRestInterface(String certPath)
88 certificatePath = certPath;
94 * @param nodeKey the node key
96 * @throws UnsupportedEncodingException the unsupported encoding exception
98 public String encodeURL (String nodeKey) throws UnsupportedEncodingException {
99 return URLEncoder.encode(nodeKey, "UTF-8").replaceAll("\\+", "%20");
103 * Inits the rest client.
105 private void initRestClient()
107 String methodName = "initRestClient";
109 if (client == null) {
111 client = HttpsAuthClient.getClient(certificatePath);
113 catch (KeyManagementException e){
114 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== KeyManagementException in " + methodName + e.toString());
115 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== KeyManagementException in " + methodName + e.toString());
116 } catch (Exception e) {
117 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== Exception in REST call to DB in initRestClient" + e.toString());
118 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== Exception in REST call to DB : " + e.toString());
124 * Sets the rest srvr base URL.
126 * @param baseURL the base URL
128 public void SetRestSrvrBaseURL(String baseURL)
132 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== REST Server base URL cannot be null.");
133 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== REST Server base URL cannot be null.");
136 restSrvrBaseURL = baseURL;
140 * Gets the rest srvr base URL.
142 * @return the rest srvr base URL
144 public String getRestSrvrBaseURL()
146 return restSrvrBaseURL;
153 * @param fromAppId the from app id
154 * @param transId the trans id
155 * @param requestUri the request uri
158 * @throws UnsupportedEncodingException
160 public Response RestGet(String fromAppId, String transId, String requestUri, boolean xml) throws UnsupportedEncodingException {
161 String methodName = "RestGet";
163 String responseType = MediaType.APPLICATION_JSON;
165 responseType = MediaType.APPLICATION_XML;
169 String clientCert = SystemProperties.getProperty(AAIProperties.AAI_USE_CLIENT_CERT);
171 boolean useClientCert = false;
172 if (clientCert != null &&
173 SystemProperties.getProperty(AAIProperties.AAI_USE_CLIENT_CERT).equalsIgnoreCase("true")) {
174 useClientCert = true;
177 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START_STRING);
179 url = SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL) + requestUri;
182 logger.debug(dateFormat.format(new Date()) + "<== " + url + " for the get REST API");
183 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
186 if (useClientCert == true) {
187 cres = client.target(url)
189 .accept(responseType)
190 .header(TRANSACTION_ID_HEADER, transId)
191 .header(FROM_APP_ID_HEADER, fromAppId)
192 .header("Content-Type", MediaType.APPLICATION_JSON)
193 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
197 String vidUsername = SystemProperties.getProperty(AAIProperties.AAI_VID_USERNAME);
198 String vidPassword = Password.deobfuscate(SystemProperties.getProperty(AAIProperties.AAI_VID_PASSWD_X));
199 String encodeThis = vidUsername + ":" + vidPassword;
201 cres = client.target(url)
203 .accept(responseType)
204 .header(TRANSACTION_ID_HEADER, transId)
205 .header(FROM_APP_ID_HEADER, fromAppId)
206 .header("Content-Type", "application/json")
207 .header("Authorization", "Basic " + Base64.getEncoder().encodeToString(encodeThis.getBytes("utf-8")))
208 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
211 Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
212 // String r = cres.readEntity(String.class);
213 if (cres.getStatus() == 200) {
214 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + SUCCESSFUL_API_MESSAGE);
215 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + SUCCESSFUL_API_MESSAGE);
217 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName +" with status="+cres.getStatus()+URL_DECLERATION+url);
220 // logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName +" resp=" + r );
228 * @param sourceID the source ID
229 * @param transId the trans id
230 * @param path the path
231 * @return true, if successful
233 public boolean Delete(String sourceID, String transId, String path) {
234 String methodName = "Delete";
236 transId += ":" + UUID.randomUUID().toString();
237 logger.debug(dateFormat.format(new Date()) + "<== " + methodName + START_STRING);
240 String request = "{}";
241 url = SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL) + path;
242 Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url);
243 final Response cres = client.target(url)
245 .accept(MediaType.APPLICATION_JSON)
246 .header(TRANSACTION_ID_HEADER, transId)
247 .header(FROM_APP_ID_HEADER, sourceID)
248 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
251 Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
252 if (cres.getStatus() == 404) { // resource not found
253 String msg = "Resource does not exist...: " + cres.getStatus()
254 + ":" + cres.readEntity(String.class);
255 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + msg);
257 } else if (cres.getStatus() == 200 || cres.getStatus() == 204){
258 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "Resource " + url + " deleted");
261 String msg = "Deleting Resource failed: " + cres.getStatus()
262 + ":" + cres.readEntity(String.class);
263 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + msg);
273 * @param fromAppId the from app id
274 * @param transId the trans id
275 * @param path the path
276 * @param payload the payload
280 public Response RestPut(String fromAppId, String transId, String path, String payload, boolean xml) {
281 String methodName = "RestPut";
283 transId = UUID.randomUUID().toString();
284 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START_STRING);
288 String responseType = MediaType.APPLICATION_JSON;
290 responseType = "application/xml";
294 url = SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL) + path;
295 Logging.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, payload);
296 final Response cres = client.target(url)
298 .accept(responseType)
299 .header(TRANSACTION_ID_HEADER, transId)
300 .header(FROM_APP_ID_HEADER, fromAppId)
301 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
302 .put(Entity.entity(payload, MediaType.APPLICATION_JSON));
303 Logging.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, cres);
305 if (cres.getStatus() == 200 && cres.getStatus() <= 299) {
306 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + URL_DECLERATION);
307 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + URL_DECLERATION);
309 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName +" with status="+cres.getStatus()+URL_DECLERATION+url);
312 } catch (Exception e) {
313 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + URL_DECLERATION+url+ ", Exception: " + e.toString());
323 * @param fromAppId the from app id
324 * @param transId the trans id
325 * @param path the path
326 * @param payload the payload
330 public Response RestPost(String fromAppId, String transId, String path, String payload, boolean xml) {
331 String methodName = "RestPost";
333 transId = UUID.randomUUID().toString();
334 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + START_STRING);
338 String responseType = MediaType.APPLICATION_JSON;
340 responseType = "application/xml";
344 url = SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL_BASE) + path;
345 String vidUsername = SystemProperties.getProperty(AAIProperties.AAI_VID_USERNAME);
346 String vidPassword = Password.deobfuscate(SystemProperties.getProperty(AAIProperties.AAI_VID_PASSWD_X));
347 String encodeThis = vidUsername + ":" + vidPassword;
349 Logging.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload);
350 final Response cres = client.target(url)
352 .accept(responseType)
353 .header(TRANSACTION_ID_HEADER, transId)
354 .header(FROM_APP_ID_HEADER, fromAppId)
355 .header("Authorization", "Basic " + Base64.getEncoder().encodeToString(encodeThis.getBytes("utf-8")))
356 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
357 .post(Entity.entity(payload, MediaType.APPLICATION_JSON));
358 Logging.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, cres);
360 if (cres.getStatus() == 200 && cres.getStatus() <= 299) {
361 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + URL_DECLERATION);
362 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + URL_DECLERATION);
364 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " with status="+cres.getStatus()+URL_DECLERATION+url);
367 } catch (Exception e) {
368 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + URL_DECLERATION+url+ ", Exception: " + e.toString());