1dd850be6be4fb84971cf43895cf2758155bcf14
[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 import javax.validation.constraints.NotEmpty;
29 import javax.validation.constraints.NotNull;
30 import lombok.Getter;
31 import org.oransc.policyagent.exceptions.ServiceException;
32 import org.springframework.boot.context.properties.ConfigurationProperties;
33 import org.springframework.boot.context.properties.EnableConfigurationProperties;
34
35 @EnableConfigurationProperties
36 @ConfigurationProperties("app")
37 public class ApplicationConfig {
38     @NotEmpty
39     private String filepath;
40
41     @NotEmpty
42     private String a1ControllerBaseUrl;
43
44     @NotEmpty
45     private String a1ControllerUsername;
46
47     @NotEmpty
48     private String a1ControllerPassword;
49
50     private Collection<Observer> observers = new ArrayList<>();
51     private Map<String, RicConfig> ricConfigs = new HashMap<>();
52     @Getter
53     private Properties dmaapPublisherConfig;
54     @Getter
55     private Properties dmaapConsumerConfig;
56
57     public String getLocalConfigurationFilePath() {
58         return this.filepath;
59     }
60
61     public String getA1ControllerBaseUrl() {
62         return this.a1ControllerBaseUrl;
63     }
64
65     public String getA1ControllerUsername() {
66         return this.a1ControllerUsername;
67     }
68
69     public String getA1ControllerPassword() {
70         return this.a1ControllerPassword;
71     }
72
73     /*
74      * Do not remove, used by framework!
75      */
76     public synchronized void setFilepath(String filepath) {
77         this.filepath = filepath;
78     }
79
80     public synchronized void setA1ControllerBaseUrl(String a1ControllerBaseUrl) {
81         this.a1ControllerBaseUrl = a1ControllerBaseUrl;
82     }
83
84     public synchronized void setA1ControllerUsername(String a1ControllerUsername) {
85         this.a1ControllerUsername = a1ControllerUsername;
86     }
87
88     public synchronized void setA1ControllerPassword(String a1ControllerPassword) {
89         this.a1ControllerPassword = a1ControllerPassword;
90     }
91
92     public synchronized Collection<RicConfig> getRicConfigs() {
93         return this.ricConfigs.values();
94     }
95
96     public RicConfig getRic(String ricName) throws ServiceException {
97         for (RicConfig ricConfig : getRicConfigs()) {
98             if (ricConfig.name().equals(ricName)) {
99                 return ricConfig;
100             }
101         }
102         throw new ServiceException("Could not find ric: " + ricName);
103     }
104
105     public enum RicConfigUpdate {
106         ADDED, CHANGED, REMOVED
107     }
108
109     public interface Observer {
110         void onRicConfigUpdate(RicConfig ric, RicConfigUpdate event);
111     }
112
113     public void addObserver(Observer o) {
114         this.observers.add(o);
115     }
116
117     private class Notification {
118         final RicConfig ric;
119         final RicConfigUpdate event;
120
121         Notification(RicConfig ric, RicConfigUpdate event) {
122             this.ric = ric;
123             this.event = event;
124         }
125     }
126
127     public void setConfiguration(@NotNull Collection<RicConfig> ricConfigs, Properties dmaapPublisherConfig,
128         Properties dmaapConsumerConfig) {
129
130         Collection<Notification> notifications = new ArrayList<>();
131         synchronized (this) {
132             this.dmaapPublisherConfig = dmaapPublisherConfig;
133             this.dmaapConsumerConfig = dmaapConsumerConfig;
134
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());
146                 } else {
147                     newRicConfigs.put(oldConfig.name(), oldConfig);
148                 }
149             }
150             for (RicConfig deletedConfig : this.ricConfigs.values()) {
151                 notifications.add(new Notification(deletedConfig, RicConfigUpdate.REMOVED));
152             }
153             this.ricConfigs = newRicConfigs;
154         }
155
156         notifyObservers(notifications);
157     }
158
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);
163             }
164         }
165     }
166 }