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