aboutsummaryrefslogtreecommitdiffstats
path: root/blueprints-processor/adaptors/rest-adaptor-provider/src/main/java/org/onap/ccsdk/features/rest/adaptor/service/ConfigRestAdaptorServiceImpl.java
blob: c05d6979ae63df0213ffc49bb93b6595ce4af314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * Modifications Copyright © 2018 IBM.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.features.rest.adaptor.service;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.onap.ccsdk.features.rest.adaptor.ConfigRestAdaptorConstants;
import org.onap.ccsdk.features.rest.adaptor.ConfigRestAdaptorException;
import org.onap.ccsdk.features.rest.adaptor.data.RestResponse;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

public class ConfigRestAdaptorServiceImpl implements ConfigRestAdaptorService {

    private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigRestAdaptorServiceImpl.class);
    private Map<String, String> restProperties = new ConcurrentHashMap<>();

    public ConfigRestAdaptorServiceImpl(String propertyPath) {
        initializeProperties(propertyPath);
        try {
            String envType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY
                    + ConfigRestAdaptorConstants.REST_ADAPTOR_ENV_TYPE);

            if (!(ConfigRestAdaptorConstants.PROPERTY_ENV_PROD.equalsIgnoreCase(envType)
                    || ConfigRestAdaptorConstants.PROPERTY_ENV_SOLO.equalsIgnoreCase(envType))) {
                ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
                Runnable task = () -> {
                    initializeProperties(propertyPath);
                };
                executor.scheduleWithFixedDelay(task, 60, 15, TimeUnit.MINUTES);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private void initializeProperties(String propertyPath) {
        logger.trace("Initialising Config rest adaptor Service with property directory ({})", propertyPath);
        try {
            if (StringUtils.isBlank(propertyPath)) {
                propertyPath = System.getProperty(ConfigRestAdaptorConstants.SDNC_ROOT_DIR_ENV_VAR_KEY);
            }

            if (StringUtils.isBlank(propertyPath)) {
                throw new ConfigRestAdaptorException(
                        String.format("Failed to get the property directory (%s)", propertyPath));
            }

            // Loading Default config-rest-adaptor.properties
            String propertyFile =
                    propertyPath + File.separator + ConfigRestAdaptorConstants.REST_ADAPTOR_PROPERTIES_FILE_NAME;

            Properties properties = new Properties();
            properties.load(new FileInputStream(propertyFile));

            logger.trace("Initializing properties details for property file ({}) properties ({})", propertyFile,
                    properties);
            restProperties.putAll(properties.entrySet().stream()
                    .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    @Override
    public <T> T getResource(String serviceSelector, String path, Class<T> responseType)
            throws ConfigRestAdaptorException {
        return getRestClientAdapterBySelectorName(serviceSelector).getResource(path, responseType);
    }

    @Override
    public <T> T postResource(String serviceSelector, String path, Object request, Class<T> responseType)
            throws ConfigRestAdaptorException {
        return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request, responseType);
    }

    @Override
    public <T> T exchangeResource(String serviceSelector, String path, Object request, Class<T> responseType,
            String method) throws ConfigRestAdaptorException {
        return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, responseType,
                method);
    }

    @Override
    public RestResponse getResource(String serviceSelector, String path) throws ConfigRestAdaptorException {
        return getRestClientAdapterBySelectorName(serviceSelector).getResource(path);
    }

    @Override
    public RestResponse postResource(String serviceSelector, String path, Object request)
            throws ConfigRestAdaptorException {
        return getRestClientAdapterBySelectorName(serviceSelector).postResource(path, request);
    }

    @Override
    public RestResponse exchangeResource(String serviceSelector, String path, Object request, String method)
            throws ConfigRestAdaptorException {
        return getRestClientAdapterBySelectorName(serviceSelector).exchangeResource(path, request, method);
    }

    private ConfigRestClientServiceAdapter getRestClientAdapterBySelectorName(String serviceSelector)
            throws ConfigRestAdaptorException {
        String adoptorType = restProperties.get(ConfigRestAdaptorConstants.REST_ADAPTOR_BASE_PROPERTY + serviceSelector
                + ConfigRestAdaptorConstants.SERVICE_TYPE_PROPERTY);
        if (StringUtils.isNotBlank(adoptorType)) {
            if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_GENERIC.equalsIgnoreCase(adoptorType)) {
                return new GenericRestClientAdapterImpl(restProperties, serviceSelector);
            } else if (ConfigRestAdaptorConstants.REST_ADAPTOR_TYPE_SSL.equalsIgnoreCase(adoptorType)) {
                return new SSLRestClientAdapterImpl(restProperties, serviceSelector);
            } else {
                throw new ConfigRestAdaptorException(
                        String.format("no implementation for rest adoptor type (%s) for the selector (%s).",
                                adoptorType, serviceSelector));
            }
        } else {
            throw new ConfigRestAdaptorException(
                    String.format("couldn't get rest adoptor type for the selector (%s)", serviceSelector));
        }
    }

}