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.Collection;
24 import java.util.HashMap;
26 import java.util.Properties;
27 import java.util.Vector;
29 import javax.validation.constraints.NotEmpty;
30 import javax.validation.constraints.NotNull;
32 import org.oransc.policyagent.exceptions.ServiceException;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.boot.context.properties.ConfigurationProperties;
35 import org.springframework.boot.context.properties.EnableConfigurationProperties;
37 @EnableConfigurationProperties
38 @ConfigurationProperties("app")
39 public class ApplicationConfig {
41 private String filepath;
43 private Collection<Observer> observers = new Vector<>();
44 private Map<String, RicConfig> ricConfigs = new HashMap<>();
45 private Properties dmaapConsumerConfig;
48 public ApplicationConfig() {
51 public String getLocalConfigurationFilePath() {
56 * Do not remove, used by framework!
58 public synchronized void setFilepath(String filepath) {
59 this.filepath = filepath;
62 public synchronized Collection<RicConfig> getRicConfigs() {
63 return this.ricConfigs.values();
66 public RicConfig getRic(String ricName) throws ServiceException {
67 for (RicConfig ricConfig : getRicConfigs()) {
68 if (ricConfig.name().equals(ricName)) {
72 throw new ServiceException("Could not find ric: " + ricName);
75 public Properties getDmaapConsumerConfig() {
76 return dmaapConsumerConfig;
79 public static enum RicConfigUpdate {
80 ADDED, CHANGED, REMOVED
83 public interface Observer {
84 void onRicConfigUpdate(RicConfig ric, RicConfigUpdate event);
87 public void addObserver(Observer o) {
88 this.observers.add(o);
91 private class Notification {
93 final RicConfigUpdate event;
95 Notification(RicConfig ric, RicConfigUpdate event) {
101 public void setConfiguration(@NotNull Collection<RicConfig> ricConfigs, Properties dmaapConsumerConfig) {
102 Collection<Notification> notifications = new Vector<>();
103 synchronized (this) {
104 Map<String, RicConfig> newRicConfigs = new HashMap<>();
105 for (RicConfig newConfig : ricConfigs) {
106 RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
107 if (oldConfig == null) {
108 newRicConfigs.put(newConfig.name(), newConfig);
109 notifications.add(new Notification(newConfig, RicConfigUpdate.ADDED));
110 this.ricConfigs.remove(newConfig.name());
111 } else if (!newConfig.equals(newConfig)) {
112 notifications.add(new Notification(newConfig, RicConfigUpdate.CHANGED));
113 newRicConfigs.put(newConfig.name(), newConfig);
114 this.ricConfigs.remove(newConfig.name());
116 newRicConfigs.put(oldConfig.name(), oldConfig);
119 for (RicConfig deletedConfig : this.ricConfigs.values()) {
120 notifications.add(new Notification(deletedConfig, RicConfigUpdate.REMOVED));
122 this.ricConfigs = newRicConfigs;
124 notifyObservers(notifications);
126 this.dmaapConsumerConfig = dmaapConsumerConfig;
129 private void notifyObservers(Collection<Notification> notifications) {
130 for (Observer observer : this.observers) {
131 for (Notification notif : notifications) {
132 observer.onRicConfigUpdate(notif.ric, notif.event);