Bug-4957: Make async operational DS Read
[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.AsyncFunction;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.FutureFallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.JdkFutureAdapters;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import java.util.Collections;
21 import java.util.List;
22 import javax.annotation.Nullable;
23 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
27 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
28 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.StatisticsGatherer;
30 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
31 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatisticsBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatistics;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatistics;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.config.stats.reply.MeterConfigStats;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatistics;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatisticsBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatistics;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
94 import org.opendaylight.yangtools.yang.binding.DataObject;
95 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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         final 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 AsyncFunction<RpcResult<List<MultipartReply>>, Boolean>() {
134             @Nullable
135             @Override
136             public ListenableFuture<Boolean> apply(final RpcResult<List<MultipartReply>> rpcResult) {
137                 boolean isMultipartProcessed = Boolean.TRUE;
138                 if (rpcResult.isSuccessful()) {
139                     LOG.debug("Stats reply successfully received for node {} of type {}", deviceContext.getDeviceState().getNodeId(), type);
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 (final Exception e) {
155                             LOG.warn("stats processing of type {} for node {} failed during transfomation step",
156                                     type, deviceContext.getDeviceState().getNodeId(), e);
157                             return Futures.immediateFailedFuture(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                                 /* FlowStat Processing is realized by NettyThread only by initPhase, otherwise it is realized
174                                  * by MD-SAL thread */
175                                 return processFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData, deviceContext, eventIdentifier);
176
177                             } else if (multipartData instanceof GroupDescStatsUpdated) {
178                                 processGroupDescStats((Iterable<GroupDescStatsUpdated>) allMultipartData, deviceContext);
179                             } else if (multipartData instanceof MeterConfigStatsUpdated) {
180                                 processMeterConfigStatsUpdated((Iterable<MeterConfigStatsUpdated>) allMultipartData, deviceContext);
181                             } else {
182                                 isMultipartProcessed = Boolean.FALSE;
183                             }
184                         } catch (final Exception e) {
185                             LOG.warn("stats processing of type {} for node {} failed during write-to-tx step",
186                                     type, deviceContext.getDeviceState().getNodeId(), e);
187                             return Futures.immediateFailedFuture(e);
188                         }
189
190                         LOG.debug("Stats reply added to transaction for node {} of type {}", deviceContext.getDeviceState().getNodeId(), type);
191
192                         //TODO : implement experimenter
193                     } else {
194                         LOG.debug("Stats reply was empty for node {} of type {}", deviceContext.getDeviceState().getNodeId(), type);
195                     }
196
197                 } else {
198                     LOG.debug("Stats reply FAILED for node {} of type {}: {}", deviceContext.getDeviceState().getNodeId(), type, rpcResult.getErrors());
199                     isMultipartProcessed = Boolean.FALSE;
200                 }
201                 return Futures.immediateFuture(isMultipartProcessed);
202             }
203         });
204     }
205
206     private static void processMeterConfigStatsUpdated(final Iterable<MeterConfigStatsUpdated> data, final DeviceContext deviceContext) {
207         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
208         deleteAllKnownMeters(deviceContext, fNodeIdent);
209         for (final MeterConfigStatsUpdated meterConfigStatsUpdated : data) {
210             for (final MeterConfigStats meterConfigStats : meterConfigStatsUpdated.getMeterConfigStats()) {
211                 final MeterId meterId = meterConfigStats.getMeterId();
212                 final InstanceIdentifier<Meter> meterInstanceIdentifier = fNodeIdent.child(Meter.class, new MeterKey(meterId));
213
214                 final MeterBuilder meterBuilder = new MeterBuilder(meterConfigStats);
215                 meterBuilder.setKey(new MeterKey(meterId));
216                 meterBuilder.addAugmentation(NodeMeterStatistics.class, new NodeMeterStatisticsBuilder().build());
217                 deviceContext.getDeviceMeterRegistry().store(meterId);
218                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, meterInstanceIdentifier, meterBuilder.build());
219             }
220         }
221         deviceContext.submitTransaction();
222     }
223
224     private static ListenableFuture<Boolean> processFlowStatistics(final Iterable<FlowsStatisticsUpdate> data,
225             final DeviceContext deviceContext, final EventIdentifier eventIdentifier) {
226         final ListenableFuture<Void> deleFuture = deleteAllKnownFlows(deviceContext);
227         return Futures.transform(deleFuture, new Function<Void, Boolean>() {
228
229             @Override
230             public Boolean apply(final Void input) {
231                 writeFlowStatistics(data, deviceContext);
232                 deviceContext.submitTransaction();
233                 EventsTimeCounter.markEnd(eventIdentifier);
234                 return Boolean.TRUE;
235             }
236         });
237     }
238
239     public static void writeFlowStatistics(final Iterable<FlowsStatisticsUpdate> data, final DeviceContext deviceContext) {
240         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
241         for (final FlowsStatisticsUpdate flowsStatistics : data) {
242             for (final FlowAndStatisticsMapList flowStat : flowsStatistics.getFlowAndStatisticsMapList()) {
243                 final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
244                 flowBuilder.addAugmentation(FlowStatisticsData.class, refineFlowStatisticsAugmentation(flowStat).build());
245
246                 final short tableId = flowStat.getTableId();
247                 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(flowBuilder.build());
248                 final FlowId flowId = deviceContext.getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey, tableId);
249
250                 final FlowKey flowKey = new FlowKey(flowId);
251                 flowBuilder.setKey(flowKey);
252                 final TableKey tableKey = new TableKey(tableId);
253                 final InstanceIdentifier<Flow> flowIdent = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey);
254                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
255             }
256         }
257     }
258
259     /**
260      * Method extracts flow statistics out of flowAndStatistics model
261      *
262      * @param flowAndStats
263      */
264     private static FlowStatisticsDataBuilder refineFlowStatisticsAugmentation(final FlowAndStatisticsMapList flowAndStats) {
265         final FlowStatisticsBuilder flowStatisticsBuilder = new FlowStatisticsBuilder(flowAndStats);
266         final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder();
267         flowStatisticsDataBld.setFlowStatistics(flowStatisticsBuilder.build());
268         return flowStatisticsDataBld;
269     }
270
271     public static ListenableFuture<Void> deleteAllKnownFlows(final DeviceContext deviceContext) {
272         /* DeviceState.deviceSynchronized is a marker for actual phase - false means initPhase, true means noInitPhase */
273         if (deviceContext.getDeviceState().deviceSynchronized()) {
274             final InstanceIdentifier<FlowCapableNode> flowCapableNodePath = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
275             final ReadOnlyTransaction readTx = deviceContext.getReadTransaction();
276             final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowCapableNodeFuture = readTx.read(
277                     LogicalDatastoreType.OPERATIONAL, flowCapableNodePath);
278
279             /* we wish to close readTx for fallBack */
280             Futures.withFallback(flowCapableNodeFuture, new FutureFallback<Optional<FlowCapableNode>>() {
281
282                 @Override
283                 public ListenableFuture<Optional<FlowCapableNode>> create(final Throwable t) throws Exception {
284                     readTx.close();
285                     return Futures.immediateFailedFuture(t);
286                 }
287             });
288             /*
289              * we have to read actual tables with all information before we set empty Flow list, merge is expensive and
290              * not applicable for lists
291              */
292             return Futures.transform(flowCapableNodeFuture, new AsyncFunction<Optional<FlowCapableNode>, Void>() {
293
294                 @Override
295                 public ListenableFuture<Void> apply(final Optional<FlowCapableNode> flowCapNodeOpt) throws Exception {
296                     if (flowCapNodeOpt.isPresent()) {
297                         for (final Table tableData : flowCapNodeOpt.get().getTable()) {
298                             final Table table = new TableBuilder(tableData).setFlow(Collections.<Flow> emptyList()).build();
299                             final InstanceIdentifier<Table> iiToTable = flowCapableNodePath.child(Table.class, tableData.getKey());
300                             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToTable, table);
301                         }
302                     }
303                     deviceContext.getDeviceFlowRegistry().removeMarked();
304                     readTx.close();
305                     return Futures.immediateFuture(null);
306                 }
307
308             });
309         }
310         return Futures.immediateFuture(null);
311     }
312
313     private static void processQueueStatistics(final Iterable<QueueStatisticsUpdate> data, final DeviceContext deviceContext) {
314         // TODO: clean all queues of all node-connectors before writing up-to-date stats
315         final InstanceIdentifier<Node> nodeIdent = deviceContext.getDeviceState().getNodeInstanceIdentifier();
316         for (final QueueStatisticsUpdate queueStatisticsUpdate : data) {
317             for (final QueueIdAndStatisticsMap queueStat : queueStatisticsUpdate.getQueueIdAndStatisticsMap()) {
318                 if (queueStat.getQueueId() != null) {
319                     final FlowCapableNodeConnectorQueueStatistics statChild =
320                             new FlowCapableNodeConnectorQueueStatisticsBuilder(queueStat).build();
321                     final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
322                             new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
323                     statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
324                     final QueueKey qKey = new QueueKey(queueStat.getQueueId());
325                     final InstanceIdentifier<Queue> queueIdent = nodeIdent
326                             .child(NodeConnector.class, new NodeConnectorKey(queueStat.getNodeConnectorId()))
327                             .augmentation(FlowCapableNodeConnector.class)
328                             .child(Queue.class, qKey);
329                     final QueueBuilder queueBuilder = new QueueBuilder()
330                             .setKey(qKey)
331                             .setQueueId(queueStat.getQueueId())
332                             // node-connector-id is already contained in parent node and the port-id here is of incompatible format
333                             .addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, statBuild.build());
334                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueIdent, queueBuilder.build());
335                 }
336             }
337         }
338         deviceContext.submitTransaction();
339     }
340
341     private static void processFlowTableStatistics(final Iterable<FlowTableStatisticsUpdate> data, final DeviceContext deviceContext) {
342         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
343         for (final FlowTableStatisticsUpdate flowTableStatisticsUpdate : data) {
344
345             for (final FlowTableAndStatisticsMap tableStat : flowTableStatisticsUpdate.getFlowTableAndStatisticsMap()) {
346                 final InstanceIdentifier<FlowTableStatistics> tStatIdent = fNodeIdent.child(Table.class, new TableKey(tableStat.getTableId().getValue()))
347                         .augmentation(FlowTableStatisticsData.class).child(FlowTableStatistics.class);
348                 final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
349                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);
350             }
351         }
352         deviceContext.submitTransaction();
353     }
354
355     private static void processNodeConnectorStatistics(final Iterable<NodeConnectorStatisticsUpdate> data, final DeviceContext deviceContext) {
356         final InstanceIdentifier<Node> nodeIdent = deviceContext.getDeviceState().getNodeInstanceIdentifier();
357         for (final NodeConnectorStatisticsUpdate nodeConnectorStatisticsUpdate : data) {
358             for (final NodeConnectorStatisticsAndPortNumberMap nConnectPort : nodeConnectorStatisticsUpdate.getNodeConnectorStatisticsAndPortNumberMap()) {
359                 final FlowCapableNodeConnectorStatistics stats = new FlowCapableNodeConnectorStatisticsBuilder(nConnectPort).build();
360                 final NodeConnectorKey key = new NodeConnectorKey(nConnectPort.getNodeConnectorId());
361                 final InstanceIdentifier<NodeConnector> nodeConnectorIdent = nodeIdent.child(NodeConnector.class, key);
362                 final InstanceIdentifier<FlowCapableNodeConnectorStatisticsData> nodeConnStatIdent = nodeConnectorIdent
363                         .augmentation(FlowCapableNodeConnectorStatisticsData.class);
364                 final InstanceIdentifier<FlowCapableNodeConnectorStatistics> flowCapNodeConnStatIdent =
365                         nodeConnStatIdent.child(FlowCapableNodeConnectorStatistics.class);
366                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowCapNodeConnStatIdent, stats);
367             }
368         }
369         deviceContext.submitTransaction();
370     }
371
372     private static void processMetersStatistics(final Iterable<MeterStatisticsUpdated> data,
373                                                 final DeviceContext deviceContext) {
374         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
375         for (final MeterStatisticsUpdated meterStatisticsUpdated : data) {
376             for (final MeterStats mStat : meterStatisticsUpdated.getMeterStats()) {
377                 final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
378                 final MeterId meterId = mStat.getMeterId();
379                 final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
380                 final InstanceIdentifier<NodeMeterStatistics> nodeMeterStatIdent = meterIdent
381                         .augmentation(NodeMeterStatistics.class);
382                 final InstanceIdentifier<MeterStatistics> msIdent = nodeMeterStatIdent.child(MeterStatistics.class);
383                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
384             }
385         }
386         deviceContext.submitTransaction();
387     }
388
389     private static void deleteAllKnownMeters(final DeviceContext deviceContext, final InstanceIdentifier<FlowCapableNode> fNodeIdent) {
390         for (final MeterId meterId : deviceContext.getDeviceMeterRegistry().getAllMeterIds()) {
391             final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
392             deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, meterIdent);
393         }
394         deviceContext.getDeviceMeterRegistry().removeMarked();
395     }
396
397     private static void processGroupDescStats(final Iterable<GroupDescStatsUpdated> data, final DeviceContext deviceContext) {
398         final InstanceIdentifier<FlowCapableNode> fNodeIdent =
399                 deviceContext.getDeviceState().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
400         deleteAllKnownGroups(deviceContext, fNodeIdent);
401
402         for (final GroupDescStatsUpdated groupDescStatsUpdated : data) {
403             for (final GroupDescStats groupDescStats : groupDescStatsUpdated.getGroupDescStats()) {
404                 final GroupId groupId = groupDescStats.getGroupId();
405
406                 final GroupBuilder groupBuilder = new GroupBuilder(groupDescStats);
407                 groupBuilder.setKey(new GroupKey(groupId));
408                 groupBuilder.addAugmentation(NodeGroupStatistics.class, new NodeGroupStatisticsBuilder().build());
409
410                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
411
412                 deviceContext.getDeviceGroupRegistry().store(groupId);
413                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, groupIdent, groupBuilder.build());
414             }
415         }
416         deviceContext.submitTransaction();
417     }
418
419     private static void deleteAllKnownGroups(final DeviceContext deviceContext, final InstanceIdentifier<FlowCapableNode> fNodeIdent) {
420         for (final GroupId groupId : deviceContext.getDeviceGroupRegistry().getAllGroupIds()) {
421             final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
422             deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, groupIdent);
423         }
424         deviceContext.getDeviceGroupRegistry().removeMarked();
425     }
426
427     private static void processGroupStatistics(final Iterable<GroupStatisticsUpdated> data, final DeviceContext deviceContext) {
428         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
429         for (final GroupStatisticsUpdated groupStatistics : data) {
430             for (final GroupStats groupStats : groupStatistics.getGroupStats()) {
431
432                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupStats.getGroupId()));
433                 final InstanceIdentifier<NodeGroupStatistics> nGroupStatIdent = groupIdent
434                         .augmentation(NodeGroupStatistics.class);
435
436                 final InstanceIdentifier<GroupStatistics> gsIdent = nGroupStatIdent.child(GroupStatistics.class);
437                 final GroupStatistics stats = new GroupStatisticsBuilder(groupStats).build();
438                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gsIdent, stats);
439             }
440         }
441         deviceContext.submitTransaction();
442     }
443
444     private static InstanceIdentifier<FlowCapableNode> assembleFlowCapableNodeInstanceIdentifier(final DeviceContext deviceContext) {
445         return deviceContext.getDeviceState().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
446     }
447 }