X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=policy-agent%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fpolicyagent%2Fconfiguration%2FApplicationConfig.java;h=a2533733553201f2c615b1053bea8d3b562483fb;hb=4e7db50d7fb3fd2c7101520f00f0f0b4baf9bddc;hp=d8da5ee89af883713cbce391a4e4907d6d1f4d82;hpb=2aa1689637366d03a1252afadf93be937c1ec644;p=nonrtric.git diff --git a/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfig.java b/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfig.java index d8da5ee8..a2533733 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfig.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/configuration/ApplicationConfig.java @@ -20,114 +20,120 @@ package org.oransc.policyagent.configuration; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonSyntaxException; -import com.google.gson.TypeAdapterFactory; - -import java.io.BufferedInputStream; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.Optional; -import java.util.Properties; -import java.util.ServiceLoader; -import java.util.Vector; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; import javax.validation.constraints.NotEmpty; -import javax.validation.constraints.NotNull; + +import lombok.Getter; import org.oransc.policyagent.exceptions.ServiceException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import reactor.core.publisher.Mono; +import reactor.core.publisher.Flux; @EnableConfigurationProperties -@ConfigurationProperties("app") +@ConfigurationProperties() public class ApplicationConfig { - private static final Logger logger = LoggerFactory.getLogger(ApplicationConfig.class); + @NotEmpty + @Getter + @Value("${app.filepath}") + private String localConfigurationFilePath; - @Value("#{systemEnvironment}") - Properties systemEnvironment; + @Value("${app.webclient.trust-store-used}") + private boolean sslTrustStoreUsed = false; - @NotEmpty - private String filepath; + @Value("${app.webclient.trust-store-password}") + private String sslTrustStorePassword = ""; - private Vector ricConfigs; + @Value("${app.webclient.trust-store}") + private String sslTrustStore = ""; - @Autowired - public ApplicationConfig() { - } + private Map ricConfigs = new HashMap<>(); + + @Getter + private String dmaapConsumerTopicUrl; - public synchronized void setFilepath(String filepath) { - this.filepath = filepath; + @Getter + private String dmaapProducerTopicUrl; + + private Map controllerConfigs = new HashMap<>(); + + public synchronized Collection getRicConfigs() { + return this.ricConfigs.values(); } - public Vector getRicConfigs() { - return this.ricConfigs; + public WebClientConfig getWebClientConfig() { + return ImmutableWebClientConfig.builder() // + .isTrustStoreUsed(this.sslTrustStoreUsed) // + .trustStore(this.sslTrustStore) // + .trustStorePassword(this.sslTrustStorePassword) // + .build(); } - public Optional lookupRicConfigForManagedElement(String managedElementId) { - for (RicConfig ricConfig : getRicConfigs()) { - if (ricConfig.managedElementIds().contains(managedElementId)) { - return Optional.of(ricConfig); - } + public synchronized ControllerConfig getControllerConfig(String name) throws ServiceException { + ControllerConfig controllerConfig = this.controllerConfigs.get(name); + if (controllerConfig == null) { + throw new ServiceException("Could not find controller config: " + name); } - return Optional.empty(); + return controllerConfig; } - public RicConfig getRic(String ricName) throws ServiceException { - for (RicConfig ricConfig : getRicConfigs()) { - if (ricConfig.name().equals(ricName)) { - return ricConfig; - } + public synchronized RicConfig getRic(String ricName) throws ServiceException { + RicConfig ricConfig = this.ricConfigs.get(ricName); + if (ricConfig == null) { + throw new ServiceException("Could not find ric configuration: " + ricName); } - throw new ServiceException("Could not find ric: " + ricName); + return ricConfig; } - public void initialize() { - loadConfigurationFromFile(this.filepath); - } + public static class RicConfigUpdate { + public enum Type { + ADDED, CHANGED, REMOVED + } - Mono getEnvironment(Properties systemEnvironment) { - return Environment.readEnvironmentVariables(systemEnvironment); - } + @Getter + private final RicConfig ricConfig; + @Getter + private final Type type; - /** - * Reads the configuration from file. - */ - protected void loadConfigurationFromFile(String filepath) { - GsonBuilder gsonBuilder = new GsonBuilder(); - ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory); - - try (InputStream inputStream = createInputStream(filepath)) { - JsonParser parser = new JsonParser(); - JsonObject rootObject = getJsonElement(parser, inputStream).getAsJsonObject(); - if (rootObject == null) { - throw new JsonSyntaxException("Root is not a json object"); - } - ApplicationConfigParser appParser = new ApplicationConfigParser(); - appParser.parse(rootObject); - this.ricConfigs = appParser.getRicConfigs(); - logger.info("Local configuration file loaded: {}", filepath); - } catch (JsonSyntaxException | ServiceException | IOException e) { - logger.trace("Local configuration file not loaded: {}", filepath, e); + RicConfigUpdate(RicConfig ric, Type event) { + this.ricConfig = ric; + this.type = event; } } - JsonElement getJsonElement(JsonParser parser, InputStream inputStream) { - return parser.parse(new InputStreamReader(inputStream)); - } + public synchronized Flux setConfiguration( + ApplicationConfigParser.ConfigParserResult parserResult) { + + Collection modifications = new ArrayList<>(); + this.controllerConfigs = parserResult.controllerConfigs(); + + this.dmaapConsumerTopicUrl = parserResult.dmaapConsumerTopicUrl(); + this.dmaapProducerTopicUrl = parserResult.dmaapProducerTopicUrl(); + + Map newRicConfigs = new HashMap<>(); + for (RicConfig newConfig : parserResult.ricConfigs()) { + RicConfig oldConfig = this.ricConfigs.get(newConfig.name()); + this.ricConfigs.remove(newConfig.name()); + if (oldConfig == null) { + newRicConfigs.put(newConfig.name(), newConfig); + modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED)); + } else if (!newConfig.equals(oldConfig)) { + modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED)); + newRicConfigs.put(newConfig.name(), newConfig); + } else { + newRicConfigs.put(oldConfig.name(), oldConfig); + } + } + for (RicConfig deletedConfig : this.ricConfigs.values()) { + modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED)); + } + this.ricConfigs = newRicConfigs; - InputStream createInputStream(@NotNull String filepath) throws IOException { - return new BufferedInputStream(new FileInputStream(filepath)); + return Flux.fromIterable(modifications); } - }