Fixed concurrency problems
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / configuration / ApplicationConfig.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.policyagent.configuration;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import javax.validation.constraints.NotEmpty;
30 import javax.validation.constraints.NotNull;
31
32 import lombok.Getter;
33
34 import org.oransc.policyagent.exceptions.ServiceException;
35 import org.springframework.boot.context.properties.ConfigurationProperties;
36 import org.springframework.boot.context.properties.EnableConfigurationProperties;
37
38 @EnableConfigurationProperties
39 @ConfigurationProperties("app")
40 public class ApplicationConfig {
41     @NotEmpty
42     private String filepath;
43
44     @NotEmpty
45     private String a1ControllerBaseUrl;
46
47     @NotEmpty
48     private String a1ControllerUsername;
49
50     @NotEmpty
51     private String a1ControllerPassword;
52
53     private Collection<Observer> observers = new ArrayList<>();
54     private Map<String, RicConfig> ricConfigs = new HashMap<>();
55     @Getter
56     private Properties dmaapPublisherConfig;
57     @Getter
58     private Properties dmaapConsumerConfig;
59
60     public String getLocalConfigurationFilePath() {
61         return this.filepath;
62     }
63
64     public String getA1ControllerBaseUrl() {
65         return this.a1ControllerBaseUrl;
66     }
67
68     public String getA1ControllerUsername() {
69         return this.a1ControllerUsername;
70     }
71
72     public String getA1ControllerPassword() {
73         return this.a1ControllerPassword;
74     }
75
76     /*
77      * Do not remove, used by framework!
78      */
79     public synchronized void setFilepath(String filepath) {
80         this.filepath = filepath;
81     }
82
83     public synchronized void setA1ControllerBaseUrl(String a1ControllerBaseUrl) {
84         this.a1ControllerBaseUrl = a1ControllerBaseUrl;
85     }
86
87     public synchronized void setA1ControllerUsername(String a1ControllerUsername) {
88         this.a1ControllerUsername = a1ControllerUsername;
89     }
90
91     public synchronized void setA1ControllerPassword(String a1ControllerPassword) {
92         this.a1ControllerPassword = a1ControllerPassword;
93     }
94
95     public synchronized Collection<RicConfig> getRicConfigs() {
96         return this.ricConfigs.values();
97     }
98
99     public RicConfig getRic(String ricName) throws ServiceException {
100         for (RicConfig ricConfig : getRicConfigs()) {
101             if (ricConfig.name().equals(ricName)) {
102                 return ricConfig;
103             }
104         }
105         throw new ServiceException("Could not find ric: " + ricName);
106     }
107
108     public enum RicConfigUpdate {
109         ADDED, CHANGED, REMOVED
110     }
111
112     public interface Observer {
113         void onRicConfigUpdate(RicConfig ric, RicConfigUpdate event);
114     }
115
116     public void addObserver(Observer o) {
117         this.observers.add(o);
118     }
119
120     private class Notification {
121         final RicConfig ric;
122         final RicConfigUpdate event;
123
124         Notification(RicConfig ric, RicConfigUpdate event) {
125             this.ric = ric;
126             this.event = event;
127         }
128     }
129
130     public void setConfiguration(@NotNull Collection<RicConfig> ricConfigs, Properties dmaapPublisherConfig,
131         Properties dmaapConsumerConfig) {
132
133         Collection<Notification> notifications = new ArrayList<>();
134         synchronized (this) {
135             this.dmaapPublisherConfig = dmaapPublisherConfig;
136             this.dmaapConsumerConfig = dmaapConsumerConfig;
137
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());
149                 } else {
150                     newRicConfigs.put(oldConfig.name(), oldConfig);
151                 }
152             }
153             for (RicConfig deletedConfig : this.ricConfigs.values()) {
154                 notifications.add(new Notification(deletedConfig, RicConfigUpdate.REMOVED));
155             }
156             this.ricConfigs = newRicConfigs;
157         }
158
159         notifyObservers(notifications);
160     }
161
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);
166             }
167         }
168     }
169 }