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.features.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.features.rest.adaptor.ConfigRestAdaptorConstants;
\r
31 import org.onap.ccsdk.features.rest.adaptor.ConfigRestAdaptorException;
\r
32 import org.onap.ccsdk.features.rest.adaptor.data.RestResponse;
\r
34 import com.att.eelf.configuration.EELFLogger;
\r
35 import com.att.eelf.configuration.EELFManager;
\r
37 public class ConfigRestAdaptorServiceImpl implements ConfigRestAdaptorService {
\r
39 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigRestAdaptorServiceImpl.class);
\r
40 private Map<String, String> restProperties = new ConcurrentHashMap<>();
\r
42 public ConfigRestAdaptorServiceImpl(String propertyPath) {
\r
43 initializeProperties(propertyPath);
\r
45 String envType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY
\r
46 + ConfigRestAdaptorConstants.REST_ADAPTOR_ENV_TYPE);
\r
48 if (!(ConfigRestAdaptorConstants.PROPERTY_ENV_PROD.equalsIgnoreCase(envType)
\r
49 || ConfigRestAdaptorConstants.PROPERTY_ENV_SOLO.equalsIgnoreCase(envType))) {
\r
50 ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
\r
51 Runnable task = () -> {
\r
52 initializeProperties(propertyPath);
\r
54 executor.scheduleWithFixedDelay(task, 60, 15, TimeUnit.MINUTES);
\r
56 } catch (Exception e) {
\r
57 logger.error(e.getMessage(), e);
\r
61 private void initializeProperties(String propertyPath) {
\r
62 logger.trace("Initialising Config rest adaptor Service with property directory ({})", propertyPath);
\r
64 if (StringUtils.isBlank(propertyPath)) {
\r
65 propertyPath = System.getProperty(ConfigRestAdaptorConstants.SDNC_ROOT_DIR_ENV_VAR_KEY);
\r
68 if (StringUtils.isBlank(propertyPath)) {
\r
69 throw new ConfigRestAdaptorException(
\r
70 String.format("Failed to get the property directory (%s)", propertyPath));
\r
73 // Loading Default config-rest-adaptor.properties
\r
74 String propertyFile =
\r
75 propertyPath + File.separator + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;
\r
77 Properties properties = new Properties();
\r
78 properties.load(new FileInputStream(propertyFile));
\r
80 logger.trace("Initializing properties details for property file ({}) properties ({})", propertyFile,
\r
82 restProperties.putAll(properties.entrySet().stream()
\r
83 .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));
\r
85 } catch (Exception e) {
\r
86 logger.error(e.getMessage(), e);
\r
91 public <T> T getResource(String serviceSelector, String path, Class<T> responseType)
\r
92 throws ConfigRestAdaptorException {
\r
93 return getRestClientAdapterBySelectorName(serviceSelector).getResource(path, responseType);
\r
97 public <T> T postResource(String serviceSelector, String path, Object request, Class<T> responseType)
\r
98 throws ConfigRestAdaptorException {
\r
99 return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request, responseType);
\r
103 public <T> T exchangeResource(String serviceSelector, String path, Object request, Class<T> responseType,
\r
104 String method) throws ConfigRestAdaptorException {
\r
105 return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, responseType,
\r
110 public RestResponse getResource(String serviceSelector, String path) throws ConfigRestAdaptorException {
\r
111 return getRestClientAdapterBySelectorName(serviceSelector).getResource(path);
\r
115 public RestResponse postResource(String serviceSelector, String path, Object request)
\r
116 throws ConfigRestAdaptorException {
\r
117 return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request);
\r
121 public RestResponse exchangeResource(String serviceSelector, String path, Object request, String method)
\r
122 throws ConfigRestAdaptorException {
\r
123 return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, method);
\r
126 private ConfigRestClientServiceAdapter getRestClientAdapterBySelectorName(String serviceSelector)
\r
127 throws ConfigRestAdaptorException {
\r
128 String adoptorType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
\r
129 + ConfigRestAdaptorConstants.SERVICE_TYPE_PROPERTY);
\r
130 if (StringUtils.isNotBlank(adoptorType)) {
\r
131 if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_GENERIC.equalsIgnoreCase(adoptorType)) {
\r
132 return new GenericRestClientAdapterImpl(restProperties, serviceSelector);
\r
133 } else if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_SSL.equalsIgnoreCase(adoptorType)) {
\r
134 return new SSLRestClientAdapterImpl(restProperties, serviceSelector);
\r
136 throw new ConfigRestAdaptorException(
\r
137 String.format("no implementation for rest adoptor type (%s) for the selector (%s).",
\r
138 adoptorType, serviceSelector));
\r
141 throw new ConfigRestAdaptorException(
\r
142 String.format("couldn't get rest adoptor type for the selector (%s)", serviceSelector));
\r