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