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.eclipse.jetty.util.security.Password;
8 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
9 import org.onap.portalsdk.core.util.SystemProperties;
10 import org.onap.vid.client.SyncRestClient;
11 import org.onap.vid.client.SyncRestClientInterface;
12 import org.onap.vid.exceptions.GenericUncheckedException;
13 import org.onap.vid.utils.Logging;
14 import org.springframework.http.HttpMethod;
15 import org.springframework.stereotype.Service;
17 import java.util.Base64;
18 import java.util.Collections;
20 import java.util.function.Function;
22 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
25 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
27 final private static EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("scheduler");
28 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
29 private SyncRestClientInterface syncRestClient;
30 private Function<String, String> propertyGetter;
31 private Map<String, String> commonHeaders;
33 public SchedulerRestInterface() {
34 this.propertyGetter = SystemProperties::getProperty;
37 public SchedulerRestInterface(Function<String, String> propertyGetter) {
38 this.propertyGetter = propertyGetter;
41 public void initRestClient() {
42 logger.info("Starting to initialize rest client ");
43 String authStringEnc = calcEncodedAuthString();
45 commonHeaders = Maps.newHashMap();
46 commonHeaders.put("Authorization", "Basic " + authStringEnc);
48 syncRestClient = new SyncRestClient();
50 logger.info("\t<== Client Initialized \n");
53 public <T> void Get(T t, String sourceId, String path, org.onap.vid.scheduler.RestObject<T> restObject) {
55 String methodName = "Get";
56 String url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
57 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
58 Map<String, String> requestHeaders = ImmutableMap.<String, String>builder()
59 .putAll(commonHeaders)
60 .put(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()).build();
61 final HttpResponse<T> response = ((HttpResponse<T>) syncRestClient.get(url, requestHeaders,
62 Collections.emptyMap(), t.getClass()));
63 Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response);
64 int status = response.getStatus();
65 restObject.setStatusCode(status);
68 t = response.getBody();
72 throw new GenericUncheckedException(String.format("%s with status=%d, url=%s", methodName, status, url));
76 public <T> void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject<T> restObject) {
78 String url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
79 Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url);
80 Map<String, String> requestHeaders = ImmutableMap.<String, String>builder()
81 .putAll(commonHeaders)
82 .put(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()).build();
83 final HttpResponse<T> response = (HttpResponse<T>) syncRestClient.delete(url, requestHeaders, t.getClass());
85 Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, response);
87 int status = response.getStatus();
88 restObject.setStatusCode(status);
90 t = response.getBody();
94 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException {
95 return clazz.newInstance();
98 private String calcEncodedAuthString() {
99 String retrievedUsername = propertyGetter.apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
100 final String username = retrievedUsername.isEmpty() ? "" : retrievedUsername;
102 String retrievedPassword = propertyGetter.apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
103 final String password = retrievedPassword.isEmpty() ? "" : getDeobfuscatedPassword(retrievedPassword);
105 return Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
108 private static String getDeobfuscatedPassword(String password) {
109 return password.contains("OBF:") ? Password.deobfuscate(password) : password;