2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
\r
5 * in compliance with the License. You may obtain a copy of the License at
\r
7 * http://www.apache.org/licenses/LICENSE-2.0
\r
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
\r
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
\r
11 * or implied. See the License for the specific language governing permissions and limitations under
\r
15 package org.onap.ccsdk.config.rest.adaptor.service;
\r
17 import java.io.File;
\r
18 import java.io.FileInputStream;
\r
19 import java.util.Map;
\r
20 import java.util.Properties;
\r
21 import java.util.concurrent.ConcurrentHashMap;
\r
22 import java.util.concurrent.Executors;
\r
23 import java.util.concurrent.ScheduledExecutorService;
\r
24 import java.util.concurrent.TimeUnit;
\r
25 import java.util.stream.Collectors;
\r
26 import org.apache.commons.lang3.StringUtils;
\r
27 import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorConstants;
\r
28 import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;
\r
29 import org.onap.ccsdk.config.rest.adaptor.data.RestResponse;
\r
30 import com.att.eelf.configuration.EELFLogger;
\r
31 import com.att.eelf.configuration.EELFManager;
\r
33 public class ConfigRestAdaptorServiceImpl implements ConfigRestAdaptorService {
\r
35 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigRestAdaptorServiceImpl.class);
\r
36 private Map<String, String> restProperties = new ConcurrentHashMap<>();
\r
38 public ConfigRestAdaptorServiceImpl(String propertyPath) {
\r
39 initializeProperties(propertyPath);
\r
41 String envType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY
\r
42 + ConfigRestAdaptorConstants.REST_ADAPTOR_ENV_TYPE);
\r
44 if (!(ConfigRestAdaptorConstants.PROPERTY_ENV_PROD.equalsIgnoreCase(envType)
\r
45 || ConfigRestAdaptorConstants.PROPERTY_ENV_SOLO.equalsIgnoreCase(envType))) {
\r
46 ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
\r
47 Runnable task = () -> {
\r
48 initializeProperties(propertyPath);
\r
50 executor.scheduleWithFixedDelay(task, 60, 15, TimeUnit.MINUTES);
\r
52 } catch (Exception e) {
\r
53 logger.error(e.getMessage(), e);
\r
57 private void initializeProperties(String propertyPath) {
\r
58 logger.trace("Initialising Config rest adaptor Service with property directory ({})", propertyPath);
\r
60 if (StringUtils.isBlank(propertyPath)) {
\r
61 propertyPath = System.getProperty(ConfigRestAdaptorConstants.SDNC_ROOT_DIR_ENV_VAR_KEY);
\r
64 if (StringUtils.isBlank(propertyPath)) {
\r
65 throw new ConfigRestAdaptorException(
\r
66 String.format("Failed to get the property directory (%s)", propertyPath));
\r
69 // Loading Default config-rest-adaptor.properties
\r
70 String propertyFile =
\r
71 propertyPath + File.separator + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;
\r
73 Properties properties = new Properties();
\r
74 properties.load(new FileInputStream(propertyFile));
\r
76 logger.trace("Initializing properties details for property file ({}) properties ({})", propertyFile,
\r
78 restProperties.putAll(properties.entrySet().stream()
\r
79 .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));
\r
81 } catch (Exception e) {
\r
82 logger.error(e.getMessage(), e);
\r
87 public <T> T getResource(String serviceSelector, String path, Class<T> responseType)
\r
88 throws ConfigRestAdaptorException {
\r
89 return getRestClientAdapterBySelectorName(serviceSelector).getResource(path, responseType);
\r
93 public <T> T postResource(String serviceSelector, String path, Object request, Class<T> responseType)
\r
94 throws ConfigRestAdaptorException {
\r
95 return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request, responseType);
\r
99 public <T> T exchangeResource(String serviceSelector, String path, Object request, Class<T> responseType,
\r
100 String method) throws ConfigRestAdaptorException {
\r
101 return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, responseType,
\r
106 public RestResponse getResource(String serviceSelector, String path) throws ConfigRestAdaptorException {
\r
107 return getRestClientAdapterBySelectorName(serviceSelector).getResource(path);
\r
111 public RestResponse postResource(String serviceSelector, String path, Object request)
\r
112 throws ConfigRestAdaptorException {
\r
113 return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request);
\r
117 public RestResponse exchangeResource(String serviceSelector, String path, Object request, String method)
\r
118 throws ConfigRestAdaptorException {
\r
119 return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, method);
\r
122 private ConfigRestClientServiceAdapter getRestClientAdapterBySelectorName(String serviceSelector)
\r
123 throws ConfigRestAdaptorException {
\r
124 String adoptorType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
\r
125 + ConfigRestAdaptorConstants.SERVICE_TYPE_PROPERTY);
\r
126 if (StringUtils.isNotBlank(adoptorType)) {
\r
127 if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_GENERIC.equalsIgnoreCase(adoptorType)) {
\r
128 return new GenericRestClientAdapterImpl(restProperties, serviceSelector);
\r
129 } else if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_SSL.equalsIgnoreCase(adoptorType)) {
\r
130 return new SSLRestClientAdapterImpl(restProperties, serviceSelector);
\r
132 throw new ConfigRestAdaptorException(
\r
133 String.format("no implementation for rest adoptor type (%s) for the selector (%s).",
\r
134 adoptorType, serviceSelector));
\r
137 throw new ConfigRestAdaptorException(
\r
138 String.format("couldn't get rest adoptor type for the selector (%s)", serviceSelector));
\r