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