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("${app.webclient.trust-store-used}")
47 private boolean sslTrustStoreUsed = false;
49 @Value("${app.webclient.trust-store-password}")
50 private String sslTrustStorePassword = "";
52 @Value("${app.webclient.trust-store}")
53 private String sslTrustStore = "";
55 private Map<String, RicConfig> ricConfigs = new HashMap<>();
58 private String dmaapConsumerTopicUrl;
61 private String dmaapProducerTopicUrl;
63 private Map<String, ControllerConfig> controllerConfigs = new HashMap<>();
65 public synchronized Collection<RicConfig> getRicConfigs() {
66 return this.ricConfigs.values();
69 public WebClientConfig getWebClientConfig() {
70 return ImmutableWebClientConfig.builder() //
71 .isTrustStoreUsed(this.sslTrustStoreUsed) //
72 .trustStore(this.sslTrustStore) //
73 .trustStorePassword(this.sslTrustStorePassword) //
77 public synchronized ControllerConfig getControllerConfig(String name) throws ServiceException {
78 ControllerConfig controllerConfig = this.controllerConfigs.get(name);
79 if (controllerConfig == null) {
80 throw new ServiceException("Could not find controller config: " + name);
82 return controllerConfig;
85 public synchronized RicConfig getRic(String ricName) throws ServiceException {
86 RicConfig ricConfig = this.ricConfigs.get(ricName);
87 if (ricConfig == null) {
88 throw new ServiceException("Could not find ric configuration: " + ricName);
93 public static class RicConfigUpdate {
95 ADDED, CHANGED, REMOVED
99 private final RicConfig ricConfig;
101 private final Type type;
103 RicConfigUpdate(RicConfig ric, Type event) {
104 this.ricConfig = ric;
109 public synchronized Flux<RicConfigUpdate> setConfiguration(
110 ApplicationConfigParser.ConfigParserResult parserResult) {
112 Collection<RicConfigUpdate> modifications = new ArrayList<>();
113 this.controllerConfigs = parserResult.controllerConfigs();
115 this.dmaapConsumerTopicUrl = parserResult.dmaapConsumerTopicUrl();
116 this.dmaapProducerTopicUrl = parserResult.dmaapProducerTopicUrl();
118 Map<String, RicConfig> newRicConfigs = new HashMap<>();
119 for (RicConfig newConfig : parserResult.ricConfigs()) {
120 RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
121 this.ricConfigs.remove(newConfig.name());
122 if (oldConfig == null) {
123 newRicConfigs.put(newConfig.name(), newConfig);
124 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
125 } else if (!newConfig.equals(oldConfig)) {
126 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
127 newRicConfigs.put(newConfig.name(), newConfig);
129 newRicConfigs.put(oldConfig.name(), oldConfig);
132 for (RicConfig deletedConfig : this.ricConfigs.values()) {
133 modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
135 this.ricConfigs = newRicConfigs;
137 return Flux.fromIterable(modifications);