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.boot.context.properties.ConfigurationProperties;
35 import org.springframework.boot.context.properties.EnableConfigurationProperties;
36 import reactor.core.publisher.Flux;
38 @EnableConfigurationProperties
39 @ConfigurationProperties("app")
40 public class ApplicationConfig {
42 private String filepath;
44 private Map<String, RicConfig> ricConfigs = new HashMap<>();
46 private Properties dmaapPublisherConfig;
48 private Properties dmaapConsumerConfig;
50 private Map<String, ControllerConfig> controllerConfigs = new HashMap<>();
52 public String getLocalConfigurationFilePath() {
57 * Do not remove, used by framework!
59 public synchronized void setFilepath(String filepath) {
60 this.filepath = filepath;
63 public synchronized Collection<RicConfig> getRicConfigs() {
64 return this.ricConfigs.values();
67 public synchronized ControllerConfig getControllerConfig(String name) throws ServiceException {
68 ControllerConfig controllerConfig = this.controllerConfigs.get(name);
69 if (controllerConfig == null) {
70 throw new ServiceException("Could not find controller config: " + name);
72 return controllerConfig;
75 public synchronized RicConfig getRic(String ricName) throws ServiceException {
76 RicConfig ricConfig = this.ricConfigs.get(ricName);
77 if (ricConfig == null) {
78 throw new ServiceException("Could not find ric configuration: " + ricName);
83 public static class RicConfigUpdate {
85 ADDED, CHANGED, REMOVED
89 private final RicConfig ricConfig;
91 private final Type type;
93 RicConfigUpdate(RicConfig ric, Type event) {
99 public synchronized Flux<RicConfigUpdate> setConfiguration(
100 ApplicationConfigParser.ConfigParserResult parserResult) {
102 Collection<RicConfigUpdate> modifications = new ArrayList<>();
103 this.dmaapPublisherConfig = parserResult.dmaapPublisherConfig();
104 this.dmaapConsumerConfig = parserResult.dmaapConsumerConfig();
105 this.controllerConfigs = parserResult.controllerConfigs();
107 Map<String, RicConfig> newRicConfigs = new HashMap<>();
108 for (RicConfig newConfig : parserResult.ricConfigs()) {
109 RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
110 this.ricConfigs.remove(newConfig.name());
111 if (oldConfig == null) {
112 newRicConfigs.put(newConfig.name(), newConfig);
113 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
114 } else if (!newConfig.equals(oldConfig)) {
115 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
116 newRicConfigs.put(newConfig.name(), newConfig);
118 newRicConfigs.put(oldConfig.name(), oldConfig);
121 for (RicConfig deletedConfig : this.ricConfigs.values()) {
122 modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
124 this.ricConfigs = newRicConfigs;
126 return Flux.fromIterable(modifications);