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=deacb446dba4108237676ea1176cc273fb0c1d04;hb=9013ed7ad46ce6927fbf69890487e8df61b7d7ee;hp=04471c88629d656ec41f834628004e0ea83c83eb;hpb=6fe1880bfac3daa86c05461da80147325f5f47a2;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 04471c88..deacb446 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,178 +20,150 @@ 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.time.Duration; -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 org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClient; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest; -import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties; -import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext; +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.Disposable; -import reactor.core.publisher.Flux; -import reactor.core.publisher.Mono; @EnableConfigurationProperties @ConfigurationProperties("app") public class ApplicationConfig { - private static final Logger logger = LoggerFactory.getLogger(ApplicationConfig.class); + @NotEmpty + private String filepath; - @Value("#{systemEnvironment}") - Properties systemEnvironment; + @NotEmpty + private String a1ControllerBaseUrl; - private Disposable refreshConfigTask = null; + @NotEmpty + private String a1ControllerUsername; @NotEmpty - private String filepath; + private String a1ControllerPassword; - private Vector ricConfigs; + private Collection observers = new ArrayList<>(); + private Map ricConfigs = new HashMap<>(); + @Getter + private Properties dmaapPublisherConfig; + @Getter + private Properties dmaapConsumerConfig; - @Autowired - public ApplicationConfig() { + public String getLocalConfigurationFilePath() { + return this.filepath; } - public synchronized void setFilepath(String filepath) { - this.filepath = filepath; + public String getA1ControllerBaseUrl() { + return this.a1ControllerBaseUrl; } - public Vector getRicConfigs() { - return this.ricConfigs; + public String getA1ControllerUsername() { + return this.a1ControllerUsername; } - public Optional lookupRicConfigForManagedElement(String managedElementId) { - for (RicConfig ricConfig : getRicConfigs()) { - if (ricConfig.managedElementIds().contains(managedElementId)) { - return Optional.of(ricConfig); - } - } - return Optional.empty(); + public String getA1ControllerPassword() { + return this.a1ControllerPassword; } - public RicConfig getRic(String ricName) throws ServiceException { - for (RicConfig ricConfig : getRicConfigs()) { - if (ricConfig.name().equals(ricName)) { - return ricConfig; - } - } - throw new ServiceException("Could not find ric: " + ricName); + /* + * Do not remove, used by framework! + */ + public synchronized void setFilepath(String filepath) { + this.filepath = filepath; } - public void initialize() { - stop(); - loadConfigurationFromFile(this.filepath); - - refreshConfigTask = createRefreshTask() // - .subscribe(e -> logger.info("Refreshed configuration data"), - throwable -> logger.error("Configuration refresh terminated due to exception", throwable), - () -> logger.error("Configuration refresh terminated")); + public synchronized void setA1ControllerBaseUrl(String a1ControllerBaseUrl) { + this.a1ControllerBaseUrl = a1ControllerBaseUrl; } - Mono getEnvironment(Properties systemEnvironment) { - return EnvironmentProcessor.readEnvironmentVariables(systemEnvironment); + public synchronized void setA1ControllerUsername(String a1ControllerUsername) { + this.a1ControllerUsername = a1ControllerUsername; } - Flux createRefreshTask() { - return getEnvironment(systemEnvironment) // - .flatMap(this::createCbsClient) // - .flatMapMany(this::periodicConfigurationUpdates) // - .map(this::parseRicConfigurationfromConsul) // - .onErrorResume(this::onErrorResume); + public synchronized void setA1ControllerPassword(String a1ControllerPassword) { + this.a1ControllerPassword = a1ControllerPassword; } - Mono createCbsClient(EnvProperties env) { - return CbsClientFactory.createCbsClient(env); + public synchronized Collection getRicConfigs() { + return this.ricConfigs.values(); } - private Flux periodicConfigurationUpdates(CbsClient cbsClient) { - final Duration initialDelay = Duration.ZERO; - final Duration refreshPeriod = Duration.ofMinutes(1); - final CbsRequest getConfigRequest = CbsRequests.getAll(RequestDiagnosticContext.create()); - return cbsClient.updates(getConfigRequest, initialDelay, refreshPeriod); + public RicConfig getRic(String ricName) throws ServiceException { + for (RicConfig ricConfig : getRicConfigs()) { + if (ricConfig.name().equals(ricName)) { + return ricConfig; + } + } + throw new ServiceException("Could not find ric: " + ricName); } - private Mono onErrorResume(Throwable trowable) { - logger.error("Could not refresh application configuration {}", trowable.toString()); - return Mono.empty(); + public enum RicConfigUpdate { + ADDED, CHANGED, REMOVED } - private ApplicationConfig parseRicConfigurationfromConsul(JsonObject jsonObject) { - try { - ApplicationConfigParser parser = new ApplicationConfigParser(); - parser.parse(jsonObject); - setConfiguration(parser.getRicConfigs()); - - } catch (ServiceException e) { - logger.error("Could not parse configuration {}", e.toString(), e); - } - return this; + public interface Observer { + void onRicConfigUpdate(RicConfig ric, RicConfigUpdate event); } - private synchronized void setConfiguration(@NotNull Vector ricConfigs) { - this.ricConfigs = ricConfigs; + public void addObserver(Observer o) { + this.observers.add(o); } - public void stop() { - if (refreshConfigTask != null) { - refreshConfigTask.dispose(); - refreshConfigTask = null; + private class Notification { + final RicConfig ric; + final RicConfigUpdate event; + + Notification(RicConfig ric, RicConfigUpdate event) { + this.ric = ric; + this.event = event; } } - /** - * 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"); + public void setConfiguration(@NotNull Collection ricConfigs, Properties dmaapPublisherConfig, + Properties dmaapConsumerConfig) { + + Collection notifications = new ArrayList<>(); + synchronized (this) { + 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); + notifications.add(new Notification(newConfig, RicConfigUpdate.ADDED)); + this.ricConfigs.remove(newConfig.name()); + } else if (!newConfig.equals(oldConfig)) { + 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)); } - 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); + this.ricConfigs = newRicConfigs; } - } - JsonElement getJsonElement(JsonParser parser, InputStream inputStream) { - return parser.parse(new InputStreamReader(inputStream)); + notifyObservers(notifications); } - InputStream createInputStream(@NotNull String filepath) throws IOException { - return new BufferedInputStream(new FileInputStream(filepath)); + private void notifyObservers(Collection notifications) { + for (Observer observer : this.observers) { + for (Notification notif : notifications) { + observer.onRicConfigUpdate(notif.ric, notif.event); + } + } } - }