1 package org.onap.vid.scheduler;
3 import com.att.eelf.configuration.EELFLogger;
4 import com.google.common.collect.ImmutableMap;
5 import com.google.common.collect.Maps;
6 import io.joshworks.restclient.http.HttpResponse;
7 import org.apache.http.HttpException;
8 import org.eclipse.jetty.util.security.Password;
9 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
10 import org.onap.portalsdk.core.util.SystemProperties;
11 import org.onap.vid.aai.ExceptionWithRequestInfo;
12 import org.onap.vid.client.SyncRestClient;
13 import org.onap.vid.client.SyncRestClientInterface;
14 import org.onap.vid.exceptions.GenericUncheckedException;
15 import org.onap.vid.mso.RestObject;
16 import org.onap.vid.mso.RestObjectWithRequestInfo;
17 import org.onap.vid.utils.Logging;
18 import org.springframework.http.HttpMethod;
19 import org.springframework.stereotype.Service;
21 import java.util.Base64;
22 import java.util.Collections;
24 import java.util.function.Function;
26 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
29 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
31 private static final EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("scheduler");
32 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
33 private static final String SUCCESSFUL_API_MESSAGE=" REST api GET was successful!";
34 private SyncRestClientInterface syncRestClient;
35 private Function<String, String> propertyGetter;
36 private Map<String, String> commonHeaders;
38 public SchedulerRestInterface() {
39 this.propertyGetter = SystemProperties::getProperty;
42 public SchedulerRestInterface(Function<String, String> propertyGetter) {
43 this.propertyGetter = propertyGetter;
46 public void initRestClient() {
47 logger.info("Starting to initialize rest client ");
48 String authStringEnc = calcEncodedAuthString();
50 commonHeaders = Maps.newHashMap();
51 commonHeaders.put("Authorization", "Basic " + authStringEnc);
53 syncRestClient = new SyncRestClient();
55 logger.info("\t<== Client Initialized \n");
58 public <T> RestObjectWithRequestInfo<T> Get(T t, String path, RestObject<T> restObject) {
61 String rawData = null;
62 Integer status = null;
65 String methodName = "Get";
66 url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
68 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
69 Map<String, String> requestHeaders = ImmutableMap.<String, String>builder()
70 .putAll(commonHeaders)
71 .put(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
73 final HttpResponse<T> response = ((HttpResponse<T>) syncRestClient.get(url, requestHeaders,
74 Collections.emptyMap(), t.getClass()));
75 Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response);
76 status = response.getStatus();
77 restObject.setStatusCode(status);
80 t = response.getBody();
82 logger.debug(EELFLoggerDelegate.debugLogger, "<== " + methodName + SUCCESSFUL_API_MESSAGE);
83 logger.info(EELFLoggerDelegate.errorLogger, "<== " + methodName + SUCCESSFUL_API_MESSAGE);
85 throw new GenericUncheckedException(new HttpException(String.format("%s with status=%d, url=%s", methodName, status, url)));
87 return new RestObjectWithRequestInfo<>(HttpMethod.GET, url, restObject, status, rawData);
89 catch (RuntimeException e) {
90 throw new ExceptionWithRequestInfo(HttpMethod.GET, url, rawData, status, e);
94 public <T> void Delete(T t, String sourceID, String path, RestObject<T> restObject) {
96 String url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
97 Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url);
98 Map<String, String> requestHeaders = ImmutableMap.<String, String>builder()
99 .putAll(commonHeaders)
100 .put(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()).build();
101 final HttpResponse<T> response = (HttpResponse<T>) syncRestClient.delete(url, requestHeaders, t.getClass());
103 Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, response);
105 int status = response.getStatus();
106 restObject.setStatusCode(status);
108 t = response.getBody();
112 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException {
113 return clazz.newInstance();
116 private String calcEncodedAuthString() {
117 String retrievedUsername = propertyGetter.apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
118 final String username = retrievedUsername.isEmpty() ? "" : retrievedUsername;
120 String retrievedPassword = propertyGetter.apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
121 final String password = retrievedPassword.isEmpty() ? "" : getDeobfuscatedPassword(retrievedPassword);
123 return Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
126 private static String getDeobfuscatedPassword(String password) {
127 return password.contains("OBF:") ? Password.deobfuscate(password) : password;