Reducate IetfZeroTouchCallHomeServerProvider ceremony 06/105806/7
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 3 May 2023 03:16:24 +0000 (05:16 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 3 May 2023 11:30:42 +0000 (13:30 +0200)
Use Uint16 to reduce the dance around mount.Configuration, as we simply
cannot hit IllegalValueException.

While we are at it, remove init() and initialize within the constructor,
makes things consistent w.r.t. AutoCloseable contract.

JIRA: NETCONF-949
Change-Id: I5dffdd64bfc126bbbf4d068a74ca68c16fefada9
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
apps/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/Configuration.java
apps/callhome-provider/src/main/java/org/opendaylight/netconf/callhome/mount/IetfZeroTouchCallHomeServerProvider.java
apps/callhome-provider/src/main/resources/OSGI-INF/blueprint/callhome-topology.xml

index ab6c997ef3d9e007af43e82c6ae7877ae2c6fa89..7b1772f8f30f20bc0ff417f2d9d3a3c42f52259b 100644 (file)
@@ -50,33 +50,6 @@ public class Configuration {
         }
     }
 
-    public static class IllegalValueException extends ConfigurationException {
-        private static final long serialVersionUID = -1172346869408302687L;
-
-        private final String key;
-        private final String value;
-
-        IllegalValueException(final String key, final String value) {
-            super("Key has an illegal value. Key: " + key + ", Value: " + value);
-            this.key = key;
-            this.value = value;
-        }
-
-        IllegalValueException(final String key, final String value, final Exception cause) {
-            super("Key has an illegal value. Key: " + key + ", Value: " + value, cause);
-            this.key = key;
-            this.value = value;
-        }
-
-        public String getKey() {
-            return key;
-        }
-
-        public String getValue() {
-            return value;
-        }
-    }
-
     private Properties properties;
 
     public Configuration() {
@@ -85,7 +58,7 @@ public class Configuration {
 
     public Configuration(final String path) throws ConfigurationException {
         try {
-            this.properties = readFromPath(path);
+            properties = readFromPath(path);
         } catch (IOException ioe) {
             throw new ReadException(path, ioe);
         }
@@ -118,17 +91,4 @@ public class Configuration {
         }
         return result;
     }
-
-    public int getAsPort(final String key) {
-        String keyValue = get(key);
-        try {
-            int newPort = Integer.parseInt(keyValue);
-            if (newPort < 0 || newPort > 65535) {
-                throw new IllegalValueException(key, keyValue);
-            }
-            return newPort;
-        } catch (NumberFormatException e) {
-            throw new IllegalValueException(key, keyValue, e);
-        }
-    }
 }
index 18559458e0cf6bd5306de38b282a280d72a32df8..e26d2dbde8c1f6be87949d11cbb687a704f6f0a0 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.netconf.callhome.mount;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.ListenableFuture;
@@ -46,10 +48,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconf.callhome.server.rev230428.netconf.callhome.server.allowed.devices.device.transport.ssh.SshClientParamsBuilder;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint16;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataTreeChangeListener<AllowedDevices> {
+public final class IetfZeroTouchCallHomeServerProvider
+    implements AutoCloseable, DataTreeChangeListener<AllowedDevices> {
     private static final String APPNAME = "CallHomeServer";
     static final InstanceIdentifier<AllowedDevices> ALL_DEVICES = InstanceIdentifier.create(NetconfCallhomeServer.class)
             .child(AllowedDevices.class);
@@ -59,46 +63,42 @@ public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable, DataT
     private final DataBroker dataBroker;
     private final CallHomeMountDispatcher mountDispacher;
     private final CallHomeAuthProviderImpl authProvider;
+    private final CallhomeStatusReporter statusReporter;
+    private final int port;
 
-    protected NetconfCallHomeServer server;
-
+    private NetconfCallHomeServer server;
     private ListenerRegistration<IetfZeroTouchCallHomeServerProvider> listenerReg = null;
 
-    private static final String CALL_HOME_PORT_KEY = "DefaultCallHomePort";
-    private int port = 0; // 0 = use default in NetconfCallHomeBuilder
-    private final CallhomeStatusReporter statusReporter;
-
     public IetfZeroTouchCallHomeServerProvider(final DataBroker dataBroker,
             final CallHomeMountDispatcher mountDispacher) {
-        this.dataBroker = dataBroker;
-        this.mountDispacher = mountDispacher;
+        this(dataBroker, mountDispacher, Uint16.valueOf(4334));
+    }
+
+    public IetfZeroTouchCallHomeServerProvider(final DataBroker dataBroker,
+            final CallHomeMountDispatcher mountDispacher, final Uint16 port) {
+        this.dataBroker = requireNonNull(dataBroker);
+        this.mountDispacher = requireNonNull(mountDispacher);
+
+        LOG.info("Setting port for call home server to {}", port);
+        this.port = port.toJava();
+
         // FIXME: these should be separate components
         authProvider = new CallHomeAuthProviderImpl(dataBroker);
         statusReporter = new CallhomeStatusReporter(dataBroker);
-    }
 
-    public void init() {
+        LOG.info("Initializing provider for {}", APPNAME);
+
         // Register itself as a listener to changes in Devices subtree
         try {
-            LOG.info("Initializing provider for {}", APPNAME);
             initializeServer();
-            listenerReg = dataBroker.registerDataTreeChangeListener(
-                DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, ALL_DEVICES), this);
-            LOG.info("Initialization complete for {}", APPNAME);
         } catch (IOException | Configuration.ConfigurationException e) {
             LOG.error("Unable to successfully initialize", e);
+            return;
         }
-    }
 
-    public void setPort(final String portStr) {
-        try {
-            Configuration configuration = new Configuration();
-            configuration.set(CALL_HOME_PORT_KEY, portStr);
-            port = configuration.getAsPort(CALL_HOME_PORT_KEY);
-            LOG.info("Setting port for call home server to {}", portStr);
-        } catch (Configuration.ConfigurationException e) {
-            LOG.error("Problem trying to set port for call home server {}", portStr, e);
-        }
+        listenerReg = dataBroker.registerDataTreeChangeListener(
+            DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION, ALL_DEVICES), this);
+        LOG.info("Initialization complete for {}", APPNAME);
     }
 
     private void initializeServer() throws IOException {
index 5b1c8889b889962d8b306a13e53f62406e19f8a5..1d8b6ccd08c35ffd2aa44f7eed142989d98a8f4a 100644 (file)
                interface="org.opendaylight.netconf.callhome.protocol.tls.TlsAllowedDevicesMonitor"/>
 
     <bean id="callhomeProvider" class="org.opendaylight.netconf.callhome.mount.IetfZeroTouchCallHomeServerProvider"
-          init-method="init"
           destroy-method="close" >
         <argument ref="dataBroker" />
         <argument ref="callhomeDispatcher" />
-        <property name="port" value="4334" />
     </bean>
 
     <bean id="callhomeDispatcher" class="org.opendaylight.netconf.callhome.mount.CallHomeMountDispatcher">