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