Bug:995-Controller does not learn node connectors from mininet OF10
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / ConnectionConductorImpl.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.openflow.md.core;
10
11 import java.util.concurrent.Future;
12 import java.util.concurrent.LinkedBlockingQueue;
13 import java.util.concurrent.ThreadPoolExecutor;
14 import java.util.concurrent.TimeUnit;
15
16 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
17 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
18 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
19 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
20 import org.opendaylight.openflowplugin.openflow.md.core.session.PortFeaturesUtil;
21 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
22 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionManager;
23 import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeper;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatus;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestDescCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupFeaturesCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterFeaturesCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestPortDescCaseBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.common.RpcError;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 import com.google.common.util.concurrent.Futures;
57
58 /**
59  * @author mirehak
60  */
61 public class ConnectionConductorImpl implements OpenflowProtocolListener,
62         SystemNotificationsListener, ConnectionConductor, ConnectionReadyListener, HandshakeListener {
63
64     protected static final Logger LOG = LoggerFactory
65             .getLogger(ConnectionConductorImpl.class);
66
67     /* variable to make BitMap-based negotiation enabled / disabled.
68      * it will help while testing and isolating issues related to processing of
69      * BitMaps from switches.
70      */
71     private boolean isBitmapNegotiationEnable = true;
72     protected ErrorHandler errorHandler;
73
74     private final ConnectionAdapter connectionAdapter;
75     private ConnectionConductor.CONDUCTOR_STATE conductorState;
76     private Short version;
77
78     protected SwitchConnectionDistinguisher auxiliaryKey;
79
80     protected SessionContext sessionContext;
81
82     private QueueKeeper<OfHeader, DataObject> queueKeeper;
83     private ThreadPoolExecutor hsPool;
84     private HandshakeManager handshakeManager;
85
86     private boolean firstHelloProcessed;
87     
88     private PortFeaturesUtil portFeaturesUtils;
89
90     /**
91      * @param connectionAdapter
92      */
93     public ConnectionConductorImpl(ConnectionAdapter connectionAdapter) {
94         this.connectionAdapter = connectionAdapter;
95         conductorState = CONDUCTOR_STATE.HANDSHAKING;
96         int handshakeThreadLimit = 1;
97         hsPool = new ThreadPoolLoggingExecutor(handshakeThreadLimit , handshakeThreadLimit, 0L, 
98                 TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
99         firstHelloProcessed = false;
100         handshakeManager = new HandshakeManagerImpl(connectionAdapter,
101                 ConnectionConductor.versionOrder.get(0), ConnectionConductor.versionOrder);
102         handshakeManager.setUseVersionBitmap(isBitmapNegotiationEnable);
103         handshakeManager.setHandshakeListener(this);
104         portFeaturesUtils = PortFeaturesUtil.getInstance();
105     }
106
107     @Override
108     public void init() {
109         connectionAdapter.setMessageListener(this);
110         connectionAdapter.setSystemListener(this);
111         connectionAdapter.setConnectionReadyListener(this);
112     }
113
114     @Override
115     public void setQueueKeeper(QueueKeeper<OfHeader, DataObject> queueKeeper) {
116         this.queueKeeper = queueKeeper;
117     }
118
119     /**
120      * @param errorHandler the errorHandler to set
121      */
122     @Override
123     public void setErrorHandler(ErrorHandler errorHandler) {
124         this.errorHandler = errorHandler;
125         handshakeManager.setErrorHandler(errorHandler);
126     }
127
128     @Override
129     public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
130         new Thread(new Runnable() {
131             @Override
132             public void run() {
133                 LOG.debug("echo request received: " + echoRequestMessage.getXid());
134                 EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
135                 builder.setVersion(echoRequestMessage.getVersion());
136                 builder.setXid(echoRequestMessage.getXid());
137                 builder.setData(echoRequestMessage.getData());
138
139                 getConnectionAdapter().echoReply(builder.build());
140             }
141         }).start();
142     }
143
144     @Override
145     public void onErrorMessage(ErrorMessage errorMessage) {
146         queueKeeper.push(errorMessage, this);
147     }
148
149     @Override
150     public void onExperimenterMessage(ExperimenterMessage experimenterMessage) {
151         queueKeeper.push(experimenterMessage, this);
152     }
153
154     @Override
155     public void onFlowRemovedMessage(FlowRemovedMessage message) {
156         queueKeeper.push(message, this);
157     }
158
159
160     /**
161      * version negotiation happened as per following steps:
162      * 1. If HelloMessage version field has same version, continue connection processing.
163      *    If HelloMessage version is lower than supported versions, just disconnect.
164      * 2. If HelloMessage contains bitmap and common version found in bitmap
165      *    then continue connection processing. if no common version found, just disconnect.
166      * 3. If HelloMessage version is not supported, send HelloMessage with lower supported version.
167      * 4. If Hello message received again with not supported version, just disconnect.
168      */
169     @Override
170     public synchronized void onHelloMessage(final HelloMessage hello) {
171         LOG.debug("processing HELLO.xid: {}", hello.getXid());
172         firstHelloProcessed = true;
173         checkState(CONDUCTOR_STATE.HANDSHAKING);
174         HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
175                 hello, handshakeManager, connectionAdapter);
176         hsPool.submit(handshakeStepWrapper);
177     }
178
179     /**
180      * @return rpc-response timeout in [ms]
181      */
182     protected long getMaxTimeout() {
183         // TODO:: get from configuration
184         return 2000;
185     }
186
187     /**
188      * @return milliseconds
189      */
190     protected TimeUnit getMaxTimeoutUnit() {
191         // TODO:: get from configuration
192         return TimeUnit.MILLISECONDS;
193     }
194
195     @Override
196     public void onMultipartReplyMessage(MultipartReplyMessage message) {
197         queueKeeper.push(message, this);
198     }
199
200     @Override
201     public void onPacketInMessage(PacketInMessage message) {
202         queueKeeper.push(message, this, QueueKeeper.QueueType.UNORDERED);
203     }
204
205     @Override
206     public void onPortStatusMessage(PortStatusMessage message) {
207         processPortStatusMsg(message);
208         queueKeeper.push(message, this);
209     }
210     
211     protected void processPortStatusMsg(PortStatus msg) {
212         if (msg.getReason().getIntValue() == 2) {
213             updatePort(msg);
214         } else if (msg.getReason().getIntValue() == 0) {
215             updatePort(msg);
216         } else if (msg.getReason().getIntValue() == 1) {
217             deletePort(msg);
218         }
219     }
220     
221     protected void updatePort(PortStatus msg) {
222         Long portNumber = msg.getPortNo();        
223         Boolean portBandwidth = portFeaturesUtils.getPortBandwidth(msg);
224         
225         if(portBandwidth == null) {
226             LOG.debug("can't get bandwidth info from port: {}, aborting port update", msg.toString());
227         } else {
228             this.getSessionContext().getPhysicalPorts().put(portNumber, msg);
229             this.getSessionContext().getPortsBandwidth().put(portNumber, portBandwidth);                   
230         }            
231     }
232     
233     protected void deletePort(PortGrouping port) {
234         Long portNumber = port.getPortNo();
235         
236         this.getSessionContext().getPhysicalPorts().remove(portNumber);
237         this.getSessionContext().getPortsBandwidth().remove(portNumber);
238     }
239
240     @Override
241     public void onSwitchIdleEvent(SwitchIdleEvent notification) {
242         new Thread(new Runnable() {
243             @Override
244             public void run() {
245                 if (!CONDUCTOR_STATE.WORKING.equals(getConductorState())) {
246                     // idle state in any other conductorState than WORKING means real
247                     // problem and wont be handled by echoReply, but disconnection
248                     disconnect();
249                     OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
250                 } else {
251                     LOG.debug("first idle state occured, sessionCtx={}|auxId={}", sessionContext, auxiliaryKey);
252                     EchoInputBuilder builder = new EchoInputBuilder();
253                     builder.setVersion(getVersion());
254                     builder.setXid(getSessionContext().getNextXid());
255
256                     Future<RpcResult<EchoOutput>> echoReplyFuture = getConnectionAdapter()
257                             .echo(builder.build());
258
259                     try {
260                         RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(getMaxTimeout(),
261                                 getMaxTimeoutUnit());
262                         if (echoReplyValue.isSuccessful()) {
263                             setConductorState(CONDUCTOR_STATE.WORKING);
264                         } else {
265                             for (RpcError replyError : echoReplyValue.getErrors()) {
266                                 Throwable cause = replyError.getCause();
267                                 LOG.error(
268                                         "while receiving echoReply in TIMEOUTING state: "
269                                                 + cause.getMessage(), cause);
270                             }
271                             //switch issue occurred
272                             throw new Exception("switch issue occurred");
273                         }
274                     } catch (Exception e) {
275                         LOG.error("while waiting for echoReply in TIMEOUTING state: "
276                                 + e.getMessage());
277                         errorHandler.handleException(e, sessionContext);
278                         //switch is not responding
279                         disconnect();
280                         OFSessionUtil.getSessionManager().invalidateOnDisconnect(ConnectionConductorImpl.this);
281                     }
282                 }
283             }
284
285         }).start();
286     }
287
288     /**
289      * @param conductorState
290      *            the connectionState to set
291      */
292     @Override
293     public void setConductorState(CONDUCTOR_STATE conductorState) {
294         this.conductorState = conductorState;
295     }
296
297     @Override
298     public CONDUCTOR_STATE getConductorState() {
299         return conductorState;
300     }
301
302     /**
303      * @param handshaking
304      */
305     protected void checkState(CONDUCTOR_STATE expectedState) {
306         if (!conductorState.equals(expectedState)) {
307             throw new IllegalStateException("Expected state: " + expectedState
308                     + ", actual state:" + conductorState);
309         }
310     }
311
312     @Override
313     public void onDisconnectEvent(DisconnectEvent arg0) {
314         SessionManager sessionManager = OFSessionUtil.getSessionManager();
315         sessionManager.invalidateOnDisconnect(this);
316     }
317
318     @Override
319     public Short getVersion() {
320         return version;
321     }
322
323     @Override
324     public Future<Boolean> disconnect() {
325         LOG.trace("disconnecting: sessionCtx={}|auxId={}", sessionContext, auxiliaryKey);
326
327         Future<Boolean> result = null;
328         if (connectionAdapter.isAlive()) {
329             result = connectionAdapter.disconnect();
330         } else {
331             LOG.debug("connection already disconnected");
332             result = Futures.immediateFuture(true);
333         }
334
335         return result;
336     }
337
338     @Override
339     public void setConnectionCookie(SwitchConnectionDistinguisher auxiliaryKey) {
340         this.auxiliaryKey = auxiliaryKey;
341     }
342
343     @Override
344     public void setSessionContext(SessionContext sessionContext) {
345         this.sessionContext = sessionContext;
346     }
347
348     @Override
349     public SwitchConnectionDistinguisher getAuxiliaryKey() {
350         return auxiliaryKey;
351     }
352
353     @Override
354     public SessionContext getSessionContext() {
355         return sessionContext;
356     }
357
358     @Override
359     public ConnectionAdapter getConnectionAdapter() {
360         return connectionAdapter;
361     }
362
363     @Override
364     public synchronized void onConnectionReady() {
365         LOG.debug("connection is ready-to-use");
366         if (! firstHelloProcessed) {
367             HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
368                     null, handshakeManager, connectionAdapter);
369             hsPool.execute(handshakeStepWrapper);
370             firstHelloProcessed = true;
371         } else {
372             LOG.debug("already touched by hello message");
373         }
374     }
375
376     @Override
377     public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput,
378             Short negotiatedVersion) {
379         postHandshakeBasic(featureOutput, negotiatedVersion);
380         
381         // post-handshake actions
382         if(version == OFConstants.OFP_VERSION_1_3){
383             requestGroupFeatures();
384             requestMeterFeatures();
385         } else if (version == OFConstants.OFP_VERSION_1_0) {
386             //  Because the GetFeaturesOutput contains information about the port
387             //  in OF1.0 (that we would otherwise get from the PortDesc) we have to pass
388             //  it up for parsing to convert into a NodeConnectorUpdate
389             queueKeeper.push(featureOutput, this);
390         }
391         
392         requestDesc();
393         requestPorts();
394     }
395
396     /**
397      * used by tests
398      * @param featureOutput
399      * @param negotiatedVersion
400      */
401     protected void postHandshakeBasic(GetFeaturesOutput featureOutput,
402             Short negotiatedVersion) {
403         version = negotiatedVersion;
404         conductorState = CONDUCTOR_STATE.WORKING;
405         OFSessionUtil.registerSession(this, featureOutput, negotiatedVersion);
406         hsPool.shutdown();
407         hsPool.purge();
408     }
409
410     /*
411      *  Send an OFPMP_DESC request message to the switch
412      */
413
414     private void requestDesc() {
415         MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
416         builder.setType(MultipartType.OFPMPDESC);
417         builder.setVersion(getVersion());
418         builder.setFlags(new MultipartRequestFlags(false));
419         builder.setMultipartRequestBody(new MultipartRequestDescCaseBuilder().build());
420         builder.setXid(getSessionContext().getNextXid());
421         getConnectionAdapter().multipartRequest(builder.build());
422     }
423
424     private void requestPorts() {
425         MultipartRequestInputBuilder builder = new MultipartRequestInputBuilder();
426         builder.setType(MultipartType.OFPMPPORTDESC);
427         builder.setVersion(getVersion());
428         builder.setFlags(new MultipartRequestFlags(false));
429         builder.setMultipartRequestBody(new MultipartRequestPortDescCaseBuilder().build());
430         builder.setXid(getSessionContext().getNextXid());
431         getConnectionAdapter().multipartRequest(builder.build());
432     }
433     private void requestGroupFeatures(){
434         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
435         mprInput.setType(MultipartType.OFPMPGROUPFEATURES);
436         mprInput.setVersion(getVersion());
437         mprInput.setFlags(new MultipartRequestFlags(false));
438         mprInput.setXid(getSessionContext().getNextXid());
439
440         MultipartRequestGroupFeaturesCaseBuilder mprGroupFeaturesBuild = 
441                 new MultipartRequestGroupFeaturesCaseBuilder();
442         mprInput.setMultipartRequestBody(mprGroupFeaturesBuild.build());
443
444         LOG.debug("Send group features statistics request :{}",mprGroupFeaturesBuild);
445         getConnectionAdapter().multipartRequest(mprInput.build());
446         
447     }
448     private void requestMeterFeatures(){
449         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
450         mprInput.setType(MultipartType.OFPMPMETERFEATURES);
451         mprInput.setVersion(getVersion());
452         mprInput.setFlags(new MultipartRequestFlags(false));
453         mprInput.setXid(getSessionContext().getNextXid());
454
455         MultipartRequestMeterFeaturesCaseBuilder mprMeterFeaturesBuild =
456                 new MultipartRequestMeterFeaturesCaseBuilder();
457         mprInput.setMultipartRequestBody(mprMeterFeaturesBuild.build());
458
459         LOG.debug("Send meter features statistics request :{}",mprMeterFeaturesBuild);
460         getConnectionAdapter().multipartRequest(mprInput.build());
461         
462     }
463     /**
464      * @param isBitmapNegotiationEnable the isBitmapNegotiationEnable to set
465      */
466     public void setBitmapNegotiationEnable(
467             boolean isBitmapNegotiationEnable) {
468         this.isBitmapNegotiationEnable = isBitmapNegotiationEnable;
469     }
470
471     protected void shutdownPool() {
472         hsPool.shutdownNow();
473         LOG.debug("pool is terminated: {}", hsPool.isTerminated());
474     }
475 }