aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java
blob: f5625ce211282408234c56cc22a5c1c6d45cf6cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2018 Nokia. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.vid.aai.util;


import com.att.eelf.configuration.EELFLogger;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.vid.aai.ExceptionWithRequestInfo;
import org.onap.vid.aai.ResponseWithRequestInfo;
import org.onap.vid.aai.exceptions.InvalidPropertyException;
import org.onap.vid.utils.Logging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Optional;
import java.util.UUID;

import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;


/**
 * The Class AAIRestInterface.
 */
public class AAIRestInterface {

	/** The logger. */
	protected EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AAIRestInterface.class);

	protected final EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("aai");


	/** The client. */
	private Client client = null;

	/** The rest srvr base URL. */
	private String restSrvrBaseURL;

	@Autowired
	protected HttpsAuthClient httpsAuthClientFactory;
	private final ServletRequestHelper servletRequestHelper;
	private final SystemPropertyHelper systemPropertyHelper;

	protected static final String START_STRING = " start";
	protected static final String TRANSACTION_ID_HEADER = "X-TransactionId";
	protected static final String FROM_APP_ID_HEADER = "X-FromAppId";
	protected static final String SUCCESSFUL_API_MESSAGE = " REST api call was successful!";
	protected static final String URL_DECLARATION = ", url=";

	public AAIRestInterface(HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) {
		this.httpsAuthClientFactory = httpsAuthClientFactory;
		this.servletRequestHelper = servletRequestHelper;
		this.systemPropertyHelper = systemPropertyHelper;
		initRestClient();
	}

	/**
	 * For testing purpose
	 */
	AAIRestInterface(Optional<Client> client,
					 HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper){
		this.httpsAuthClientFactory = httpsAuthClientFactory;
		this.servletRequestHelper = servletRequestHelper;
		this.systemPropertyHelper = systemPropertyHelper;
		if (client != null && client.isPresent()){
			this.client = client.get();
		}

	}

	/**
	 * Encode URL.
	 *
	 * @param nodeKey the node key
	 * @return the string
	 * @throws UnsupportedEncodingException the unsupported encoding exception
	 */
	public String encodeURL (String nodeKey) throws UnsupportedEncodingException {
		return URLEncoder.encode(nodeKey, "UTF-8").replaceAll("\\+", "%20");
	}

    private void initRestClient() {
        initRestClient(false);
    }


	private void initRestClient(boolean propagateExceptions) {
		if (client == null) {
			try {
				client = httpsAuthClientFactory.getClient(HttpClientMode.WITH_KEYSTORE);
			} catch (Exception e) {
				logger.info(EELFLoggerDelegate.errorLogger, "Exception in REST call to DB in initRestClient" + e.toString());
				logger.debug(EELFLoggerDelegate.debugLogger, "Exception in REST call to DB : " + e.toString());
				if (propagateExceptions) {
					ExceptionUtils.rethrow(e);
				}
			}
		}
	}



	/**
	 * Sets the rest srvr base URL.
	 *
	 * @param baseURL the base URL
	 */
	public void SetRestSrvrBaseURL(String baseURL)
	{
		if (baseURL == null) {
			logger.info(EELFLoggerDelegate.errorLogger, "REST Server base URL cannot be null.");
			logger.debug(EELFLoggerDelegate.debugLogger, "REST Server base URL cannot be null.");
		}

		restSrvrBaseURL = baseURL;
	}

	/**
	 * Gets the rest srvr base URL.
	 *
	 * @return the rest srvr base URL
	 */
	public String getRestSrvrBaseURL() {
		return restSrvrBaseURL;
	}


	/**
	 * Rest get.
	 *
	 * @param fromAppId the from app id
	 * @param transId the trans id
	 * @param requestUri the request uri
	 * @param xml the xml
	 * @return the string
	 */
	public ResponseWithRequestInfo RestGet(String fromAppId, String transId, String requestUri, boolean xml) {
		return RestGet(fromAppId, transId, requestUri, xml, false);
	}

	public ResponseWithRequestInfo RestGet(String fromAppId, String transId, String requestUri, boolean xml, boolean propagateExceptions) {
        String methodName = "RestGet";
		String url = systemPropertyHelper.getFullServicePath(requestUri);
		try {
			initRestClient(propagateExceptions);

			logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_STRING);
			logger.debug(EELFLoggerDelegate.debugLogger, url + " for the get REST API");

			Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);

			final Response response;
            Invocation.Builder requestBuilder = client.target(url)
                    .request()
                    .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON)
                    .header(TRANSACTION_ID_HEADER, transId)
                    .header(FROM_APP_ID_HEADER, fromAppId)
                    .header("Content-Type", MediaType.APPLICATION_JSON)
                    .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId());
            response = systemPropertyHelper.isClientCertEnabled() ?
                    requestBuilder.get() : authenticateRequest(requestBuilder).get();
			Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response);

			if (response.getStatusInfo().equals(Response.Status.OK)) {
				logger.debug(EELFLoggerDelegate.debugLogger, methodName + SUCCESSFUL_API_MESSAGE);
				logger.info(EELFLoggerDelegate.errorLogger, methodName + SUCCESSFUL_API_MESSAGE);
			} else {
				logger.debug(EELFLoggerDelegate.debugLogger, getInvalidResponseLogMessage(url, methodName, response));
			}
			return new ResponseWithRequestInfo(response, url, HttpMethod.GET);
		} catch (Exception e) {
			logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e));
			if (propagateExceptions) {
                throw new ExceptionWithRequestInfo(HttpMethod.GET, defaultIfNull(url, requestUri), e);
            } else {
                return new ResponseWithRequestInfo(null, url, HttpMethod.GET);
            }
		}
	}

	protected String extractOrGenerateRequestId() {
		return servletRequestHelper.extractOrGenerateRequestId();
	}


	/**
	 * Delete.
	 *
	 * @param sourceID the source ID
	 * @param transId the trans id
	 * @param path the path
	 * @return true, if successful
	 */
	public boolean Delete(String sourceID, String transId, String path) {
		String methodName = "Delete";
		transId += ":" + UUID.randomUUID().toString();
		logger.debug(methodName + START_STRING);
		Boolean response = false;
		String url = systemPropertyHelper.getFullServicePath(path);;
		try {

			initRestClient();
			Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url);
			final Response cres = client.target(url)
					.request()
					.accept(MediaType.APPLICATION_JSON)
					.header(TRANSACTION_ID_HEADER, transId)
					.header(FROM_APP_ID_HEADER, sourceID)
					.header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId())
					.delete();
			Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
			if (cres.getStatusInfo().equals(Response.Status.NOT_FOUND)) {
				logger.debug(EELFLoggerDelegate.debugLogger, "Resource does not exist...: " + cres.getStatus()
						+ ":" + cres.readEntity(String.class));
				response = false;
			} else if (cres.getStatusInfo().equals(Response.Status.OK) || cres.getStatusInfo().equals(Response.Status.NO_CONTENT)) {
				logger.debug(EELFLoggerDelegate.debugLogger, "Resource " + url + " deleted");
				logger.info(EELFLoggerDelegate.errorLogger, "Resource " + url + " deleted");
				response = true;
			} else {
				logger.debug(EELFLoggerDelegate.debugLogger, "Deleting Resource failed: " + cres.getStatus()
						+ ":" + cres.readEntity(String.class));
				response = false;
			}

		} catch (Exception e) {
			logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e));
		}
		return response;
	}


	/**
	 * Rest put.
	 *
	 * @param fromAppId the from app id
	 * @param path the path
	 * @param payload the payload
	 * @param xml the xml
	 * @return the string
	 */
	public Response RestPut(String fromAppId, String path, String payload, boolean xml) {
		String methodName = "RestPut";
		String url=systemPropertyHelper.getFullServicePath(path);
		String transId = UUID.randomUUID().toString();
		logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_STRING);

        Response response = null;
		try {
			initRestClient();
			Logging.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, payload);
			response = authenticateRequest(client.target(url)
					.request()
                    .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON)
					.header(TRANSACTION_ID_HEADER, transId)
					.header(FROM_APP_ID_HEADER,  fromAppId))
					.header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId())
					.put(Entity.entity(payload, MediaType.APPLICATION_JSON));
			Logging.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, response);

			if (response.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) {
				logger.info(EELFLoggerDelegate.errorLogger, getValidResponseLogMessage(methodName));
				logger.debug(EELFLoggerDelegate.debugLogger, getValidResponseLogMessage(methodName));
			} else {
				logger.debug(EELFLoggerDelegate.debugLogger, getInvalidResponseLogMessage(url, methodName, response));
			}
		} catch (Exception e) {
			logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e));
		}
		return response;
	}



	/**
	 * Rest post.
	 *
	 * @param fromAppId the from app id
	 * @param path the path
	 * @param payload the payload
	 * @param xml the xml
	 * @return the string
	 */
	public Response RestPost(String fromAppId, String path, String payload, boolean xml) {
		String methodName = "RestPost";
		String url=systemPropertyHelper.getServiceBasePath(path);
		String transId = UUID.randomUUID().toString();
		logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_STRING);

        Response response = null;
		try {
			initRestClient();
			Logging.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload);
			response = authenticateRequest(client.target(systemPropertyHelper.getServiceBasePath(path))
					.request()
                    .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON)
					.header(TRANSACTION_ID_HEADER, transId)
					.header(FROM_APP_ID_HEADER,  fromAppId))
					.header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId())
					.post(Entity.entity(payload, MediaType.APPLICATION_JSON));
			Logging.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, response);

			if (response.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) {
				logger.info(EELFLoggerDelegate.errorLogger, getValidResponseLogMessage(methodName));
				logger.debug(EELFLoggerDelegate.debugLogger, getValidResponseLogMessage(methodName));
			} else {
				logger.debug(EELFLoggerDelegate.debugLogger, getInvalidResponseLogMessage(url, methodName, response));
			}
		} catch (Exception e) {
			logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e));
		}
		return response;
	}

	protected String getFailedResponseLogMessage(String path, String methodName, Exception e) {
		return methodName + URL_DECLARATION + path + ", Exception: " + e.toString();
	}

	protected String getValidResponseLogMessage(String methodName) {
		return methodName + URL_DECLARATION;
	}

	protected String getInvalidResponseLogMessage(String path, String methodName, Response cres) {
		return methodName + " with status=" + cres.getStatus() + URL_DECLARATION + path;
	}

	private Invocation.Builder authenticateRequest(Invocation.Builder requestBuilder) throws InvalidPropertyException, UnsupportedEncodingException {
		return requestBuilder
				.header("Authorization", "Basic " + systemPropertyHelper.getEncodedCredentials());
	}

}