Merge "BUG-2188: To populate the port_number of switches - yang model"
[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         final String deviceId = deviceContext.getPrimaryConnectionContext().getNodeId().toString();
118         EventIdentifier wholeProcessEventIdentifier = null;
119         if (MultipartType.OFPMPFLOW.equals(type)) {
120             wholeProcessEventIdentifier = new EventIdentifier(type.toString(), deviceId);
121             EventsTimeCounter.markStart(wholeProcessEventIdentifier);
122         }
123         EventIdentifier ofpQueuToRequestContextEventIdentifier = new EventIdentifier(QUEUE2_REQCTX + type.toString(), deviceId);
124         final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture =
125                 JdkFutureAdapters.listenInPoolThread(statisticsGatheringService.getStatisticsOfType(
126                         ofpQueuToRequestContextEventIdentifier, type));
127         return transformAndStoreStatisticsData(statisticsDataInFuture, deviceContext, wholeProcessEventIdentifier);
128     }
129
130     private static ListenableFuture<Boolean> transformAndStoreStatisticsData(final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture,
131                                                                              final DeviceContext deviceContext,
132                                                                              final EventIdentifier eventIdentifier) {
133         return Futures.transform(statisticsDataInFuture, new Function<RpcResult<List<MultipartReply>>, Boolean>() {
134             @Nullable
135             @Override
136             public Boolean apply(final RpcResult<List<MultipartReply>> rpcResult) {
137                 if (rpcResult.isSuccessful()) {
138                     boolean isMultipartProcessed = Boolean.TRUE;
139
140                     // TODO: in case the result value is null then multipart data probably got processed on the fly -
141                     // TODO: this contract should by clearly stated and enforced - now simple true value is returned
142                     if (null != rpcResult.getResult()) {
143                         Iterable<? extends DataObject> allMultipartData = Collections.emptyList();
144                         DataObject multipartData = null;
145                         for (final MultipartReply singleReply : rpcResult.getResult()) {
146                             final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply);
147                             multipartData = multipartDataList.get(0);
148                             allMultipartData = Iterables.concat(allMultipartData, multipartDataList);
149                         }
150
151                         if (multipartData instanceof GroupStatisticsUpdated) {
152                             processGroupStatistics((Iterable<GroupStatisticsUpdated>) allMultipartData, deviceContext);
153                         } else if (multipartData instanceof MeterStatisticsUpdated) {
154                             processMetersStatistics((Iterable<MeterStatisticsUpdated>) allMultipartData, deviceContext);
155                         } else if (multipartData instanceof NodeConnectorStatisticsUpdate) {
156                             processNodeConnectorStatistics((Iterable<NodeConnectorStatisticsUpdate>) allMultipartData, deviceContext);
157                         } else if (multipartData instanceof FlowTableStatisticsUpdate) {
158                             processFlowTableStatistics((Iterable<FlowTableStatisticsUpdate>) allMultipartData, deviceContext);
159                         } else if (multipartData instanceof QueueStatisticsUpdate) {
160                             processQueueStatistics((Iterable<QueueStatisticsUpdate>) allMultipartData, deviceContext);
161                         } else if (multipartData instanceof FlowsStatisticsUpdate) {
162                             processFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData, deviceContext);
163                             EventsTimeCounter.markEnd(eventIdentifier);
164                         } else if (multipartData instanceof GroupDescStatsUpdated) {
165                             processGroupDescStats((Iterable<GroupDescStatsUpdated>) allMultipartData, deviceContext);
166                         } else if (multipartData instanceof MeterConfigStatsUpdated) {
167                             processMeterConfigStatsUpdated((Iterable<MeterConfigStatsUpdated>) allMultipartData, deviceContext);
168                         } else {
169                             isMultipartProcessed = Boolean.FALSE;
170                         }
171                         //TODO : implement experimenter
172                     }
173
174                     return isMultipartProcessed;
175                 }
176                 return Boolean.FALSE;
177             }
178         });
179     }
180
181     private static void processMeterConfigStatsUpdated(final Iterable<MeterConfigStatsUpdated> data, final DeviceContext deviceContext) {
182         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
183         deleteAllKnownMeters(deviceContext, fNodeIdent);
184         for (final MeterConfigStatsUpdated meterConfigStatsUpdated : data) {
185             for (final MeterConfigStats meterConfigStats : meterConfigStatsUpdated.getMeterConfigStats()) {
186                 final MeterId meterId = meterConfigStats.getMeterId();
187                 final InstanceIdentifier<Meter> meterInstanceIdentifier = fNodeIdent.child(Meter.class, new MeterKey(meterId));
188
189                 final MeterBuilder meterBuilder = new MeterBuilder(meterConfigStats);
190                 meterBuilder.setKey(new MeterKey(meterId));
191                 meterBuilder.addAugmentation(NodeMeterStatistics.class, new NodeMeterStatisticsBuilder().build());
192                 deviceContext.getDeviceMeterRegistry().store(meterId);
193                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, meterInstanceIdentifier, meterBuilder.build());
194             }
195         }
196         deviceContext.submitTransaction();
197     }
198
199     private static void processFlowStatistics(final Iterable<FlowsStatisticsUpdate> data, final DeviceContext deviceContext) {
200         deleteAllKnownFlows(deviceContext);
201         writeFlowStatistics(data, deviceContext);
202         deviceContext.submitTransaction();
203     }
204
205     public static void writeFlowStatistics(Iterable<FlowsStatisticsUpdate> data, DeviceContext deviceContext) {
206         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
207         for (final FlowsStatisticsUpdate flowsStatistics : data) {
208             for (final FlowAndStatisticsMapList flowStat : flowsStatistics.getFlowAndStatisticsMapList()) {
209                 final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
210                 flowBuilder.addAugmentation(FlowStatisticsData.class, refineFlowStatisticsAugmentation(flowStat).build());
211
212                 final short tableId = flowStat.getTableId();
213                 final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(flowBuilder.build());
214                 final FlowId flowId = deviceContext.getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey, tableId);
215
216                 final FlowKey flowKey = new FlowKey(flowId);
217                 flowBuilder.setKey(flowKey);
218                 final TableKey tableKey = new TableKey(tableId);
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     /**
226      * Method extracts flow statistics out of flowAndStatistics model
227      *
228      * @param flowAndStats
229      */
230     private static FlowStatisticsDataBuilder refineFlowStatisticsAugmentation(final FlowAndStatisticsMapList flowAndStats) {
231         final FlowStatisticsBuilder flowStatisticsBuilder = new FlowStatisticsBuilder(flowAndStats);
232         final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder();
233         flowStatisticsDataBld.setFlowStatistics(flowStatisticsBuilder.build());
234         return flowStatisticsDataBld;
235     }
236
237     public static void deleteAllKnownFlows(final DeviceContext deviceContext) {
238         if (deviceContext.getDeviceState().deviceSynchronized()) {
239             InstanceIdentifier<FlowCapableNode> flowCapableNodePath = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
240             final Short numOfTablesOnDevice = deviceContext.getDeviceState().getFeatures().getTables();
241             for (short i = 0; i < numOfTablesOnDevice; i++) {
242                 final KeyedInstanceIdentifier<Table, TableKey> iiToTable = flowCapableNodePath.child(Table.class, new TableKey(i));
243                 final ReadTransaction readTx = deviceContext.getReadTransaction();
244                 final CheckedFuture<Optional<Table>, ReadFailedException> tableDataFuture = readTx.read(LogicalDatastoreType.OPERATIONAL, iiToTable);
245                 try {
246                     final Optional<Table> tableDataOpt = tableDataFuture.get();
247                     if (tableDataOpt.isPresent()) {
248                         final Table tableData = tableDataOpt.get();
249                         final Table table = new TableBuilder(tableData).setFlow(Collections.<Flow>emptyList()).build();
250                         deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToTable, table);
251                     }
252                 } catch (final InterruptedException e) {
253                     LOG.trace("Reading of table features for table wit ID {} was interrputed.", i);
254                 } catch (final ExecutionException e) {
255                     LOG.trace("Reading of table features for table wit ID {} encountered execution exception {}.", i, e);
256                 }
257             }
258         }
259     }
260
261     private static void processQueueStatistics(final Iterable<QueueStatisticsUpdate> data, final DeviceContext deviceContext) {
262         // TODO: clean all queues of all node-connectors before writing up-to-date stats
263         final InstanceIdentifier<Node> nodeIdent = deviceContext.getDeviceState().getNodeInstanceIdentifier();
264         for (final QueueStatisticsUpdate queueStatisticsUpdate : data) {
265             for (final QueueIdAndStatisticsMap queueStat : queueStatisticsUpdate.getQueueIdAndStatisticsMap()) {
266                 if (queueStat.getQueueId() != null) {
267                     final FlowCapableNodeConnectorQueueStatistics statChild =
268                             new FlowCapableNodeConnectorQueueStatisticsBuilder(queueStat).build();
269                     final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
270                             new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
271                     statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
272                     final QueueKey qKey = new QueueKey(queueStat.getQueueId());
273                     final InstanceIdentifier<Queue> queueIdent = nodeIdent
274                             .child(NodeConnector.class, new NodeConnectorKey(queueStat.getNodeConnectorId()))
275                             .augmentation(FlowCapableNodeConnector.class)
276                             .child(Queue.class, qKey);
277                     final QueueBuilder queueBuilder = new QueueBuilder()
278                             .setKey(qKey)
279                             .setQueueId(queueStat.getQueueId())
280                             // node-connector-id is already contained in parent node and the port-id here is of incompatible format
281                             .addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, statBuild.build());
282                     deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueIdent, queueBuilder.build());
283                 }
284             }
285         }
286         deviceContext.submitTransaction();
287     }
288
289     private static void processFlowTableStatistics(final Iterable<FlowTableStatisticsUpdate> data, final DeviceContext deviceContext) {
290         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
291         for (final FlowTableStatisticsUpdate flowTableStatisticsUpdate : data) {
292
293             for (final FlowTableAndStatisticsMap tableStat : flowTableStatisticsUpdate.getFlowTableAndStatisticsMap()) {
294                 final InstanceIdentifier<FlowTableStatistics> tStatIdent = fNodeIdent.child(Table.class, new TableKey(tableStat.getTableId().getValue()))
295                         .augmentation(FlowTableStatisticsData.class).child(FlowTableStatistics.class);
296                 final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
297                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);
298             }
299         }
300         deviceContext.submitTransaction();
301     }
302
303     private static void processNodeConnectorStatistics(final Iterable<NodeConnectorStatisticsUpdate> data, final DeviceContext deviceContext) {
304         final InstanceIdentifier<Node> nodeIdent = deviceContext.getDeviceState().getNodeInstanceIdentifier();
305         for (final NodeConnectorStatisticsUpdate nodeConnectorStatisticsUpdate : data) {
306             for (final NodeConnectorStatisticsAndPortNumberMap nConnectPort : nodeConnectorStatisticsUpdate.getNodeConnectorStatisticsAndPortNumberMap()) {
307                 final FlowCapableNodeConnectorStatistics stats = new FlowCapableNodeConnectorStatisticsBuilder(nConnectPort).build();
308                 final NodeConnectorKey key = new NodeConnectorKey(nConnectPort.getNodeConnectorId());
309                 final InstanceIdentifier<NodeConnector> nodeConnectorIdent = nodeIdent.child(NodeConnector.class, key);
310                 final InstanceIdentifier<FlowCapableNodeConnectorStatisticsData> nodeConnStatIdent = nodeConnectorIdent
311                         .augmentation(FlowCapableNodeConnectorStatisticsData.class);
312                 final InstanceIdentifier<FlowCapableNodeConnectorStatistics> flowCapNodeConnStatIdent =
313                         nodeConnStatIdent.child(FlowCapableNodeConnectorStatistics.class);
314                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowCapNodeConnStatIdent, stats);
315             }
316         }
317         deviceContext.submitTransaction();
318     }
319
320     private static void processMetersStatistics(final Iterable<MeterStatisticsUpdated> data,
321                                                 final DeviceContext deviceContext) {
322         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
323         for (final MeterStatisticsUpdated meterStatisticsUpdated : data) {
324             for (final MeterStats mStat : meterStatisticsUpdated.getMeterStats()) {
325                 final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
326                 final MeterId meterId = mStat.getMeterId();
327                 final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
328                 final InstanceIdentifier<NodeMeterStatistics> nodeMeterStatIdent = meterIdent
329                         .augmentation(NodeMeterStatistics.class);
330                 final InstanceIdentifier<MeterStatistics> msIdent = nodeMeterStatIdent.child(MeterStatistics.class);
331                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
332             }
333         }
334         deviceContext.submitTransaction();
335     }
336
337     private static void deleteAllKnownMeters(final DeviceContext deviceContext, final InstanceIdentifier<FlowCapableNode> fNodeIdent) {
338         for (final MeterId meterId : deviceContext.getDeviceMeterRegistry().getAllMeterIds()) {
339             final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
340             deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, meterIdent);
341         }
342         deviceContext.getDeviceMeterRegistry().removeMarked();
343     }
344
345     private static void processGroupDescStats(final Iterable<GroupDescStatsUpdated> data, final DeviceContext deviceContext) {
346         final InstanceIdentifier<FlowCapableNode> fNodeIdent =
347                 deviceContext.getDeviceState().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
348         deleteAllKnownGroups(deviceContext, fNodeIdent);
349
350         for (final GroupDescStatsUpdated groupDescStatsUpdated : data) {
351             for (final GroupDescStats groupDescStats : groupDescStatsUpdated.getGroupDescStats()) {
352                 final GroupId groupId = groupDescStats.getGroupId();
353
354                 final GroupBuilder groupBuilder = new GroupBuilder(groupDescStats);
355                 groupBuilder.setKey(new GroupKey(groupId));
356                 groupBuilder.addAugmentation(NodeGroupStatistics.class, new NodeGroupStatisticsBuilder().build());
357
358                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
359
360                 deviceContext.getDeviceGroupRegistry().store(groupId);
361                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, groupIdent, groupBuilder.build());
362             }
363         }
364         deviceContext.submitTransaction();
365     }
366
367     private static void deleteAllKnownGroups(final DeviceContext deviceContext, final InstanceIdentifier<FlowCapableNode> fNodeIdent) {
368         for (final GroupId groupId : deviceContext.getDeviceGroupRegistry().getAllGroupIds()) {
369             final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
370             deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, groupIdent);
371         }
372         deviceContext.getDeviceGroupRegistry().removeMarked();
373     }
374
375     private static void processGroupStatistics(final Iterable<GroupStatisticsUpdated> data, final DeviceContext deviceContext) {
376         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext);
377         for (final GroupStatisticsUpdated groupStatistics : data) {
378             for (final GroupStats groupStats : groupStatistics.getGroupStats()) {
379
380                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupStats.getGroupId()));
381                 final InstanceIdentifier<NodeGroupStatistics> nGroupStatIdent = groupIdent
382                         .augmentation(NodeGroupStatistics.class);
383
384                 final InstanceIdentifier<GroupStatistics> gsIdent = nGroupStatIdent.child(GroupStatistics.class);
385                 final GroupStatistics stats = new GroupStatisticsBuilder(groupStats).build();
386                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gsIdent, stats);
387             }
388         }
389         deviceContext.submitTransaction();
390     }
391
392     private static InstanceIdentifier<FlowCapableNode> assembleFlowCapableNodeInstanceIdentifier(final DeviceContext deviceContext) {
393         return deviceContext.getDeviceState().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
394     }
395 }