Merge "Remove RoleManager and RoleContext" into stable/boron
[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(flowBuilder.build());
288                     final FlowDescriptor flowDescriptor = registry.retrieveIdForFlow(flowRegistryKey);
289
290                     if(Objects.nonNull(flowDescriptor)) {
291                         final FlowId flowId = flowDescriptor.getFlowId();
292                         final FlowKey flowKey = new FlowKey(flowId);
293                         flowBuilder.setKey(flowKey);
294                         final TableKey tableKey = new TableKey(tableId);
295                         final InstanceIdentifier<Flow> flowIdent = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey);
296                         txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
297                     } else {
298                         LOG.warn("Skip write statistics. Flow hash: {} not present in DeviceFlowRegistry", flowRegistryKey.hashCode());
299                     }
300                 }
301             }
302         } catch (Exception e) {
303             LOG.warn("Not able to write to transaction: {}", e.getMessage());
304         }
305     }
306
307     /**
308      * Method extracts flow statistics out of flowAndStatistics model
309      *
310      * @param flowAndStats
311      */
312     private static FlowStatisticsDataBuilder refineFlowStatisticsAugmentation(final FlowAndStatisticsMapList flowAndStats) {
313         final FlowStatisticsBuilder flowStatisticsBuilder = new FlowStatisticsBuilder(flowAndStats);
314         final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder();
315         flowStatisticsDataBld.setFlowStatistics(flowStatisticsBuilder.build());
316         return flowStatisticsDataBld;
317     }
318
319     public static ListenableFuture<Void> deleteAllKnownFlows(final DeviceInfo deviceInfo,
320                                                              final DeviceFlowRegistry registry,
321                                                              final TxFacade txFacade) {
322         final InstanceIdentifier<FlowCapableNode> flowCapableNodePath = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
323         final ReadOnlyTransaction readTx = txFacade.getReadTransaction();
324         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowCapableNodeFuture = readTx.read(
325                 LogicalDatastoreType.OPERATIONAL, flowCapableNodePath);
326
327         /* we wish to close readTx for fallBack */
328         Futures.withFallback(flowCapableNodeFuture, new FutureFallback<Optional<FlowCapableNode>>() {
329
330             @Override
331             public ListenableFuture<Optional<FlowCapableNode>> create(final Throwable t) throws Exception {
332                 readTx.close();
333                 return Futures.immediateFailedFuture(t);
334             }
335         });
336         /*
337          * we have to read actual tables with all information before we set empty Flow list, merge is expensive and
338          * not applicable for lists
339          */
340         return Futures.transform(flowCapableNodeFuture, new AsyncFunction<Optional<FlowCapableNode>, Void>() {
341
342             @Override
343             public ListenableFuture<Void> apply(final Optional<FlowCapableNode> flowCapNodeOpt) throws Exception {
344                 if (flowCapNodeOpt.isPresent()) {
345                     for (final Table tableData : flowCapNodeOpt.get().getTable()) {
346                         final Table table = new TableBuilder(tableData).setFlow(Collections.<Flow>emptyList()).build();
347                         final InstanceIdentifier<Table> iiToTable = flowCapableNodePath.child(Table.class, tableData.getKey());
348                         txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToTable, table);
349                     }
350                 }
351                 readTx.close();
352                 return Futures.immediateFuture(null);
353             }
354
355         });
356     }
357
358     private static void processQueueStatistics(final Iterable<QueueStatisticsUpdate> data, final DeviceInfo deviceInfo, final TxFacade txFacade) throws Exception {
359         // TODO: clean all queues of all node-connectors before writing up-to-date stats
360         final InstanceIdentifier<Node> nodeIdent = deviceInfo.getNodeInstanceIdentifier();
361         for (final QueueStatisticsUpdate queueStatisticsUpdate : data) {
362             for (final QueueIdAndStatisticsMap queueStat : queueStatisticsUpdate.getQueueIdAndStatisticsMap()) {
363                 if (queueStat.getQueueId() != null) {
364                     final FlowCapableNodeConnectorQueueStatistics statChild =
365                             new FlowCapableNodeConnectorQueueStatisticsBuilder(queueStat).build();
366                     final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
367                             new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
368                     statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
369                     final QueueKey qKey = new QueueKey(queueStat.getQueueId());
370                     final InstanceIdentifier<Queue> queueIdent = nodeIdent
371                             .child(NodeConnector.class, new NodeConnectorKey(queueStat.getNodeConnectorId()))
372                             .augmentation(FlowCapableNodeConnector.class)
373                             .child(Queue.class, qKey);
374                     final QueueBuilder queueBuilder = new QueueBuilder()
375                             .setKey(qKey)
376                             .setQueueId(queueStat.getQueueId())
377                             // node-connector-id is already contained in parent node and the port-id here is of incompatible format
378                             .addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, statBuild.build());
379                     txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueIdent, queueBuilder.build());
380                 }
381             }
382         }
383         txFacade.submitTransaction();
384     }
385
386     private static void processFlowTableStatistics(final Iterable<FlowTableStatisticsUpdate> data, final DeviceInfo deviceInfo, final TxFacade txFacade) throws Exception {
387         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
388         for (final FlowTableStatisticsUpdate flowTableStatisticsUpdate : data) {
389
390             for (final FlowTableAndStatisticsMap tableStat : flowTableStatisticsUpdate.getFlowTableAndStatisticsMap()) {
391                 final InstanceIdentifier<FlowTableStatistics> tStatIdent = fNodeIdent.child(Table.class, new TableKey(tableStat.getTableId().getValue()))
392                         .augmentation(FlowTableStatisticsData.class).child(FlowTableStatistics.class);
393                 final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
394                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);
395             }
396         }
397         txFacade.submitTransaction();
398     }
399
400     private static void processNodeConnectorStatistics(final Iterable<NodeConnectorStatisticsUpdate> data, final DeviceInfo deviceInfo, final TxFacade txFacade) throws Exception {
401         final InstanceIdentifier<Node> nodeIdent = deviceInfo.getNodeInstanceIdentifier();
402         for (final NodeConnectorStatisticsUpdate nodeConnectorStatisticsUpdate : data) {
403             for (final NodeConnectorStatisticsAndPortNumberMap nConnectPort : nodeConnectorStatisticsUpdate.getNodeConnectorStatisticsAndPortNumberMap()) {
404                 final FlowCapableNodeConnectorStatistics stats = new FlowCapableNodeConnectorStatisticsBuilder(nConnectPort).build();
405                 final NodeConnectorKey key = new NodeConnectorKey(nConnectPort.getNodeConnectorId());
406                 final InstanceIdentifier<NodeConnector> nodeConnectorIdent = nodeIdent.child(NodeConnector.class, key);
407                 final InstanceIdentifier<FlowCapableNodeConnectorStatisticsData> nodeConnStatIdent = nodeConnectorIdent
408                         .augmentation(FlowCapableNodeConnectorStatisticsData.class);
409                 final InstanceIdentifier<FlowCapableNodeConnectorStatistics> flowCapNodeConnStatIdent =
410                         nodeConnStatIdent.child(FlowCapableNodeConnectorStatistics.class);
411                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowCapNodeConnStatIdent, stats);
412             }
413         }
414         txFacade.submitTransaction();
415     }
416
417     private static void processMetersStatistics(final Iterable<MeterStatisticsUpdated> data,
418                                                 final DeviceInfo deviceInfo,
419                                                 final TxFacade txFacade) throws Exception {
420         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
421         for (final MeterStatisticsUpdated meterStatisticsUpdated : data) {
422             for (final MeterStats mStat : meterStatisticsUpdated.getMeterStats()) {
423                 final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
424                 final MeterId meterId = mStat.getMeterId();
425                 final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
426                 final InstanceIdentifier<NodeMeterStatistics> nodeMeterStatIdent = meterIdent
427                         .augmentation(NodeMeterStatistics.class);
428                 final InstanceIdentifier<MeterStatistics> msIdent = nodeMeterStatIdent.child(MeterStatistics.class);
429                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
430             }
431         }
432         txFacade.submitTransaction();
433     }
434
435     private static void deleteAllKnownMeters(final DeviceMeterRegistry meterRegistry, final InstanceIdentifier<FlowCapableNode> fNodeIdent, final TxFacade txFacade) throws Exception {
436         for (final MeterId meterId : meterRegistry.getAllMeterIds()) {
437             final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
438             txFacade.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, meterIdent);
439         }
440         meterRegistry.removeMarked();
441     }
442
443     private static void processGroupDescStats(final Iterable<GroupDescStatsUpdated> data, final DeviceInfo deviceInfo, final TxFacade txFacade, final DeviceGroupRegistry groupRegistry) throws Exception {
444         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
445         deleteAllKnownGroups(txFacade, fNodeIdent, groupRegistry);
446
447         for (final GroupDescStatsUpdated groupDescStatsUpdated : data) {
448             for (final GroupDescStats groupDescStats : groupDescStatsUpdated.getGroupDescStats()) {
449                 final GroupId groupId = groupDescStats.getGroupId();
450
451                 final GroupBuilder groupBuilder = new GroupBuilder(groupDescStats);
452                 groupBuilder.setKey(new GroupKey(groupId));
453                 groupBuilder.addAugmentation(NodeGroupStatistics.class, new NodeGroupStatisticsBuilder().build());
454
455                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
456
457                 groupRegistry.store(groupId);
458                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, groupIdent, groupBuilder.build());
459             }
460         }
461         txFacade.submitTransaction();
462     }
463
464     private static void deleteAllKnownGroups(final TxFacade txFacade, final InstanceIdentifier<FlowCapableNode> fNodeIdent, final DeviceGroupRegistry groupRegistry) throws Exception {
465         for (final GroupId groupId : groupRegistry.getAllGroupIds()) {
466             final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
467             txFacade.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, groupIdent);
468         }
469         groupRegistry.removeMarked();
470     }
471
472     private static void processGroupStatistics(final Iterable<GroupStatisticsUpdated> data, final DeviceInfo deviceInfo, final TxFacade txFacade) throws Exception {
473         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
474         for (final GroupStatisticsUpdated groupStatistics : data) {
475             for (final GroupStats groupStats : groupStatistics.getGroupStats()) {
476
477                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupStats.getGroupId()));
478                 final InstanceIdentifier<NodeGroupStatistics> nGroupStatIdent = groupIdent
479                         .augmentation(NodeGroupStatistics.class);
480
481                 final InstanceIdentifier<GroupStatistics> gsIdent = nGroupStatIdent.child(GroupStatistics.class);
482                 final GroupStatistics stats = new GroupStatisticsBuilder(groupStats).build();
483                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gsIdent, stats);
484             }
485         }
486         txFacade.submitTransaction();
487     }
488
489     private static InstanceIdentifier<FlowCapableNode> assembleFlowCapableNodeInstanceIdentifier(final DeviceInfo deviceInfo) {
490         return deviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
491     }
492
493     /**
494      * Writes snapshot gathering start timestamp + cleans end mark
495      *
496      * @param deviceContext txManager + node path keeper
497      */
498     static void markDeviceStateSnapshotStart(final DeviceContext deviceContext) {
499         final InstanceIdentifier<FlowCapableStatisticsGatheringStatus> statusPath = deviceContext.getDeviceInfo()
500                 .getNodeInstanceIdentifier().augmentation(FlowCapableStatisticsGatheringStatus.class);
501
502         final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_AND_TIME_FORMAT);
503         final FlowCapableStatisticsGatheringStatus gatheringStatus = new FlowCapableStatisticsGatheringStatusBuilder()
504                 .setSnapshotGatheringStatusStart(new SnapshotGatheringStatusStartBuilder()
505                         .setBegin(new DateAndTime(simpleDateFormat.format(new Date())))
506                         .build())
507                 .setSnapshotGatheringStatusEnd(null) // TODO: reconsider if really need to clean end mark here
508                 .build();
509         try {
510             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, statusPath, gatheringStatus);
511         } catch (final TransactionChainClosedException e) {
512             LOG.warn("Can't write to transaction, transaction chain probably closed.");
513             LOG.trace("Write to transaction exception: ", e);
514         }
515
516         deviceContext.submitTransaction();
517     }
518
519     /**
520      * Writes snapshot gathering end timestamp + outcome
521      *
522      * @param deviceContext txManager + node path keeper
523      * @param succeeded     outcome of currently finished gathering
524      */
525     static void markDeviceStateSnapshotEnd(final DeviceContext deviceContext, final boolean succeeded) {
526         final InstanceIdentifier<SnapshotGatheringStatusEnd> statusEndPath = deviceContext.getDeviceInfo()
527                 .getNodeInstanceIdentifier().augmentation(FlowCapableStatisticsGatheringStatus.class)
528                 .child(SnapshotGatheringStatusEnd.class);
529
530         final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_AND_TIME_FORMAT);
531         final SnapshotGatheringStatusEnd gatheringStatus = new SnapshotGatheringStatusEndBuilder()
532                 .setEnd(new DateAndTime(simpleDateFormat.format(new Date())))
533                 .setSucceeded(succeeded)
534                 .build();
535         try {
536             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, statusEndPath, gatheringStatus);
537         } catch (TransactionChainClosedException e) {
538             LOG.warn("Can't write to transaction, transaction chain probably closed.");
539             LOG.trace("Write to transaction exception: ", e);
540         }
541
542         deviceContext.submitTransaction();
543     }
544 }