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