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.io.IOException;
\r
23 import java.io.InputStream;
\r
24 import java.net.URL;
\r
25 import java.util.Map;
\r
26 import java.util.Properties;
\r
27 import java.util.concurrent.ConcurrentHashMap;
\r
28 import java.util.concurrent.Executors;
\r
29 import java.util.concurrent.ScheduledExecutorService;
\r
30 import java.util.concurrent.TimeUnit;
\r
31 import java.util.stream.Collectors;
\r
32 import org.apache.commons.lang3.StringUtils;
\r
33 import org.onap.ccsdk.features.rest.adaptor.ConfigRestAdaptorConstants;
\r
34 import org.onap.ccsdk.features.rest.adaptor.ConfigRestAdaptorException;
\r
35 import org.onap.ccsdk.features.rest.adaptor.data.RestResponse;
\r
36 import com.att.eelf.configuration.EELFLogger;
\r
37 import com.att.eelf.configuration.EELFManager;
\r
38 import org.osgi.framework.Bundle;
\r
39 import org.osgi.framework.BundleContext;
\r
40 import org.osgi.framework.FrameworkUtil;
\r
42 public class ConfigRestAdaptorServiceImpl implements ConfigRestAdaptorService {
\r
44 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigRestAdaptorServiceImpl.class);
\r
45 private Map<String, String> restProperties = new ConcurrentHashMap<>();
\r
47 public ConfigRestAdaptorServiceImpl() {
\r
51 public ConfigRestAdaptorServiceImpl(final String propertyFilePath) {
\r
52 loadProps(propertyFilePath);
\r
54 String envType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY
\r
55 + ConfigRestAdaptorConstants.REST_ADAPTOR_ENV_TYPE);
\r
57 if (!(ConfigRestAdaptorConstants.PROPERTY_ENV_PROD.equalsIgnoreCase(envType)
\r
58 || ConfigRestAdaptorConstants.PROPERTY_ENV_SOLO.equalsIgnoreCase(envType))) {
\r
59 ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
\r
60 Runnable task = () -> loadProps(propertyFilePath);
\r
61 executor.scheduleWithFixedDelay(task, 60, 15, TimeUnit.MINUTES);
\r
63 } catch (Exception e) {
\r
64 logger.error(e.getMessage(), e);
\r
69 // propertyFilePath is only specified in test case.
\r
70 private void loadProps(final String propertyFilePath) {
\r
71 Properties properties = new Properties();
\r
72 if (propertyFilePath != null) {
\r
73 // Loading Default properties
\r
74 String propertyFile =
\r
75 propertyFilePath + File.separator + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;
\r
76 doLoadFromPath(propertyFile, properties);
\r
78 // Try to load config from dir
\r
79 final String ccsdkConfigDir =
\r
80 System.getProperty(ConfigRestAdaptorConstants.SDNC_ROOT_DIR_ENV_VAR_KEY) + File.separator
\r
81 + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;
\r
82 try (FileInputStream in = new FileInputStream(ccsdkConfigDir)) {
\r
83 properties.load(in);
\r
84 logger.info("Loaded {} properties from file {}", properties.size(), ccsdkConfigDir);
\r
85 } catch (Exception e) {
\r
86 // Try to load config from jar
\r
87 final Bundle bundle = FrameworkUtil.getBundle(ConfigRestAdaptorServiceImpl.class);
\r
88 final BundleContext ctx = bundle.getBundleContext();
\r
89 final URL url = ctx.getBundle()
\r
90 .getResource(ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME);
\r
91 doLoadFromPath(url.getPath(), properties);
\r
94 restProperties.putAll(properties.entrySet().stream()
\r
95 .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));
\r
98 private void doLoadFromPath(final String propertyFile, final Properties properties) {
\r
99 try (FileInputStream in = new FileInputStream(propertyFile)) {
\r
100 properties.load(in);
\r
101 logger.info("Loaded {} properties from file {}", properties.size(), propertyFile);
\r
102 } catch (Exception e) {
\r
103 logger.error("Failed to load properties for file: {} "
\r
104 + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME, e);
\r
109 public <T> T getResource(String serviceSelector, String path, Class<T> responseType)
\r
110 throws ConfigRestAdaptorException {
\r
111 return getRestClientAdapterBySelectorName(serviceSelector).getResource(path, responseType);
\r
115 public <T> T postResource(String serviceSelector, String path, Object request, Class<T> responseType)
\r
116 throws ConfigRestAdaptorException {
\r
117 return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request, responseType);
\r
121 public <T> T exchangeResource(String serviceSelector, String path, Object request, Class<T> responseType,
\r
122 String method) throws ConfigRestAdaptorException {
\r
123 return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, responseType,
\r
128 public RestResponse getResource(String serviceSelector, String path) throws ConfigRestAdaptorException {
\r
129 return getRestClientAdapterBySelectorName(serviceSelector).getResource(path);
\r
133 public RestResponse postResource(String serviceSelector, String path, Object request)
\r
134 throws ConfigRestAdaptorException {
\r
135 return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request);
\r
139 public RestResponse exchangeResource(String serviceSelector, String path, Object request, String method)
\r
140 throws ConfigRestAdaptorException {
\r
141 return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, method);
\r
144 private ConfigRestClientServiceAdapter getRestClientAdapterBySelectorName(String serviceSelector)
\r
145 throws ConfigRestAdaptorException {
\r
146 String adoptorType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
\r
147 + ConfigRestAdaptorConstants.SERVICE_TYPE_PROPERTY);
\r
148 if (StringUtils.isNotBlank(adoptorType)) {
\r
149 if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_GENERIC.equalsIgnoreCase(adoptorType)) {
\r
150 return new GenericRestClientAdapterImpl(restProperties, serviceSelector);
\r
151 } else if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_SSL.equalsIgnoreCase(adoptorType)) {
\r
152 return new SSLRestClientAdapterImpl(restProperties, serviceSelector);
\r
154 throw new ConfigRestAdaptorException(
\r
155 String.format("no implementation for rest adoptor type (%s) for the selector (%s).",
\r
156 adoptorType, serviceSelector));
\r
159 throw new ConfigRestAdaptorException(
\r
160 String.format("couldn't get rest adoptor type for the selector (%s)", serviceSelector));
\r