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;
38 @EnableConfigurationProperties
39 @ConfigurationProperties("app")
40 public class ApplicationConfig {
42 private String filepath;
45 private String a1ControllerBaseUrl;
48 private String a1ControllerUsername;
51 private String a1ControllerPassword;
53 private Collection<Observer> observers = new ArrayList<>();
54 private Map<String, RicConfig> ricConfigs = new HashMap<>();
56 private Properties dmaapPublisherConfig;
58 private Properties dmaapConsumerConfig;
60 public String getLocalConfigurationFilePath() {
64 public String getA1ControllerBaseUrl() {
65 return this.a1ControllerBaseUrl;
68 public String getA1ControllerUsername() {
69 return this.a1ControllerUsername;
72 public 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 enum RicConfigUpdate {
109 ADDED, CHANGED, REMOVED
112 public interface Observer {
113 void onRicConfigUpdate(RicConfig ric, RicConfigUpdate event);
116 public void addObserver(Observer o) {
117 this.observers.add(o);
120 private class Notification {
122 final RicConfigUpdate event;
124 Notification(RicConfig ric, RicConfigUpdate event) {
130 public void setConfiguration(@NotNull Collection<RicConfig> ricConfigs, Properties dmaapPublisherConfig,
131 Properties dmaapConsumerConfig) {
133 Collection<Notification> notifications = new ArrayList<>();
134 synchronized (this) {
135 this.dmaapPublisherConfig = dmaapPublisherConfig;
136 this.dmaapConsumerConfig = dmaapConsumerConfig;
138 Map<String, RicConfig> newRicConfigs = new HashMap<>();
139 for (RicConfig newConfig : ricConfigs) {
140 RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
141 if (oldConfig == null) {
142 newRicConfigs.put(newConfig.name(), newConfig);
143 notifications.add(new Notification(newConfig, RicConfigUpdate.ADDED));
144 this.ricConfigs.remove(newConfig.name());
145 } else if (!newConfig.equals(oldConfig)) {
146 notifications.add(new Notification(newConfig, RicConfigUpdate.CHANGED));
147 newRicConfigs.put(newConfig.name(), newConfig);
148 this.ricConfigs.remove(newConfig.name());
150 newRicConfigs.put(oldConfig.name(), oldConfig);
153 for (RicConfig deletedConfig : this.ricConfigs.values()) {
154 notifications.add(new Notification(deletedConfig, RicConfigUpdate.REMOVED));
156 this.ricConfigs = newRicConfigs;
159 notifyObservers(notifications);
162 private void notifyObservers(Collection<Notification> notifications) {
163 for (Observer observer : this.observers) {
164 for (Notification notif : notifications) {
165 observer.onRicConfigUpdate(notif.ric, notif.event);