Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / scheduler / SchedulerRestInterface.java
1 package org.onap.vid.scheduler;
2
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;
20
21 import java.util.Base64;
22 import java.util.Collections;
23 import java.util.Map;
24 import java.util.function.Function;
25
26 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
27
28 @Service
29 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
30
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;
37
38     public SchedulerRestInterface() {
39         this.propertyGetter = SystemProperties::getProperty;
40     }
41
42     public SchedulerRestInterface(Function<String, String> propertyGetter) {
43         this.propertyGetter = propertyGetter;
44     }
45
46     public void initRestClient() {
47         logger.info("Starting to initialize rest client ");
48         String authStringEnc = calcEncodedAuthString();
49
50         commonHeaders = Maps.newHashMap();
51         commonHeaders.put("Authorization", "Basic " + authStringEnc);
52
53         syncRestClient = new SyncRestClient();
54
55         logger.info("\t<== Client Initialized \n");
56     }
57
58     public <T> RestObjectWithRequestInfo<T> Get(T t, String path, RestObject<T> restObject) {
59
60         String url = null;
61         String rawData = null;
62         Integer status = null;
63
64         try {
65             String methodName = "Get";
66             url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
67             initRestClient();
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())
72                     .build();
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);
78
79             if (status == 200) {
80                 t = response.getBody();
81                 restObject.set(t);
82                 logger.debug(EELFLoggerDelegate.debugLogger, "<== " + methodName + SUCCESSFUL_API_MESSAGE);
83                 logger.info(EELFLoggerDelegate.errorLogger, "<== " + methodName + SUCCESSFUL_API_MESSAGE);
84             } else {
85                 throw new GenericUncheckedException(new HttpException(String.format("%s with status=%d, url=%s", methodName, status, url)));
86             }
87             return new RestObjectWithRequestInfo<>(HttpMethod.GET, url, restObject, status, rawData);
88         }
89         catch (RuntimeException e) {
90             throw new ExceptionWithRequestInfo(HttpMethod.GET, url, rawData, status, e);
91         }
92     }
93
94     public <T> void Delete(T t, String sourceID, String path, RestObject<T> restObject) {
95         initRestClient();
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());
102
103         Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, response);
104
105         int status = response.getStatus();
106         restObject.setStatusCode(status);
107
108         t = response.getBody();
109         restObject.set(t);
110     }
111
112     public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException {
113         return clazz.newInstance();
114     }
115
116     private String calcEncodedAuthString() {
117         String retrievedUsername = propertyGetter.apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
118         final String username = retrievedUsername.isEmpty() ? "" : retrievedUsername;
119
120         String retrievedPassword = propertyGetter.apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
121         final String password = retrievedPassword.isEmpty() ? "" : getDeobfuscatedPassword(retrievedPassword);
122
123         return Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
124     }
125
126     private static String getDeobfuscatedPassword(String password) {
127         return password.contains("OBF:") ? Password.deobfuscate(password) : password;
128     }
129 }