f23b5e24882fa5e23aecd8213c6a036dbcc2ebca
[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     private Collection<Observer> observers = new Vector<>();
46     private Map<String, RicConfig> ricConfigs = new HashMap<>();
47     @Getter
48     private Properties dmaapPublisherConfig;
49     @Getter
50     private Properties dmaapConsumerConfig;
51
52     @Autowired
53     public ApplicationConfig() {
54     }
55
56     public String getLocalConfigurationFilePath() {
57         return this.filepath;
58     }
59
60     /*
61      * Do not remove, used by framework!
62      */
63     public synchronized void setFilepath(String filepath) {
64         this.filepath = filepath;
65     }
66
67     public synchronized Collection<RicConfig> getRicConfigs() {
68         return this.ricConfigs.values();
69     }
70
71     public RicConfig getRic(String ricName) throws ServiceException {
72         for (RicConfig ricConfig : getRicConfigs()) {
73             if (ricConfig.name().equals(ricName)) {
74                 return ricConfig;
75             }
76         }
77         throw new ServiceException("Could not find ric: " + ricName);
78     }
79
80     public static enum RicConfigUpdate {
81         ADDED, CHANGED, REMOVED
82     }
83
84     public interface Observer {
85         void onRicConfigUpdate(RicConfig ric, RicConfigUpdate event);
86     }
87
88     public void addObserver(Observer o) {
89         this.observers.add(o);
90     }
91
92     private class Notification {
93         final RicConfig ric;
94         final RicConfigUpdate event;
95
96         Notification(RicConfig ric, RicConfigUpdate event) {
97             this.ric = ric;
98             this.event = event;
99         }
100     }
101
102     public void setConfiguration(@NotNull Collection<RicConfig> ricConfigs, Properties dmaapPublisherConfig,
103         Properties dmaapConsumerConfig) {
104         Collection<Notification> notifications = new Vector<>();
105         synchronized (this) {
106             Map<String, RicConfig> newRicConfigs = new HashMap<>();
107             for (RicConfig newConfig : ricConfigs) {
108                 RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
109                 if (oldConfig == null) {
110                     newRicConfigs.put(newConfig.name(), newConfig);
111                     notifications.add(new Notification(newConfig, RicConfigUpdate.ADDED));
112                     this.ricConfigs.remove(newConfig.name());
113                 } else if (!newConfig.equals(oldConfig)) {
114                     notifications.add(new Notification(newConfig, RicConfigUpdate.CHANGED));
115                     newRicConfigs.put(newConfig.name(), newConfig);
116                     this.ricConfigs.remove(newConfig.name());
117                 } else {
118                     newRicConfigs.put(oldConfig.name(), oldConfig);
119                 }
120             }
121             for (RicConfig deletedConfig : this.ricConfigs.values()) {
122                 notifications.add(new Notification(deletedConfig, RicConfigUpdate.REMOVED));
123             }
124             this.ricConfigs = newRicConfigs;
125         }
126         notifyObservers(notifications);
127
128         this.dmaapPublisherConfig = dmaapPublisherConfig;
129         this.dmaapConsumerConfig = dmaapConsumerConfig;
130     }
131
132     private void notifyObservers(Collection<Notification> notifications) {
133         for (Observer observer : this.observers) {
134             for (Notification notif : notifications) {
135                 observer.onRicConfigUpdate(notif.ric, notif.event);
136             }
137         }
138     }
139 }