summaryrefslogtreecommitdiffstats
path: root/components/bbs-event-processor/src/main/java/org/onap/bbs/event/processor/config/ConsulConfigurationGateway.java
blob: 6530f0b2b53b1d821166e99b0884519719beaf6b (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * ============LICENSE_START=======================================================
 * BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
 * ================================================================================
 * Copyright (C) 2019 NOKIA Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.bbs.event.processor.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.time.Duration;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.jetbrains.annotations.NotNull;
import org.onap.bbs.event.processor.exceptions.ApplicationEnvironmentException;
import org.onap.bbs.event.processor.model.GeneratedAppConfigObject;
import org.onap.bbs.event.processor.model.ImmutableDmaapInfo;
import org.onap.bbs.event.processor.model.ImmutableGeneratedAppConfigObject;
import org.onap.bbs.event.processor.model.ImmutableStreamsObject;
import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import reactor.core.Disposable;

@Component
public class ConsulConfigurationGateway {

    private static final Logger LOGGER = LoggerFactory.getLogger(ConsulConfigurationGateway.class);

    private static final String CONSUL_HOST = "CONSUL_HOST";
    private static final String CONFIG_BINDING_SERVICE = "CONFIG_BINDING_SERVICE";
    private static final String HOSTNAME = "HOSTNAME";

    private final ApplicationConfiguration configuration;
    private Gson gson;
    private Disposable cbsFetchPipeline;

    @Autowired
    ConsulConfigurationGateway(ApplicationConfiguration configuration) {
        this.configuration = configuration;
        gson = new GsonBuilder().setPrettyPrinting().create();
    }

    /**
     * Periodically fetch application configuration via CBS service of DCAE.
     * @param initialDelay initial delay before initiation of polling
     * @param period polling interval
     */
    public void periodicallyFetchConfigFromCbs(Duration initialDelay, Duration period) {
        if (environmentNotReady()) {
            throw new ApplicationEnvironmentException(
                    String.format("Application Environment missing critical parameters: %s",
                            getMissingEnvironmentVariables()));
        }

        fetchConfig(initialDelay, period);
    }

    boolean environmentNotReady() {
        String consulHost = System.getenv().get(CONSUL_HOST);
        String cbs = System.getenv().get(CONFIG_BINDING_SERVICE);
        String hostname = System.getenv().get(HOSTNAME);
        return consulHost == null || cbs == null || hostname == null;
    }

    /**
     * Reschedule application configuration periodic retrieval via CBS service of DCAE.
     * @param initialDelay initial delay before rescheduling
     * @param period new polling interval
     */
    public void rescheduleCbsConfigurationRetrieval(Duration initialDelay, Duration period) {
        if (cbsFetchPipeline != null && !cbsFetchPipeline.isDisposed()) {
            LOGGER.info("Disposing old CBS Config fetch job");
            cbsFetchPipeline.dispose();
        }
        periodicallyFetchConfigFromCbs(initialDelay, period);
    }

    private void parseConsulRetrievedConfiguration(JsonObject jsonObject) {

        GeneratedAppConfigObject generatedAppConfigObject = generateAppConfigObject(jsonObject);
        LOGGER.trace("Consul-Retrieved Application Generated Object:\n{}", generatedAppConfigObject);
        configuration.updateCurrentConfiguration(generatedAppConfigObject);
    }

    private void handleErrors(Throwable throwable) {
        LOGGER.error("Periodic CBS configuration polling was terminated with error: {}", throwable);
        LOGGER.info("Will restart CBS configuration fetching job due to abnormal termination."
                + " Will start fetching after 60 seconds (please correct configuration in the meantime)"
                + " and it will then poll every {} seconds (reverting to default)",
                configuration.getCbsPollingInterval());
        fetchConfig(Duration.ofSeconds(60), Duration.ofSeconds(configuration.getCbsPollingInterval()));
    }

    private void fetchConfig(Duration initialDelay, Duration period) {
        RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();

        // Necessary properties from the environment (Consul host:port, service-name (hostname), CBS name)
        EnvProperties env = EnvProperties.fromEnvironment();

        // Create the client and use it to get the configuration
        cbsFetchPipeline = CbsClientFactory.createCbsClient(env)
                .doOnError(e -> LOGGER.warn("CBS Configuration fetch failed with error: {}", e))
                .retry(e -> true)
                .flatMapMany(cbsClient -> cbsClient.updates(diagnosticContext, initialDelay, period))
                .subscribe(this::parseConsulRetrievedConfiguration, this::handleErrors);
    }

    @NotNull
    GeneratedAppConfigObject generateAppConfigObject(JsonObject configObject) {

        if (LOGGER.isInfoEnabled()) {
            String configAsString = gson.toJson(configObject);
            LOGGER.info("Received App Config object\n{}", configAsString);
        }

        final String dmaapProtocol = configObject.get("dmaap.protocol").getAsString();
        final String dmaapContentType  = configObject.get("dmaap.contentType").getAsString();
        final String dmaapConsumerId  = configObject.get("dmaap.consumer.consumerId").getAsString();
        final String dmaapConsumerGroup = configObject.get("dmaap.consumer.consumerGroup").getAsString();
        final int dmaapMessageLimit  = configObject.get("dmaap.messageLimit").getAsInt();
        final int dmaapTimeoutMs  = configObject.get("dmaap.timeoutMs").getAsInt();

        final String aaiHost = configObject.get("aai.host").getAsString();
        final int aaiPort = configObject.get("aai.port").getAsInt();
        final String aaiProtocol = configObject.get("aai.protocol").getAsString();
        final String aaiUsername = configObject.get("aai.username").getAsString();
        final String aaiPassword = configObject.get("aai.password").getAsString();
        final boolean aaiIgnoreSslCertificateErrors =
                configObject.get("aai.aaiIgnoreSslCertificateErrors").getAsBoolean();

        final int pipelinesPollingIntervalSec = configObject.get("application.pipelinesPollingIntervalSec").getAsInt();
        final int pipelinesTimeoutSec = configObject.get("application.pipelinesTimeoutSec").getAsInt();
        final int cbsPollingIntervalSec = configObject.get("application.cbsPollingIntervalSec").getAsInt();

        final String reRegPolicyScope = configObject.get("application.reregistration.policyScope").getAsString();
        final String reRegClControlName = configObject.get("application.reregistration.clControlName").getAsString();
        final String cpeAuthPolicyScope = configObject.get("application.cpe.authentication.policyScope").getAsString();
        final String cpeAuthClControlName =
                configObject.get("application.cpe.authentication.clControlName").getAsString();

        final String policyVersion = configObject.get("application.policyVersion").getAsString();
        final String closeLoopTargetType = configObject.get("application.clTargetType").getAsString();
        final String closeLoopEventStatus = configObject.get("application.clEventStatus").getAsString();
        final String closeLoopVersion = configObject.get("application.clVersion").getAsString();
        final String closeLoopTarget = configObject.get("application.clTarget").getAsString();
        final String closeLoopOriginator = configObject.get("application.clOriginator").getAsString();

        final String reRegConfigKey = configObject.get("application.reregistration.configKey").getAsString();
        final String cpeAuthConfigKey = configObject.get("application.cpeAuth.configKey").getAsString();
        final String closeLoopConfigKey = configObject.get("application.closeLoop.configKey").getAsString();

        final JsonObject streamsPublishes = configObject.getAsJsonObject("streams_publishes");
        final JsonObject streamsSubscribes = configObject.getAsJsonObject("streams_subscribes");

        return ImmutableGeneratedAppConfigObject.builder()
                .dmaapProtocol(dmaapProtocol)
                .dmaapContentType(dmaapContentType)
                .dmaapConsumerConsumerId(dmaapConsumerId)
                .dmaapConsumerConsumerGroup(dmaapConsumerGroup)
                .dmaapMessageLimit(dmaapMessageLimit)
                .dmaapTimeoutMs(dmaapTimeoutMs)
                .aaiHost(aaiHost)
                .aaiPort(aaiPort)
                .aaiProtocol(aaiProtocol)
                .aaiUsername(aaiUsername)
                .aaiPassword(aaiPassword)
                .aaiIgnoreSslCertificateErrors(aaiIgnoreSslCertificateErrors)
                .pipelinesPollingIntervalSec(pipelinesPollingIntervalSec)
                .pipelinesTimeoutSec(pipelinesTimeoutSec)
                .cbsPollingIntervalSec(cbsPollingIntervalSec)
                .reRegistrationPolicyScope(reRegPolicyScope)
                .reRegistrationClControlName(reRegClControlName)
                .policyVersion(policyVersion)
                .closeLoopTargetType(closeLoopTargetType)
                .closeLoopEventStatus(closeLoopEventStatus)
                .closeLoopVersion(closeLoopVersion)
                .closeLoopTarget(closeLoopTarget)
                .closeLoopOriginator(closeLoopOriginator)
                .cpeAuthPolicyScope(cpeAuthPolicyScope)
                .cpeAuthClControlName(cpeAuthClControlName)
                .reRegConfigKey(reRegConfigKey)
                .cpeAuthConfigKey(cpeAuthConfigKey)
                .closeLoopConfigKey(closeLoopConfigKey)
                .streamSubscribesMap(parseStreamsObjects(streamsSubscribes))
                .streamPublishesMap(parseStreamsObjects(streamsPublishes))
                .build();
    }

    private Set<String> getMissingEnvironmentVariables() {
        Set<String> missingVars = new HashSet<>();
        if (System.getenv().get(CONSUL_HOST) == null) {
            missingVars.add(CONSUL_HOST);
        }
        if (System.getenv().get(CONFIG_BINDING_SERVICE) == null) {
            missingVars.add(CONFIG_BINDING_SERVICE);
        }
        if (System.getenv().get(HOSTNAME) == null) {
            missingVars.add(HOSTNAME);
        }
        return missingVars;
    }

    private Map<String, GeneratedAppConfigObject.StreamsObject> parseStreamsObjects(
            JsonObject jsonObject) {
        Map<String, GeneratedAppConfigObject.StreamsObject> streams = new HashMap<>();

        jsonObject.entrySet().stream()
                .map(this::parseStreamsSingleObject)
                .forEach(e -> streams.put(e.getKey(), e.getValue()));

        return streams;
    }

    private Map.Entry<String, GeneratedAppConfigObject.StreamsObject> parseStreamsSingleObject(
            Map.Entry<String, JsonElement> jsonEntry) {

        JsonObject closeLoopOutput = (JsonObject) jsonEntry.getValue();

        String type = closeLoopOutput.get("type").getAsString();
        String aafUsername = closeLoopOutput.get("aaf_username") != null
                ? closeLoopOutput.get("aaf_username").getAsString() : "";
        String aafPassword = closeLoopOutput.get("aaf_password") != null
                ? closeLoopOutput.get("aaf_password").getAsString() : "";

        JsonObject dmaapInfo = closeLoopOutput.getAsJsonObject("dmaap_info");
        String clientId = dmaapInfo.get("client_id") != null
                ? dmaapInfo.get("client_id").getAsString() : "";
        String clientRole = dmaapInfo.get("client_role") != null
                ? dmaapInfo.get("client_role").getAsString() : "";
        String location = dmaapInfo.get("location") != null
                ? dmaapInfo.get("location").getAsString() : "";
        String topicUrl = dmaapInfo.get("topic_url").getAsString();

        GeneratedAppConfigObject.DmaapInfo dmaapInfoObject = ImmutableDmaapInfo.builder()
                .clientId(clientId)
                .clientRole(clientRole)
                .location(location)
                .topicUrl(topicUrl)
                .build();
        GeneratedAppConfigObject.StreamsObject streamsObject = ImmutableStreamsObject.builder()
                .type(type)
                .aafUsername(aafUsername)
                .aafPassword(aafPassword)
                .dmaapInfo(dmaapInfoObject)
                .build();

        return new AbstractMap.SimpleEntry<>(jsonEntry.getKey(), streamsObject);
    }
}