2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.scheduler;
23 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
24 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.google.common.collect.ImmutableMap;
28 import com.google.common.collect.Maps;
29 import io.joshworks.restclient.http.HttpResponse;
30 import java.util.Base64;
31 import java.util.Collections;
33 import java.util.function.Function;
34 import org.apache.http.HttpException;
35 import org.eclipse.jetty.util.security.Password;
36 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
37 import org.onap.portalsdk.core.util.SystemProperties;
38 import org.onap.vid.aai.ExceptionWithRequestInfo;
39 import org.onap.vid.client.SyncRestClient;
40 import org.onap.vid.client.SyncRestClientInterface;
41 import org.onap.vid.exceptions.GenericUncheckedException;
42 import org.onap.vid.mso.RestObject;
43 import org.onap.vid.mso.RestObjectWithRequestInfo;
44 import org.onap.vid.utils.Logging;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.http.HttpMethod;
47 import org.springframework.stereotype.Service;
50 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
52 private static final EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("scheduler");
53 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
54 private static final String SUCCESSFUL_API_MESSAGE=" REST api GET was successful!";
55 private SyncRestClientInterface syncRestClient;
56 private Function<String, String> propertyGetter;
57 private Map<String, String> commonHeaders;
59 private Logging loggingService;
61 public SchedulerRestInterface(Function<String, String> propertyGetter, Logging loggingService) {
62 this.loggingService = loggingService;
63 this.propertyGetter = propertyGetter;
67 public SchedulerRestInterface(Logging loggingService) {
68 this.loggingService = loggingService;
69 this.propertyGetter = SystemProperties::getProperty;
72 public void initRestClient() {
73 logger.info("Starting to initialize rest client ");
74 String authStringEnc = calcEncodedAuthString();
76 commonHeaders = Maps.newHashMap();
77 commonHeaders.put("Authorization", "Basic " + authStringEnc);
79 syncRestClient = new SyncRestClient(loggingService);
81 logger.info("\t<== Client Initialized \n");
84 public <T> RestObjectWithRequestInfo<T> Get(T t, String path, RestObject<T> restObject) {
87 String rawData = null;
88 Integer status = null;
91 String methodName = "Get";
92 url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
94 loggingService.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
95 Map<String, String> requestHeaders = ImmutableMap.<String, String>builder()
96 .putAll(commonHeaders)
97 .put(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
99 final HttpResponse<String> response = syncRestClient.get(url, requestHeaders,
100 Collections.emptyMap(), String.class);
101 loggingService.logRequest(outgoingRequestsLogger, HttpMethod.GET, url, response);
102 status = response.getStatus();
103 restObject.setStatusCode(status);
104 rawData = response.getBody();
105 restObject.setRaw(rawData);
107 if (t instanceof String) {
108 restObject.set((T)rawData);
111 restObject.set(JACKSON_OBJECT_MAPPER.readValue(rawData, (Class<T>)t.getClass()));
113 logger.debug(EELFLoggerDelegate.debugLogger, "<== " + methodName + SUCCESSFUL_API_MESSAGE);
114 logger.info(EELFLoggerDelegate.errorLogger, "<== " + methodName + SUCCESSFUL_API_MESSAGE);
116 throw new GenericUncheckedException(new HttpException(String.format("%s with status=%d, url=%s", methodName, status, url)));
118 return new RestObjectWithRequestInfo<>(HttpMethod.GET, url, restObject, status, rawData);
120 catch (Exception e) {
121 throw new ExceptionWithRequestInfo(HttpMethod.GET, url, rawData, status, e);
125 public <T> void Delete(T t, String sourceID, String path, RestObject<T> restObject) {
127 String url = String.format("%s%s", propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
128 loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url);
129 Map<String, String> requestHeaders = ImmutableMap.<String, String>builder()
130 .putAll(commonHeaders)
131 .put(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()).build();
132 final HttpResponse<T> response = (HttpResponse<T>) syncRestClient.delete(url, requestHeaders, t.getClass());
134 loggingService.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url, response);
136 int status = response.getStatus();
137 restObject.setStatusCode(status);
139 t = response.getBody();
143 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException {
144 return clazz.newInstance();
147 private String calcEncodedAuthString() {
148 String retrievedUsername = propertyGetter.apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
149 final String username = retrievedUsername.isEmpty() ? "" : retrievedUsername;
151 String retrievedPassword = propertyGetter.apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
152 final String password = retrievedPassword.isEmpty() ? "" : getDeobfuscatedPassword(retrievedPassword);
154 return Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
157 private static String getDeobfuscatedPassword(String password) {
158 return password.contains("OBF:") ? Password.deobfuscate(password) : password;