Infrastructure for having mutiple RIC APIs
[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.Vector;
27
28 import javax.validation.constraints.NotEmpty;
29 import javax.validation.constraints.NotNull;
30
31 import org.oransc.policyagent.exceptions.ServiceException;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.boot.context.properties.ConfigurationProperties;
34 import org.springframework.boot.context.properties.EnableConfigurationProperties;
35
36 @EnableConfigurationProperties
37 @ConfigurationProperties("app")
38 public class ApplicationConfig {
39     @NotEmpty
40     private String filepath;
41
42     private Collection<Observer> observers = new Vector<>();
43     private Map<String, RicConfig> ricConfigs = new HashMap<>();
44
45     @Autowired
46     public ApplicationConfig() {
47     }
48
49     public String getLocalConfigurationFilePath() {
50         return this.filepath;
51     }
52
53     public synchronized Collection<RicConfig> getRicConfigs() {
54         return this.ricConfigs.values();
55     }
56
57     public RicConfig getRic(String ricName) throws ServiceException {
58         for (RicConfig ricConfig : getRicConfigs()) {
59             if (ricConfig.name().equals(ricName)) {
60                 return ricConfig;
61             }
62         }
63         throw new ServiceException("Could not find ric: " + ricName);
64     }
65
66     public static enum RicConfigUpdate {
67         ADDED, CHANGED, REMOVED
68     }
69
70     public interface Observer {
71         void onRicConfigUpdate(RicConfig ric, RicConfigUpdate event);
72     }
73
74     public void addObserver(Observer o) {
75         this.observers.add(o);
76     }
77
78     private class Notification {
79         final RicConfig ric;
80         final RicConfigUpdate event;
81
82         Notification(RicConfig ric, RicConfigUpdate event) {
83             this.ric = ric;
84             this.event = event;
85         }
86     }
87
88     public void setConfiguration(@NotNull Collection<RicConfig> ricConfigs) {
89         Collection<Notification> notifications = new Vector<>();
90         synchronized (this) {
91             Map<String, RicConfig> newRicConfigs = new HashMap<>();
92             for (RicConfig newConfig : ricConfigs) {
93                 RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
94                 if (oldConfig == null) {
95                     newRicConfigs.put(newConfig.name(), newConfig);
96                     notifications.add(new Notification(newConfig, RicConfigUpdate.ADDED));
97                     this.ricConfigs.remove(newConfig.name());
98                 } else if (!newConfig.equals(newConfig)) {
99                     notifications.add(new Notification(newConfig, RicConfigUpdate.CHANGED));
100                     newRicConfigs.put(newConfig.name(), newConfig);
101                     this.ricConfigs.remove(newConfig.name());
102                 } else {
103                     newRicConfigs.put(oldConfig.name(), oldConfig);
104                 }
105             }
106             for (RicConfig deletedConfig : this.ricConfigs.values()) {
107                 notifications.add(new Notification(deletedConfig, RicConfigUpdate.REMOVED));
108             }
109             this.ricConfigs = newRicConfigs;
110         }
111         notifyObservers(notifications);
112     }
113
114     private void notifyObservers(Collection<Notification> notifications) {
115         for (Observer observer : this.observers) {
116             for (Notification notif : notifications) {
117                 observer.onRicConfigUpdate(notif.ric, notif.event);
118             }
119         }
120     }
121 }