Do not require NetconfSessionImpl
[netconf.git] / netconf / callhome-protocol / src / main / java / org / opendaylight / netconf / callhome / protocol / CallHomeAuthorization.java
index c2b3b2dc61045cd66f62617595b94c694633bf03..6bcabfe9179a6c4e79e1cbb62debabdba208600b 100644 (file)
@@ -7,14 +7,14 @@
  */
 package org.opendaylight.netconf.callhome.protocol;
 
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.collect.ImmutableSet;
 import java.security.KeyPair;
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Set;
-import org.apache.sshd.ClientSession;
-import org.apache.sshd.client.session.ClientSessionImpl;
+import org.apache.sshd.client.session.ClientSession;
 
 /**
  * Authorization context for incoming call home sessions.
@@ -35,7 +35,7 @@ public abstract class CallHomeAuthorization {
         }
 
         @Override
-        protected void applyTo(ClientSession session) {
+        protected void applyTo(final ClientSession session) {
             throw new IllegalStateException("Server is not allowed.");
         }
     };
@@ -43,9 +43,11 @@ public abstract class CallHomeAuthorization {
     /**
      * Returns CallHomeAuthorization object with intent to
      * reject incoming connection.
+     *
      * <p>
      * {@link CallHomeAuthorizationProvider} may use returned object
-     * as return value for {@link CallHomeAuthorizationProvider#provideAuth(java.net.SocketAddress, java.security.PublicKey)}
+     * as return value for
+     * {@link CallHomeAuthorizationProvider#provideAuth(java.net.SocketAddress, java.security.PublicKey)}
      * if the incoming session should be rejected due to policy implemented
      * by provider.
      *
@@ -58,6 +60,7 @@ public abstract class CallHomeAuthorization {
     /**
      * Creates a builder for CallHomeAuthorization with intent
      * to accept incoming connection and to provide credentials.
+     *
      * <p>
      * Note: If session with same sessionName is already opened and
      * active, incoming session will be rejected.
@@ -66,7 +69,7 @@ public abstract class CallHomeAuthorization {
      * @param username    Username to be used for authorization
      * @return Builder which allows to specify credentials.
      */
-    public static final Builder serverAccepted(String sessionName, String username) {
+    public static final Builder serverAccepted(final String sessionName, final String username) {
         return new Builder(sessionName, username);
     }
 
@@ -78,7 +81,7 @@ public abstract class CallHomeAuthorization {
     public abstract boolean isServerAllowed();
 
     /**
-     * Applies provided authentification to Mina SSH Client Session
+     * Applies provided authentification to Mina SSH Client Session.
      *
      * @param session Client Session to which authorization parameters will by applied
      */
@@ -88,6 +91,7 @@ public abstract class CallHomeAuthorization {
 
     /**
      * Builder for CallHomeAuthorization which accepts incoming connection.
+     *
      * <p>
      * Use {@link CallHomeAuthorization#serverAccepted(String, String)} to instantiate
      * builder.
@@ -96,12 +100,12 @@ public abstract class CallHomeAuthorization {
 
         private final String nodeId;
         private final String username;
-        private Set<String> passwords = new HashSet<>();
-        private Set<KeyPair> clientKeys = new HashSet<>();
+        private final Set<String> passwords = new HashSet<>();
+        private final Set<KeyPair> clientKeys = new HashSet<>();
 
-        private Builder(String nodeId, String username) {
-            this.nodeId = Preconditions.checkNotNull(nodeId);
-            this.username = Preconditions.checkNotNull(username);
+        Builder(final String nodeId, final String username) {
+            this.nodeId = requireNonNull(nodeId);
+            this.username = requireNonNull(username);
         }
 
         /**
@@ -110,7 +114,7 @@ public abstract class CallHomeAuthorization {
          * @param password Password to be used for password-based authorization.
          * @return this builder.
          */
-        public Builder addPassword(String password) {
+        public Builder addPassword(final String password) {
             this.passwords.add(password);
             return this;
         }
@@ -121,7 +125,7 @@ public abstract class CallHomeAuthorization {
          * @param clientKey Keys to be used for authorization.
          * @return this builder.
          */
-        public Builder addClientKeys(KeyPair clientKey) {
+        public Builder addClientKeys(final KeyPair clientKey) {
             this.clientKeys.add(clientKey);
             return this;
         }
@@ -140,11 +144,12 @@ public abstract class CallHomeAuthorization {
         private final Set<String> passwords;
         private final Set<KeyPair> clientKeyPair;
 
-        ServerAllowed(String nodeId, String username, Collection<String> passwords, Collection<KeyPair> clientKeyPairs) {
-            this.username = Preconditions.checkNotNull(username);
+        ServerAllowed(final String nodeId, final String username, final Collection<String> passwords,
+                      final Collection<KeyPair> clientKeyPairs) {
+            this.username = requireNonNull(username);
             this.passwords = ImmutableSet.copyOf(passwords);
             this.clientKeyPair = ImmutableSet.copyOf(clientKeyPairs);
-            this.nodeId = Preconditions.checkNotNull(nodeId);
+            this.nodeId = requireNonNull(nodeId);
         }
 
         @Override
@@ -158,9 +163,8 @@ public abstract class CallHomeAuthorization {
         }
 
         @Override
-        protected void applyTo(ClientSession session) {
-            Preconditions.checkArgument(session instanceof ClientSessionImpl);
-            ((ClientSessionImpl) session).setUsername(username);
+        protected void applyTo(final ClientSession session) {
+            session.setUsername(username);
 
             // First try authentication using server host keys, else try password.
             for (KeyPair keyPair : clientKeyPair) {