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