a2533733553201f2c615b1053bea8d3b562483fb
[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("${app.webclient.trust-store-used}")
47     private boolean sslTrustStoreUsed = false;
48
49     @Value("${app.webclient.trust-store-password}")
50     private String sslTrustStorePassword = "";
51
52     @Value("${app.webclient.trust-store}")
53     private String sslTrustStore = "";
54
55     private Map<String, RicConfig> ricConfigs = new HashMap<>();
56
57     @Getter
58     private String dmaapConsumerTopicUrl;
59
60     @Getter
61     private String dmaapProducerTopicUrl;
62
63     private Map<String, ControllerConfig> controllerConfigs = new HashMap<>();
64
65     public synchronized Collection<RicConfig> getRicConfigs() {
66         return this.ricConfigs.values();
67     }
68
69     public WebClientConfig getWebClientConfig() {
70         return ImmutableWebClientConfig.builder() //
71             .isTrustStoreUsed(this.sslTrustStoreUsed) //
72             .trustStore(this.sslTrustStore) //
73             .trustStorePassword(this.sslTrustStorePassword) //
74             .build();
75     }
76
77     public synchronized ControllerConfig getControllerConfig(String name) throws ServiceException {
78         ControllerConfig controllerConfig = this.controllerConfigs.get(name);
79         if (controllerConfig == null) {
80             throw new ServiceException("Could not find controller config: " + name);
81         }
82         return controllerConfig;
83     }
84
85     public synchronized RicConfig getRic(String ricName) throws ServiceException {
86         RicConfig ricConfig = this.ricConfigs.get(ricName);
87         if (ricConfig == null) {
88             throw new ServiceException("Could not find ric configuration: " + ricName);
89         }
90         return ricConfig;
91     }
92
93     public static class RicConfigUpdate {
94         public enum Type {
95             ADDED, CHANGED, REMOVED
96         }
97
98         @Getter
99         private final RicConfig ricConfig;
100         @Getter
101         private final Type type;
102
103         RicConfigUpdate(RicConfig ric, Type event) {
104             this.ricConfig = ric;
105             this.type = event;
106         }
107     }
108
109     public synchronized Flux<RicConfigUpdate> setConfiguration(
110         ApplicationConfigParser.ConfigParserResult parserResult) {
111
112         Collection<RicConfigUpdate> modifications = new ArrayList<>();
113         this.controllerConfigs = parserResult.controllerConfigs();
114
115         this.dmaapConsumerTopicUrl = parserResult.dmaapConsumerTopicUrl();
116         this.dmaapProducerTopicUrl = parserResult.dmaapProducerTopicUrl();
117
118         Map<String, RicConfig> newRicConfigs = new HashMap<>();
119         for (RicConfig newConfig : parserResult.ricConfigs()) {
120             RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
121             this.ricConfigs.remove(newConfig.name());
122             if (oldConfig == null) {
123                 newRicConfigs.put(newConfig.name(), newConfig);
124                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
125             } else if (!newConfig.equals(oldConfig)) {
126                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
127                 newRicConfigs.put(newConfig.name(), newConfig);
128             } else {
129                 newRicConfigs.put(oldConfig.name(), oldConfig);
130             }
131         }
132         for (RicConfig deletedConfig : this.ricConfigs.values()) {
133             modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
134         }
135         this.ricConfigs = newRicConfigs;
136
137         return Flux.fromIterable(modifications);
138     }
139 }