Bump upstreams to SNAPSHOTs
[netconf.git] / netconf / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / mount / CallHomeMountSessionContext.java
index 42d6aab1498af109103da1f69114f8e4ad2ba51d..69ab5cd533e73ad3b5641752730032abd3454445 100644 (file)
@@ -5,13 +5,12 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.netconf.callhome.mount;
 
-import com.google.common.base.Preconditions;
-import io.netty.util.concurrent.Promise;
-import java.net.InetSocketAddress;
-import java.security.PublicKey;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.MoreObjects;
+import io.netty.util.concurrent.Future;
 import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.NetconfTerminationReason;
 import org.opendaylight.netconf.callhome.protocol.CallHomeChannelActivator;
@@ -19,19 +18,22 @@ import org.opendaylight.netconf.callhome.protocol.CallHomeProtocolSessionContext
 import org.opendaylight.netconf.client.NetconfClientSession;
 import org.opendaylight.netconf.client.NetconfClientSessionListener;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.Protocol;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.ProtocolBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
+import org.opendaylight.yangtools.yang.common.Decimal64;
+import org.opendaylight.yangtools.yang.common.Uint16;
+import org.opendaylight.yangtools.yang.common.Uint32;
 
+// Non-final to allow mocking
 class CallHomeMountSessionContext {
-
-    public interface CloseCallback {
-
+    @FunctionalInterface
+    interface CloseCallback {
         void onClosed(CallHomeMountSessionContext deviceContext);
-
     }
 
     private final NodeId nodeId;
@@ -41,57 +43,88 @@ class CallHomeMountSessionContext {
     // FIXME: Remove this
     private final ContextKey key;
 
-    CallHomeMountSessionContext(String nodeId, CallHomeProtocolSessionContext protocol,
-            CallHomeChannelActivator activator, CloseCallback callback) {
+    CallHomeMountSessionContext(final String nodeId, final CallHomeProtocolSessionContext protocol,
+                                final CallHomeChannelActivator activator, final CloseCallback callback) {
+
+        this.nodeId = new NodeId(requireNonNull(nodeId, "nodeId"));
+        key = ContextKey.from(protocol.getRemoteAddress());
+        this.protocol = requireNonNull(protocol, "protocol");
+        this.activator = requireNonNull(activator, "activator");
+        onClose = requireNonNull(callback, "callback");
+    }
 
-        this.nodeId = new NodeId(Preconditions.checkNotNull(nodeId, "nodeId"));
-        this.key = ContextKey.from(protocol.getRemoteAddress());
-        this.protocol = Preconditions.checkNotNull(protocol, "protocol");
-        this.activator = Preconditions.checkNotNull(activator, "activator");
-        this.onClose = Preconditions.checkNotNull(callback, "callback");
+    CallHomeProtocolSessionContext getProtocol() {
+        return protocol;
     }
 
     NodeId getId() {
         return nodeId;
     }
 
-    public ContextKey getContextKey() {
+    ContextKey getContextKey() {
         return key;
     }
 
+    /**
+     * Create device default configuration.
+     *
+     * <p>
+     * This configuration is a replacement of configuration device data
+     * which is normally stored in configuration datastore but is absent for call-home devices.
+     *
+     * @return {@link Node} containing the default device configuration
+     */
+    // FIXME make these defaults tune-able in odl-netconf-callhome-server
     Node getConfigNode() {
-        NodeBuilder builder = new NodeBuilder();
-
-        return builder.setNodeId(getId()).addAugmentation(NetconfNode.class, configNetconfNode()).build();
-
+        return new NodeBuilder()
+                .setNodeId(getId())
+                .addAugmentation(new NetconfNodeBuilder()
+                        .setHost(new Host(key.getIpAddress()))
+                        .setPort(key.getPort())
+                        .setTcpOnly(false)
+                        .setProtocol(new ProtocolBuilder()
+                                .setName(Protocol.Name.valueOf(protocol.getTransportType().name()))
+                                .build())
+                        .setSchemaless(false)
+                        .setReconnectOnChangedSchema(false)
+                        .setConnectionTimeoutMillis(Uint32.valueOf(20000))
+                        .setDefaultRequestTimeoutMillis(Uint32.valueOf(60000))
+                        .setMaxConnectionAttempts(Uint32.ZERO)
+                        .setBetweenAttemptsTimeoutMillis(Uint16.valueOf(2000))
+                        .setSleepFactor(Decimal64.valueOf("1.5"))
+                        .setKeepaliveDelay(Uint32.valueOf(120))
+                        .setConcurrentRpcLimit(Uint16.ZERO)
+                        .setActorResponseWaitTime(Uint16.valueOf(5))
+                        // the real call-home device credentials are applied in CallHomeAuthProviderImpl
+                        .setCredentials(new LoginPasswordBuilder()
+                                .setUsername("omitted")
+                                .setPassword("omitted")
+                                .build())
+                    .build())
+                .build();
     }
 
-    private NetconfNode configNetconfNode() {
-        NetconfNodeBuilder node = new NetconfNodeBuilder();
-        node.setHost(new Host(key.getIpAddress()));
-        node.setPort(key.getPort());
-        node.setTcpOnly(Boolean.FALSE);
-        node.setCredentials(new LoginPasswordBuilder().setUsername("ommited").setPassword("ommited").build());
-        node.setSchemaless(Boolean.FALSE);
-        return node.build();
+    Future<NetconfClientSession> activateNetconfChannel(final NetconfClientSessionListener sessionListener) {
+        return activator.activate(wrap(sessionListener));
     }
 
-    @SuppressWarnings("unchecked")
-     <V> Promise<V> activateNetconfChannel(NetconfClientSessionListener sessionListener) {
-        return (Promise<V>) activator.activate(wrap(sessionListener));
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("address", protocol.getRemoteAddress())
+                .add("hostKey", protocol.getRemoteServerKey())
+                .toString();
     }
 
-    @SuppressWarnings("deprecation")
     private NetconfClientSessionListener wrap(final NetconfClientSessionListener delegate) {
         return new NetconfClientSessionListener() {
-
             @Override
-            public void onSessionUp(NetconfClientSession session) {
+            public void onSessionUp(final NetconfClientSession session) {
                 delegate.onSessionUp(session);
             }
 
             @Override
-            public void onSessionTerminated(NetconfClientSession session, NetconfTerminationReason reason) {
+            public void onSessionTerminated(final NetconfClientSession session, final NetconfTerminationReason reason) {
                 try {
                     delegate.onSessionTerminated(session, reason);
                 } finally {
@@ -100,16 +133,16 @@ class CallHomeMountSessionContext {
             }
 
             @Override
-            public void onSessionDown(NetconfClientSession session, Exception e) {
+            public void onSessionDown(final NetconfClientSession session, final Exception exc) {
                 try {
                     removeSelf();
                 } finally {
-                    delegate.onSessionDown(session, e);
+                    delegate.onSessionDown(session, exc);
                 }
             }
 
             @Override
-            public void onMessage(NetconfClientSession session, NetconfMessage message) {
+            public void onMessage(final NetconfClientSession session, final NetconfMessage message) {
                 delegate.onMessage(session, message);
             }
         };
@@ -118,13 +151,4 @@ class CallHomeMountSessionContext {
     private void removeSelf() {
         onClose.onClosed(this);
     }
-
-    InetSocketAddress getRemoteAddress() {
-        return protocol.getRemoteAddress();
-    }
-
-    PublicKey getRemoteServerKey() {
-        return protocol.getRemoteServerKey();
-    }
-
 }