3b4f8104b377b5dd31daa89b20e5ac37dc77b861
[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 import reactor.core.publisher.Flux;
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 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 synchronized String getA1ControllerBaseUrl() {
65         return this.a1ControllerBaseUrl;
66     }
67
68     public synchronized String getA1ControllerUsername() {
69         return this.a1ControllerUsername;
70     }
71
72     public synchronized 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 static class RicConfigUpdate {
109         public enum Type {
110             ADDED, CHANGED, REMOVED
111         }
112
113         @Getter
114         private final RicConfig ricConfig;
115         @Getter
116         private final Type type;
117
118         RicConfigUpdate(RicConfig ric, Type event) {
119             this.ricConfig = ric;
120             this.type = event;
121         }
122     }
123
124     public synchronized Flux<RicConfigUpdate> setConfiguration(@NotNull Collection<RicConfig> ricConfigs,
125         Properties dmaapPublisherConfig, Properties dmaapConsumerConfig) {
126
127         Collection<RicConfigUpdate> modifications = new ArrayList<>();
128         this.dmaapPublisherConfig = dmaapPublisherConfig;
129         this.dmaapConsumerConfig = dmaapConsumerConfig;
130
131         Map<String, RicConfig> newRicConfigs = new HashMap<>();
132         for (RicConfig newConfig : ricConfigs) {
133             RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
134             if (oldConfig == null) {
135                 newRicConfigs.put(newConfig.name(), newConfig);
136                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
137                 this.ricConfigs.remove(newConfig.name());
138             } else if (!newConfig.equals(oldConfig)) {
139                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
140                 newRicConfigs.put(newConfig.name(), newConfig);
141                 this.ricConfigs.remove(newConfig.name());
142             } else {
143                 newRicConfigs.put(oldConfig.name(), oldConfig);
144             }
145         }
146         for (RicConfig deletedConfig : this.ricConfigs.values()) {
147             modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
148         }
149         this.ricConfigs = newRicConfigs;
150
151         return Flux.fromIterable(modifications);
152     }
153 }