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=1ed3fdb2b70706ac54c958eaa3e308973ef743e0;hb=f3461cb776023b950d62edd25eca148b6d354c9c;hp=79c82a789844d21d058ea9b97c83084111aa2cfa;hpb=412e0a2f5d16a227374ebd417e7545a514df1143;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..1ed3fdb2 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,124 @@ 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.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 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; -@Component @EnableConfigurationProperties @ConfigurationProperties("app") public class ApplicationConfig { - private static final Logger logger = LoggerFactory.getLogger(ApplicationConfig.class); - - @Value("#{systemEnvironment}") - Properties systemEnvironment; - @NotEmpty private String filepath; - private Vector ricConfigs; + private Collection observers = new Vector<>(); + private Map ricConfigs = new HashMap<>(); + private Properties dmaapPublisherConfig; + private Properties dmaapConsumerConfig; @Autowired public ApplicationConfig() { } + public String getLocalConfigurationFilePath() { + return this.filepath; + } + + /* + * Do not remove, used by framework! + */ public synchronized void setFilepath(String filepath) { this.filepath = filepath; } - public Vector getRicConfigs() { - return this.ricConfigs; + public synchronized Collection getRicConfigs() { + return this.ricConfigs.values(); } - public Optional getRicConfig(String managedElementId) { + public RicConfig getRic(String ricName) throws ServiceException { for (RicConfig ricConfig : getRicConfigs()) { - if (ricConfig.managedElementIds().contains(managedElementId)) { - return Optional.of(ricConfig); - + if (ricConfig.name().equals(ricName)) { + return ricConfig; } } - return Optional.empty(); + throw new ServiceException("Could not find ric: " + ricName); } - public void initialize() { - loadConfigurationFromFile(); + public Properties getDmaapPublisherConfig() { + return dmaapConsumerConfig; } - Mono getEnvironment(Properties systemEnvironment) { - return Environment.readEnvironmentVariables(systemEnvironment); + public Properties getDmaapConsumerConfig() { + return dmaapConsumerConfig; } - /** - * 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"); - } - 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); - } + public static enum RicConfigUpdate { + ADDED, CHANGED, REMOVED + } + + public interface Observer { + void onRicConfigUpdate(RicConfig ric, RicConfigUpdate event); } - JsonElement getJsonElement(JsonParser parser, InputStream inputStream) { - return parser.parse(new InputStreamReader(inputStream)); + public void addObserver(Observer o) { + this.observers.add(o); } - InputStream createInputStream(@NotNull String filepath) throws IOException { - return new BufferedInputStream(new FileInputStream(filepath)); + private class Notification { + final RicConfig ric; + final RicConfigUpdate event; + + Notification(RicConfig ric, RicConfigUpdate event) { + this.ric = ric; + this.event = event; + } } + public void setConfiguration(@NotNull Collection ricConfigs, Properties dmaapPublisherConfig, + Properties dmaapConsumerConfig) { + Collection notifications = new Vector<>(); + synchronized (this) { + Map newRicConfigs = new HashMap<>(); + for (RicConfig newConfig : ricConfigs) { + RicConfig oldConfig = this.ricConfigs.get(newConfig.name()); + if (oldConfig == null) { + newRicConfigs.put(newConfig.name(), newConfig); + notifications.add(new Notification(newConfig, RicConfigUpdate.ADDED)); + this.ricConfigs.remove(newConfig.name()); + } else if (!newConfig.equals(newConfig)) { + notifications.add(new Notification(newConfig, RicConfigUpdate.CHANGED)); + newRicConfigs.put(newConfig.name(), newConfig); + this.ricConfigs.remove(newConfig.name()); + } else { + newRicConfigs.put(oldConfig.name(), oldConfig); + } + } + for (RicConfig deletedConfig : this.ricConfigs.values()) { + notifications.add(new Notification(deletedConfig, RicConfigUpdate.REMOVED)); + } + this.ricConfigs = newRicConfigs; + } + notifyObservers(notifications); + + this.dmaapPublisherConfig = dmaapPublisherConfig; + this.dmaapConsumerConfig = dmaapConsumerConfig; + } + + private void notifyObservers(Collection notifications) { + for (Observer observer : this.observers) { + for (Notification notif : notifications) { + observer.onRicConfigUpdate(notif.ric, notif.event); + } + } + } }