add lib-plugin interaction implementations on library side 24/1224/1
authorMichal Rehak <mirehak@cisco.com>
Thu, 12 Sep 2013 20:37:27 +0000 (22:37 +0200)
committerMichal Rehak <mirehak@cisco.com>
Tue, 17 Sep 2013 14:52:01 +0000 (16:52 +0200)
- startup, shutdown of multiple servers creates chained future result
- update yangtool version to 0.5.8-SNAPSHOT

Change-Id: Id8f2fe0f5a4050592d62af2c5b416574150b6f9d
Signed-off-by: Michal Rehak <mirehak@cisco.com>
14 files changed:
openflow-protocol-api/pom.xml
openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/connection/ConnectionConfiguration.java [new file with mode: 0644]
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/ConnectionAdapterImpl.java
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/MessageConsumer.java [new file with mode: 0644]
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/OnlineProvider.java [new file with mode: 0644]
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/RpcResponseKey.java [new file with mode: 0644]
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/ServerFacade.java [new file with mode: 0644]
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/ShutdownProvider.java [new file with mode: 0644]
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImpl.java [new file with mode: 0644]
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/PublishingChannelInitializer.java
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/TcpHandler.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/TcpHandlerTest.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/core/TlsDetectorTest.java
openflow-protocol-spi/src/main/java/org/opendaylight/openflowjava/protocol/spi/connection/SwitchConnectionProvider.java

index 63757a7749c34f96291201f53ffe9a1f1ab720a5..65374963bf59d81e5de587ca6eaae7ec9889508a 100644 (file)
@@ -8,13 +8,17 @@
        </parent>\r
        <artifactId>openflow-protocol-api</artifactId>\r
        <name>Openflow Protocol Library - API</name>\r
+    \r
+    <properties>\r
+        <yangtools.version>0.5.8-SNAPSHOT</yangtools.version>\r
+    </properties>\r
 \r
        <build>\r
         <plugins>\r
             <plugin>\r
                 <groupId>org.opendaylight.yangtools</groupId>\r
                 <artifactId>yang-maven-plugin</artifactId>\r
-                <version>0.5.7-SNAPSHOT</version>\r
+                <version>${yangtools.version}</version>\r
                 <executions>\r
                     <execution>\r
                         <goals>\r
@@ -41,7 +45,7 @@
                     <dependency>\r
                         <groupId>org.opendaylight.yangtools</groupId>\r
                         <artifactId>maven-sal-api-gen-plugin</artifactId>\r
-                        <version>0.5.7-SNAPSHOT</version>\r
+                        <version>${yangtools.version}</version>\r
                         <type>jar</type>\r
                     </dependency>\r
                 </dependencies>\r
         <dependency>\r
             <groupId>org.opendaylight.yangtools</groupId>\r
             <artifactId>yang-binding</artifactId>\r
-            <version>0.5.7-SNAPSHOT</version>\r
+            <version>${yangtools.version}</version>\r
         </dependency>\r
         <dependency>\r
             <groupId>org.opendaylight.yangtools</groupId>\r
             <artifactId>yang-common</artifactId>\r
-            <version>0.5.7-SNAPSHOT</version>\r
+            <version>${yangtools.version}</version>\r
         </dependency>\r
         <dependency>\r
             <groupId>org.opendaylight.yangtools.model</groupId>\r
diff --git a/openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/connection/ConnectionConfiguration.java b/openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/connection/ConnectionConfiguration.java
new file mode 100644 (file)
index 0000000..16ffde3
--- /dev/null
@@ -0,0 +1,51 @@
+/**
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.openflowjava.protocol.api.connection;
+
+import java.net.InetAddress;
+
+/**
+ * @author mirehak
+ *
+ */
+public interface ConnectionConfiguration {
+    
+    /**
+     * connection functionality support types
+     */
+    public enum FEATURE_SUPPORT {
+        /** feature is not supported at all */
+        NOT_SUPPORTED,
+        /** feature is supported */
+        SUPPORTED,
+        /** feature is supported and has to be used by clients */
+        REQUIRED
+    }
+    
+    /**
+     * @return address to bind, if null, all available interfaces will be used
+     */
+    public InetAddress getAddress();
+    
+    /**
+     * @return port to bind
+     */
+    public int getPort();
+    
+    /**
+     * @return transport protocol to use
+     */
+    public Object getTransferProtocol();
+    
+    /**
+     * @return encryption feature support
+     */
+    public FEATURE_SUPPORT getTlsSupport();
+
+}
index 0423d25bffcb2d763e6b9ff4202ac88decafb06b..5f670132de9812d8ae5d1c1beb34cb03a77085bb 100644 (file)
@@ -14,6 +14,7 @@ import io.netty.util.concurrent.GenericFutureListener;
 
 import java.util.Collection;
 import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
 
 import org.opendaylight.controller.sal.common.util.RpcErrors;
 import org.opendaylight.controller.sal.common.util.Rpcs;
@@ -23,8 +24,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
@@ -35,20 +40,34 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.Notification;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.RemovalNotification;
 import com.google.common.collect.Lists;
 import com.google.common.util.concurrent.SettableFuture;
 
@@ -56,12 +75,39 @@ import com.google.common.util.concurrent.SettableFuture;
  * @author mirehak
  *
  */
-public class ConnectionAdapterImpl implements ConnectionAdapter {
+public class ConnectionAdapterImpl implements ConnectionAdapter, MessageConsumer {
+    
+    /** after this time, rpc future response objects will be thrown away (in minutes) */
+    public static final int RPC_RESPONSE_EXPIRATION = 1;
+
+    protected static final Logger LOG = LoggerFactory
+            .getLogger(ConnectionAdapterImpl.class);
     
     private static final String APPLICATION_TAG = "OPENFLOW_LIBRARY";
     private static final String TAG = "OPENFLOW";
     private Channel channel;
     private OpenflowProtocolListener messageListener;
+    /** expiring cache for future rpcResponses */
+    protected Cache<RpcResponseKey, SettableFuture<?>> responseCache;
+    
+    
+    /**
+     * default ctor 
+     */
+    public ConnectionAdapterImpl() {
+        responseCache = CacheBuilder.newBuilder()
+                .concurrencyLevel(1)
+                .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
+                .removalListener(new RemovalListener<RpcResponseKey, SettableFuture<?>>() {
+
+                    @Override
+                    public void onRemoval(
+                            RemovalNotification<RpcResponseKey, SettableFuture<?>> notification) {
+                        LOG.warn("rpc response discarded: "+notification.getKey());
+                        notification.getValue().cancel(true);
+                    }
+                }).build();
+    }
     
     /**
      * @param channel the channel to set
@@ -72,118 +118,102 @@ public class ConnectionAdapterImpl implements ConnectionAdapter {
 
     @Override
     public Future<RpcResult<BarrierOutput>> barrier(BarrierInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchExpectRpcResultFuture(
+                input, BarrierOutput.class, "barrier-input sending failed");
     }
 
     @Override
     public Future<RpcResult<EchoOutput>> echo(EchoInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchExpectRpcResultFuture(
+                input, EchoOutput.class, "echo-input sending failed");
     }
 
     @Override
     public Future<RpcResult<Void>> echoReply(EchoReplyInput input) {
-        return sendToSwitchFuture(input, "echo reply sending failed");
+        return sendToSwitchFuture(input, "echo-reply sending failed");
     }
 
     @Override
     public Future<RpcResult<Void>> experimenter(ExperimenterInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchFuture(input, "experimenter sending failed");
     }
 
     @Override
     public Future<RpcResult<Void>> flowMod(FlowModInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchFuture(input, "flow-mod sending failed");
     }
 
     @Override
     public Future<RpcResult<GetConfigOutput>> getConfig(GetConfigInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchExpectRpcResultFuture(
+                input, GetConfigOutput.class, "get-config-input sending failed");
     }
 
     @Override
     public Future<RpcResult<GetFeaturesOutput>> getFeatures(
             GetFeaturesInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchExpectRpcResultFuture(
+                input, GetFeaturesOutput.class, "get-features-input sending failed");
     }
 
     @Override
     public Future<RpcResult<GetQueueConfigOutput>> getQueueConfig(
             GetQueueConfigInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchExpectRpcResultFuture(
+                input, GetQueueConfigOutput.class, "get-queue-config-input sending failed");
     }
 
     @Override
     public Future<RpcResult<Void>> groupMod(GroupModInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchFuture(input, "group-mod-input sending failed");
     }
 
     @Override
     public Future<RpcResult<Void>> hello(HelloInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchFuture(input, "hello-input sending failed");
     }
 
     @Override
     public Future<RpcResult<Void>> meterMod(MeterModInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchFuture(input, "meter-mod-input sending failed");
     }
 
     @Override
     public Future<RpcResult<Void>> packetOut(PacketOutInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchFuture(input, "packet-out-input sending failed");
     }
 
     @Override
     public Future<RpcResult<Void>> portMod(PortModInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchFuture(input, "port-mod-input sending failed");
     }
 
     @Override
     public Future<RpcResult<RoleRequestOutput>> roleRequest(
             RoleRequestInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchExpectRpcResultFuture(
+                input, RoleRequestOutput.class, "role-request-config-input sending failed");
     }
 
     @Override
     public Future<RpcResult<Void>> setConfig(SetConfigInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchFuture(input, "set-config-input sending failed");
     }
 
     @Override
     public Future<RpcResult<Void>> tableMod(TableModInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchFuture(input, "table-mod-input sending failed");
     }
 
-    /* (non-Javadoc)
-     * @see org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolService#getAsync(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput)
-     */
     @Override
     public Future<RpcResult<GetAsyncOutput>> getAsync(GetAsyncInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchExpectRpcResultFuture(
+                input, GetAsyncOutput.class, "get-async-input sending failed");
     }
 
-    /* (non-Javadoc)
-     * @see org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolService#setAsync(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput)
-     */
     @Override
     public Future<RpcResult<Void>> setAsync(SetAsyncInput input) {
-        // TODO Auto-generated method stub
-        return null;
+        return sendToSwitchFuture(input, "set-async-input sending failed");
     }
 
     @Override
@@ -205,18 +235,90 @@ public class ConnectionAdapterImpl implements ConnectionAdapter {
     public void setMessageListener(OpenflowProtocolListener messageListener) {
         this.messageListener = messageListener;
     }
+    
+    @Override
+    public void consume(DataObject message) {
+        if (message instanceof Notification) {
+            if (message instanceof EchoRequestMessage) {
+                messageListener.onEchoRequestMessage((EchoRequestMessage) message);
+            } else if (message instanceof ErrorMessage) {
+                messageListener.onErrorMessage((ErrorMessage) message);
+            } else if (message instanceof ExperimenterMessage) {
+                messageListener.onExperimenterMessage((ExperimenterMessage) message);
+            } else if (message instanceof FlowRemovedMessage) {
+                messageListener.onFlowRemovedMessage((FlowRemovedMessage) message);
+            } else if (message instanceof HelloMessage) {
+                messageListener.onHelloMessage((HelloMessage) message);
+            } else if (message instanceof MultipartReplyMessage) {
+                messageListener.onMultipartReplyMessage((MultipartReplyMessage) message);
+            } else if (message instanceof MultipartRequestMessage) {
+                messageListener.onMultipartRequestMessage((MultipartRequestMessage) message);
+            } else if (message instanceof PacketInMessage) {
+                messageListener.onPacketInMessage((PacketInMessage) message);
+            } else if (message instanceof PortStatusMessage) {
+                messageListener.onPortStatusMessage((PortStatusMessage) message);
+            } else {
+                LOG.warn("message listening not supported for type: "+message.getClass());
+            }
+        } else {
+            if (message instanceof OfHeader) {
+                RpcResponseKey key = createRpcResponseKey((OfHeader) message);
+                SettableFuture<RpcResult<?>> rpcFuture = findRpcResponse(key);
+                if (rpcFuture != null) {
+                    rpcFuture.set(Rpcs.getRpcResult(true, message, null));
+                    responseCache.invalidate(key);
+                } else {
+                    LOG.warn("received unexpected rpc response: "+key);
+                }
+                
+            } else {
+                LOG.warn("message listening not supported for type: "+message.getClass());
+            }
+        }
+    }
 
     /**
-     * @param input
-     * @return
+     * sends given message to switch, sending result will be reported via return value
+     * @param input message to send
+     * @param failureInfo describes, what type of message caused failure by sending 
+     * @return future object, <ul>
+     *  <li>if send successful, {@link RpcResult} without errors and successful 
+     *  status will be returned, </li>
+     *  <li>else {@link RpcResult} will contain errors and failed status</li>
+     *  </ul>    
      */
     private SettableFuture<RpcResult<Void>> sendToSwitchFuture(
-            EchoReplyInput input, final String failureInfo) {
+            DataObject input, final String failureInfo) {
         ChannelFuture resultFuture = channel.writeAndFlush(input);
         
         ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
-        String message = "check switch connection";
-        return handleRpcChannelFuture(resultFuture, failureInfo, errorSeverity, message);
+        String errorMessage = "check switch connection";
+        return handleRpcChannelFuture(resultFuture, failureInfo, errorSeverity, errorMessage);
+    }
+    
+    /**
+     * sends given message to switch, sending result or switch response will be reported via return value
+     * @param input message to send
+     * @param responseClazz type of response
+     * @param failureInfo describes, what type of message caused failure by sending 
+     * @return future object, <ul>
+     *  <li>if send fails, {@link RpcResult} will contain errors and failed status </li>
+     *  <li>else {@link RpcResult} will be stored in responseCache and wait for particular timeout 
+     *  ({@link ConnectionAdapterImpl#RPC_RESPONSE_EXPIRATION}), 
+     *  <ul><li>either switch will manage to answer
+     *  and then corresponding response message will be set into returned future</li>
+     *  <li>or response in cache will expire and returned future will be cancelled</li></ul>
+     *  </li>
+     *  </ul>     
+     */
+    private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> sendToSwitchExpectRpcResultFuture(
+            IN input, Class<OUT> responseClazz, final String failureInfo) {
+        ChannelFuture resultFuture = channel.writeAndFlush(input);
+        
+        ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
+        String errorMessage = "check switch connection";
+        return handleRpcChannelFutureWithResponse(resultFuture, failureInfo, errorSeverity, 
+                errorMessage, input, responseClazz);
     }
 
     /**
@@ -226,7 +328,7 @@ public class ConnectionAdapterImpl implements ConnectionAdapter {
      */
     private SettableFuture<RpcResult<Void>> handleRpcChannelFuture(
             ChannelFuture resultFuture, final String failureInfo, 
-            final ErrorSeverity errorSeverity, final String message) {
+            final ErrorSeverity errorSeverity, final String errorMessage) {
         
         final SettableFuture<RpcResult<Void>> rpcResult = SettableFuture.create();
         
@@ -240,7 +342,7 @@ public class ConnectionAdapterImpl implements ConnectionAdapter {
                 
                 if (future.cause() != null) {
                     RpcError rpcError = buildRpcError(failureInfo, 
-                            errorSeverity, message, future.cause());
+                            errorSeverity, errorMessage, future.cause());
                     errors = Lists.newArrayList(rpcError);
                 }
                 
@@ -254,12 +356,58 @@ public class ConnectionAdapterImpl implements ConnectionAdapter {
         return rpcResult;
     }
     
+    /**
+     * @param input
+     * @param responseClazz
+     * @param resultFuture
+     * @param failureInfo
+     * @param errorSeverity
+     * @param errorMessage
+     * @return
+     */
+    private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> handleRpcChannelFutureWithResponse(
+            ChannelFuture resultFuture, final String failureInfo,
+            final ErrorSeverity errorSeverity, final String errorMessage,
+            final IN input, Class<OUT> responseClazz) {
+        final SettableFuture<RpcResult<OUT>> rpcResult = SettableFuture.create();
+        
+        resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
+            
+            @Override
+            public void operationComplete(
+                    io.netty.util.concurrent.Future<? super Void> future)
+                    throws Exception {
+                
+                if (future.cause() != null) {
+                    Collection<RpcError> errors = null;
+                    RpcError rpcError = buildRpcError(failureInfo, 
+                            errorSeverity, errorMessage, future.cause());
+                    errors = Lists.newArrayList(rpcError);
+                    rpcResult.set(Rpcs.getRpcResult(
+                            future.isSuccess(), 
+                            (OUT) null, 
+                            errors)
+                            );
+                } else {
+                    RpcResponseKey key = new RpcResponseKey(input.getXid(), input.getClass().toString());
+                    if (responseCache.getIfPresent(key) != null) {
+                        responseCache.invalidate(key);
+                    }
+                    responseCache.put(key, rpcResult);
+                }
+            }
+        });
+        return rpcResult;
+    }
+
     /**
      * @param resultFuture
      * @param failureInfo
+     * @param errorSeverity 
+     * @param message 
      * @return
      */
-    private SettableFuture<Boolean> handleTransportChannelFuture(
+    private static SettableFuture<Boolean> handleTransportChannelFuture(
             ChannelFuture resultFuture, final String failureInfo, 
             final ErrorSeverity errorSeverity, final String message) {
         
@@ -272,6 +420,7 @@ public class ConnectionAdapterImpl implements ConnectionAdapter {
                     io.netty.util.concurrent.Future<? super Void> future)
                     throws Exception {
                 transportResult.set(future.isSuccess());
+                transportResult.setException(future.cause());
             }
         });
         return transportResult;
@@ -299,4 +448,20 @@ public class ConnectionAdapterImpl implements ConnectionAdapter {
         return error;
     }
 
+    /**
+     * @param message
+     * @return
+     */
+    private static RpcResponseKey createRpcResponseKey(OfHeader message) {
+        return new RpcResponseKey(message.getXid(), message.getClass().toString());
+    }
+
+    /**
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    private SettableFuture<RpcResult<?>> findRpcResponse(RpcResponseKey key) {
+        return (SettableFuture<RpcResult<?>>) responseCache.getIfPresent(key);
+    }
+
 }
diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/MessageConsumer.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/MessageConsumer.java
new file mode 100644 (file)
index 0000000..ca6d7f7
--- /dev/null
@@ -0,0 +1,23 @@
+/**
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.openflowjava.protocol.impl.connection;
+
+import org.opendaylight.yangtools.yang.binding.DataObject;
+
+/**
+ * @author mirehak
+ *
+ */
+public interface MessageConsumer {
+
+    /**
+     * @param message to process
+     */
+    public void consume(DataObject message);
+}
diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/OnlineProvider.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/OnlineProvider.java
new file mode 100644 (file)
index 0000000..dc68ca6
--- /dev/null
@@ -0,0 +1,24 @@
+/**
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.openflowjava.protocol.impl.connection;
+
+import com.google.common.util.concurrent.ListenableFuture;
+
+/**
+ * @author mirehak
+ *
+ */
+public interface OnlineProvider {
+
+    /**
+     * @return the isOnlineFuture
+     */
+    public ListenableFuture<Boolean> getIsOnlineFuture();
+
+}
diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/RpcResponseKey.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/RpcResponseKey.java
new file mode 100644 (file)
index 0000000..1ef1df0
--- /dev/null
@@ -0,0 +1,79 @@
+/**
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.openflowjava.protocol.impl.connection;
+
+
+/**
+ * @author mirehak
+ *
+ */
+public class RpcResponseKey {
+    
+    private final long xid;
+    private final String outputClazz;
+    /**
+     * @param xid
+     * @param outputClazz
+     */
+    public RpcResponseKey(long xid, String outputClazz) {
+        super();
+        this.xid = xid;
+        this.outputClazz = outputClazz;
+    }
+    
+    /**
+     * @return the xid
+     */
+    public long getXid() {
+        return xid;
+    }
+
+    /**
+     * @return the outputClazz
+     */
+    public String getOutputClazz() {
+        return outputClazz;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result
+                + ((outputClazz == null) ? 0 : outputClazz.hashCode());
+        result = prime * result + (int) (xid ^ (xid >>> 32));
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        RpcResponseKey other = (RpcResponseKey) obj;
+        if (outputClazz == null) {
+            if (other.outputClazz != null)
+                return false;
+        } else if (!outputClazz.equals(other.outputClazz))
+            return false;
+        if (xid != other.xid)
+            return false;
+        return true;
+    }
+
+    @Override
+    public String toString() {
+        return "RpcResultKey [xid=" + xid + ", outputClazz=" + outputClazz
+                + "]";
+    }
+
+}
diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/ServerFacade.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/ServerFacade.java
new file mode 100644 (file)
index 0000000..108e53a
--- /dev/null
@@ -0,0 +1,18 @@
+/**
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.openflowjava.protocol.impl.connection;
+
+/**
+ * @author mirehak
+ *
+ */
+public interface ServerFacade extends ShutdownProvider, OnlineProvider, Runnable {
+
+    // empty unifying superinterface
+}
diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/ShutdownProvider.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/ShutdownProvider.java
new file mode 100644 (file)
index 0000000..1be047a
--- /dev/null
@@ -0,0 +1,25 @@
+/**
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.openflowjava.protocol.impl.connection;
+
+import com.google.common.util.concurrent.ListenableFuture;
+
+
+/**
+ * @author mirehak
+ *
+ */
+public interface ShutdownProvider {
+
+    /**
+     * @return shutdown future 
+     */
+    public ListenableFuture<Boolean> shutdown();
+
+}
diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImpl.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/connection/SwitchConnectionProviderImpl.java
new file mode 100644 (file)
index 0000000..858abfa
--- /dev/null
@@ -0,0 +1,94 @@
+/**
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.openflowjava.protocol.impl.connection;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.Future;
+
+import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
+import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
+import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
+import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
+
+/**
+ * @author mirehak
+ *
+ */
+public class SwitchConnectionProviderImpl implements SwitchConnectionProvider {
+    
+    private static final Logger LOG = LoggerFactory
+            .getLogger(SwitchConnectionProviderImpl.class);
+    private SwitchConnectionHandler switchConnectionHandler;
+    private Set<ServerFacade> serverLot;
+
+    @Override
+    public void configure(Collection<ConnectionConfiguration> connConfigs) {
+        LOG.debug("configurating ..");
+
+        //TODO - add and configure servers according to configuration
+        serverLot = new HashSet<>();
+        serverLot.add(new TcpHandler(6633));
+    }
+
+    @Override
+    public void setSwitchConnectionListener(SwitchConnectionHandler switchConnectionHandler) {
+        this.switchConnectionHandler = switchConnectionHandler;
+    }
+
+    @Override
+    public Future<Boolean> shutdown() {
+        LOG.debug("shutdown summoned");
+        for (ServerFacade server : serverLot) {
+            server.shutdown();
+        }
+        return null;
+    }
+
+    @Override
+    public Future<List<Boolean>> startup() {
+        LOG.debug("startup summoned");
+        ListenableFuture<List<Boolean>> result = SettableFuture.create();
+        try {
+            //TODO - check config, status of servers
+            if (switchConnectionHandler == null) {
+                throw new IllegalStateException("switchConnectionHandler is not set");
+            }
+            
+            // starting
+            List<ListenableFuture<Boolean>> starterChain = new ArrayList<>();
+            for (ServerFacade server : serverLot) {
+                new Thread(server).start();
+                ListenableFuture<Boolean> isOnlineFuture = server.getIsOnlineFuture();
+                starterChain.add(isOnlineFuture);
+            }
+            
+            if (!starterChain.isEmpty()) {
+                result = Futures.allAsList(starterChain);
+            } else {
+                throw new IllegalStateException("no servers configured");
+            }
+        } catch (Exception e) {
+            SettableFuture<List<Boolean>> exFuture = SettableFuture.create();
+            exFuture.setException(e);
+            result = exFuture;
+        }
+        return result;
+    }
+
+}
index 0cf8a5bd6f54978d3831616eedec653fbf1bcec7..5f339fda0868cc41c5735c52d5742d9742057151 100644 (file)
@@ -3,18 +3,22 @@ package org.opendaylight.openflowjava.protocol.impl.core;
 import io.netty.channel.Channel;\r
 import io.netty.channel.ChannelInitializer;\r
 import io.netty.channel.group.DefaultChannelGroup;\r
+import io.netty.channel.socket.SocketChannel;\r
 \r
 import java.util.Iterator;\r
 \r
+import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;\r
+import org.opendaylight.openflowjava.protocol.impl.connection.ConnectionAdapterFactory;\r
 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler.COMPONENT_NAMES;\r
 \r
 /**\r
  * @author michal.polkorab\r
  *\r
  */\r
-public class PublishingChannelInitializer extends ChannelInitializer<Channel> {\r
+public class PublishingChannelInitializer extends ChannelInitializer<SocketChannel> {\r
 \r
     private DefaultChannelGroup allChannels;\r
+    private SwitchConnectionHandler switchConnectionHandler;\r
     \r
     /**\r
      * default ctor\r
@@ -24,9 +28,15 @@ public class PublishingChannelInitializer extends ChannelInitializer<Channel> {
     }\r
     \r
     @Override\r
-    protected void initChannel(Channel ch) throws Exception {\r
+    protected void initChannel(SocketChannel ch) throws Exception {\r
         allChannels.add(ch);\r
+        //TODO - create inBoundHandler\r
+        if (switchConnectionHandler != null) {\r
+            switchConnectionHandler.onSwitchConnected(ConnectionAdapterFactory.createConnectionAdapter(ch));\r
+            //TODO - check OpenflowProtocolListener, set it to inBoundHandler\r
+        }\r
         ch.pipeline().addLast(COMPONENT_NAMES.TLS_DETECTOR.name(), new TlsDetector());\r
+        //TODO - chain inBoundHandler to pipe\r
     }\r
     \r
     /**\r
@@ -43,6 +53,10 @@ public class PublishingChannelInitializer extends ChannelInitializer<Channel> {
         return allChannels.size();\r
     }\r
     \r
-    \r
-\r
+    /**\r
+     * @param switchListener the switchListener to set\r
+     */\r
+    public void setSwitchConnectionHandler(SwitchConnectionHandler switchListener) {\r
+        this.switchConnectionHandler = switchListener;\r
+    }\r
 }\r
index 5d342fdecff5daa416c151621193a9765ca1ab33..2d12c5c7657f1fd2fb7c2070282e2963ee4012e0 100644 (file)
@@ -8,12 +8,15 @@ import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
 import io.netty.handler.logging.LogLevel;
 import io.netty.handler.logging.LoggingHandler;
+import io.netty.util.concurrent.GenericFutureListener;
 
 import java.net.InetSocketAddress;
 
+import org.opendaylight.openflowjava.protocol.impl.connection.ServerFacade;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
 
 /**
@@ -21,7 +24,7 @@ import com.google.common.util.concurrent.SettableFuture;
  *
  * @author michal.polkorab
  */
-public class TcpHandler extends Thread {
+public class TcpHandler implements ServerFacade {
 
     private int port;
     private String address;
@@ -111,9 +114,22 @@ public class TcpHandler extends Thread {
     /**
      * Shuts down {@link TcpHandler}}
      */
-    public void shutdown() {
+    @Override
+    public ListenableFuture<Boolean> shutdown() {
+        final SettableFuture<Boolean> result = SettableFuture.create();
         workerGroup.shutdownGracefully();
-        bossGroup.shutdownGracefully();
+        // boss will shutdown as soon, as worker is down
+        bossGroup.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
+
+            @Override
+            public void operationComplete(
+                    io.netty.util.concurrent.Future<Object> downResult) throws Exception {
+                result.set(downResult.isSuccess());
+                result.setException(downResult.cause());
+            }
+            
+        });
+        return result;
     }
     
     /**
@@ -144,13 +160,11 @@ public class TcpHandler extends Thread {
         } else {
             port = 6633;
         }
-        new TcpHandler(port).start();
+        new Thread(new TcpHandler(port)).start();
     }
     
-    /**
-     * @return the isOnlineFuture
-     */
-    public SettableFuture<Boolean> getIsOnlineFuture() {
+    @Override
+    public ListenableFuture<Boolean> getIsOnlineFuture() {
         return isOnlineFuture;
     }
     
index 47b2a33415b56564ccea9bfb0be73a071fc8fc63..f06f0804ba5b22756f4ff7fe79612a7700baf641 100644 (file)
@@ -45,7 +45,7 @@ public class TcpHandlerTest {
     @Before
     public void setUp() throws InterruptedException, ExecutionException {
         tcphandler = new TcpHandler(0);
-        tcphandler.start();
+        new Thread(tcphandler).start();
         tcphandler.getIsOnlineFuture().get();
         port = tcphandler.getPort();
         address = tcphandler.getAddress();
@@ -53,10 +53,12 @@ public class TcpHandlerTest {
     
     /**
      * stop {@link TcpHandler}
+     * @throws ExecutionException 
+     * @throws InterruptedException 
      */
     @After
-    public void tearDown() {
-        tcphandler.shutdown();
+    public void tearDown() throws InterruptedException, ExecutionException {
+        tcphandler.shutdown().get();
     }
 
     /**
index 1279ef6b08bb352940711c85fbf6e79f6b948333..f0d941160d408e8f3aa49d6207beedc944559548 100644 (file)
@@ -7,8 +7,6 @@ import io.netty.channel.embedded.EmbeddedChannel;
 import org.junit.Assert;\r
 import org.junit.Before;\r
 import org.junit.Test;\r
-import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;\r
-import org.opendaylight.openflowjava.protocol.impl.core.TlsDetector;\r
 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler.COMPONENT_NAMES;\r
 \r
 /**\r
index c6f8ba48ad3bdbe7d2b1eb55799e8250c0c0d779..2d1a16caf4bc75ce6adfaa3b7726b0e71bb7f5fe 100644 (file)
@@ -8,8 +8,11 @@
 
 package org.opendaylight.openflowjava.protocol.spi.connection;
 
+import java.util.Collection;
+import java.util.List;
 import java.util.concurrent.Future;
 
+import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
 
 /**
@@ -19,17 +22,16 @@ import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHan
 public interface SwitchConnectionProvider {
 
     /**
-     * @param configuration contains protocols, ports, addresses and similar connection parameters
-     * TODO - create configuration interface proposal
+     * @param configurations list of [protocol, port, address and supported features]
      */
-    public void configure(Object configuration);
+    public void configure(Collection<ConnectionConfiguration> configurations);
     
     /**
      * start listening to switches, but please don't forget to do
      * {@link #setSwitchConnectionListener(SwitchConnectionHandler)} first
      * @return future, triggered to true, when all listening channels are up and running
      */
-    public Future<Boolean> startup();
+    public Future<List<Boolean>> startup();
     
     /**
      * stop listening to switches
@@ -37,7 +39,6 @@ public interface SwitchConnectionProvider {
      */
     public Future<Boolean> shutdown();
     
-    
     /**
      * @param switchConListener instance being informed when new switch connects
      */