Speed up packetin throttling
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / ConnectionContextImpl.java
1 /**
2  * Copyright (c) 2015 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 package org.opendaylight.openflowplugin.impl.connection;
9
10 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
11 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
12 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
13 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
14 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
15 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  *
23  */
24 public class ConnectionContextImpl implements ConnectionContext {
25
26     private final ConnectionAdapter connectionAdapter;
27     private CONNECTION_STATE connectionState;
28     private FeaturesReply featuresReply;
29     private NodeId nodeId;
30     private DeviceDisconnectedHandler deviceDisconnectedHandler;
31     private static final Logger LOG = LoggerFactory.getLogger(ConnectionContextImpl.class);
32     private OutboundQueueProvider outboundQueueProvider;
33     private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration;
34
35     /**
36      * @param connectionAdapter
37      */
38     public ConnectionContextImpl(final ConnectionAdapter connectionAdapter) {
39         this.connectionAdapter = connectionAdapter;
40     }
41
42     @Override
43     public ConnectionAdapter getConnectionAdapter() {
44         return connectionAdapter;
45     }
46
47     @Override
48     public OutboundQueue getOutboundQueueProvider() {
49         return this.outboundQueueProvider;
50     }
51
52     @Override
53     public void setOutboundQueueProvider(final OutboundQueueProvider outboundQueueProvider) {
54         this.outboundQueueProvider = outboundQueueProvider;
55     }
56
57     @Override
58     public CONNECTION_STATE getConnectionState() {
59         return connectionState;
60     }
61
62     @Override
63     public NodeId getNodeId() {
64         return nodeId;
65     }
66
67     @Override
68     public void setNodeId(final NodeId nodeId) {
69         this.nodeId = nodeId;
70     }
71
72     @Override
73     public void setConnectionState(final CONNECTION_STATE connectionState) {
74         this.connectionState = connectionState;
75     }
76
77     @Override
78     public FeaturesReply getFeatures() {
79         return featuresReply;
80     }
81
82     @Override
83     public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
84         this.deviceDisconnectedHandler = deviceDisconnectedHandler;
85     }
86
87     @Override
88     public void propagateClosingConnection() {
89         if (null != deviceDisconnectedHandler) {
90             LOG.trace("Populating connection closed event.");
91             this.deviceDisconnectedHandler.onDeviceDisconnected(this);
92         }
93     }
94
95     @Override
96     public void setFeatures(final FeaturesReply featuresReply) {
97         this.featuresReply = featuresReply;
98     }
99
100     @Override
101     public void close() {
102         if (getConnectionAdapter().isAlive()) {
103             setConnectionState(ConnectionContext.CONNECTION_STATE.RIP);
104             getConnectionAdapter().disconnect();
105         }
106         if (outboundQueueHandlerRegistration != null) {
107             outboundQueueHandlerRegistration.close();
108             outboundQueueHandlerRegistration = null;
109         }
110     }
111
112     @Override
113     public void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration) {
114         this.outboundQueueHandlerRegistration = outboundQueueHandlerRegistration;
115     }
116 }