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