Simplified startup
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / configuration / ApplicationConfig.java
index f23b5e2..3b4f810 100644 (file)
 
 package org.oransc.policyagent.configuration;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
-import java.util.Vector;
 
 import javax.validation.constraints.NotEmpty;
 import javax.validation.constraints.NotNull;
@@ -32,9 +32,9 @@ import javax.validation.constraints.NotNull;
 import lombok.Getter;
 
 import org.oransc.policyagent.exceptions.ServiceException;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import reactor.core.publisher.Flux;
 
 @EnableConfigurationProperties
 @ConfigurationProperties("app")
@@ -42,21 +42,37 @@ public class ApplicationConfig {
     @NotEmpty
     private String filepath;
 
-    private Collection<Observer> observers = new Vector<>();
+    @NotEmpty
+    private String a1ControllerBaseUrl;
+
+    @NotEmpty
+    private String a1ControllerUsername;
+
+    @NotEmpty
+    private String a1ControllerPassword;
+
     private Map<String, RicConfig> 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!
      */
@@ -64,6 +80,18 @@ public class ApplicationConfig {
         this.filepath = filepath;
     }
 
+    public synchronized void setA1ControllerBaseUrl(String a1ControllerBaseUrl) {
+        this.a1ControllerBaseUrl = a1ControllerBaseUrl;
+    }
+
+    public synchronized void setA1ControllerUsername(String a1ControllerUsername) {
+        this.a1ControllerUsername = a1ControllerUsername;
+    }
+
+    public synchronized void setA1ControllerPassword(String a1ControllerPassword) {
+        this.a1ControllerPassword = a1ControllerPassword;
+    }
+
     public synchronized Collection<RicConfig> getRicConfigs() {
         return this.ricConfigs.values();
     }
@@ -77,63 +105,49 @@ public class ApplicationConfig {
         throw new ServiceException("Could not find ric: " + ricName);
     }
 
-    public static enum RicConfigUpdate {
-        ADDED, CHANGED, REMOVED
-    }
-
-    public interface Observer {
-        void onRicConfigUpdate(RicConfig ric, RicConfigUpdate event);
-    }
-
-    public void addObserver(Observer o) {
-        this.observers.add(o);
-    }
+    public static class RicConfigUpdate {
+        public enum Type {
+            ADDED, CHANGED, REMOVED
+        }
 
-    private class Notification {
-        final RicConfig ric;
-        final RicConfigUpdate event;
+        @Getter
+        private final RicConfig ricConfig;
+        @Getter
+        private final Type type;
 
-        Notification(RicConfig ric, RicConfigUpdate event) {
-            this.ric = ric;
-            this.event = event;
+        RicConfigUpdate(RicConfig ric, Type event) {
+            this.ricConfig = ric;
+            this.type = event;
         }
     }
 
-    public void setConfiguration(@NotNull Collection<RicConfig> ricConfigs, Properties dmaapPublisherConfig,
-        Properties dmaapConsumerConfig) {
-        Collection<Notification> notifications = new Vector<>();
-        synchronized (this) {
-            Map<String, RicConfig> 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));
-            }
-            this.ricConfigs = newRicConfigs;
-        }
-        notifyObservers(notifications);
+    public synchronized Flux<RicConfigUpdate> setConfiguration(@NotNull Collection<RicConfig> ricConfigs,
+        Properties dmaapPublisherConfig, Properties dmaapConsumerConfig) {
 
+        Collection<RicConfigUpdate> modifications = new ArrayList<>();
         this.dmaapPublisherConfig = dmaapPublisherConfig;
         this.dmaapConsumerConfig = dmaapConsumerConfig;
-    }
 
-    private void notifyObservers(Collection<Notification> notifications) {
-        for (Observer observer : this.observers) {
-            for (Notification notif : notifications) {
-                observer.onRicConfigUpdate(notif.ric, notif.event);
+        Map<String, RicConfig> 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);
     }
 }