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;
30 import javax.validation.constraints.NotNull;
34 import org.oransc.policyagent.exceptions.ServiceException;
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("app")
41 public class ApplicationConfig {
43 private String filepath;
46 private String a1ControllerBaseUrl;
49 private String a1ControllerUsername;
52 private String a1ControllerPassword;
54 private Map<String, RicConfig> ricConfigs = new HashMap<>();
56 private Properties dmaapPublisherConfig;
58 private Properties dmaapConsumerConfig;
60 public String getLocalConfigurationFilePath() {
64 public synchronized String getA1ControllerBaseUrl() {
65 return this.a1ControllerBaseUrl;
68 public synchronized String getA1ControllerUsername() {
69 return this.a1ControllerUsername;
72 public synchronized String getA1ControllerPassword() {
73 return this.a1ControllerPassword;
77 * Do not remove, used by framework!
79 public synchronized void setFilepath(String filepath) {
80 this.filepath = filepath;
83 public synchronized void setA1ControllerBaseUrl(String a1ControllerBaseUrl) {
84 this.a1ControllerBaseUrl = a1ControllerBaseUrl;
87 public synchronized void setA1ControllerUsername(String a1ControllerUsername) {
88 this.a1ControllerUsername = a1ControllerUsername;
91 public synchronized void setA1ControllerPassword(String a1ControllerPassword) {
92 this.a1ControllerPassword = a1ControllerPassword;
95 public synchronized Collection<RicConfig> getRicConfigs() {
96 return this.ricConfigs.values();
99 public RicConfig getRic(String ricName) throws ServiceException {
100 for (RicConfig ricConfig : getRicConfigs()) {
101 if (ricConfig.name().equals(ricName)) {
105 throw new ServiceException("Could not find ric: " + ricName);
108 public static class RicConfigUpdate {
110 ADDED, CHANGED, REMOVED
114 private final RicConfig ricConfig;
116 private final Type type;
118 RicConfigUpdate(RicConfig ric, Type event) {
119 this.ricConfig = ric;
124 public synchronized Flux<RicConfigUpdate> setConfiguration(@NotNull Collection<RicConfig> ricConfigs,
125 Properties dmaapPublisherConfig, Properties dmaapConsumerConfig) {
127 Collection<RicConfigUpdate> modifications = new ArrayList<>();
128 this.dmaapPublisherConfig = dmaapPublisherConfig;
129 this.dmaapConsumerConfig = dmaapConsumerConfig;
131 Map<String, RicConfig> newRicConfigs = new HashMap<>();
132 for (RicConfig newConfig : ricConfigs) {
133 RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
134 if (oldConfig == null) {
135 newRicConfigs.put(newConfig.name(), newConfig);
136 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
137 this.ricConfigs.remove(newConfig.name());
138 } else if (!newConfig.equals(oldConfig)) {
139 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
140 newRicConfigs.put(newConfig.name(), newConfig);
141 this.ricConfigs.remove(newConfig.name());
143 newRicConfigs.put(oldConfig.name(), oldConfig);
146 for (RicConfig deletedConfig : this.ricConfigs.values()) {
147 modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
149 this.ricConfigs = newRicConfigs;
151 return Flux.fromIterable(modifications);