Fix comparison between port numbers in match
[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.AsyncFunction;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.FutureFallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.JdkFutureAdapters;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import java.text.SimpleDateFormat;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.Nullable;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceRegistry;
33 import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
34 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
35 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
36 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
37 import org.opendaylight.openflowplugin.api.openflow.registry.group.DeviceGroupRegistry;
38 import org.opendaylight.openflowplugin.api.openflow.registry.meter.DeviceMeterRegistry;
39 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
40 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.StatisticsGatherer;
41 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
42 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableStatisticsGatheringStatus;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableStatisticsGatheringStatusBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.snapshot.gathering.status.grouping.SnapshotGatheringStatusEnd;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.snapshot.gathering.status.grouping.SnapshotGatheringStatusEndBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.snapshot.gathering.status.grouping.SnapshotGatheringStatusStartBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatistics;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.NodeGroupStatisticsBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatistics;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.group.statistics.GroupStatisticsBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatisticsBuilder;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatistics;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.nodes.node.meter.MeterStatisticsBuilder;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.config.stats.reply.MeterConfigStats;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.NodeConnectorStatisticsUpdate;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatistics;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.flow.capable.node.connector.statistics.FlowCapableNodeConnectorStatisticsBuilder;
104 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.node.connector.statistics.and.port.number.map.NodeConnectorStatisticsAndPortNumberMap;
105 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsData;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.FlowCapableNodeConnectorQueueStatisticsDataBuilder;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.QueueStatisticsUpdate;
108 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatistics;
109 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.flow.capable.node.connector.queue.statistics.FlowCapableNodeConnectorQueueStatisticsBuilder;
110 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
111 import org.opendaylight.yangtools.yang.binding.DataObject;
112 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
113 import org.opendaylight.yangtools.yang.common.RpcResult;
114 import org.slf4j.Logger;
115 import org.slf4j.LoggerFactory;
116
117 /**
118  * Utils for gatherig statistics
119  */
120 public final class StatisticsGatheringUtils {
121
122     private static String DATE_AND_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
123
124     private static final Logger LOG = LoggerFactory.getLogger(StatisticsGatheringUtils.class);
125     private static final String QUEUE2_REQCTX = "QUEUE2REQCTX-";
126
127     private StatisticsGatheringUtils() {
128         throw new IllegalStateException("This class should not be instantiated.");
129     }
130
131     //TODO: Flow-,Group- and Meter- registry should be not in device context, consider move it in separate class
132     static ListenableFuture<Boolean> gatherStatistics(final StatisticsGatherer statisticsGatheringService,
133                                                       final DeviceInfo deviceInfo,
134                                                       final MultipartType type,
135                                                       final TxFacade txFacade,
136                                                       final DeviceRegistry registry,
137                                                       final Boolean initial,
138                                                       final SinglePurposeMultipartReplyTranslator multipartReplyTranslator) {
139         EventIdentifier wholeProcessEventIdentifier = null;
140         if (MultipartType.OFPMPFLOW.equals(type)) {
141             wholeProcessEventIdentifier = new EventIdentifier(type.toString(), deviceInfo.getNodeId().getValue());
142             EventsTimeCounter.markStart(wholeProcessEventIdentifier);
143         }
144         final EventIdentifier ofpQueuToRequestContextEventIdentifier = new EventIdentifier(QUEUE2_REQCTX + type.toString(), deviceInfo.getNodeId().toString());
145         final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture =
146                 JdkFutureAdapters.listenInPoolThread(statisticsGatheringService.getStatisticsOfType(
147                         ofpQueuToRequestContextEventIdentifier, type));
148         return transformAndStoreStatisticsData(statisticsDataInFuture, deviceInfo, wholeProcessEventIdentifier, type, txFacade, registry, initial, multipartReplyTranslator);
149     }
150
151     private static ListenableFuture<Boolean> transformAndStoreStatisticsData(final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture,
152                                                                              final DeviceInfo deviceInfo,
153                                                                              final EventIdentifier eventIdentifier,
154                                                                              final MultipartType type,
155                                                                              final TxFacade txFacade,
156                                                                              final DeviceRegistry registry,
157                                                                              final boolean initial,
158                                                                              final SinglePurposeMultipartReplyTranslator multipartReplyTranslator) {
159         return Futures.transform(statisticsDataInFuture, new AsyncFunction<RpcResult<List<MultipartReply>>, Boolean>() {
160             @Nullable
161             @Override
162             public ListenableFuture<Boolean> apply(final RpcResult<List<MultipartReply>> rpcResult) {
163                 boolean isMultipartProcessed = Boolean.TRUE;
164                 if (rpcResult.isSuccessful()) {
165                     LOG.debug("Stats reply successfully received for node {} of type {}", deviceInfo.getNodeId(), type);
166
167                     // TODO: in case the result value is null then multipart data probably got processed on the fly -
168                     // TODO: this contract should by clearly stated and enforced - now simple true value is returned
169                     if (null != rpcResult.getResult()) {
170                         Iterable<? extends DataObject> allMultipartData = Collections.emptyList();
171                         DataObject multipartData = null;
172
173
174                         try {
175                             for (final MultipartReply singleReply : rpcResult.getResult()) {
176                                 final List<? extends DataObject> multipartDataList = multipartReplyTranslator.translate(
177                                         deviceInfo.getDatapathId(),
178                                         deviceInfo.getVersion(), singleReply);
179                                 multipartData = multipartDataList.get(0);
180                                 allMultipartData = Iterables.concat(allMultipartData, multipartDataList);
181                             }
182                         } catch (final Exception e) {
183                             LOG.warn("stats processing of type {} for node {} failed during transfomation step",
184                                     type, deviceInfo.getNodeId(), e);
185                             return Futures.immediateFailedFuture(e);
186                         }
187
188
189                         try {
190                             if (multipartData instanceof GroupStatisticsUpdated) {
191                                 processGroupStatistics((Iterable<GroupStatisticsUpdated>) allMultipartData, deviceInfo, txFacade);
192                             } else if (multipartData instanceof MeterStatisticsUpdated) {
193                                 processMetersStatistics((Iterable<MeterStatisticsUpdated>) allMultipartData, deviceInfo, txFacade);
194                             } else if (multipartData instanceof NodeConnectorStatisticsUpdate) {
195                                 processNodeConnectorStatistics((Iterable<NodeConnectorStatisticsUpdate>) allMultipartData, deviceInfo, txFacade);
196                             } else if (multipartData instanceof FlowTableStatisticsUpdate) {
197                                 processFlowTableStatistics((Iterable<FlowTableStatisticsUpdate>) allMultipartData, deviceInfo, txFacade);
198                             } else if (multipartData instanceof QueueStatisticsUpdate) {
199                                 processQueueStatistics((Iterable<QueueStatisticsUpdate>) allMultipartData, deviceInfo, txFacade);
200                             } else if (multipartData instanceof FlowsStatisticsUpdate) {
201                                 /* FlowStat Processing is realized by NettyThread only by initPhase, otherwise it is realized
202                                  * by MD-SAL thread */
203                                 return processFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData, deviceInfo, txFacade, registry.getDeviceFlowRegistry(), initial, eventIdentifier);
204
205                             } else if (multipartData instanceof GroupDescStatsUpdated) {
206                                 processGroupDescStats((Iterable<GroupDescStatsUpdated>) allMultipartData, deviceInfo, txFacade, registry.getDeviceGroupRegistry());
207                             } else if (multipartData instanceof MeterConfigStatsUpdated) {
208                                 processMeterConfigStatsUpdated((Iterable<MeterConfigStatsUpdated>) allMultipartData, deviceInfo, txFacade, registry.getDeviceMeterRegistry());
209                             } else {
210                                 isMultipartProcessed = Boolean.FALSE;
211                             }
212                         } catch (final Exception e) {
213                             LOG.warn("stats processing of type {} for node {} failed during write-to-tx step",
214                                     type, deviceInfo.getNodeId(), e);
215                             return Futures.immediateFailedFuture(e);
216                         }
217
218                         LOG.debug("Stats reply added to transaction for node {} of type {}", deviceInfo.getNodeId(), type);
219
220                         //TODO : implement experimenter
221                     } else {
222                         LOG.debug("Stats reply was empty for node {} of type {}", deviceInfo.getNodeId(), type);
223                     }
224
225                 } else {
226                     LOG.debug("Stats reply FAILED for node {} of type {}: {}", deviceInfo.getNodeId(), type, rpcResult.getErrors());
227                     isMultipartProcessed = Boolean.FALSE;
228                 }
229                 return Futures.immediateFuture(isMultipartProcessed);
230             }
231         });
232     }
233
234     private static void processMeterConfigStatsUpdated(final Iterable<MeterConfigStatsUpdated> data,
235                                                        final DeviceInfo deviceInfo,
236                                                        final TxFacade txFacade,
237                                                        final DeviceMeterRegistry meterRegistry) throws Exception {
238         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
239         deleteAllKnownMeters(meterRegistry, fNodeIdent, txFacade);
240         for (final MeterConfigStatsUpdated meterConfigStatsUpdated : data) {
241             for (final MeterConfigStats meterConfigStats : meterConfigStatsUpdated.getMeterConfigStats()) {
242                 final MeterId meterId = meterConfigStats.getMeterId();
243                 final InstanceIdentifier<Meter> meterInstanceIdentifier = fNodeIdent.child(Meter.class, new MeterKey(meterId));
244
245                 final MeterBuilder meterBuilder = new MeterBuilder(meterConfigStats);
246                 meterBuilder.setKey(new MeterKey(meterId));
247                 meterBuilder.addAugmentation(NodeMeterStatistics.class, new NodeMeterStatisticsBuilder().build());
248                 meterRegistry.store(meterId);
249                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, meterInstanceIdentifier, meterBuilder.build());
250             }
251         }
252         txFacade.submitTransaction();
253     }
254
255     private static ListenableFuture<Boolean> processFlowStatistics(final Iterable<FlowsStatisticsUpdate> data,
256                                                                    final DeviceInfo deviceInfo,
257                                                                    final TxFacade txFacade,
258                                                                    final DeviceFlowRegistry flowRegistry,
259                                                                    final boolean initial,
260                                                                    final EventIdentifier eventIdentifier) {
261         final ListenableFuture<Void> deleteFuture = initial ? Futures.immediateFuture(null) : deleteAllKnownFlows(deviceInfo,
262                 flowRegistry, txFacade);
263         return Futures.transform(deleteFuture, new Function<Void, Boolean>() {
264
265             @Override
266             public Boolean apply(final Void input) {
267                 writeFlowStatistics(data, deviceInfo, flowRegistry, txFacade);
268                 txFacade.submitTransaction();
269                 EventsTimeCounter.markEnd(eventIdentifier);
270                 return Boolean.TRUE;
271             }
272         });
273     }
274
275     public static void writeFlowStatistics(final Iterable<FlowsStatisticsUpdate> data,
276                                            final DeviceInfo deviceInfo,
277                                            final DeviceFlowRegistry registry,
278                                            final TxFacade txFacade) {
279         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
280         try {
281             for (final FlowsStatisticsUpdate flowsStatistics : data) {
282                 for (final FlowAndStatisticsMapList flowStat : flowsStatistics.getFlowAndStatisticsMapList()) {
283                     final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
284                     flowBuilder.addAugmentation(FlowStatisticsData.class, refineFlowStatisticsAugmentation(flowStat).build());
285
286                     final short tableId = flowStat.getTableId();
287                     final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), flowBuilder.build());
288                     final FlowId flowId = registry.storeIfNecessary(flowRegistryKey);
289
290                     final FlowKey flowKey = new FlowKey(flowId);
291                     flowBuilder.setKey(flowKey);
292                     final TableKey tableKey = new TableKey(tableId);
293                     final InstanceIdentifier<Flow> flowIdent = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey);
294                     txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
295                 }
296             }
297         } catch (Exception e) {
298             LOG.warn("Not able to write to transaction: {}", e.getMessage());
299         }
300     }
301
302     /**
303      * Method extracts flow statistics out of flowAndStatistics model
304      *
305      * @param flowAndStats
306      */
307     private static FlowStatisticsDataBuilder refineFlowStatisticsAugmentation(final FlowAndStatisticsMapList flowAndStats) {
308         final FlowStatisticsBuilder flowStatisticsBuilder = new FlowStatisticsBuilder(flowAndStats);
309         final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder();
310         flowStatisticsDataBld.setFlowStatistics(flowStatisticsBuilder.build());
311         return flowStatisticsDataBld;
312     }
313
314     public static ListenableFuture<Void> deleteAllKnownFlows(final DeviceInfo deviceInfo,
315                                                              final DeviceFlowRegistry registry,
316                                                              final TxFacade txFacade) {
317         final InstanceIdentifier<FlowCapableNode> flowCapableNodePath = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
318         final ReadOnlyTransaction readTx = txFacade.getReadTransaction();
319         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowCapableNodeFuture = readTx.read(
320                 LogicalDatastoreType.OPERATIONAL, flowCapableNodePath);
321
322         /* we wish to close readTx for fallBack */
323         Futures.withFallback(flowCapableNodeFuture, new FutureFallback<Optional<FlowCapableNode>>() {
324
325             @Override
326             public ListenableFuture<Optional<FlowCapableNode>> create(final Throwable t) throws Exception {
327                 readTx.close();
328                 return Futures.immediateFailedFuture(t);
329             }
330         });
331         /*
332          * we have to read actual tables with all information before we set empty Flow list, merge is expensive and
333          * not applicable for lists
334          */
335         return Futures.transform(flowCapableNodeFuture, new AsyncFunction<Optional<FlowCapableNode>, Void>() {
336
337             @Override
338             public ListenableFuture<Void> apply(final Optional<FlowCapableNode> flowCapNodeOpt) throws Exception {
339                 if (flowCapNodeOpt.isPresent()) {
340                     for (final Table tableData : flowCapNodeOpt.get().getTable()) {
341                         final Table table = new TableBuilder(tableData).setFlow(Collections.<Flow>emptyList()).build();
342                         final InstanceIdentifier<Table> iiToTable = flowCapableNodePath.child(Table.class, tableData.getKey());
343                         txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToTable, table);
344                     }
345                 }
346                 readTx.close();
347                 return Futures.immediateFuture(null);
348             }
349
350         });
351     }
352
353     private static void processQueueStatistics(final Iterable<QueueStatisticsUpdate> data, final DeviceInfo deviceInfo, final TxFacade txFacade) throws Exception {
354         // TODO: clean all queues of all node-connectors before writing up-to-date stats
355         final InstanceIdentifier<Node> nodeIdent = deviceInfo.getNodeInstanceIdentifier();
356         for (final QueueStatisticsUpdate queueStatisticsUpdate : data) {
357             for (final QueueIdAndStatisticsMap queueStat : queueStatisticsUpdate.getQueueIdAndStatisticsMap()) {
358                 if (queueStat.getQueueId() != null) {
359                     final FlowCapableNodeConnectorQueueStatistics statChild =
360                             new FlowCapableNodeConnectorQueueStatisticsBuilder(queueStat).build();
361                     final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
362                             new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
363                     statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
364                     final QueueKey qKey = new QueueKey(queueStat.getQueueId());
365                     final InstanceIdentifier<Queue> queueIdent = nodeIdent
366                             .child(NodeConnector.class, new NodeConnectorKey(queueStat.getNodeConnectorId()))
367                             .augmentation(FlowCapableNodeConnector.class)
368                             .child(Queue.class, qKey);
369                     final QueueBuilder queueBuilder = new QueueBuilder()
370                             .setKey(qKey)
371                             .setQueueId(queueStat.getQueueId())
372                             // node-connector-id is already contained in parent node and the port-id here is of incompatible format
373                             .addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, statBuild.build());
374                     txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueIdent, queueBuilder.build());
375                 }
376             }
377         }
378         txFacade.submitTransaction();
379     }
380
381     private static void processFlowTableStatistics(final Iterable<FlowTableStatisticsUpdate> data, final DeviceInfo deviceInfo, final TxFacade txFacade) throws Exception {
382         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
383         for (final FlowTableStatisticsUpdate flowTableStatisticsUpdate : data) {
384
385             for (final FlowTableAndStatisticsMap tableStat : flowTableStatisticsUpdate.getFlowTableAndStatisticsMap()) {
386                 final InstanceIdentifier<FlowTableStatistics> tStatIdent = fNodeIdent.child(Table.class, new TableKey(tableStat.getTableId().getValue()))
387                         .augmentation(FlowTableStatisticsData.class).child(FlowTableStatistics.class);
388                 final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
389                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);
390             }
391         }
392         txFacade.submitTransaction();
393     }
394
395     private static void processNodeConnectorStatistics(final Iterable<NodeConnectorStatisticsUpdate> data, final DeviceInfo deviceInfo, final TxFacade txFacade) throws Exception {
396         final InstanceIdentifier<Node> nodeIdent = deviceInfo.getNodeInstanceIdentifier();
397         for (final NodeConnectorStatisticsUpdate nodeConnectorStatisticsUpdate : data) {
398             for (final NodeConnectorStatisticsAndPortNumberMap nConnectPort : nodeConnectorStatisticsUpdate.getNodeConnectorStatisticsAndPortNumberMap()) {
399                 final FlowCapableNodeConnectorStatistics stats = new FlowCapableNodeConnectorStatisticsBuilder(nConnectPort).build();
400                 final NodeConnectorKey key = new NodeConnectorKey(nConnectPort.getNodeConnectorId());
401                 final InstanceIdentifier<NodeConnector> nodeConnectorIdent = nodeIdent.child(NodeConnector.class, key);
402                 final InstanceIdentifier<FlowCapableNodeConnectorStatisticsData> nodeConnStatIdent = nodeConnectorIdent
403                         .augmentation(FlowCapableNodeConnectorStatisticsData.class);
404                 final InstanceIdentifier<FlowCapableNodeConnectorStatistics> flowCapNodeConnStatIdent =
405                         nodeConnStatIdent.child(FlowCapableNodeConnectorStatistics.class);
406                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowCapNodeConnStatIdent, stats);
407             }
408         }
409         txFacade.submitTransaction();
410     }
411
412     private static void processMetersStatistics(final Iterable<MeterStatisticsUpdated> data,
413                                                 final DeviceInfo deviceInfo,
414                                                 final TxFacade txFacade) throws Exception {
415         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
416         for (final MeterStatisticsUpdated meterStatisticsUpdated : data) {
417             for (final MeterStats mStat : meterStatisticsUpdated.getMeterStats()) {
418                 final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
419                 final MeterId meterId = mStat.getMeterId();
420                 final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
421                 final InstanceIdentifier<NodeMeterStatistics> nodeMeterStatIdent = meterIdent
422                         .augmentation(NodeMeterStatistics.class);
423                 final InstanceIdentifier<MeterStatistics> msIdent = nodeMeterStatIdent.child(MeterStatistics.class);
424                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
425             }
426         }
427         txFacade.submitTransaction();
428     }
429
430     private static void deleteAllKnownMeters(final DeviceMeterRegistry meterRegistry, final InstanceIdentifier<FlowCapableNode> fNodeIdent, final TxFacade txFacade) throws Exception {
431         for (final MeterId meterId : meterRegistry.getAllMeterIds()) {
432             final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
433             txFacade.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, meterIdent);
434         }
435         meterRegistry.removeMarked();
436     }
437
438     private static void processGroupDescStats(final Iterable<GroupDescStatsUpdated> data, final DeviceInfo deviceInfo, final TxFacade txFacade, final DeviceGroupRegistry groupRegistry) throws Exception {
439         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
440         deleteAllKnownGroups(txFacade, fNodeIdent, groupRegistry);
441
442         for (final GroupDescStatsUpdated groupDescStatsUpdated : data) {
443             for (final GroupDescStats groupDescStats : groupDescStatsUpdated.getGroupDescStats()) {
444                 final GroupId groupId = groupDescStats.getGroupId();
445
446                 final GroupBuilder groupBuilder = new GroupBuilder(groupDescStats);
447                 groupBuilder.setKey(new GroupKey(groupId));
448                 groupBuilder.addAugmentation(NodeGroupStatistics.class, new NodeGroupStatisticsBuilder().build());
449
450                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
451
452                 groupRegistry.store(groupId);
453                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, groupIdent, groupBuilder.build());
454             }
455         }
456         txFacade.submitTransaction();
457     }
458
459     private static void deleteAllKnownGroups(final TxFacade txFacade, final InstanceIdentifier<FlowCapableNode> fNodeIdent, final DeviceGroupRegistry groupRegistry) throws Exception {
460         for (final GroupId groupId : groupRegistry.getAllGroupIds()) {
461             final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
462             txFacade.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, groupIdent);
463         }
464         groupRegistry.removeMarked();
465     }
466
467     private static void processGroupStatistics(final Iterable<GroupStatisticsUpdated> data, final DeviceInfo deviceInfo, final TxFacade txFacade) throws Exception {
468         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
469         for (final GroupStatisticsUpdated groupStatistics : data) {
470             for (final GroupStats groupStats : groupStatistics.getGroupStats()) {
471
472                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupStats.getGroupId()));
473                 final InstanceIdentifier<NodeGroupStatistics> nGroupStatIdent = groupIdent
474                         .augmentation(NodeGroupStatistics.class);
475
476                 final InstanceIdentifier<GroupStatistics> gsIdent = nGroupStatIdent.child(GroupStatistics.class);
477                 final GroupStatistics stats = new GroupStatisticsBuilder(groupStats).build();
478                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gsIdent, stats);
479             }
480         }
481         txFacade.submitTransaction();
482     }
483
484     private static InstanceIdentifier<FlowCapableNode> assembleFlowCapableNodeInstanceIdentifier(final DeviceInfo deviceInfo) {
485         return deviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
486     }
487
488     /**
489      * Writes snapshot gathering start timestamp + cleans end mark
490      *
491      * @param deviceContext txManager + node path keeper
492      */
493     static void markDeviceStateSnapshotStart(final DeviceContext deviceContext) {
494         final InstanceIdentifier<FlowCapableStatisticsGatheringStatus> statusPath = deviceContext.getDeviceInfo()
495                 .getNodeInstanceIdentifier().augmentation(FlowCapableStatisticsGatheringStatus.class);
496
497         final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_AND_TIME_FORMAT);
498         final FlowCapableStatisticsGatheringStatus gatheringStatus = new FlowCapableStatisticsGatheringStatusBuilder()
499                 .setSnapshotGatheringStatusStart(new SnapshotGatheringStatusStartBuilder()
500                         .setBegin(new DateAndTime(simpleDateFormat.format(new Date())))
501                         .build())
502                 .setSnapshotGatheringStatusEnd(null) // TODO: reconsider if really need to clean end mark here
503                 .build();
504         try {
505             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, statusPath, gatheringStatus);
506         } catch (final TransactionChainClosedException e) {
507             LOG.warn("Can't write to transaction, transaction chain probably closed.");
508             LOG.trace("Write to transaction exception: ", e);
509         }
510
511         deviceContext.submitTransaction();
512     }
513
514     /**
515      * Writes snapshot gathering end timestamp + outcome
516      *
517      * @param deviceContext txManager + node path keeper
518      * @param succeeded     outcome of currently finished gathering
519      */
520     static void markDeviceStateSnapshotEnd(final DeviceContext deviceContext, final boolean succeeded) {
521         final InstanceIdentifier<SnapshotGatheringStatusEnd> statusEndPath = deviceContext.getDeviceInfo()
522                 .getNodeInstanceIdentifier().augmentation(FlowCapableStatisticsGatheringStatus.class)
523                 .child(SnapshotGatheringStatusEnd.class);
524
525         final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_AND_TIME_FORMAT);
526         final SnapshotGatheringStatusEnd gatheringStatus = new SnapshotGatheringStatusEndBuilder()
527                 .setEnd(new DateAndTime(simpleDateFormat.format(new Date())))
528                 .setSucceeded(succeeded)
529                 .build();
530         try {
531             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, statusEndPath, gatheringStatus);
532         } catch (TransactionChainClosedException e) {
533             LOG.warn("Can't write to transaction, transaction chain probably closed.");
534             LOG.trace("Write to transaction exception: ", e);
535         }
536
537         deviceContext.submitTransaction();
538     }
539 }