2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oransc.policyagent.configuration;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
27 import java.util.Properties;
29 import javax.validation.constraints.NotEmpty;
33 import org.oransc.policyagent.exceptions.ServiceException;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.boot.context.properties.ConfigurationProperties;
36 import org.springframework.boot.context.properties.EnableConfigurationProperties;
37 import reactor.core.publisher.Flux;
39 @EnableConfigurationProperties
40 @ConfigurationProperties()
41 public class ApplicationConfig {
44 @Value("${app.filepath}")
45 private String localConfigurationFilePath;
47 @Value("${app.webclient.trust-store-used}")
48 private boolean sslTrustStoreUsed = false;
50 @Value("${app.webclient.trust-store-password}")
51 private String sslTrustStorePassword = "";
53 @Value("${app.webclient.trust-store}")
54 private String sslTrustStore = "";
56 private Map<String, RicConfig> ricConfigs = new HashMap<>();
58 private Properties dmaapPublisherConfig;
60 private Properties dmaapConsumerConfig;
62 private Map<String, ControllerConfig> controllerConfigs = new HashMap<>();
64 public synchronized Collection<RicConfig> getRicConfigs() {
65 return this.ricConfigs.values();
68 public WebClientConfig getWebClientConfig() {
69 return ImmutableWebClientConfig.builder() //
70 .isTrustStoreUsed(this.sslTrustStoreUsed) //
71 .trustStore(this.sslTrustStore) //
72 .trustStorePassword(this.sslTrustStorePassword) //
76 public synchronized ControllerConfig getControllerConfig(String name) throws ServiceException {
77 ControllerConfig controllerConfig = this.controllerConfigs.get(name);
78 if (controllerConfig == null) {
79 throw new ServiceException("Could not find controller config: " + name);
81 return controllerConfig;
84 public synchronized RicConfig getRic(String ricName) throws ServiceException {
85 RicConfig ricConfig = this.ricConfigs.get(ricName);
86 if (ricConfig == null) {
87 throw new ServiceException("Could not find ric configuration: " + ricName);
92 public static class RicConfigUpdate {
94 ADDED, CHANGED, REMOVED
98 private final RicConfig ricConfig;
100 private final Type type;
102 RicConfigUpdate(RicConfig ric, Type event) {
103 this.ricConfig = ric;
108 public synchronized Flux<RicConfigUpdate> setConfiguration(
109 ApplicationConfigParser.ConfigParserResult parserResult) {
111 Collection<RicConfigUpdate> modifications = new ArrayList<>();
112 this.dmaapPublisherConfig = parserResult.dmaapPublisherConfig();
113 this.dmaapConsumerConfig = parserResult.dmaapConsumerConfig();
114 this.controllerConfigs = parserResult.controllerConfigs();
116 Map<String, RicConfig> newRicConfigs = new HashMap<>();
117 for (RicConfig newConfig : parserResult.ricConfigs()) {
118 RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
119 this.ricConfigs.remove(newConfig.name());
120 if (oldConfig == null) {
121 newRicConfigs.put(newConfig.name(), newConfig);
122 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
123 } else if (!newConfig.equals(oldConfig)) {
124 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
125 newRicConfigs.put(newConfig.name(), newConfig);
127 newRicConfigs.put(oldConfig.name(), oldConfig);
130 for (RicConfig deletedConfig : this.ricConfigs.values()) {
131 modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
133 this.ricConfigs = newRicConfigs;
135 return Flux.fromIterable(modifications);