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;
28 import javax.validation.constraints.NotEmpty;
32 import org.oransc.policyagent.exceptions.ServiceException;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.boot.context.properties.ConfigurationProperties;
35 import org.springframework.boot.context.properties.EnableConfigurationProperties;
36 import reactor.core.publisher.Flux;
38 @EnableConfigurationProperties
39 @ConfigurationProperties()
40 public class ApplicationConfig {
43 @Value("${app.filepath}")
44 private String localConfigurationFilePath;
46 @Value("${server.ssl.key-store-type}")
47 private String sslKeyStoreType = "";
49 @Value("${server.ssl.key-store-password}")
50 private String sslKeyStorePassword = "";
52 @Value("${server.ssl.key-store}")
53 private String sslKeyStore = "";
55 @Value("${server.ssl.key-password}")
56 private String sslKeyPassword = "";
58 @Value("${app.webclient.trust-store-used}")
59 private boolean sslTrustStoreUsed = false;
61 @Value("${app.webclient.trust-store-password}")
62 private String sslTrustStorePassword = "";
64 @Value("${app.webclient.trust-store}")
65 private String sslTrustStore = "";
67 private Map<String, RicConfig> ricConfigs = new HashMap<>();
70 private String dmaapConsumerTopicUrl;
73 private String dmaapProducerTopicUrl;
75 private Map<String, ControllerConfig> controllerConfigs = new HashMap<>();
77 public synchronized Collection<RicConfig> getRicConfigs() {
78 return this.ricConfigs.values();
81 public WebClientConfig getWebClientConfig() {
82 return ImmutableWebClientConfig.builder() //
83 .keyStoreType(this.sslKeyStoreType) //
84 .keyStorePassword(this.sslKeyStorePassword) //
85 .keyStore(this.sslKeyStore) //
86 .keyPassword(this.sslKeyPassword) //
87 .isTrustStoreUsed(this.sslTrustStoreUsed) //
88 .trustStore(this.sslTrustStore) //
89 .trustStorePassword(this.sslTrustStorePassword) //
93 public synchronized ControllerConfig getControllerConfig(String name) throws ServiceException {
94 ControllerConfig controllerConfig = this.controllerConfigs.get(name);
95 if (controllerConfig == null) {
96 throw new ServiceException("Could not find controller config: " + name);
98 return controllerConfig;
101 public synchronized RicConfig getRic(String ricName) throws ServiceException {
102 RicConfig ricConfig = this.ricConfigs.get(ricName);
103 if (ricConfig == null) {
104 throw new ServiceException("Could not find ric configuration: " + ricName);
109 public static class RicConfigUpdate {
111 ADDED, CHANGED, REMOVED
115 private final RicConfig ricConfig;
117 private final Type type;
119 RicConfigUpdate(RicConfig ric, Type event) {
120 this.ricConfig = ric;
125 public synchronized Flux<RicConfigUpdate> setConfiguration(
126 ApplicationConfigParser.ConfigParserResult parserResult) {
128 Collection<RicConfigUpdate> modifications = new ArrayList<>();
129 this.controllerConfigs = parserResult.controllerConfigs();
131 this.dmaapConsumerTopicUrl = parserResult.dmaapConsumerTopicUrl();
132 this.dmaapProducerTopicUrl = parserResult.dmaapProducerTopicUrl();
134 Map<String, RicConfig> newRicConfigs = new HashMap<>();
135 for (RicConfig newConfig : parserResult.ricConfigs()) {
136 RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
137 this.ricConfigs.remove(newConfig.name());
138 if (oldConfig == null) {
139 newRicConfigs.put(newConfig.name(), newConfig);
140 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
141 } else if (!newConfig.equals(oldConfig)) {
142 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
143 newRicConfigs.put(newConfig.name(), newConfig);
145 newRicConfigs.put(oldConfig.name(), oldConfig);
148 for (RicConfig deletedConfig : this.ricConfigs.values()) {
149 modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
151 this.ricConfigs = newRicConfigs;
153 return Flux.fromIterable(modifications);