Merge "Rename vid-simulator packages"
[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 org.apache.commons.codec.binary.Base64;
5 import org.eclipse.jetty.util.security.Password;
6 import org.onap.vid.aai.util.HttpClientMode;
7 import org.onap.vid.aai.util.HttpsAuthClient;
8 import org.onap.vid.client.HttpBasicClient;
9 import org.onap.vid.exceptions.GenericUncheckedException;
10 import org.onap.vid.utils.Logging;
11 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
12 import org.onap.portalsdk.core.util.SystemProperties;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.http.HttpMethod;
15 import org.springframework.stereotype.Service;
16
17 import javax.ws.rs.client.Client;
18 import javax.ws.rs.core.MultivaluedHashMap;
19 import javax.ws.rs.core.Response;
20 import java.util.Collections;
21 import java.util.function.Function;
22
23 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
24
25 @Service
26 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
27
28     private Client client = null;
29     private Function<String, String> propertyGetter;
30
31     @Autowired
32     HttpsAuthClient httpsAuthClient;
33
34     private MultivaluedHashMap<String, Object> commonHeaders;
35
36     /** The logger. */
37     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
38     final private static EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("scheduler");
39
40     @Autowired
41     public SchedulerRestInterface(){
42         this.propertyGetter = SystemProperties::getProperty;
43     }
44
45     public SchedulerRestInterface(Function<String, String> propertyGetter){
46         this.propertyGetter = propertyGetter;
47     }
48
49     public void initRestClient() {
50         logger.info("Starting to initialize rest client ");
51
52         final String username;
53         final String password;
54
55                 /*Setting user name based on properties*/
56         String retrievedUsername = propertyGetter.apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
57         if(retrievedUsername.isEmpty()) {
58             username = "";
59         } else {
60             username = retrievedUsername;
61         }
62
63                 /*Setting password based on properties*/
64         String retrievedPassword = propertyGetter.apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
65         if(retrievedPassword.isEmpty()) {
66             password = "";
67         } else {
68             if (retrievedPassword.contains("OBF:")) {
69                 password = Password.deobfuscate(retrievedPassword);
70             } else {
71                 password = retrievedPassword;
72             }
73         }
74
75         String authString = username + ":" + password;
76
77         byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
78         String authStringEnc = new String(authEncBytes);
79
80         commonHeaders = new MultivaluedHashMap<> ();
81         commonHeaders.put("Authorization", Collections.singletonList("Basic " + authStringEnc));
82
83         try {
84             if ( !username.isEmpty() ) {
85                 client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE);
86             }
87             else {
88
89                 client = HttpBasicClient.getClient();
90             }
91         } catch (Exception e) {
92             logger.error(" <== Unable to initialize rest client ", e);
93         }
94
95         logger.info("\t<== Client Initialized \n");
96     }
97
98     public <T> void Get (T t, String sourceId, String path, org.onap.vid.scheduler.RestObject<T> restObject ) {
99
100         String methodName = "Get";
101         String url = String.format("%s%s",propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
102         initRestClient();
103         Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
104         final Response cres = client.target(url)
105                 .request()
106                 .accept("application/json")
107                 .headers(commonHeaders)
108                 .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
109                 .get();
110         Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
111         int status = cres.getStatus();
112         restObject.setStatusCode (status);
113
114         if (status == 200) {
115             t = (T) cres.readEntity(t.getClass());
116             restObject.set(t);
117
118         } else {
119             throw new GenericUncheckedException(String.format("%s with status=%d, url=%s", methodName, status,url));
120         }
121
122     }
123
124     public <T> void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject<T> restObject) {
125
126         initRestClient();
127
128         String url = String.format( "%s%s",propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
129         Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url);
130         Response cres = client.target(url)
131                 .request()
132                 .accept("application/json")
133                 .headers(commonHeaders)
134                 .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
135                 .delete();
136         Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
137
138         int status = cres.getStatus();
139         restObject.setStatusCode(status);
140
141         t = (T) cres.readEntity(t.getClass());
142         restObject.set(t);
143
144     }
145
146     public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
147     {
148         return clazz.newInstance();
149     }
150
151 }