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;
28 import javax.validation.constraints.NotEmpty;
29 import javax.validation.constraints.NotNull;
31 import org.oransc.policyagent.exceptions.ServiceException;
32 import org.springframework.boot.context.properties.ConfigurationProperties;
33 import org.springframework.boot.context.properties.EnableConfigurationProperties;
35 @EnableConfigurationProperties
36 @ConfigurationProperties("app")
37 public class ApplicationConfig {
39 private String filepath;
42 private String a1ControllerBaseUrl;
45 private String a1ControllerUsername;
48 private String a1ControllerPassword;
50 private Collection<Observer> observers = new ArrayList<>();
51 private Map<String, RicConfig> ricConfigs = new HashMap<>();
53 private Properties dmaapPublisherConfig;
55 private Properties dmaapConsumerConfig;
57 public String getLocalConfigurationFilePath() {
61 public String getA1ControllerBaseUrl() {
62 return this.a1ControllerBaseUrl;
65 public String getA1ControllerUsername() {
66 return this.a1ControllerUsername;
69 public String getA1ControllerPassword() {
70 return this.a1ControllerPassword;
74 * Do not remove, used by framework!
76 public synchronized void setFilepath(String filepath) {
77 this.filepath = filepath;
80 public synchronized void setA1ControllerBaseUrl(String a1ControllerBaseUrl) {
81 this.a1ControllerBaseUrl = a1ControllerBaseUrl;
84 public synchronized void setA1ControllerUsername(String a1ControllerUsername) {
85 this.a1ControllerUsername = a1ControllerUsername;
88 public synchronized void setA1ControllerPassword(String a1ControllerPassword) {
89 this.a1ControllerPassword = a1ControllerPassword;
92 public synchronized Collection<RicConfig> getRicConfigs() {
93 return this.ricConfigs.values();
96 public RicConfig getRic(String ricName) throws ServiceException {
97 for (RicConfig ricConfig : getRicConfigs()) {
98 if (ricConfig.name().equals(ricName)) {
102 throw new ServiceException("Could not find ric: " + ricName);
105 public enum RicConfigUpdate {
106 ADDED, CHANGED, REMOVED
109 public interface Observer {
110 void onRicConfigUpdate(RicConfig ric, RicConfigUpdate event);
113 public void addObserver(Observer o) {
114 this.observers.add(o);
117 private class Notification {
119 final RicConfigUpdate event;
121 Notification(RicConfig ric, RicConfigUpdate event) {
127 public void setConfiguration(@NotNull Collection<RicConfig> ricConfigs, Properties dmaapPublisherConfig,
128 Properties dmaapConsumerConfig) {
130 Collection<Notification> notifications = new ArrayList<>();
131 synchronized (this) {
132 this.dmaapPublisherConfig = dmaapPublisherConfig;
133 this.dmaapConsumerConfig = dmaapConsumerConfig;
135 Map<String, RicConfig> newRicConfigs = new HashMap<>();
136 for (RicConfig newConfig : ricConfigs) {
137 RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
138 if (oldConfig == null) {
139 newRicConfigs.put(newConfig.name(), newConfig);
140 notifications.add(new Notification(newConfig, RicConfigUpdate.ADDED));
141 this.ricConfigs.remove(newConfig.name());
142 } else if (!newConfig.equals(oldConfig)) {
143 notifications.add(new Notification(newConfig, RicConfigUpdate.CHANGED));
144 newRicConfigs.put(newConfig.name(), newConfig);
145 this.ricConfigs.remove(newConfig.name());
147 newRicConfigs.put(oldConfig.name(), oldConfig);
150 for (RicConfig deletedConfig : this.ricConfigs.values()) {
151 notifications.add(new Notification(deletedConfig, RicConfigUpdate.REMOVED));
153 this.ricConfigs = newRicConfigs;
156 notifyObservers(notifications);
159 private void notifyObservers(Collection<Notification> notifications) {
160 for (Observer observer : this.observers) {
161 for (Notification notif : notifications) {
162 observer.onRicConfigUpdate(notif.ric, notif.event);