Merge "BUG-5841: Enhances bulk-o-matic to stress test Data Store and Openflowplugin...
[openflowplugin.git] / openflowplugin-api / src / main / java / org / opendaylight / openflowplugin / api / openflow / device / DeviceContext.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
9 package org.opendaylight.openflowplugin.api.openflow.device;
10
11 import com.google.common.util.concurrent.ListenableFuture;
12 import io.netty.util.Timeout;
13 import java.math.BigInteger;
14 import java.util.List;
15 import javax.annotation.CheckForNull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
18 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
19 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.openflowplugin.api.openflow.OpenFlowPluginTimer;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceReplyProcessor;
24 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
25 import org.opendaylight.openflowplugin.api.openflow.registry.ItemLifeCycleRegistry;
26 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
27 import org.opendaylight.openflowplugin.api.openflow.registry.group.DeviceGroupRegistry;
28 import org.opendaylight.openflowplugin.api.openflow.registry.meter.DeviceMeterRegistry;
29 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
30 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
31 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36
37 /**
38  * <p>
39  * The central entity of OFP is the Device Context, which encapsulate the logical state of a switch
40  * as seen by the controller. Each OpenFlow session is tracked by a Connection Context.
41  * These attach to a particular Device Context in such a way, that there is at most one primary
42  * session associated with a Device Context. Whenever the controller needs to interact with a
43  * particular switch, it will do so in the context of the calling thread, obtaining a lock on
44  * the corresponding Device Context – thus the Device Context becomes the fine-grained point
45  * of synchronization. The only two entities allowed to send requests towards the switch are
46  * Statistics Manager and RPC Manager. Each of them allocates a Request Context for interacting
47  * with a particular Device Context. The Request Contexts are the basic units of fairness,
48  * which is enforced by keeping a cap on the number of outstanding requests a particular Request
49  * Context can have at any point in time. Should this quota be exceeded, any further attempt to make
50  * a request to the switch will fail immediately, with proper error indication.
51  * </p>
52  * Created by Martin Bobak &lt;mbobak@cisco.com&gt; on 25.2.2015.
53  */
54 public interface DeviceContext extends AutoCloseable,
55         DeviceReplyProcessor,
56         PortNumberCache,
57         XidSequencer {
58
59     void setStatisticsRpcEnabled(boolean isStatisticsRpcEnabled);
60
61     /**
62      * distinguished device context states
63      */
64     enum DEVICE_CONTEXT_STATE {
65         /**
66          * initial phase of talking to switch
67          */
68         INITIALIZATION,
69         /**
70          * standard phase - interacting with switch
71          */
72         WORKING,
73         /**
74          * termination phase of talking to switch
75          */
76         TERMINATION
77     }
78
79     /**
80      * Method returns current device context state.
81      *
82      * @return {@link DeviceContext.DEVICE_CONTEXT_STATE}
83      */
84     DEVICE_CONTEXT_STATE getDeviceContextState();
85
86     /**
87      * Method close all auxiliary connections and primary connection.
88      */
89     void shutdownConnection();
90
91     /**
92      * Method add auxiliary connection contexts to this context representing single device connection.
93      *
94      * @param connectionContext
95      */
96     void addAuxiliaryConnectionContext(ConnectionContext connectionContext);
97
98     /**
99      * Method removes auxiliary connection context from this context representing single device connection.
100      *
101      * @param connectionContext
102      */
103     void removeAuxiliaryConnectionContext(ConnectionContext connectionContext);
104
105     /**
106      * Method provides state of device represented by this device context.
107      *
108      * @return {@link DeviceState}
109      */
110     DeviceState getDeviceState();
111
112     /**
113      * Method has to activate (MASTER) or deactivate (SLAVE) TransactionChainManager.
114      * TransactionChainManager represents possibility to write or delete Node subtree data
115      * for actual Controller Cluster Node. We are able to have an active TxManager only if
116      * newRole is {@link OfpRole#BECOMESLAVE}.
117      * Parameters are used as marker to be sure it is change to SLAVE from MASTER or from
118      * MASTER to SLAVE and the last parameter "cleanDataStore" is used for validation only.
119      * @param oldRole - old role for quick validation for needed processing
120      * @param role - NewRole expect to be {@link OfpRole#BECOMESLAVE} or {@link OfpRole#BECOMEMASTER}
121      * @return RoleChangeTxChainManager future for activation/deactivation
122      * @deprecated replaced by method onDeviceTakeClusterLeadership and onDevicLostClusterLeadership
123      */
124     @Deprecated
125     ListenableFuture<Void> onClusterRoleChange(@Nullable OfpRole oldRole, @CheckForNull OfpRole role);
126
127     /**
128      * Method has to activate TransactionChainManager and prepare all Contexts from Device Contects suite
129      * to Taking ClusterLeadership role {@link OfpRole#BECOMEMASTER} (e.g. Routed RPC registration, StatPolling ...)
130      * @return DeviceInitialization furure
131      */
132     ListenableFuture<Void> onDeviceTakeClusterLeadership();
133
134     /**
135      * Method has to deactivate TransactionChainManager and prepare all Contexts from Device Contects suite
136      * to Lost ClusterLeadership role {@link OfpRole#BECOMESLAVE} (e.g. Stop RPC rounting, stop StatPolling ...)
137      * @return RoleChangeTxChainManager future for deactivation
138      */
139     ListenableFuture<Void> onDeviceLostClusterLeadership();
140
141     /**
142      * Method creates put operation using provided data in underlying transaction chain.
143      */
144     <T extends DataObject> void writeToTransaction(final LogicalDatastoreType store, final InstanceIdentifier<T> path, final T data) throws Exception;
145
146     /**
147      * Method creates delete operation for provided path in underlying transaction chain.
148      */
149     <T extends DataObject> void addDeleteToTxChain(final LogicalDatastoreType store, final InstanceIdentifier<T> path) throws Exception;
150
151     /**
152      * Method submits Transaction to DataStore.
153      * @return transaction is submitted successfully
154      */
155     boolean submitTransaction();
156
157     /**
158      * Method has to close TxManager ASAP we are notified about Closed Connection
159      * @return sync. future for Slave and MD-SAL completition for Master
160      */
161     ListenableFuture<Void> shuttingDownDataStoreTransactions();
162
163     /**
164      * Method exposes transaction created for device
165      * represented by this context. This read only transaction has a fresh dataStore snapshot.
166      * There is a possibility to get different data set from  DataStore
167      * as write transaction in this context.
168      * @return readOnlyTransaction - Don't forget to close it after finish reading
169      */
170     ReadOnlyTransaction getReadTransaction();
171
172
173     /**
174      * Method provides current devices connection context.
175      *
176      * @return
177      */
178     ConnectionContext getPrimaryConnectionContext();
179
180     /**
181      * Method provides current devices auxiliary connection contexts.
182      *
183      * @return
184      */
185     ConnectionContext getAuxiliaryConnectiobContexts(BigInteger cookie);
186
187     /**
188      * Method exposes flow registry used for storing flow ids identified by calculated flow hash.
189      *
190      * @return
191      */
192     DeviceFlowRegistry getDeviceFlowRegistry();
193
194     /**
195      * Method exposes device group registry used for storing group ids.
196      *
197      * @return
198      */
199     DeviceGroupRegistry getDeviceGroupRegistry();
200
201     /**
202      * Method exposes device meter registry used for storing meter ids.
203      *
204      * @return
205      */
206     DeviceMeterRegistry getDeviceMeterRegistry();
207
208
209     /**
210      * @return translator library
211      */
212     TranslatorLibrary oook();
213
214     /**
215      * store cancellable timeout handler of currently running barrier task
216      */
217     void setCurrentBarrierTimeout(Timeout timeout);
218
219     /**
220      * @return cancellable timeout handle of currently running barrier task
221      */
222     Timeout getBarrierTaskTimeout();
223
224     void setNotificationPublishService(NotificationPublishService notificationPublishService);
225
226     MessageSpy getMessageSpy();
227
228     MultiMsgCollector getMultiMsgCollector(final RequestContext<List<MultipartReply>> requestContext);
229
230     /**
231      * indicates that device context is fully published (e.g.: packetIn messages should be passed)
232      */
233     void onPublished();
234
235     /**
236      * change packetIn rate limiter borders
237      *
238      * @param upperBound max amount of outstanding packetIns
239      */
240     void updatePacketInRateLimit(long upperBound);
241
242     /**
243      * @return registry point for item life cycle sources of device
244      */
245     ItemLifeCycleRegistry getItemLifeCycleSourceRegistry();
246
247     void setRpcContext(RpcContext rpcContext);
248
249     RpcContext getRpcContext();
250
251     void setStatisticsContext(StatisticsContext statisticsContext);
252
253     StatisticsContext getStatisticsContext();
254
255     @Override
256     void close();
257 }
258