BUG-3774: 100k flows initial stats fail - logging
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsGatheringUtils.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.impl.statistics;
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.Iterables;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.JdkFutureAdapters;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.concurrent.ExecutionException;
21 import javax.annotation.Nullable;
22 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
27 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
28 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.StatisticsGatherer;
29 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
30 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatisticsBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatistics;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatistics;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.config.stats.reply.MeterConfigStats;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatistics;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatisticsBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatistics;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
93 import org.opendaylight.yangtools.yang.binding.DataObject;
94 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
95 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
96 import org.opendaylight.yangtools.yang.common.RpcResult;
97 import org.slf4j.Logger;
98 import org.slf4j.LoggerFactory;
99
100 /**
101  * Created by Martin Bobak <mbobak@cisco.com> on 2.4.2015.
102  */
103 public final class StatisticsGatheringUtils {
104
105     private static final Logger LOG = LoggerFactory.getLogger(StatisticsGatheringUtils.class);
106     private static final SinglePurposeMultipartReplyTranslator MULTIPART_REPLY_TRANSLATOR = new SinglePurposeMultipartReplyTranslator();
107     public static final String QUEUE2_REQCTX = "QUEUE2REQCTX-";
108
109     private StatisticsGatheringUtils() {
110         throw new IllegalStateException("This class should not be instantiated.");
111     }
112
113
114     public static ListenableFuture<Boolean> gatherStatistics(final StatisticsGatherer statisticsGatheringService,
115                                                              final DeviceContext deviceContext,
116                                                              final MultipartType type) {
117         final String deviceId = deviceContext.getPrimaryConnectionContext().getNodeId().toString();
118         EventIdentifier wholeProcessEventIdentifier = null;
119         if (MultipartType.OFPMPFLOW.equals(type)) {
120             wholeProcessEventIdentifier = new EventIdentifier(type.toString(), deviceId);
121             EventsTimeCounter.markStart(wholeProcessEventIdentifier);
122         }
123         EventIdentifier ofpQueuToRequestContextEventIdentifier = new EventIdentifier(QUEUE2_REQCTX + type.toString(), deviceId);
124         final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture =
125                 JdkFutureAdapters.listenInPoolThread(statisticsGatheringService.getStatisticsOfType(
126                         ofpQueuToRequestContextEventIdentifier, type));
127         return transformAndStoreStatisticsData(statisticsDataInFuture, deviceContext, wholeProcessEventIdentifier, type);
128     }
129
130     private static ListenableFuture<Boolean> transformAndStoreStatisticsData(final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture,
131                                                                              final DeviceContext deviceContext,
132                                                                              final EventIdentifier eventIdentifier, final MultipartType type) {
133         return Futures.transform(statisticsDataInFuture, new Function<RpcResult<List<MultipartReply>>, Boolean>() {
134             @Nullable
135             @Override
136             public Boolean apply(final RpcResult<List<MultipartReply>> rpcResult) {
137                 if (rpcResult.isSuccessful()) {
138                     LOG.debug("Stats reply successfully received for node {} of type {}", deviceContext.getDeviceState().getNodeId(), type);
139                     boolean isMultipartProcessed = Boolean.TRUE;
140
141                     // TODO: in case the result value is null then multipart data probably got processed on the fly -
142                     // TODO: this contract should by clearly stated and enforced - now simple true value is returned
143                     if (null != rpcResult.getResult()) {
144                         Iterable<? extends DataObject> allMultipartData = Collections.emptyList();
145                         DataObject multipartData = null;
146
147
148                         try {
149                             for (final MultipartReply singleReply : rpcResult.getResult()) {
150                                 final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply);
151                                 multipartData = multipartDataList.get(0);
152                                 allMultipartData = Iterables.concat(allMultipartData, multipartDataList);
153                             }
154                         } catch (Exception e) {
155                             LOG.warn("stats processing of type {} for node {} failed during transfomation step",
156                                     type, deviceContext.getDeviceState().getNodeId(), e);
157                             throw e;
158                         }
159
160
161                         try {
162                             if (multipartData instanceof GroupStatisticsUpdated) {
163                                 processGroupStatistics((Iterable<GroupStatisticsUpdated>) allMultipartData, deviceContext);
164                             } else if (multipartData instanceof MeterStatisticsUpdated) {
165                                 processMetersStatistics((Iterable<MeterStatisticsUpdated>) allMultipartData, deviceContext);
166                             } else if (multipartData instanceof NodeConnectorStatisticsUpdate) {
167                                 processNodeConnectorStatistics((Iterable<NodeConnectorStatisticsUpdate>) allMultipartData, deviceContext);
168                             } else if (multipartData instanceof FlowTableStatisticsUpdate) {
169                                 processFlowTableStatistics((Iterable<FlowTableStatisticsUpdate>) allMultipartData, deviceContext);
170                             } else if (multipartData instanceof QueueStatisticsUpdate) {
171                                 processQueueStatistics((Iterable<QueueStatisticsUpdate>) allMultipartData, deviceContext);
172                             } else if (multipartData instanceof FlowsStatisticsUpdate) {
173                                 processFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData, deviceContext);
174                                 EventsTimeCounter.markEnd(eventIdentifier);
175                             } else if (multipartData instanceof GroupDescStatsUpdated) {
176                                 processGroupDescStats((Iterable<GroupDescStatsUpdated>) allMultipartData, deviceContext);
177                             } else if (multipartData instanceof MeterConfigStatsUpdated) {
178                                 processMeterConfigStatsUpdated((Iterable<MeterConfigStatsUpdated>) allMultipartData, deviceContext);
179                             } else {
180                                 isMultipartProcessed = Boolean.FALSE;
181                             }
182                         } catch (Exception e) {
183                             LOG.warn("stats processing of type {} for node {} failed during write-to-tx step",
184                                     type, deviceContext.getDeviceState().getNodeId(), e);
185                             throw e;
186                         }
187
188                         LOG.debug("Stats reply added to transaction for node {} of type {}", deviceContext.getDeviceState().getNodeId(), type);
189
190                         //TODO : implement experimenter
191                     } else {
192                         LOG.debug("Stats reply was empty for node {} of type {}", deviceContext.getDeviceState().getNodeId(), type);
193                     }
194
195                     return isMultipartProcessed;
196                 } else {
197                     LOG.debug("Stats reply FAILED for node {} of type {}: {}", deviceContext.getDeviceState().getNodeId(), type, rpcResult.getErrors());
198                 }
199                 return Boolean.FALSE;
200             }
201         });
202     }
203
204     private static void processMeterConfigStatsUpdated(final Iterable<MeterConfigStatsUpdated> data, final DeviceContext deviceContext) {
205         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
206         deleteAllKnownMeters(deviceContext, fNodeIdent);
207         for (final MeterConfigStatsUpdated meterConfigStatsUpdated : data) {
208             for (final MeterConfigStats meterConfigStats : meterConfigStatsUpdated.getMeterConfigStats()) {
209                 final MeterId meterId = meterConfigStats.getMeterId();
210                 final InstanceIdentifier<Meter> meterInstanceIdentifier = fNodeIdent.child(Meter.class, new MeterKey(meterId));
211
212                 final MeterBuilder meterBuilder = new MeterBuilder(meterConfigStats);
213                 meterBuilder.setKey(new MeterKey(meterId));
214                 meterBuilder.addAugmentation(NodeMeterStatistics.class, new NodeMeterStatisticsBuilder().build());
215                 deviceContext.getDeviceMeterRegistry().store(meterId);
216                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, meterInstanceIdentifier, meterBuilder.build());
217             }
218         }
219         deviceContext.submitTransaction();
220     }
221
222     private static void processFlowStatistics(final Iterable<FlowsStatisticsUpdate> data, final DeviceContext deviceContext) {
223         deleteAllKnownFlows(deviceContext);
224         writeFlowStatistics(data, deviceContext);
225         deviceContext.submitTransaction();
226     }
227
228     public static void writeFlowStatistics(Iterable<FlowsStatisticsUpdate> data, DeviceContext deviceContext) {
229         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
230         for (final FlowsStatisticsUpdate flowsStatistics : data) {
231             for (final FlowAndStatisticsMapList flowStat : flowsStatistics.getFlowAndStatisticsMapList()) {
232                 final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
233                 flowBuilder.addAugmentation(FlowStatisticsData.class, refineFlowStatisticsAugmentation(flowStat).build());
234
235                 final short tableId = flowStat.getTableId();
236                 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(flowBuilder.build());
237                 final FlowId flowId = deviceContext.getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey, tableId);
238
239                 final FlowKey flowKey = new FlowKey(flowId);
240                 flowBuilder.setKey(flowKey);
241                 final TableKey tableKey = new TableKey(tableId);
242                 final InstanceIdentifier<Flow> flowIdent = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey);
243                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
244             }
245         }
246     }
247
248     /**
249      * Method extracts flow statistics out of flowAndStatistics model
250      *
251      * @param flowAndStats
252      */
253     private static FlowStatisticsDataBuilder refineFlowStatisticsAugmentation(final FlowAndStatisticsMapList flowAndStats) {
254         final FlowStatisticsBuilder flowStatisticsBuilder = new FlowStatisticsBuilder(flowAndStats);
255         final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder();
256         flowStatisticsDataBld.setFlowStatistics(flowStatisticsBuilder.build());
257         return flowStatisticsDataBld;
258     }
259
260     public static void deleteAllKnownFlows(final DeviceContext deviceContext) {
261         if (deviceContext.getDeviceState().deviceSynchronized()) {
262             InstanceIdentifier<FlowCapableNode> flowCapableNodePath = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
263             final Short numOfTablesOnDevice = deviceContext.getDeviceState().getFeatures().getTables();
264             for (short i = 0; i < numOfTablesOnDevice; i++) {
265                 final KeyedInstanceIdentifier<Table, TableKey> iiToTable = flowCapableNodePath.child(Table.class, new TableKey(i));
266                 final ReadTransaction readTx = deviceContext.getReadTransaction();
267                 final CheckedFuture<Optional<Table>, ReadFailedException> tableDataFuture = readTx.read(LogicalDatastoreType.OPERATIONAL, iiToTable);
268                 try {
269                     final Optional<Table> tableDataOpt = tableDataFuture.get();
270                     if (tableDataOpt.isPresent()) {
271                         final Table tableData = tableDataOpt.get();
272                         final Table table = new TableBuilder(tableData).setFlow(Collections.<Flow>emptyList()).build();
273                         deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToTable, table);
274                     }
275                 } catch (final InterruptedException e) {
276                     LOG.trace("Reading of table features for table wit ID {} was interrputed.", i);
277                 } catch (final ExecutionException e) {
278                     LOG.trace("Reading of table features for table wit ID {} encountered execution exception {}.", i, e);
279                 }
280             }
281             deviceContext.getDeviceFlowRegistry().removeMarked();
282         }
283     }
284
285     private static void processQueueStatistics(final Iterable<QueueStatisticsUpdate> data, final DeviceContext deviceContext) {
286         // TODO: clean all queues of all node-connectors before writing up-to-date stats
287         final InstanceIdentifier<Node> nodeIdent = deviceContext.getDeviceState().getNodeInstanceIdentifier();
288         for (final QueueStatisticsUpdate queueStatisticsUpdate : data) {
289             for (final QueueIdAndStatisticsMap queueStat : queueStatisticsUpdate.getQueueIdAndStatisticsMap()) {
290                 if (queueStat.getQueueId() != null) {
291                     final FlowCapableNodeConnectorQueueStatistics statChild =
292                             new FlowCapableNodeConnectorQueueStatisticsBuilder(queueStat).build();
293                     final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
294                             new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
295                     statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
296                     final QueueKey qKey = new QueueKey(queueStat.getQueueId());
297                     final InstanceIdentifier<Queue> queueIdent = nodeIdent
298                             .child(NodeConnector.class, new NodeConnectorKey(queueStat.getNodeConnectorId()))
299                             .augmentation(FlowCapableNodeConnector.class)
300                             .child(Queue.class, qKey);
301                     final QueueBuilder queueBuilder = new QueueBuilder()
302                             .setKey(qKey)
303                             .setQueueId(queueStat.getQueueId())
304                             // node-connector-id is already contained in parent node and the port-id here is of incompatible format
305                             .addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, statBuild.build());
306                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueIdent, queueBuilder.build());
307                 }
308             }
309         }
310         deviceContext.submitTransaction();
311     }
312
313     private static void processFlowTableStatistics(final Iterable<FlowTableStatisticsUpdate> data, final DeviceContext deviceContext) {
314         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
315         for (final FlowTableStatisticsUpdate flowTableStatisticsUpdate : data) {
316
317             for (final FlowTableAndStatisticsMap tableStat : flowTableStatisticsUpdate.getFlowTableAndStatisticsMap()) {
318                 final InstanceIdentifier<FlowTableStatistics> tStatIdent = fNodeIdent.child(Table.class, new TableKey(tableStat.getTableId().getValue()))
319                         .augmentation(FlowTableStatisticsData.class).child(FlowTableStatistics.class);
320                 final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
321                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);
322             }
323         }
324         deviceContext.submitTransaction();
325     }
326
327     private static void processNodeConnectorStatistics(final Iterable<NodeConnectorStatisticsUpdate> data, final DeviceContext deviceContext) {
328         final InstanceIdentifier<Node> nodeIdent = deviceContext.getDeviceState().getNodeInstanceIdentifier();
329         for (final NodeConnectorStatisticsUpdate nodeConnectorStatisticsUpdate : data) {
330             for (final NodeConnectorStatisticsAndPortNumberMap nConnectPort : nodeConnectorStatisticsUpdate.getNodeConnectorStatisticsAndPortNumberMap()) {
331                 final FlowCapableNodeConnectorStatistics stats = new FlowCapableNodeConnectorStatisticsBuilder(nConnectPort).build();
332                 final NodeConnectorKey key = new NodeConnectorKey(nConnectPort.getNodeConnectorId());
333                 final InstanceIdentifier<NodeConnector> nodeConnectorIdent = nodeIdent.child(NodeConnector.class, key);
334                 final InstanceIdentifier<FlowCapableNodeConnectorStatisticsData> nodeConnStatIdent = nodeConnectorIdent
335                         .augmentation(FlowCapableNodeConnectorStatisticsData.class);
336                 final InstanceIdentifier<FlowCapableNodeConnectorStatistics> flowCapNodeConnStatIdent =
337                         nodeConnStatIdent.child(FlowCapableNodeConnectorStatistics.class);
338                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowCapNodeConnStatIdent, stats);
339             }
340         }
341         deviceContext.submitTransaction();
342     }
343
344     private static void processMetersStatistics(final Iterable<MeterStatisticsUpdated> data,
345                                                 final DeviceContext deviceContext) {
346         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
347         for (final MeterStatisticsUpdated meterStatisticsUpdated : data) {
348             for (final MeterStats mStat : meterStatisticsUpdated.getMeterStats()) {
349                 final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
350                 final MeterId meterId = mStat.getMeterId();
351                 final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
352                 final InstanceIdentifier<NodeMeterStatistics> nodeMeterStatIdent = meterIdent
353                         .augmentation(NodeMeterStatistics.class);
354                 final InstanceIdentifier<MeterStatistics> msIdent = nodeMeterStatIdent.child(MeterStatistics.class);
355                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
356             }
357         }
358         deviceContext.submitTransaction();
359     }
360
361     private static void deleteAllKnownMeters(final DeviceContext deviceContext, final InstanceIdentifier<FlowCapableNode> fNodeIdent) {
362         for (final MeterId meterId : deviceContext.getDeviceMeterRegistry().getAllMeterIds()) {
363             final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
364             deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, meterIdent);
365         }
366         deviceContext.getDeviceMeterRegistry().removeMarked();
367     }
368
369     private static void processGroupDescStats(final Iterable<GroupDescStatsUpdated> data, final DeviceContext deviceContext) {
370         final InstanceIdentifier<FlowCapableNode> fNodeIdent =
371                 deviceContext.getDeviceState().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
372         deleteAllKnownGroups(deviceContext, fNodeIdent);
373
374         for (final GroupDescStatsUpdated groupDescStatsUpdated : data) {
375             for (final GroupDescStats groupDescStats : groupDescStatsUpdated.getGroupDescStats()) {
376                 final GroupId groupId = groupDescStats.getGroupId();
377
378                 final GroupBuilder groupBuilder = new GroupBuilder(groupDescStats);
379                 groupBuilder.setKey(new GroupKey(groupId));
380                 groupBuilder.addAugmentation(NodeGroupStatistics.class, new NodeGroupStatisticsBuilder().build());
381
382                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
383
384                 deviceContext.getDeviceGroupRegistry().store(groupId);
385                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, groupIdent, groupBuilder.build());
386             }
387         }
388         deviceContext.submitTransaction();
389     }
390
391     private static void deleteAllKnownGroups(final DeviceContext deviceContext, final InstanceIdentifier<FlowCapableNode> fNodeIdent) {
392         for (final GroupId groupId : deviceContext.getDeviceGroupRegistry().getAllGroupIds()) {
393             final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
394             deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, groupIdent);
395         }
396         deviceContext.getDeviceGroupRegistry().removeMarked();
397     }
398
399     private static void processGroupStatistics(final Iterable<GroupStatisticsUpdated> data, final DeviceContext deviceContext) {
400         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
401         for (final GroupStatisticsUpdated groupStatistics : data) {
402             for (final GroupStats groupStats : groupStatistics.getGroupStats()) {
403
404                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupStats.getGroupId()));
405                 final InstanceIdentifier<NodeGroupStatistics> nGroupStatIdent = groupIdent
406                         .augmentation(NodeGroupStatistics.class);
407
408                 final InstanceIdentifier<GroupStatistics> gsIdent = nGroupStatIdent.child(GroupStatistics.class);
409                 final GroupStatistics stats = new GroupStatisticsBuilder(groupStats).build();
410                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gsIdent, stats);
411             }
412         }
413         deviceContext.submitTransaction();
414     }
415
416     private static InstanceIdentifier<FlowCapableNode> assembleFlowCapableNodeInstanceIdentifier(final DeviceContext deviceContext) {
417         return deviceContext.getDeviceState().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
418     }
419 }