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;
34 import org.oransc.policyagent.exceptions.ServiceException;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.boot.context.properties.ConfigurationProperties;
37 import org.springframework.boot.context.properties.EnableConfigurationProperties;
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 Collection<Observer> observers = new Vector<>();
55 private Map<String, RicConfig> ricConfigs = new HashMap<>();
57 private Properties dmaapPublisherConfig;
59 private Properties dmaapConsumerConfig;
62 public ApplicationConfig() {
65 public String getLocalConfigurationFilePath() {
69 public String getA1ControllerBaseUrl() {
70 return this.a1ControllerBaseUrl;
73 public String getA1ControllerUsername() {
74 return this.a1ControllerUsername;
77 public String getA1ControllerPassword() {
78 return this.a1ControllerPassword;
82 * Do not remove, used by framework!
84 public synchronized void setFilepath(String filepath) {
85 this.filepath = filepath;
88 public synchronized void setA1ControllerBaseUrl(String a1ControllerBaseUrl) {
89 this.a1ControllerBaseUrl = a1ControllerBaseUrl;
92 public synchronized void setA1ControllerUsername(String a1ControllerUsername) {
93 this.a1ControllerUsername = a1ControllerUsername;
96 public synchronized void setA1ControllerPassword(String a1ControllerPassword) {
97 this.a1ControllerPassword = a1ControllerPassword;
100 public synchronized Collection<RicConfig> getRicConfigs() {
101 return this.ricConfigs.values();
104 public RicConfig getRic(String ricName) throws ServiceException {
105 for (RicConfig ricConfig : getRicConfigs()) {
106 if (ricConfig.name().equals(ricName)) {
110 throw new ServiceException("Could not find ric: " + ricName);
113 public static enum RicConfigUpdate {
114 ADDED, CHANGED, REMOVED
117 public interface Observer {
118 void onRicConfigUpdate(RicConfig ric, RicConfigUpdate event);
121 public void addObserver(Observer o) {
122 this.observers.add(o);
125 private class Notification {
127 final RicConfigUpdate event;
129 Notification(RicConfig ric, RicConfigUpdate event) {
135 public void setConfiguration(@NotNull Collection<RicConfig> ricConfigs, Properties dmaapPublisherConfig,
136 Properties dmaapConsumerConfig) {
137 Collection<Notification> notifications = new Vector<>();
138 synchronized (this) {
139 Map<String, RicConfig> newRicConfigs = new HashMap<>();
140 for (RicConfig newConfig : ricConfigs) {
141 RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
142 if (oldConfig == null) {
143 newRicConfigs.put(newConfig.name(), newConfig);
144 notifications.add(new Notification(newConfig, RicConfigUpdate.ADDED));
145 this.ricConfigs.remove(newConfig.name());
146 } else if (!newConfig.equals(oldConfig)) {
147 notifications.add(new Notification(newConfig, RicConfigUpdate.CHANGED));
148 newRicConfigs.put(newConfig.name(), newConfig);
149 this.ricConfigs.remove(newConfig.name());
151 newRicConfigs.put(oldConfig.name(), oldConfig);
154 for (RicConfig deletedConfig : this.ricConfigs.values()) {
155 notifications.add(new Notification(deletedConfig, RicConfigUpdate.REMOVED));
157 this.ricConfigs = newRicConfigs;
159 notifyObservers(notifications);
161 this.dmaapPublisherConfig = dmaapPublisherConfig;
162 this.dmaapConsumerConfig = dmaapConsumerConfig;
165 private void notifyObservers(Collection<Notification> notifications) {
166 for (Observer observer : this.observers) {
167 for (Notification notif : notifications) {
168 observer.onRicConfigUpdate(notif.ric, notif.event);