blob: c764108551d28c732ba52f8454188daca583acdc [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21
22
23package org.onap.osam.scheduler;
24
25import com.att.eelf.configuration.EELFLogger;
26import com.google.common.collect.ImmutableMap;
27import com.google.common.collect.Maps;
28import io.joshworks.restclient.http.HttpResponse;
29import org.eclipse.jetty.util.security.Password;
30import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
31import org.onap.portalsdk.core.util.SystemProperties;
32import org.onap.osam.client.SyncRestClient;
33import org.onap.osam.client.SyncRestClientInterface;
34import org.onap.osam.exceptions.GenericUncheckedException;
35import org.onap.osam.utils.Logging;
36import org.springframework.http.HttpMethod;
37import org.springframework.stereotype.Service;
38
39import java.util.Base64;
40import java.util.Collections;
41import java.util.Map;
42import java.util.function.Function;
43
44import static org.onap.osam.utils.Logging.REQUEST_ID_HEADER_KEY;
45
46@Service
47public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
48
49 final private static EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("scheduler");
50 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
51 private SyncRestClientInterface syncRestClient;
52 private Function<String, String> propertyGetter;
53 private Map<String, String> commonHeaders;
54
55 public SchedulerRestInterface() {
56 this.propertyGetter = SystemProperties::getProperty;
57 }
58
59 public SchedulerRestInterface(Function<String, String> propertyGetter) {
60 this.propertyGetter = propertyGetter;
61 }
62
63 public void initRestClient() {
64 logger.info("Starting to initialize rest client ");
65 String authStringEnc = calcEncodedAuthString();
66
67 commonHeaders = Maps.newHashMap();
68 commonHeaders.put("Authorization", "Basic " + authStringEnc);
69
70 syncRestClient = new SyncRestClient();
71
72 logger.info("\t<== Client Initialized \n");
73 }
74
75 public <T> void Get(T t, String sourceId, String path, org.onap.osam.scheduler.RestObject<T> restObject) {
76 initRestClient();
77 String methodName = "Get";
78 String url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
79 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, 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.get(url, requestHeaders,
84 Collections.emptyMap(), t.getClass()));
85 Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response);
86 int status = response.getStatus();
87 restObject.setStatusCode(status);
88
89 if (status == 200) {
90 t = response.getBody();
91 restObject.set(t);
92
93 } else {
94 throw new GenericUncheckedException(String.format("%s with status=%d, url=%s", methodName, status, url));
95 }
96 }
97
98 public <T> void Delete(T t, String sourceID, String path, org.onap.osam.scheduler.RestObject<T> restObject) {
99 initRestClient();
100 String url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
101 Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url);
102 Map<String, String> requestHeaders = ImmutableMap.<String, String>builder()
103 .putAll(commonHeaders)
104 .put(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()).build();
105 final HttpResponse<T> response = (HttpResponse<T>) syncRestClient.delete(url, requestHeaders, t.getClass());
106
107 Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, response);
108
109 int status = response.getStatus();
110 restObject.setStatusCode(status);
111
112 t = response.getBody();
113 restObject.set(t);
114 }
115
116 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException {
117 return clazz.newInstance();
118 }
119
120 private String calcEncodedAuthString() {
121 String retrievedUsername = propertyGetter.apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
122 final String username = retrievedUsername.isEmpty() ? "" : retrievedUsername;
123
124 String retrievedPassword = propertyGetter.apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
125 final String password = retrievedPassword.isEmpty() ? "" : getDeobfuscatedPassword(retrievedPassword);
126
127 return Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
128 }
129
130 private static String getDeobfuscatedPassword(String password) {
131 return password.contains("OBF:") ? Password.deobfuscate(password) : password;
132 }
133}