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