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=3b4f8104b377b5dd31daa89b20e5ac37dc77b861;hb=3e8bccd59c63f424052fcef5930e94a6629a1a95;hp=79c82a789844d21d058ea9b97c83084111aa2cfa;hpb=def3c3e28fb8616a444ad3caaf2f789292402a02;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 79c82a78..3b4f8104 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,108 +20,134 @@ 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.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; -import java.util.ServiceLoader; -import java.util.Vector; 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 org.springframework.stereotype.Component; -import reactor.core.publisher.Mono; +import reactor.core.publisher.Flux; -@Component @EnableConfigurationProperties @ConfigurationProperties("app") public class ApplicationConfig { - private static final Logger logger = LoggerFactory.getLogger(ApplicationConfig.class); + @NotEmpty + private String filepath; + + @NotEmpty + private String a1ControllerBaseUrl; - @Value("#{systemEnvironment}") - Properties systemEnvironment; + @NotEmpty + private String a1ControllerUsername; @NotEmpty - private String filepath; + private String a1ControllerPassword; - private Vector ricConfigs; + private Map ricConfigs = new HashMap<>(); + @Getter + private Properties dmaapPublisherConfig; + @Getter + private Properties dmaapConsumerConfig; - @Autowired - public ApplicationConfig() { + public String getLocalConfigurationFilePath() { + return this.filepath; } + public synchronized String getA1ControllerBaseUrl() { + return this.a1ControllerBaseUrl; + } + + public synchronized String getA1ControllerUsername() { + return this.a1ControllerUsername; + } + + public synchronized String getA1ControllerPassword() { + return this.a1ControllerPassword; + } + + /* + * Do not remove, used by framework! + */ public synchronized void setFilepath(String filepath) { this.filepath = filepath; } - public Vector getRicConfigs() { - return this.ricConfigs; + public synchronized void setA1ControllerBaseUrl(String a1ControllerBaseUrl) { + this.a1ControllerBaseUrl = a1ControllerBaseUrl; } - public Optional getRicConfig(String managedElementId) { - for (RicConfig ricConfig : getRicConfigs()) { - if (ricConfig.managedElementIds().contains(managedElementId)) { - return Optional.of(ricConfig); - - } - } - return Optional.empty(); + public synchronized void setA1ControllerUsername(String a1ControllerUsername) { + this.a1ControllerUsername = a1ControllerUsername; } - public void initialize() { - loadConfigurationFromFile(); + public synchronized void setA1ControllerPassword(String a1ControllerPassword) { + this.a1ControllerPassword = a1ControllerPassword; } - Mono getEnvironment(Properties systemEnvironment) { - return Environment.readEnvironmentVariables(systemEnvironment); + public synchronized Collection getRicConfigs() { + return this.ricConfigs.values(); } - /** - * Reads the configuration from file. - */ - void loadConfigurationFromFile() { - 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"); + public RicConfig getRic(String ricName) throws ServiceException { + for (RicConfig ricConfig : getRicConfigs()) { + if (ricConfig.name().equals(ricName)) { + return ricConfig; } - 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); } + throw new ServiceException("Could not find ric: " + ricName); } - JsonElement getJsonElement(JsonParser parser, InputStream inputStream) { - return parser.parse(new InputStreamReader(inputStream)); - } + public static class RicConfigUpdate { + public enum Type { + ADDED, CHANGED, REMOVED + } - InputStream createInputStream(@NotNull String filepath) throws IOException { - return new BufferedInputStream(new FileInputStream(filepath)); + @Getter + private final RicConfig ricConfig; + @Getter + private final Type type; + + RicConfigUpdate(RicConfig ric, Type event) { + this.ricConfig = ric; + this.type = event; + } } + public synchronized Flux setConfiguration(@NotNull Collection ricConfigs, + Properties dmaapPublisherConfig, Properties dmaapConsumerConfig) { + + Collection modifications = new ArrayList<>(); + this.dmaapPublisherConfig = dmaapPublisherConfig; + this.dmaapConsumerConfig = dmaapConsumerConfig; + + Map newRicConfigs = new HashMap<>(); + for (RicConfig newConfig : ricConfigs) { + RicConfig oldConfig = this.ricConfigs.get(newConfig.name()); + if (oldConfig == null) { + newRicConfigs.put(newConfig.name(), newConfig); + modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.ADDED)); + this.ricConfigs.remove(newConfig.name()); + } else if (!newConfig.equals(oldConfig)) { + modifications.add(new RicConfigUpdate(newConfig, RicConfigUpdate.Type.CHANGED)); + newRicConfigs.put(newConfig.name(), newConfig); + this.ricConfigs.remove(newConfig.name()); + } else { + newRicConfigs.put(oldConfig.name(), oldConfig); + } + } + for (RicConfig deletedConfig : this.ricConfigs.values()) { + modifications.add(new RicConfigUpdate(deletedConfig, RicConfigUpdate.Type.REMOVED)); + } + this.ricConfigs = newRicConfigs; + + return Flux.fromIterable(modifications); + } }