1) Fixed minor issue caused by change in port statistics models
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / ConnectionConductorImpl.java
index b906117f6a3bd297af4e7502863fa213fcd1dda3..98e9037eb18f29ee5161f7be18816f2e9c7098d3 100644 (file)
 
 package org.opendaylight.openflowplugin.openflow.md.core;
 
-import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
-import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
 
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
+import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
+import org.opendaylight.openflowplugin.openflow.md.OFConstants;
 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
+import org.opendaylight.openflowplugin.openflow.md.core.session.PortFeaturesUtil;
+import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
+import org.opendaylight.openflowplugin.openflow.md.core.session.SessionManager;
+import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeper;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
 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.ExperimenterMessage;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
 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.MultipartRequestInputBuilder;
+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.Port;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatus;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestDescCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupFeaturesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterFeaturesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestPortDescCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.Futures;
 
 /**
  * @author mirehak
  */
 public class ConnectionConductorImpl implements OpenflowProtocolListener,
-        SystemNotificationsListener, ConnectionConductor {
+        SystemNotificationsListener, ConnectionConductor, ConnectionReadyListener, HandshakeListener {
 
-    private static final Logger LOG = LoggerFactory
+    protected static final Logger LOG = LoggerFactory
             .getLogger(ConnectionConductorImpl.class);
 
-    private LinkedBlockingQueue<Exception> errorQueue = new LinkedBlockingQueue<>();
+    /* variable to make BitMap-based negotiation enabled / disabled.
+     * it will help while testing and isolating issues related to processing of
+     * BitMaps from switches.
+     */
+    private boolean isBitmapNegotiationEnable = true;
+    protected ErrorHandler errorHandler;
 
     private final ConnectionAdapter connectionAdapter;
-    private final List<Short> versionOrder;
     private ConnectionConductor.CONDUCTOR_STATE conductorState;
     private Short version;
 
+    private SwitchConnectionDistinguisher auxiliaryKey;
+
+    private SessionContext sessionContext;
+
+    private QueueKeeper<OfHeader, DataObject> queueKeeper;
+    private ExecutorService hsPool;
+    private HandshakeManager handshakeManager;
+
+    private boolean firstHelloProcessed;
+    
+    private PortFeaturesUtil portFeaturesUtils;
+
     /**
      * @param connectionAdapter
      */
     public ConnectionConductorImpl(ConnectionAdapter connectionAdapter) {
         this.connectionAdapter = connectionAdapter;
         conductorState = CONDUCTOR_STATE.HANDSHAKING;
-        versionOrder = Lists.newArrayList((short) 0x04, (short) 0x01);
-        new Thread(new ErrorQueueHandler(errorQueue)).start();
+        hsPool = Executors.newFixedThreadPool(1);
+        firstHelloProcessed = false;
+        handshakeManager = new HandshakeManagerImpl(connectionAdapter,
+                ConnectionConductor.versionOrder.get(0), ConnectionConductor.versionOrder);
+        handshakeManager.setUseVersionBitmap(isBitmapNegotiationEnable);
+        handshakeManager.setHandshakeListener(this);
+        portFeaturesUtils = PortFeaturesUtil.getInstance();
     }
 
     @Override
     public void init() {
         connectionAdapter.setMessageListener(this);
         connectionAdapter.setSystemListener(this);
+        connectionAdapter.setConnectionReadyListener(this);
     }
 
     @Override
-    public void onEchoRequestMessage(EchoRequestMessage echoRequestMessage) {
-        LOG.debug("echo request received: " + echoRequestMessage.getXid());
-        EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
-        builder.setVersion(echoRequestMessage.getVersion());
-        builder.setXid(echoRequestMessage.getXid());
-        builder.setData(echoRequestMessage.getData());
+    public void setQueueKeeper(QueueKeeper<OfHeader, DataObject> queueKeeper) {
+        this.queueKeeper = queueKeeper;
+    }
 
-        connectionAdapter.echoReply(builder.build());
+    /**
+     * @param errorHandler the errorHandler to set
+     */
+    @Override
+    public void setErrorHandler(ErrorHandler errorHandler) {
+        this.errorHandler = errorHandler;
+        handshakeManager.setErrorHandler(errorHandler);
+    }
+
+    @Override
+    public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                LOG.debug("echo request received: " + echoRequestMessage.getXid());
+                EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
+                builder.setVersion(echoRequestMessage.getVersion());
+                builder.setXid(echoRequestMessage.getXid());
+                builder.setData(echoRequestMessage.getData());
+
+                getConnectionAdapter().echoReply(builder.build());
+            }
+        }).start();
     }
 
     @Override
     public void onErrorMessage(ErrorMessage errorMessage) {
-        // TODO Auto-generated method stub
-        LOG.debug("error received, type: " + errorMessage.getType()
-                + "; code: " + errorMessage.getCode());
+        queueKeeper.push(errorMessage, this);
     }
 
     @Override
     public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
-        // TODO Auto-generated method stub
-        LOG.debug("experimenter received, type: "
-                + experimenterMessage.getExpType());
+        queueKeeper.push(experimenterMessage, this);
     }
 
     @Override
-    public void onFlowRemovedMessage(FlowRemovedMessage arg0) {
-        // TODO Auto-generated method stub
+    public void onFlowRemovedMessage(FlowRemovedMessage message) {
+        queueKeeper.push(message, this);
     }
 
+
+    /**
+     * version negotiation happened as per following steps:
+     * 1. If HelloMessage version field has same version, continue connection processing.
+     *    If HelloMessage version is lower than supported versions, just disconnect.
+     * 2. If HelloMessage contains bitmap and common version found in bitmap
+     *    then continue connection processing. if no common version found, just disconnect.
+     * 3. If HelloMessage version is not supported, send HelloMessage with lower supported version.
+     * 4. If Hello message received again with not supported version, just disconnect.
+     *
+     *   TODO: Better to handle handshake into a maintainable innerclass which uses State-Pattern.
+     */
     @Override
-    public void onHelloMessage(HelloMessage hello) {
-        // do handshake
-        LOG.info("handshake STARTED");
+    public synchronized void onHelloMessage(final HelloMessage hello) {
+        LOG.debug("processing HELLO.xid: {}", hello.getXid());
+        firstHelloProcessed = true;
         checkState(CONDUCTOR_STATE.HANDSHAKING);
-
-        Short remoteVersion = hello.getVersion();
-        short proposedVersion;
-        try {
-            proposedVersion = proposeVersion(remoteVersion);
-        } catch (Exception e) {
-            handleException(e);
-            throw e;
-        }
-        HelloInputBuilder helloBuilder = new HelloInputBuilder();
-        helloBuilder.setVersion(proposedVersion).setXid(hello.getXid());
-        LOG.debug("sending helloReply");
-        connectionAdapter.hello(helloBuilder.build());
-
-        if (proposedVersion != remoteVersion) {
-            // need to wait for another hello
-        } else {
-            // sent version is equal to remote --> version is negotiated
-            version = proposedVersion;
-            LOG.debug("version set: " + proposedVersion);
-
-            // request features
-            GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
-            featuresBuilder.setVersion(version).setXid(hello.getXid());
-            Future<RpcResult<GetFeaturesOutput>> featuresFuture = connectionAdapter
-                    .getFeatures(featuresBuilder.build());
-            LOG.debug("waiting for features");
-            RpcResult<GetFeaturesOutput> rpcFeatures;
-            try {
-                rpcFeatures = featuresFuture.get(getMaxTimeout(),
-                        TimeUnit.MILLISECONDS);
-                if (!rpcFeatures.isSuccessful()) {
-                    LOG.error("obtained features problem: "
-                            + rpcFeatures.getErrors());
-                } else {
-                    LOG.debug("obtained features: datapathId="
-                            + rpcFeatures.getResult().getDatapathId());
-                    conductorState = CONDUCTOR_STATE.WORKING;
-
-                    OFSessionUtil.registerSession(this, rpcFeatures.getResult(), version);
-                    LOG.info("handshake SETTLED");
-                }
-            } catch (Exception e) {
-                handleException(e);
-            }
-        }
+        HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
+                hello, handshakeManager, connectionAdapter);
+        hsPool.execute(handshakeStepWrapper);
     }
 
     /**
      * @return rpc-response timeout in [ms]
      */
-    private long getMaxTimeout() {
+    protected long getMaxTimeout() {
         // TODO:: get from configuration
         return 2000;
     }
 
     /**
-     * @param e
+     * @return milliseconds
      */
-    private void handleException(Exception e) {
-        try {
-            errorQueue.put(e);
-        } catch (InterruptedException e1) {
-            LOG.error(e1.getMessage(), e1);
-        }
+    protected TimeUnit getMaxTimeoutUnit() {
+        // TODO:: get from configuration
+        return TimeUnit.MILLISECONDS;
     }
 
     @Override
-    public void onMultipartReplyMessage(MultipartReplyMessage arg0) {
-        // TODO Auto-generated method stub
+    public void onMultipartReplyMessage(MultipartReplyMessage message) {
+        queueKeeper.push(message, this);
     }
 
     @Override
-    public void onMultipartRequestMessage(MultipartRequestMessage arg0) {
-        // TODO Auto-generated method stub
+    public void onPacketInMessage(PacketInMessage message) {
+        queueKeeper.push(message, this, QueueKeeper.QueueType.UNORDERED);
     }
 
     @Override
-    public void onPacketInMessage(PacketInMessage arg0) {
-        // TODO Auto-generated method stub
+    public void onPortStatusMessage(PortStatusMessage message) {
+        processPortStatusMsg(message);
+        queueKeeper.push(message, this);
+    }
+    
+    protected void processPortStatusMsg(PortStatus msg) {
+        if (msg.getReason().getIntValue() == 2) {
+            updatePort(msg);
+        } else if (msg.getReason().getIntValue() == 0) {
+            updatePort(msg);
+        } else if (msg.getReason().getIntValue() == 1) {
+            deletePort(msg);
+        }
+    }
+    
+    protected void updatePort(PortStatus msg) {
+        Long portNumber = msg.getPortNo();        
+        Boolean portBandwidth = portFeaturesUtils.getPortBandwidth(msg);
+        
+        if(portBandwidth == null) {
+            LOG.warn("can't get bandwidth info from port: {}, aborting port update", msg.toString());
+        } else {
+            this.getSessionContext().getPhysicalPorts().put(portNumber, msg);
+            this.getSessionContext().getPortsBandwidth().put(portNumber, portBandwidth);                   
+        }            
+    }
+    
+    protected void deletePort(Port port) {
+        Long portNumber = port.getPortNo();
+        
+        this.getSessionContext().getPhysicalPorts().remove(portNumber);
+        this.getSessionContext().getPortsBandwidth().remove(portNumber);
     }
 
     @Override
-    public void onPortStatusMessage(PortStatusMessage arg0) {
-        // TODO Auto-generated method stub
+    public void onSwitchIdleEvent(SwitchIdleEvent notification) {
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                if (!CONDUCTOR_STATE.WORKING.equals(getConductorState())) {
+                    // idle state in any other conductorState than WORKING means real
+                    // problem and wont be handled by echoReply, but disconnection
+                    disconnect();
+                    OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
+                } else {
+                    LOG.debug("first idle state occured");
+                    EchoInputBuilder builder = new EchoInputBuilder();
+                    builder.setVersion(getVersion());
+                    builder.setXid(getSessionContext().getNextXid());
+
+                    Future<RpcResult<EchoOutput>> echoReplyFuture = getConnectionAdapter()
+                            .echo(builder.build());
+
+                    try {
+                        RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(getMaxTimeout(),
+                                getMaxTimeoutUnit());
+                        if (echoReplyValue.isSuccessful()) {
+                            setConductorState(CONDUCTOR_STATE.WORKING);
+                        } else {
+                            for (RpcError replyError : echoReplyValue.getErrors()) {
+                                Throwable cause = replyError.getCause();
+                                LOG.error(
+                                        "while receiving echoReply in TIMEOUTING state: "
+                                                + cause.getMessage(), cause);
+                            }
+                            //switch issue occurred
+                            throw new Exception("switch issue occurred");
+                        }
+                    } catch (Exception e) {
+                        LOG.error("while waiting for echoReply in TIMEOUTING state: "
+                                + e.getMessage());
+                        errorHandler.handleException(e, sessionContext);
+                        //switch is not responding
+                        disconnect();
+                        OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
+                    }
+                }
+            }
+
+        }).start();
     }
 
     /**
@@ -208,7 +302,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     /**
      * @param handshaking
      */
-    private void checkState(CONDUCTOR_STATE expectedState) {
+    protected void checkState(CONDUCTOR_STATE expectedState) {
         if (!conductorState.equals(expectedState)) {
             throw new IllegalStateException("Expected state: " + expectedState
                     + ", actual state:" + conductorState);
@@ -217,26 +311,145 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
 
     @Override
     public void onDisconnectEvent(DisconnectEvent arg0) {
-        // TODO Auto-generated method stub
+        SessionManager sessionManager = OFSessionUtil.getSessionManager();
+        sessionManager.invalidateOnDisconnect(this);
     }
 
-    protected short proposeVersion(short remoteVersion) {
-        Short proposal = null;
-        for (short offer : versionOrder) {
-            if (offer <= remoteVersion) {
-                proposal = offer;
-                break;
-            }
+    @Override
+    public Short getVersion() {
+        return version;
+    }
+
+    @Override
+    public Future<Boolean> disconnect() {
+        LOG.info("disconnecting: sessionCtx="+sessionContext+"|auxId="+auxiliaryKey);
+
+        Future<Boolean> result = null;
+        if (connectionAdapter.isAlive()) {
+            result = connectionAdapter.disconnect();
+        } else {
+            LOG.debug("connection already disconnected");
+            result = Futures.immediateFuture(true);
         }
-        if (proposal == null) {
-            throw new IllegalArgumentException("unsupported version: "
-                    + remoteVersion);
+
+        return result;
+    }
+
+    @Override
+    public void setConnectionCookie(SwitchConnectionDistinguisher auxiliaryKey) {
+        this.auxiliaryKey = auxiliaryKey;
+    }
+
+    @Override
+    public void setSessionContext(SessionContext sessionContext) {
+        this.sessionContext = sessionContext;
+    }
+
+    @Override
+    public SwitchConnectionDistinguisher getAuxiliaryKey() {
+        return auxiliaryKey;
+    }
+
+    @Override
+    public SessionContext getSessionContext() {
+        return sessionContext;
+    }
+
+    @Override
+    public ConnectionAdapter getConnectionAdapter() {
+        return connectionAdapter;
+    }
+
+    @Override
+    public synchronized void onConnectionReady() {
+        LOG.debug("connection is ready-to-use");
+        if (! firstHelloProcessed) {
+            HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
+                    null, handshakeManager, connectionAdapter);
+            hsPool.execute(handshakeStepWrapper);
+            firstHelloProcessed = true;
+        } else {
+            LOG.debug("already touched by hello message");
         }
-        return proposal;
     }
 
     @Override
-    public Short getVersion() {
-        return version;
+    public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput,
+            Short negotiatedVersion) {
+        version = negotiatedVersion;
+        conductorState = CONDUCTOR_STATE.WORKING;
+        OFSessionUtil.registerSession(this, featureOutput, negotiatedVersion);
+        requestDesc();
+        requestPorts();
+        if(version == OFConstants.OFP_VERSION_1_3){
+            requestGroupFeatures();
+            requestMeterFeatures();
+        }
+    }
+
+    /*
+     *  Send an OFPMP_DESC request message to the switch
+     */
+
+    private void requestDesc() {
+        MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
+        builder.setType(MultipartType.OFPMPDESC);
+        builder.setVersion(getVersion());
+        builder.setFlags(new MultipartRequestFlags(false));
+        builder.setMultipartRequestBody(new MultipartRequestDescCaseBuilder().build());
+        builder.setXid(getSessionContext().getNextXid());
+        getConnectionAdapter().multipartRequest(builder.build());
+    }
+
+    private void requestPorts() {
+        MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
+        builder.setType(MultipartType.OFPMPPORTDESC);
+        builder.setVersion(getVersion());
+        builder.setFlags(new MultipartRequestFlags(false));
+        builder.setMultipartRequestBody(new MultipartRequestPortDescCaseBuilder().build());
+        builder.setXid(getSessionContext().getNextXid());
+        getConnectionAdapter().multipartRequest(builder.build());
+    }
+    private void requestGroupFeatures(){
+        MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
+        mprInput.setType(MultipartType.OFPMPGROUPFEATURES);
+        mprInput.setVersion(getVersion());
+        mprInput.setFlags(new MultipartRequestFlags(false));
+        mprInput.setXid(getSessionContext().getNextXid());
+
+        MultipartRequestGroupFeaturesCaseBuilder mprGroupFeaturesBuild = 
+                new MultipartRequestGroupFeaturesCaseBuilder();
+        mprInput.setMultipartRequestBody(mprGroupFeaturesBuild.build());
+
+        LOG.debug("Send group features statistics request :{}",mprGroupFeaturesBuild);
+        getConnectionAdapter().multipartRequest(mprInput.build());
+        
+    }
+    private void requestMeterFeatures(){
+        MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
+        mprInput.setType(MultipartType.OFPMPMETERFEATURES);
+        mprInput.setVersion(getVersion());
+        mprInput.setFlags(new MultipartRequestFlags(false));
+        mprInput.setXid(getSessionContext().getNextXid());
+
+        MultipartRequestMeterFeaturesCaseBuilder mprMeterFeaturesBuild =
+                new MultipartRequestMeterFeaturesCaseBuilder();
+        mprInput.setMultipartRequestBody(mprMeterFeaturesBuild.build());
+
+        LOG.debug("Send meter features statistics request :{}",mprMeterFeaturesBuild);
+        getConnectionAdapter().multipartRequest(mprInput.build());
+        
+    }
+    /**
+     * @param isBitmapNegotiationEnable the isBitmapNegotiationEnable to set
+     */
+    public void setBitmapNegotiationEnable(
+            boolean isBitmapNegotiationEnable) {
+        this.isBitmapNegotiationEnable = isBitmapNegotiationEnable;
+    }
+
+    protected void shutdownPool() {
+        hsPool.shutdownNow();
+        LOG.debug("pool is terminated: {}", hsPool.isTerminated());
     }
 }