Merge "Multipart processing - gett id from device context."
[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 java.util.concurrent.ExecutionException;
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
15 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
17 import com.google.common.collect.Iterables;
18 import java.util.Collections;
19 import com.google.common.base.Function;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.JdkFutureAdapters;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import java.util.List;
24 import javax.annotation.Nullable;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
27 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
28 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
29 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowHash;
30 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
31 import org.opendaylight.openflowplugin.impl.registry.flow.FlowHashFactory;
32 import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
33 import org.opendaylight.openflowplugin.impl.util.FlowUtil;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
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.table.statistics.rev131215.FlowTableStatisticsData;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatisticsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatistics;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
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.inventory.rev130819.nodes.NodeKey;
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.binding.KeyedInstanceIdentifier;
97 import org.opendaylight.yangtools.yang.common.RpcResult;
98 import org.slf4j.Logger;
99 import org.slf4j.LoggerFactory;
100
101 /**
102  * Created by Martin Bobak <mbobak@cisco.com> on 2.4.2015.
103  */
104 public final class StatisticsGatheringUtils {
105
106     private static final Logger LOG = LoggerFactory.getLogger(StatisticsGatheringUtils.class);
107     private static final SinglePurposeMultipartReplyTranslator MULTIPART_REPLY_TRANSLATOR = new SinglePurposeMultipartReplyTranslator();
108
109     private StatisticsGatheringUtils() {
110         throw new IllegalStateException("This class should not be instantiated.");
111     }
112
113     private static KeyedInstanceIdentifier<Node, NodeKey> getInstanceIdentifier(final DeviceContext deviceContext) {
114         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(deviceContext.getPrimaryConnectionContext().getNodeId()));
115     }
116
117     public static ListenableFuture<Boolean> gatherStatistics(final StatisticsGatheringService statisticsGatheringService,
118                                                              final DeviceContext deviceContext,
119                                                              final MultipartType type) {
120         //FIXME : anytype listener must not be send as parameter, it has to be extracted from device context inside service
121         final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture =
122                 JdkFutureAdapters.listenInPoolThread(statisticsGatheringService.getStatisticsOfType(type));
123         return transformAndStoreStatisticsData(statisticsDataInFuture, deviceContext);
124     }
125
126     private static ListenableFuture<Boolean> transformAndStoreStatisticsData(final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture,
127                                                                              final DeviceContext deviceContext) {
128         return Futures.transform(statisticsDataInFuture, new Function<RpcResult<List<MultipartReply>>, Boolean>() {
129             @Nullable
130             @Override
131             public Boolean apply(final RpcResult<List<MultipartReply>> rpcResult) {
132                 if (rpcResult.isSuccessful()) {
133                     boolean isMultipartProcessed = Boolean.TRUE;
134                     Iterable<? extends DataObject> allMultipartData = Collections.emptyList();
135                     DataObject multipartData = null;
136                     for (final MultipartReply singleReply : rpcResult.getResult()) {
137                         final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply);
138                         multipartData = multipartDataList.get(0);
139                         allMultipartData = Iterables.concat(allMultipartData, multipartDataList);
140                     }
141
142                     if (deviceContext.getDeviceState().isValid()) {
143                         deviceContext.startGatheringOperationsToOneTransaction();
144                     }
145
146                     if (multipartData instanceof GroupStatisticsUpdated) {
147                         processGroupStatistics((Iterable<GroupStatisticsUpdated>)allMultipartData, deviceContext);
148                     } else if (multipartData instanceof MeterStatisticsUpdated) {
149                         processMetersStatistics((Iterable<MeterStatisticsUpdated>)allMultipartData, deviceContext);
150                     } else if (multipartData instanceof NodeConnectorStatisticsUpdate) {
151                         processNodeConnectorStatistics((Iterable<NodeConnectorStatisticsUpdate>) allMultipartData, deviceContext);
152                     } else if (multipartData instanceof FlowTableStatisticsUpdate) {
153                         processFlowTableStatistics((Iterable<FlowTableStatisticsUpdate>) allMultipartData, deviceContext);
154                     } else if (multipartData instanceof QueueStatisticsUpdate) {
155                         processQueueStatistics((Iterable<QueueStatisticsUpdate>) allMultipartData, deviceContext);
156                     } else if (multipartData instanceof FlowsStatisticsUpdate) {
157                         processFlowStatistics((Iterable<FlowsStatisticsUpdate>)allMultipartData, deviceContext);
158                     } else if (multipartData instanceof GroupDescStatsUpdated) {
159                         processGroupDescStats((Iterable<GroupDescStatsUpdated>) allMultipartData, deviceContext);
160                     } else if (multipartData instanceof MeterConfigStatsUpdated) {
161                         processMeterConfigStatsUpdated((Iterable<MeterConfigStatsUpdated>) allMultipartData, deviceContext);
162                     } else {
163                         isMultipartProcessed = Boolean.FALSE;
164                     }
165                     //TODO : implement experimenter
166
167                     if (deviceContext.getDeviceState().isValid()) {
168                         deviceContext.commitOperationsGatheredInOneTransaction();
169                     }
170                     return isMultipartProcessed;
171                 }
172                 return Boolean.FALSE;
173             }
174         });
175     }
176
177     private static void processMeterConfigStatsUpdated(final Iterable<MeterConfigStatsUpdated> data, final DeviceContext deviceContext) {
178         for (final MeterConfigStatsUpdated meterConfigStatsUpdated : data) {
179             NodeId nodeId = meterConfigStatsUpdated.getId();
180             final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(nodeId);
181             for (MeterConfigStats meterConfigStats : meterConfigStatsUpdated.getMeterConfigStats()) {
182                 final MeterId meterId = meterConfigStats.getMeterId();
183                 final InstanceIdentifier<Meter> meterInstanceIdentifier = fNodeIdent.child(Meter.class, new MeterKey(meterId));
184     
185                 final MeterBuilder meterBuilder = new MeterBuilder(meterConfigStats);
186                 meterBuilder.setKey(new MeterKey(meterId));
187                 meterBuilder.addAugmentation(NodeMeterStatistics.class, new NodeMeterStatisticsBuilder().build());
188                 deviceContext.getDeviceMeterRegistry().store(meterId);
189                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, meterInstanceIdentifier, meterBuilder.build());
190             }
191         }
192     }
193
194     private static void processFlowStatistics(final Iterable<FlowsStatisticsUpdate> data, final DeviceContext deviceContext) {
195         final NodeId nodeId = deviceContext.getDeviceState().getNodeId();
196         final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
197                 .child(Node.class, new NodeKey(nodeId));
198         deleteAllKnownFlows(deviceContext, nodeIdent);
199         for (final FlowsStatisticsUpdate flowsStatistics : data) {
200             for (final FlowAndStatisticsMapList flowStat : flowsStatistics.getFlowAndStatisticsMapList()) {
201                 final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
202                 FlowId flowId = null;
203                 FlowHash flowHash = FlowHashFactory.create(flowBuilder.build(), deviceContext.getPrimaryConnectionContext().getFeatures().getVersion());
204                 short tableId = flowStat.getTableId();
205                 final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
206                 FlowDescriptor flowDescriptor;
207                 flowDescriptor = deviceFlowRegistry.retrieveIdForFlow(flowHash);
208                 if (null != flowDescriptor) {
209                     flowId = flowDescriptor.getFlowId();
210                 } else {
211                     LOG.trace("Flow descriptor for flow hash {} wasn't found.", flowHash.hashCode());
212                     flowId = FlowUtil.createAlienFlowId(tableId);
213                     flowDescriptor = FlowDescriptorFactory.create(tableId, flowId);
214                     deviceFlowRegistry.store(flowHash, flowDescriptor);
215                 }
216                 FlowKey flowKey = new FlowKey(flowId);
217                 flowBuilder.setKey(flowKey);
218                 final TableKey tableKey = new TableKey(tableId);
219                 final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(flowsStatistics.getId());
220                 final InstanceIdentifier<Flow> flowIdent = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey);
221                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
222             }
223         }
224     }
225
226     private static void deleteAllKnownFlows(final DeviceContext deviceContext, final InstanceIdentifier<Node> nodeIdent) {
227         if (deviceContext.getDeviceState().deviceSynchronized()) {
228             final Short numOfTablesOnDevice = deviceContext.getDeviceState().getFeatures().getTables();
229             for (short i=0; i<numOfTablesOnDevice; i++) {
230                 KeyedInstanceIdentifier<Table, TableKey> iiToTable
231                     = nodeIdent.augmentation(FlowCapableNode.class).child( Table.class, new TableKey(i) );
232                 final ReadTransaction readTx = deviceContext.getReadTransaction();
233                 CheckedFuture<Optional<Table>, ReadFailedException> tableDataFuture = readTx.read(LogicalDatastoreType.OPERATIONAL, iiToTable);
234                 try {
235                     Optional<Table> tableDataOpt = tableDataFuture.get();
236                     if (tableDataOpt.isPresent()) {
237                         final Table tableData = tableDataOpt.get();
238                         final Table table = new TableBuilder(tableData).setFlow(Collections.<Flow>emptyList()).build();
239                         deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToTable, table);
240                     }
241                 } catch (InterruptedException e) {
242                     LOG.trace("Reading of table features for table wit ID {} was interrputed.",i);
243                 } catch (ExecutionException e) {
244                     LOG.trace("Reading of table features for table wit ID {} encountered execution exception {}.",i,e);
245                 }
246             }
247         }
248     }
249
250     private static void processQueueStatistics(final Iterable<QueueStatisticsUpdate> data, final DeviceContext deviceContext) {
251         for (final QueueStatisticsUpdate queueStatisticsUpdate : data) {
252             final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
253                     .child(Node.class, new NodeKey(queueStatisticsUpdate.getId()));
254             for (final QueueIdAndStatisticsMap queueStat : queueStatisticsUpdate.getQueueIdAndStatisticsMap()) {
255                 if (queueStat.getQueueId() != null) {
256                     final FlowCapableNodeConnectorQueueStatistics statChild =
257                             new FlowCapableNodeConnectorQueueStatisticsBuilder(queueStat).build();
258                     final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
259                             new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
260                     statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
261                     final QueueKey qKey = new QueueKey(queueStat.getQueueId());
262                     final InstanceIdentifier<Queue> queueIdent = nodeIdent
263                             .child(NodeConnector.class, new NodeConnectorKey(queueStat.getNodeConnectorId()))
264                             .augmentation(FlowCapableNodeConnector.class)
265                             .child(Queue.class, qKey);
266                     final InstanceIdentifier<FlowCapableNodeConnectorQueueStatisticsData> queueStatIdent = queueIdent.augmentation(FlowCapableNodeConnectorQueueStatisticsData.class);
267                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueStatIdent, statBuild.build());
268                 }
269             }
270         }
271     }
272
273     private static void processFlowTableStatistics(final Iterable<FlowTableStatisticsUpdate> data, final DeviceContext deviceContext) {
274         for(final FlowTableStatisticsUpdate flowTableStatisticsUpdate : data) {
275             final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(flowTableStatisticsUpdate.getId());
276     
277             for (final FlowTableAndStatisticsMap tableStat : flowTableStatisticsUpdate.getFlowTableAndStatisticsMap()) {
278                 final InstanceIdentifier<FlowTableStatistics> tStatIdent = fNodeIdent.child(Table.class, new TableKey(tableStat.getTableId().getValue()))
279                         .augmentation(FlowTableStatisticsData.class).child(FlowTableStatistics.class);
280                 final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
281                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);
282             }
283         }
284     }
285
286     private static void processNodeConnectorStatistics(final Iterable<NodeConnectorStatisticsUpdate> data, final DeviceContext deviceContext) {
287         for (final NodeConnectorStatisticsUpdate nodeConnectorStatisticsUpdate : data) {
288             final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
289                     .child(Node.class, new NodeKey(nodeConnectorStatisticsUpdate.getId()));
290             for (final NodeConnectorStatisticsAndPortNumberMap nConnectPort : nodeConnectorStatisticsUpdate.getNodeConnectorStatisticsAndPortNumberMap()) {
291                 final FlowCapableNodeConnectorStatistics stats = new FlowCapableNodeConnectorStatisticsBuilder(nConnectPort).build();
292                 final NodeConnectorKey key = new NodeConnectorKey(nConnectPort.getNodeConnectorId());
293                 final InstanceIdentifier<NodeConnector> nodeConnectorIdent = nodeIdent.child(NodeConnector.class, key);
294                 final InstanceIdentifier<FlowCapableNodeConnectorStatisticsData> nodeConnStatIdent = nodeConnectorIdent
295                         .augmentation(FlowCapableNodeConnectorStatisticsData.class);
296                 final InstanceIdentifier<FlowCapableNodeConnectorStatistics> flowCapNodeConnStatIdent =
297                         nodeConnStatIdent.child(FlowCapableNodeConnectorStatistics.class);
298                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowCapNodeConnStatIdent, stats);
299             }
300         }
301     }
302
303     private static void processMetersStatistics(final Iterable<MeterStatisticsUpdated> data,
304                                                 final DeviceContext deviceContext) {
305         NodeId nodeId = deviceContext.getDeviceState().getNodeId();
306         final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(nodeId);
307         deleteAllKnownMeters(deviceContext, fNodeIdent);
308         for (MeterStatisticsUpdated meterStatisticsUpdated : data) {
309             for (final MeterStats mStat : meterStatisticsUpdated.getMeterStats()) {
310                 final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
311                 final MeterId meterId = mStat.getMeterId();
312                 final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
313                 final InstanceIdentifier<NodeMeterStatistics> nodeMeterStatIdent = meterIdent
314                         .augmentation(NodeMeterStatistics.class);
315                 final InstanceIdentifier<MeterStatistics> msIdent = nodeMeterStatIdent.child(MeterStatistics.class);
316                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
317             }
318         }
319     }
320
321     private static void deleteAllKnownMeters(final DeviceContext deviceContext, final InstanceIdentifier<FlowCapableNode> fNodeIdent) {
322         for (MeterId meterId : deviceContext.getDeviceMeterRegistry().getAllMeterIds()) {
323             final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
324             deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, meterIdent);
325         }
326         deviceContext.getDeviceMeterRegistry().removeMarked();
327     }
328
329     private static void processGroupDescStats(final Iterable<GroupDescStatsUpdated> data, final DeviceContext deviceContext) {
330         for ( GroupDescStatsUpdated groupDescStatsUpdated : data) {
331             NodeId nodeId = groupDescStatsUpdated.getId();
332             final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(nodeId);
333     
334             for (GroupDescStats groupDescStats : groupDescStatsUpdated.getGroupDescStats()) {
335                 final GroupId groupId = groupDescStats.getGroupId();
336     
337                 final GroupBuilder groupBuilder = new GroupBuilder(groupDescStats);
338                 groupBuilder.setKey(new GroupKey(groupId));
339                 groupBuilder.addAugmentation(NodeGroupStatistics.class, new NodeGroupStatisticsBuilder().build());
340     
341                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
342     
343                 deviceContext.getDeviceGroupRegistry().store(groupId);
344                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, groupIdent, groupBuilder.build());
345             }
346         }
347     }
348
349     private static void deleteAllKnownGroups(final DeviceContext deviceContext, final InstanceIdentifier<FlowCapableNode> fNodeIdent) {
350         for (GroupId groupId : deviceContext.getDeviceGroupRegistry().getAllGroupIds()) {
351             final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
352             deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, groupIdent);
353         }
354         deviceContext.getDeviceGroupRegistry().removeMarked();
355     }
356
357     private static void processGroupStatistics(final Iterable<GroupStatisticsUpdated> data, final DeviceContext deviceContext) {
358         NodeId nodeId = deviceContext.getDeviceState().getNodeId();
359         final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(nodeId);
360         deleteAllKnownGroups(deviceContext, fNodeIdent);
361         for (GroupStatisticsUpdated groupStatistics : data) {
362             for (final GroupStats groupStats : groupStatistics.getGroupStats()) {
363     
364                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupStats.getGroupId()));
365                 final InstanceIdentifier<NodeGroupStatistics> nGroupStatIdent = groupIdent
366                         .augmentation(NodeGroupStatistics.class);
367     
368                 final InstanceIdentifier<GroupStatistics> gsIdent = nGroupStatIdent.child(GroupStatistics.class);
369                 final GroupStatistics stats = new GroupStatisticsBuilder(groupStats).build();
370                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gsIdent, stats);
371             }
372         }
373     }
374
375     private static InstanceIdentifier<FlowCapableNode> getFlowCapableNodeInstanceIdentifier(final NodeId nodeId) {
376         final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
377                 .create(Nodes.class).child(Node.class, new NodeKey(nodeId));
378         return nodeIdent.augmentation(FlowCapableNode.class);
379     }
380 }