2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
18 package org.onap.ccsdk.config.rest.adaptor.service;
\r
20 import java.io.File;
\r
21 import java.io.FileInputStream;
\r
22 import java.util.Map;
\r
23 import java.util.Properties;
\r
24 import java.util.concurrent.ConcurrentHashMap;
\r
25 import java.util.concurrent.Executors;
\r
26 import java.util.concurrent.ScheduledExecutorService;
\r
27 import java.util.concurrent.TimeUnit;
\r
28 import java.util.stream.Collectors;
\r
29 import org.apache.commons.lang3.StringUtils;
\r
30 import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorConstants;
\r
31 import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;
\r
32 import org.onap.ccsdk.config.rest.adaptor.data.RestResponse;
\r
33 import com.att.eelf.configuration.EELFLogger;
\r
34 import com.att.eelf.configuration.EELFManager;
\r
36 public class ConfigRestAdaptorServiceImpl implements ConfigRestAdaptorService {
\r
38 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigRestAdaptorServiceImpl.class);
\r
39 private Map<String, String> restProperties = new ConcurrentHashMap<>();
\r
41 public ConfigRestAdaptorServiceImpl(String propertyPath) {
\r
42 initializeProperties(propertyPath);
\r
44 String envType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY
\r
45 + ConfigRestAdaptorConstants.REST_ADAPTOR_ENV_TYPE);
\r
47 if (!(ConfigRestAdaptorConstants.PROPERTY_ENV_PROD.equalsIgnoreCase(envType)
\r
48 || ConfigRestAdaptorConstants.PROPERTY_ENV_SOLO.equalsIgnoreCase(envType))) {
\r
49 ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
\r
50 Runnable task = () -> {
\r
51 initializeProperties(propertyPath);
\r
53 executor.scheduleWithFixedDelay(task, 60, 15, TimeUnit.MINUTES);
\r
55 } catch (Exception e) {
\r
56 logger.error(e.getMessage(), e);
\r
60 private void initializeProperties(String propertyPath) {
\r
61 logger.trace("Initialising Config rest adaptor Service with property directory ({})", propertyPath);
\r
63 if (StringUtils.isBlank(propertyPath)) {
\r
64 propertyPath = System.getProperty(ConfigRestAdaptorConstants.SDNC_ROOT_DIR_ENV_VAR_KEY);
\r
67 if (StringUtils.isBlank(propertyPath)) {
\r
68 throw new ConfigRestAdaptorException(
\r
69 String.format("Failed to get the property directory (%s)", propertyPath));
\r
72 // Loading Default config-rest-adaptor.properties
\r
73 String propertyFile =
\r
74 propertyPath + File.separator + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;
\r
76 Properties properties = new Properties();
\r
77 properties.load(new FileInputStream(propertyFile));
\r
79 logger.trace("Initializing properties details for property file ({}) properties ({})", propertyFile,
\r
81 restProperties.putAll(properties.entrySet().stream()
\r
82 .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));
\r
84 } catch (Exception e) {
\r
85 logger.error(e.getMessage(), e);
\r
90 public <T> T getResource(String serviceSelector, String path, Class<T> responseType)
\r
91 throws ConfigRestAdaptorException {
\r
92 return getRestClientAdapterBySelectorName(serviceSelector).getResource(path, responseType);
\r
96 public <T> T postResource(String serviceSelector, String path, Object request, Class<T> responseType)
\r
97 throws ConfigRestAdaptorException {
\r
98 return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request, responseType);
\r
102 public <T> T exchangeResource(String serviceSelector, String path, Object request, Class<T> responseType,
\r
103 String method) throws ConfigRestAdaptorException {
\r
104 return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, responseType,
\r
109 public RestResponse getResource(String serviceSelector, String path) throws ConfigRestAdaptorException {
\r
110 return getRestClientAdapterBySelectorName(serviceSelector).getResource(path);
\r
114 public RestResponse postResource(String serviceSelector, String path, Object request)
\r
115 throws ConfigRestAdaptorException {
\r
116 return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request);
\r
120 public RestResponse exchangeResource(String serviceSelector, String path, Object request, String method)
\r
121 throws ConfigRestAdaptorException {
\r
122 return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, method);
\r
125 private ConfigRestClientServiceAdapter getRestClientAdapterBySelectorName(String serviceSelector)
\r
126 throws ConfigRestAdaptorException {
\r
127 String adoptorType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
\r
128 + ConfigRestAdaptorConstants.SERVICE_TYPE_PROPERTY);
\r
129 if (StringUtils.isNotBlank(adoptorType)) {
\r
130 if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_GENERIC.equalsIgnoreCase(adoptorType)) {
\r
131 return new GenericRestClientAdapterImpl(restProperties, serviceSelector);
\r
132 } else if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_SSL.equalsIgnoreCase(adoptorType)) {
\r
133 return new SSLRestClientAdapterImpl(restProperties, serviceSelector);
\r
135 throw new ConfigRestAdaptorException(
\r
136 String.format("no implementation for rest adoptor type (%s) for the selector (%s).",
\r
137 adoptorType, serviceSelector));
\r
140 throw new ConfigRestAdaptorException(
\r
141 String.format("couldn't get rest adoptor type for the selector (%s)", serviceSelector));
\r