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.Vector;
28 import javax.validation.constraints.NotEmpty;
29 import javax.validation.constraints.NotNull;
31 import org.oransc.policyagent.exceptions.ServiceException;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.boot.context.properties.ConfigurationProperties;
34 import org.springframework.boot.context.properties.EnableConfigurationProperties;
36 @EnableConfigurationProperties
37 @ConfigurationProperties("app")
38 public class ApplicationConfig {
40 private String filepath;
42 private Collection<Observer> observers = new Vector<>();
43 private Map<String, RicConfig> ricConfigs = new HashMap<>();
46 public ApplicationConfig() {
49 public String getLocalConfigurationFilePath() {
53 public synchronized Collection<RicConfig> getRicConfigs() {
54 return this.ricConfigs.values();
57 public RicConfig getRic(String ricName) throws ServiceException {
58 for (RicConfig ricConfig : getRicConfigs()) {
59 if (ricConfig.name().equals(ricName)) {
63 throw new ServiceException("Could not find ric: " + ricName);
66 public static enum RicConfigUpdate {
67 ADDED, CHANGED, REMOVED
70 public interface Observer {
71 void onRicConfigUpdate(RicConfig ric, RicConfigUpdate event);
74 public void addObserver(Observer o) {
75 this.observers.add(o);
78 private class Notification {
80 final RicConfigUpdate event;
82 Notification(RicConfig ric, RicConfigUpdate event) {
88 public void setConfiguration(@NotNull Collection<RicConfig> ricConfigs) {
89 Collection<Notification> notifications = new Vector<>();
91 Map<String, RicConfig> newRicConfigs = new HashMap<>();
92 for (RicConfig newConfig : ricConfigs) {
93 RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
94 if (oldConfig == null) {
95 newRicConfigs.put(newConfig.name(), newConfig);
96 notifications.add(new Notification(newConfig, RicConfigUpdate.ADDED));
97 this.ricConfigs.remove(newConfig.name());
98 } else if (!newConfig.equals(newConfig)) {
99 notifications.add(new Notification(newConfig, RicConfigUpdate.CHANGED));
100 newRicConfigs.put(newConfig.name(), newConfig);
101 this.ricConfigs.remove(newConfig.name());
103 newRicConfigs.put(oldConfig.name(), oldConfig);
106 for (RicConfig deletedConfig : this.ricConfigs.values()) {
107 notifications.add(new Notification(deletedConfig, RicConfigUpdate.REMOVED));
109 this.ricConfigs = newRicConfigs;
111 notifyObservers(notifications);
114 private void notifyObservers(Collection<Notification> notifications) {
115 for (Observer observer : this.observers) {
116 for (Notification notif : notifications) {
117 observer.onRicConfigUpdate(notif.ric, notif.event);