Merge "Bug 4957 Fixing DeviceCtx lifeCycle"
[openflowplugin.git] / applications / statistics-manager / src / main / java / org / opendaylight / openflowplugin / applications / statistics / manager / StatisticsManager.java
1 /**
2  * Copyright (c) 2014 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.applications.statistics.manager;
10
11 import java.util.List;
12 import java.util.UUID;
13 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
14 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
16 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
17 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
18 import org.opendaylight.openflowplugin.applications.statistics.manager.StatPermCollector.StatCapabTypes;
19 import org.opendaylight.openflowplugin.applications.statistics.manager.impl.StatisticsManagerConfig;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsListener;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsListener;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.OpendaylightGroupStatisticsListener;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.OpendaylightMeterStatisticsListener;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsListener;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.OpendaylightQueueStatisticsListener;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36
37 /**
38  * statistics-manager
39  * org.opendaylight.openflowplugin.applications.statistics.manager
40  *
41  * StatisticsManager
42  * It represent a central point for whole module. Implementation
43  * StatisticsManager registers all Operation/DS {@link StatNotifyCommiter} and
44  * Config/DS {@link StatListeningCommiter}, as well as {@link StatPermCollector}
45  * for statistic collecting and {@link StatRpcMsgManager} as Device RPCs provider.
46  * In next, StatisticsManager provides all DS contact Transaction services.
47  *
48  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
49  *
50  * Created: Aug 27, 2014
51  */
52 public interface StatisticsManager extends AutoCloseable, TransactionChainListener {
53
54     /**
55      * StatDataStoreOperation
56      * Interface represent functionality to submit changes to DataStore.
57      * Internal {@link TransactionChainListener} joining all DS commits
58      * to Set of chained changes for prevent often DataStore touches.
59      */
60     abstract class StatDataStoreOperation {
61         public enum StatsManagerOperationType {
62             /**
63              * Operation will carry out work related to new node addition /
64              * update
65              */
66             NODE_UPDATE,
67             /**
68              * Operation will carry out work related to node removal
69              */
70             NODE_REMOVAL,
71             /**
72              * Operation will commit data to the operational data store
73              */
74             DATA_COMMIT_OPER_DS
75         }
76
77         private NodeId nodeId;
78         private StatsManagerOperationType operationType = StatsManagerOperationType.DATA_COMMIT_OPER_DS;
79         private UUID nodeUUID;
80
81         public StatDataStoreOperation(final StatsManagerOperationType operType, final NodeId id){
82             if(operType != null){
83                 operationType = operType;
84             }
85             nodeId = id;
86             nodeUUID = generatedUUIDForNode();
87         }
88
89         public final StatsManagerOperationType getType() {
90             return operationType;
91         }
92
93         public final NodeId getNodeId(){
94             return nodeId;
95         }
96
97         public UUID getNodeUUID() {
98             return nodeUUID;
99         }
100
101         /**
102          * Apply all read / write (put|merge) operation for DataStore
103          *
104          * @param tx {@link ReadWriteTransaction}
105          */
106         public abstract void applyOperation(ReadWriteTransaction tx);
107
108         protected abstract UUID generatedUUIDForNode();
109
110         public InstanceIdentifier<Node> getNodeIdentifier() {
111             final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
112                     .child(Node.class, new NodeKey(nodeId));
113             return nodeIdent;
114         }
115
116     }
117
118
119     class Pair<L,R> {
120
121         private final L left;
122         private final R right;
123
124         public Pair(L left, R right) {
125             this.left = left;
126             this.right = right;
127         }
128
129         public L getLeft() { return left; }
130         public R getRight() { return right; }
131
132         @Override
133         public int hashCode() { return left.hashCode() ^ right.hashCode(); }
134
135         @Override
136         public boolean equals(Object o) {
137             if (!(o instanceof Pair)) return false;
138             Pair pairo = (Pair) o;
139             return this.left.equals(pairo.getLeft()) &&
140                     this.right.equals(pairo.getRight());
141         }
142
143     }
144
145     /**
146      * Method starts whole StatisticManager functionality
147      *
148      * @param notifService
149      * @param rpcRegistry
150      */
151     void start(final NotificationProviderService notifService,
152             final RpcConsumerRegistry rpcRegistry);
153
154     /**
155      * Method provides read/write DataStore functionality cross applyOperation
156      * defined in {@link StatDataStoreOperation}
157      *
158      * @param inventoryOper - operation for DataStore
159      */
160     void enqueue(final StatDataStoreOperation inventoryOper);
161
162     /**
163      * Method wraps {@link StatisticsManager#isProvidedFlowNodeActive(InstanceIdentifier)} method
164      * to provide parallel statCollection process for Set of Nodes. So it has to
165      * identify correct Node Set by NodeIdentifier
166      *
167      * @param nodeIdent
168      */
169      boolean isProvidedFlowNodeActive(InstanceIdentifier<Node> nodeIdent);
170
171      /**
172       * Method wraps {@link StatPermCollector}.collectNextStatistics to provide
173       * parallel statCollection process for Set of Nodes. So it has to
174       * identify correct Node Set by NodeIdentifier.
175       *
176       * @param nodeIdent
177       */
178      void collectNextStatistics(InstanceIdentifier<Node> nodeIdent, TransactionId xid);
179
180      /**
181       * Method wraps {@link StatPermCollector}.connectedNodeRegistration to provide
182       * parallel statCollection process for Set of Nodes. So it has to
183       * connect node to new or not full Node statCollector Set.
184       *
185       * @param nodeIdent
186       * @param statTypes
187       * @param nrOfSwitchTables
188       */
189      void connectedNodeRegistration(InstanceIdentifier<Node> nodeIdent,
190              List<StatCapabTypes> statTypes, Short nrOfSwitchTables);
191
192      /**
193       * Method wraps {@link StatPermCollector}.disconnectedNodeUnregistration to provide
194       * parallel statCollection process for Set of Nodes. So it has to identify
195       * correct collector for disconnect node.
196       *
197       * @param nodeIdent
198       */
199      void disconnectedNodeUnregistration(InstanceIdentifier<Node> nodeIdent);
200
201      /**
202       * Method wraps {@link StatPermCollector}.registerAdditionalNodeFeature to provide
203       * possibility to register additional Node Feature {@link StatCapabTypes} for
204       * statistics collecting.
205       *
206       * @param nodeIdent
207       * @param statCapab
208       */
209      void registerAdditionalNodeFeature(InstanceIdentifier<Node> nodeIdent, StatCapabTypes statCapab);
210
211     /**
212      * Method provides access to Device RPC methods by wrapped
213      * internal method. In next {@link StatRpcMsgManager} is registered all
214      * Multipart device msg response and joining all to be able run all
215      * collected statistics in one time (easy identification Data for delete)
216      *
217      * @return {@link StatRpcMsgManager}
218      */
219     StatRpcMsgManager getRpcMsgManager();
220
221     /**
222      * Define Method : {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode}
223      * Operational/DS data change listener -&gt; impl. target -&gt; register FlowCapableNode to Statistic Collecting process
224      * @return {@link StatNodeRegistration}
225      */
226     StatNodeRegistration getNodeRegistrator();
227
228     /**
229      * Define Method : Flow Config/DS data change listener -&gt; impl. target -&gt;
230      * -&gt; make pair between Config/DS FlowId and Device Flow response Hash
231      * @return
232      */
233     StatListeningCommiter<Flow, OpendaylightFlowStatisticsListener> getFlowListenComit();
234
235     /**
236      * Define Method : Meter Config/DS data change listener and Operation/DS notify commit
237      * functionality
238      * @return
239      */
240     StatListeningCommiter<Meter, OpendaylightMeterStatisticsListener> getMeterListenCommit();
241
242     /**
243      * Define Method : Group Config/DS data change listener and Operation/DS notify commit
244      * functionality
245      * @return
246      */
247     StatListeningCommiter<Group, OpendaylightGroupStatisticsListener> getGroupListenCommit();
248
249     /**
250      * Define Method : Queue Config/DS change listener and Operation/DS notify commit functionality
251      * @return
252      */
253     StatListeningCommiter<Queue, OpendaylightQueueStatisticsListener> getQueueNotifyCommit();
254
255     /**
256      * Define Method : Table Operation/DS notify commit functionality
257      * @return
258      */
259     StatNotifyCommiter<OpendaylightFlowTableStatisticsListener> getTableNotifCommit();
260
261     /**
262      * Define Method : Port Operation/DS notify commit functionality
263      * @return
264      */
265     StatNotifyCommiter<OpendaylightPortStatisticsListener> getPortNotifyCommit();
266
267     StatisticsManagerConfig getConfiguration();
268
269     /**
270      * A unique UUID is generated with each node added by the statistics manager implementation in order to uniquely
271      * identify a session.
272      * @param nodeInstanceIdentifier
273      */
274     UUID getGeneratedUUIDForNode(InstanceIdentifier<Node> nodeInstanceIdentifier);
275
276     /*
277      * Setting entity-ownership-service
278      */
279     void setOwnershipService(EntityOwnershipService ownershipService);
280  
281     /**
282       * Getting entity-ownership-service
283       */
284     EntityOwnershipService getOwnershipService();
285
286 }
287