BUG 5523 - DeviceContext additional functionality
[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.DeviceDisconnectedHandler;
24 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceReplyProcessor;
25 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
26 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
27 import org.opendaylight.openflowplugin.api.openflow.registry.ItemLifeCycleRegistry;
28 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
29 import org.opendaylight.openflowplugin.api.openflow.registry.group.DeviceGroupRegistry;
30 import org.opendaylight.openflowplugin.api.openflow.registry.meter.DeviceMeterRegistry;
31 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
32 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
33 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38
39 /**
40  * <p>
41  * The central entity of OFP is the Device Context, which encapsulate the logical state of a switch
42  * as seen by the controller. Each OpenFlow session is tracked by a Connection Context.
43  * These attach to a particular Device Context in such a way, that there is at most one primary
44  * session associated with a Device Context. Whenever the controller needs to interact with a
45  * particular switch, it will do so in the context of the calling thread, obtaining a lock on
46  * the corresponding Device Context – thus the Device Context becomes the fine-grained point
47  * of synchronization. The only two entities allowed to send requests towards the switch are
48  * Statistics Manager and RPC Manager. Each of them allocates a Request Context for interacting
49  * with a particular Device Context. The Request Contexts are the basic units of fairness,
50  * which is enforced by keeping a cap on the number of outstanding requests a particular Request
51  * Context can have at any point in time. Should this quota be exceeded, any further attempt to make
52  * a request to the switch will fail immediately, with proper error indication.
53  * </p>
54  * Created by Martin Bobak &lt;mbobak@cisco.com&gt; on 25.2.2015.
55  */
56 public interface DeviceContext extends AutoCloseable,
57         OpenFlowPluginTimer,
58         DeviceReplyProcessor,
59         DeviceDisconnectedHandler,
60         PortNumberCache {
61
62     /**
63      * distinguished device context states
64      */
65     enum DEVICE_CONTEXT_STATE {
66         /**
67          * initial phase of talking to switch
68          */
69         INITIALIZATION,
70         /**
71          * standard phase - interacting with switch
72          */
73         WORKING,
74         /**
75          * termination phase of talking to switch
76          */
77         TERMINATION
78     }
79
80     /**
81      * Method returns current device context state.
82      *
83      * @return {@link DeviceContext.DEVICE_CONTEXT_STATE}
84      */
85     DEVICE_CONTEXT_STATE getDeviceContextState();
86
87     /**
88      * Method close all auxiliary connections and primary connection.
89      */
90     void shutdownConnection();
91
92     /**
93      * Method add auxiliary connection contexts to this context representing single device connection.
94      *
95      * @param connectionContext
96      */
97     void addAuxiliaryConnectionContext(ConnectionContext connectionContext);
98
99     /**
100      * Method removes auxiliary connection context from this context representing single device connection.
101      *
102      * @param connectionContext
103      */
104     void removeAuxiliaryConnectionContext(ConnectionContext connectionContext);
105
106
107     /**
108      * Method provides state of device represented by this device context.
109      *
110      * @return {@link DeviceState}
111      */
112     DeviceState getDeviceState();
113
114     /**
115      * Method has to activate (MASTER) or deactivate (SLAVE) TransactionChainManager.
116      * TransactionChainManager represents possibility to write or delete Node subtree data
117      * for actual Controller Cluster Node. We are able to have an active TxManager only if
118      * newRole is {@link OfpRole#BECOMESLAVE}.
119      * Parameters are used as marker to be sure it is change to SLAVE from MASTER or from
120      * MASTER to SLAVE and the last parameter "cleanDataStore" is used for validation only.
121      * @param oldRole - old role for quick validation for needed processing
122      * @param role - NewRole expect to be {@link OfpRole#BECOMESLAVE} or {@link OfpRole#BECOMEMASTER}
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);
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);
150
151     /**
152      * Method submits Transaction to DataStore.
153      * @return transaction is submitted successfully
154      */
155     boolean submitTransaction();
156
157     /**
158      * Method exposes transaction created for device
159      * represented by this context. This read only transaction has a fresh dataStore snapshot.
160      * There is a possibility to get different data set from  DataStore
161      * as write transaction in this context.
162      * @return readOnlyTransaction - Don't forget to close it after finish reading
163      */
164     ReadOnlyTransaction getReadTransaction();
165
166
167     /**
168      * Method provides current devices connection context.
169      *
170      * @return
171      */
172     ConnectionContext getPrimaryConnectionContext();
173
174     /**
175      * Method provides current devices auxiliary connection contexts.
176      *
177      * @return
178      */
179     ConnectionContext getAuxiliaryConnectiobContexts(BigInteger cookie);
180
181     /**
182      * Method exposes flow registry used for storing flow ids identified by calculated flow hash.
183      *
184      * @return
185      */
186     DeviceFlowRegistry getDeviceFlowRegistry();
187
188     /**
189      * Method exposes device group registry used for storing group ids.
190      *
191      * @return
192      */
193     DeviceGroupRegistry getDeviceGroupRegistry();
194
195     /**
196      * Method exposes device meter registry used for storing meter ids.
197      *
198      * @return
199      */
200     DeviceMeterRegistry getDeviceMeterRegistry();
201
202
203     /**
204      * @return translator library
205      */
206     TranslatorLibrary oook();
207
208     /**
209      * store cancellable timeout handler of currently running barrier task
210      */
211     void setCurrentBarrierTimeout(Timeout timeout);
212
213     /**
214      * @return cancellable timeout handle of currently running barrier task
215      */
216     Timeout getBarrierTaskTimeout();
217
218     /**
219      * Sets notification service
220      *
221      * @param notificationService
222      */
223     void setNotificationService(NotificationService notificationService);
224
225     void setNotificationPublishService(NotificationPublishService notificationPublishService);
226
227     MessageSpy getMessageSpy();
228
229     /**
230      * Method sets reference to handler used for cleanup after device context about to be closed.
231      */
232     void addDeviceContextClosedHandler(DeviceTerminationPhaseHandler deviceContextClosedHandler);
233
234     MultiMsgCollector getMultiMsgCollector(final RequestContext<List<MultipartReply>> requestContext);
235
236     /**
237      * Method is reserved unique XID for Device Message.
238      * Attention: OFJava expect the message, otherwise OutboundQueue could stop working.
239      * @return Reserved XID
240      */
241     Long reservedXidForDeviceMessage();
242
243     /**
244      * indicates that device context is fully published (e.g.: packetIn messages should be passed)
245      */
246     void onPublished();
247
248     /**
249      * change packetIn rate limiter borders
250      *
251      * @param upperBound max amount of outstanding packetIns
252      */
253     void updatePacketInRateLimit(long upperBound);
254
255     /**
256      * @return registry point for item life cycle sources of device
257      */
258     ItemLifeCycleRegistry getItemLifeCycleSourceRegistry();
259
260     void setRpcContext(RpcContext rpcContext);
261
262     RpcContext getRpcContext();
263
264     void setStatisticsContext(StatisticsContext statisticsContext);
265
266     StatisticsContext getStatisticsContext();
267
268     @Override
269     void close();
270 }
271