5d7d5d98a982e61372ff248af2a8f081554a935f
[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.boot.context.properties.ConfigurationProperties;
35 import org.springframework.boot.context.properties.EnableConfigurationProperties;
36 import reactor.core.publisher.Flux;
37
38 @EnableConfigurationProperties
39 @ConfigurationProperties("app")
40 public class ApplicationConfig {
41     @NotEmpty
42     private String filepath;
43
44     private Map<String, RicConfig> ricConfigs = new HashMap<>();
45     @Getter
46     private Properties dmaapPublisherConfig;
47     @Getter
48     private Properties dmaapConsumerConfig;
49
50     private Map<String, ControllerConfig> controllerConfigs = new HashMap<>();
51
52     public String getLocalConfigurationFilePath() {
53         return this.filepath;
54     }
55
56     /*
57      * Do not remove, used by framework!
58      */
59     public synchronized void setFilepath(String filepath) {
60         this.filepath = filepath;
61     }
62
63     public synchronized Collection<RicConfig> getRicConfigs() {
64         return this.ricConfigs.values();
65     }
66
67     public synchronized ControllerConfig getControllerConfig(String name) throws ServiceException {
68         ControllerConfig controllerConfig = this.controllerConfigs.get(name);
69         if (controllerConfig == null) {
70             throw new ServiceException("Could not find controller config: " + name);
71         }
72         return controllerConfig;
73     }
74
75     public synchronized RicConfig getRic(String ricName) throws ServiceException {
76         RicConfig ricConfig = this.ricConfigs.get(ricName);
77         if (ricConfig == null) {
78             throw new ServiceException("Could not find ric configuration: " + ricName);
79         }
80         return ricConfig;
81     }
82
83     public static class RicConfigUpdate {
84         public enum Type {
85             ADDED, CHANGED, REMOVED
86         }
87
88         @Getter
89         private final RicConfig ricConfig;
90         @Getter
91         private final Type type;
92
93         RicConfigUpdate(RicConfig ric, Type event) {
94             this.ricConfig = ric;
95             this.type = event;
96         }
97     }
98
99     public synchronized Flux<RicConfigUpdate> setConfiguration(
100         ApplicationConfigParser.ConfigParserResult parserResult) {
101
102         Collection<RicConfigUpdate> modifications = new ArrayList<>();
103         this.dmaapPublisherConfig = parserResult.dmaapPublisherConfig();
104         this.dmaapConsumerConfig = parserResult.dmaapConsumerConfig();
105         this.controllerConfigs = parserResult.controllerConfigs();
106
107         Map<String, RicConfig> newRicConfigs = new HashMap<>();
108         for (RicConfig newConfig : parserResult.ricConfigs()) {
109             RicConfig oldConfig = this.ricConfigs.get(newConfig.name());
110             if (oldConfig == null) {
111                 newRicConfigs.put(newConfig.name(), newConfig);
112                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED));
113                 this.ricConfigs.remove(newConfig.name());
114             } else if (!newConfig.equals(oldConfig)) {
115                 modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED));
116                 newRicConfigs.put(newConfig.name(), newConfig);
117                 this.ricConfigs.remove(newConfig.name());
118             } else {
119                 newRicConfigs.put(oldConfig.name(), oldConfig);
120             }
121         }
122         for (RicConfig deletedConfig : this.ricConfigs.values()) {
123             modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED));
124         }
125         this.ricConfigs = newRicConfigs;
126
127         return Flux.fromIterable(modifications);
128     }
129 }