5e020983e46608d75d460dfec0e647451f8d2730
[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
28 import javax.validation.constraints.NotEmpty;
29
30 import lombok.Getter;
31
32 import org.oransc.policyagent.exceptions.ServiceException;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.boot.context.properties.ConfigurationProperties;
35 import org.springframework.boot.context.properties.EnableConfigurationProperties;
36 import reactor.core.publisher.Flux;
37
38 @EnableConfigurationProperties
39 @ConfigurationProperties()
40 public class ApplicationConfig {
41     @NotEmpty
42     @Getter
43     @Value("${app.filepath}")
44     private String localConfigurationFilePath;
45
46     @Value("${server.ssl.key-store-type}")
47     private String sslKeyStoreType = "";
48
49     @Value("${server.ssl.key-store-password}")
50     private String sslKeyStorePassword = "";
51
52     @Value("${server.ssl.key-store}")
53     private String sslKeyStore = "";
54
55     @Value("${server.ssl.key-password}")
56     private String sslKeyPassword = "";
57
58     @Value("${app.webclient.trust-store-used}")
59     private boolean sslTrustStoreUsed = false;
60
61     @Value("${app.webclient.trust-store-password}")
62     private String sslTrustStorePassword = "";
63
64     @Value("${app.webclient.trust-store}")
65     private String sslTrustStore = "";
66
67     private Map<String, RicConfig> ricConfigs = new HashMap<>();
68
69     @Getter
70     private String dmaapConsumerTopicUrl;
71
72     @Getter
73     private String dmaapProducerTopicUrl;
74
75     private Map<String, ControllerConfig> controllerConfigs = new HashMap<>();
76
77     public synchronized Collection<RicConfig> getRicConfigs() {
78         return this.ricConfigs.values();
79     }
80
81     public WebClientConfig getWebClientConfig() {
82         return ImmutableWebClientConfig.builder() //
83             .keyStoreType(this.sslKeyStoreType) //
84             .keyStorePassword(this.sslKeyStorePassword) //
85             .keyStore(this.sslKeyStore) //
86             .keyPassword(this.sslKeyPassword) //
87             .isTrustStoreUsed(this.sslTrustStoreUsed) //
88             .trustStore(this.sslTrustStore) //
89             .trustStorePassword(this.sslTrustStorePassword) //
90             .build();
91     }
92
93     public synchronized ControllerConfig getControllerConfig(String name) throws ServiceException {
94         ControllerConfig controllerConfig = this.controllerConfigs.get(name);
95         if (controllerConfig == null) {
96             throw new ServiceException("Could not find controller config: " + name);
97         }
98         return controllerConfig;
99     }
100
101     public synchronized RicConfig getRic(String ricName) throws ServiceException {
102         RicConfig ricConfig = this.ricConfigs.get(ricName);
103         if (ricConfig == null) {
104             throw new ServiceException("Could not find ric configuration: " + ricName);
105         }
106         return ricConfig;
107     }
108
109     public static class RicConfigUpdate {
110         public enum Type {
111             ADDED, CHANGED, REMOVED
112         }
113
114         @Getter
115         private final RicConfig ricConfig;
116         @Getter
117         private final Type type;
118
119         RicConfigUpdate(RicConfig ric, Type event) {
120             this.ricConfig = ric;
121             this.type = event;
122         }
123     }
124
125     public synchronized Flux<RicConfigUpdate> setConfiguration(
126         ApplicationConfigParser.ConfigParserResult parserResult) {
127
128         Collection<RicConfigUpdate> modifications = new ArrayList<>();
129         this.controllerConfigs = parserResult.controllerConfigs();
130
131         this.dmaapConsumerTopicUrl = parserResult.dmaapConsumerTopicUrl();
132         this.dmaapProducerTopicUrl = parserResult.dmaapProducerTopicUrl();
133
134         Map<String, RicConfig> newRicConfigs = new HashMap<>();
135         for (RicConfig newConfig : parserResult.ricConfigs()) {
136             RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
137             this.ricConfigs.remove(newConfig.name());
138             if (oldConfig == null) {
139                 newRicConfigs.put(newConfig.name(), newConfig);
140                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
141             } else if (!newConfig.equals(oldConfig)) {
142                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
143                 newRicConfigs.put(newConfig.name(), newConfig);
144             } else {
145                 newRicConfigs.put(oldConfig.name(), oldConfig);
146             }
147         }
148         for (RicConfig deletedConfig : this.ricConfigs.values()) {
149             modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
150         }
151         this.ricConfigs = newRicConfigs;
152
153         return Flux.fromIterable(modifications);
154     }
155 }