966608079424ad29c84e875d2eda890e1a989e8c
[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             flowRegistry.processMarks();
325             return Boolean.TRUE;
326         });
327     }
328
329     public static void writeFlowStatistics(final Iterable<FlowsStatisticsUpdate> data,
330                                            final DeviceInfo deviceInfo,
331                                            final DeviceFlowRegistry registry,
332                                            final TxFacade txFacade) {
333         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
334         try {
335             for (final FlowsStatisticsUpdate flowsStatistics : data) {
336                 for (final FlowAndStatisticsMapList flowStat : flowsStatistics.getFlowAndStatisticsMapList()) {
337                     final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
338                     flowBuilder.addAugmentation(FlowStatisticsData.class,
339                             refineFlowStatisticsAugmentation(flowStat).build());
340
341                     final short tableId = flowStat.getTableId();
342                     final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(deviceInfo.getVersion(), flowBuilder.build());
343                     registry.store(flowRegistryKey);
344                     final FlowId flowId = registry.retrieveDescriptor(flowRegistryKey).getFlowId();
345
346                     final FlowKey flowKey = new FlowKey(flowId);
347                     flowBuilder.setKey(flowKey);
348                     final TableKey tableKey = new TableKey(tableId);
349                     final InstanceIdentifier<Flow> flowIdent
350                             = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey);
351                     txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
352                 }
353             }
354         } catch (TransactionChainClosedException e) {
355             LOG.warn("Not able to write to transaction chain: {}", e.getMessage());
356         }
357     }
358
359     /**
360      * Method extracts flow statistics out of flowAndStatistics model.
361      *
362      * @param flowAndStats flow statistics map list
363      */
364     private static FlowStatisticsDataBuilder refineFlowStatisticsAugmentation(
365             final FlowAndStatisticsMapList flowAndStats) {
366         final FlowStatisticsBuilder flowStatisticsBuilder = new FlowStatisticsBuilder(flowAndStats);
367         final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder();
368         flowStatisticsDataBld.setFlowStatistics(flowStatisticsBuilder.build());
369         return flowStatisticsDataBld;
370     }
371
372     public static ListenableFuture<Void> deleteAllKnownFlows(final DeviceInfo deviceInfo,
373                                                              final DeviceFlowRegistry registry,
374                                                              final TxFacade txFacade) {
375         final InstanceIdentifier<FlowCapableNode> flowCapableNodePath
376                 = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
377         final ReadOnlyTransaction readTx = txFacade.getReadTransaction();
378         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowCapableNodeFuture = readTx.read(
379                 LogicalDatastoreType.OPERATIONAL, flowCapableNodePath);
380
381         /* we wish to close readTx for fallBack */
382         Futures.withFallback(flowCapableNodeFuture, t -> {
383             readTx.close();
384             return Futures.immediateFailedFuture(t);
385         });
386         /*
387          * we have to read actual tables with all information before we set empty Flow list, merge is expensive and
388          * not applicable for lists
389          */
390         return Futures.transform(
391                 flowCapableNodeFuture, (AsyncFunction<Optional<FlowCapableNode>, Void>) flowCapNodeOpt -> {
392                     if (flowCapNodeOpt.isPresent()) {
393                         for (final Table tableData : flowCapNodeOpt.get().getTable()) {
394                             final Table table = new TableBuilder(tableData).setFlow(Collections.<Flow>emptyList()).build();
395                             final InstanceIdentifier<Table> iiToTable
396                                     = flowCapableNodePath.child(Table.class, tableData.getKey());
397                             txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToTable, table);
398                         }
399                     }
400                     readTx.close();
401                     return Futures.immediateFuture(null);
402                 });
403     }
404
405     public static Optional<FlowCapableNode> deleteAllKnownFlows(final DeviceInfo deviceInfo,
406                                                              final TxFacade txFacade) {
407         final InstanceIdentifier<FlowCapableNode> flowCapableNodePath
408                 = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
409         final ReadOnlyTransaction readTx = txFacade.getReadTransaction();
410         final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowCapableNodeFuture = readTx.read(
411                 LogicalDatastoreType.OPERATIONAL, flowCapableNodePath);
412
413         try {
414             Optional<FlowCapableNode> fcNodeOpt = flowCapableNodeFuture.get();
415             if ( fcNodeOpt != null && fcNodeOpt.isPresent()){
416                 for (final Table tableData : flowCapableNodeFuture.get().get().getTable()) {
417                     final Table table = new TableBuilder(tableData).setFlow(Collections.<Flow>emptyList()).build();
418                     final InstanceIdentifier<Table> iiToTable = flowCapableNodePath.child(Table.class, tableData.getKey());
419                     txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToTable, table);
420                 }
421                 return fcNodeOpt;
422             }
423         } catch (InterruptedException|ExecutionException e) {
424             LOG.error("Failed to read current OpenFlow node {] operational data", deviceInfo.getNodeId());
425         } finally {
426             if (readTx != null){
427                 readTx.close();
428             }
429         }
430         return Optional.absent();
431     }
432
433     private static void processQueueStatistics(
434             final Iterable<QueueStatisticsUpdate> data,
435             final DeviceInfo deviceInfo,
436             final TxFacade txFacade) throws TransactionChainClosedException {
437         // TODO: clean all queues of all node-connectors before writing up-to-date stats
438         final InstanceIdentifier<Node> nodeIdent = deviceInfo.getNodeInstanceIdentifier();
439         for (final QueueStatisticsUpdate queueStatisticsUpdate : data) {
440             for (final QueueIdAndStatisticsMap queueStat : queueStatisticsUpdate.getQueueIdAndStatisticsMap()) {
441                 if (queueStat.getQueueId() != null) {
442                     final FlowCapableNodeConnectorQueueStatistics statChild =
443                             new FlowCapableNodeConnectorQueueStatisticsBuilder(queueStat).build();
444                     final FlowCapableNodeConnectorQueueStatisticsDataBuilder statBuild =
445                             new FlowCapableNodeConnectorQueueStatisticsDataBuilder();
446                     statBuild.setFlowCapableNodeConnectorQueueStatistics(statChild);
447                     final QueueKey qKey = new QueueKey(queueStat.getQueueId());
448                     final InstanceIdentifier<Queue> queueIdent = nodeIdent
449                             .child(NodeConnector.class, new NodeConnectorKey(queueStat.getNodeConnectorId()))
450                             .augmentation(FlowCapableNodeConnector.class)
451                             .child(Queue.class, qKey);
452                     final QueueBuilder queueBuilder = new QueueBuilder()
453                             .setKey(qKey)
454                             .setQueueId(queueStat.getQueueId())
455                             // node-connector-id is already contained in parent node
456                             // and the port-id here is of incompatible format
457                             .addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, statBuild.build());
458                     txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueIdent, queueBuilder.build());
459                 }
460             }
461         }
462         txFacade.submitTransaction();
463     }
464
465     private static void processFlowTableStatistics(
466             final Iterable<FlowTableStatisticsUpdate> data,
467             final DeviceInfo deviceInfo,
468             final TxFacade txFacade) throws TransactionChainClosedException {
469         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
470         for (final FlowTableStatisticsUpdate flowTableStatisticsUpdate : data) {
471
472             for (final FlowTableAndStatisticsMap tableStat : flowTableStatisticsUpdate.getFlowTableAndStatisticsMap()) {
473                 final InstanceIdentifier<FlowTableStatistics> tStatIdent
474                         = fNodeIdent.child(Table.class, new TableKey(tableStat.getTableId().getValue()))
475                         .augmentation(FlowTableStatisticsData.class).child(FlowTableStatistics.class);
476                 final FlowTableStatistics stats = new FlowTableStatisticsBuilder(tableStat).build();
477                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);
478             }
479         }
480         txFacade.submitTransaction();
481     }
482
483     private static void processNodeConnectorStatistics(
484             final Iterable<NodeConnectorStatisticsUpdate> data,
485             final DeviceInfo deviceInfo,
486             final TxFacade txFacade) throws TransactionChainClosedException {
487         final InstanceIdentifier<Node> nodeIdent = deviceInfo.getNodeInstanceIdentifier();
488         for (final NodeConnectorStatisticsUpdate nodeConnectorStatisticsUpdate : data) {
489             for (final NodeConnectorStatisticsAndPortNumberMap nodeConnectPort :
490                     nodeConnectorStatisticsUpdate.getNodeConnectorStatisticsAndPortNumberMap()) {
491                 final FlowCapableNodeConnectorStatistics stats
492                         = new FlowCapableNodeConnectorStatisticsBuilder(nodeConnectPort).build();
493                 final NodeConnectorKey key = new NodeConnectorKey(nodeConnectPort.getNodeConnectorId());
494                 final InstanceIdentifier<NodeConnector> nodeConnectorIdent = nodeIdent.child(NodeConnector.class, key);
495                 final InstanceIdentifier<FlowCapableNodeConnectorStatisticsData> nodeConnStatIdent = nodeConnectorIdent
496                         .augmentation(FlowCapableNodeConnectorStatisticsData.class);
497                 final InstanceIdentifier<FlowCapableNodeConnectorStatistics> flowCapNodeConnStatIdent =
498                         nodeConnStatIdent.child(FlowCapableNodeConnectorStatistics.class);
499                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowCapNodeConnStatIdent, stats);
500             }
501         }
502         txFacade.submitTransaction();
503     }
504
505     private static void processMetersStatistics(final Iterable<MeterStatisticsUpdated> data,
506                                                 final DeviceInfo deviceInfo,
507                                                 final TxFacade txFacade) throws TransactionChainClosedException {
508         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
509         for (final MeterStatisticsUpdated meterStatisticsUpdated : data) {
510             for (final MeterStats meterStats : meterStatisticsUpdated.getMeterStats()) {
511                 final MeterStatistics stats = new MeterStatisticsBuilder(meterStats).build();
512                 final MeterId meterId = meterStats.getMeterId();
513                 final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
514                 final InstanceIdentifier<NodeMeterStatistics> nodeMeterStatIdent = meterIdent
515                         .augmentation(NodeMeterStatistics.class);
516                 final InstanceIdentifier<MeterStatistics> msIdent = nodeMeterStatIdent.child(MeterStatistics.class);
517                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
518             }
519         }
520         txFacade.submitTransaction();
521     }
522
523     private static void deleteAllKnownMeters(
524             final DeviceMeterRegistry meterRegistry,
525             final InstanceIdentifier<FlowCapableNode> flowNodeIdent,
526             final TxFacade txFacade) throws TransactionChainClosedException {
527         meterRegistry.forEach(meterId -> {
528             final InstanceIdentifier<Meter> meterIdent = flowNodeIdent.child(Meter.class, new MeterKey(meterId));
529             txFacade.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, meterIdent);
530         });
531         meterRegistry.processMarks();
532     }
533
534     private static void processGroupDescStats(
535             final Iterable<GroupDescStatsUpdated> data,
536             final DeviceInfo deviceInfo,
537             final TxFacade txFacade,
538             final DeviceGroupRegistry groupRegistry) throws TransactionChainClosedException {
539         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
540         deleteAllKnownGroups(txFacade, fNodeIdent, groupRegistry);
541
542         for (final GroupDescStatsUpdated groupDescStatsUpdated : data) {
543             for (final GroupDescStats groupDescStats : groupDescStatsUpdated.getGroupDescStats()) {
544                 final GroupId groupId = groupDescStats.getGroupId();
545
546                 final GroupBuilder groupBuilder = new GroupBuilder(groupDescStats);
547                 groupBuilder.setKey(new GroupKey(groupId));
548                 groupBuilder.addAugmentation(NodeGroupStatistics.class, new NodeGroupStatisticsBuilder().build());
549
550                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
551
552                 groupRegistry.store(groupId);
553                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, groupIdent, groupBuilder.build());
554             }
555         }
556         txFacade.submitTransaction();
557     }
558
559     private static void deleteAllKnownGroups(
560             final TxFacade txFacade,
561             final InstanceIdentifier<FlowCapableNode> flowNodeIdent,
562             final DeviceGroupRegistry groupRegistry) throws TransactionChainClosedException {
563         groupRegistry.forEach(groupId -> {
564             final InstanceIdentifier<Group> groupIdent = flowNodeIdent.child(Group.class, new GroupKey(groupId));
565             txFacade.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, groupIdent);
566         });
567         groupRegistry.processMarks();
568     }
569
570     private static void processGroupStatistics(
571             final Iterable<GroupStatisticsUpdated> data,
572             final DeviceInfo deviceInfo,
573             final TxFacade txFacade) throws TransactionChainClosedException {
574         final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
575         for (final GroupStatisticsUpdated groupStatistics : data) {
576             for (final GroupStats groupStats : groupStatistics.getGroupStats()) {
577
578                 final InstanceIdentifier<Group> groupIdent
579                         = fNodeIdent.child(Group.class, new GroupKey(groupStats.getGroupId()));
580                 final InstanceIdentifier<NodeGroupStatistics> nGroupStatIdent = groupIdent
581                         .augmentation(NodeGroupStatistics.class);
582
583                 final InstanceIdentifier<GroupStatistics> gsIdent = nGroupStatIdent.child(GroupStatistics.class);
584                 final GroupStatistics stats = new GroupStatisticsBuilder(groupStats).build();
585                 txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gsIdent, stats);
586             }
587         }
588         txFacade.submitTransaction();
589     }
590
591     private static InstanceIdentifier<FlowCapableNode> assembleFlowCapableNodeInstanceIdentifier(
592             final DeviceInfo deviceInfo) {
593         return deviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
594     }
595
596     /**
597      * Writes snapshot gathering start timestamp + cleans end mark.
598      *
599      * @param deviceContext txManager + node path keeper
600      */
601     static void markDeviceStateSnapshotStart(final DeviceContext deviceContext) {
602         final InstanceIdentifier<FlowCapableStatisticsGatheringStatus> statusPath = deviceContext.getDeviceInfo()
603                 .getNodeInstanceIdentifier().augmentation(FlowCapableStatisticsGatheringStatus.class);
604
605         final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_AND_TIME_FORMAT);
606         final FlowCapableStatisticsGatheringStatus gatheringStatus = new FlowCapableStatisticsGatheringStatusBuilder()
607                 .setSnapshotGatheringStatusStart(new SnapshotGatheringStatusStartBuilder()
608                         .setBegin(new DateAndTime(simpleDateFormat.format(new Date())))
609                         .build())
610                 .setSnapshotGatheringStatusEnd(null) // TODO: reconsider if really need to clean end mark here
611                 .build();
612         try {
613             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, statusPath, gatheringStatus);
614         } catch (final TransactionChainClosedException e) {
615             LOG.warn("Can't write to transaction, transaction chain probably closed.");
616             LOG.trace("Write to transaction exception: ", e);
617         }
618
619         deviceContext.submitTransaction();
620     }
621
622     /**
623      * Writes snapshot gathering end timestamp + outcome.
624      *
625      * @param deviceContext txManager + node path keeper
626      * @param succeeded     outcome of currently finished gathering
627      */
628     static void markDeviceStateSnapshotEnd(final DeviceContext deviceContext, final boolean succeeded) {
629         final InstanceIdentifier<SnapshotGatheringStatusEnd> statusEndPath = deviceContext.getDeviceInfo()
630                 .getNodeInstanceIdentifier().augmentation(FlowCapableStatisticsGatheringStatus.class)
631                 .child(SnapshotGatheringStatusEnd.class);
632
633         final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_AND_TIME_FORMAT);
634         final SnapshotGatheringStatusEnd gatheringStatus = new SnapshotGatheringStatusEndBuilder()
635                 .setEnd(new DateAndTime(simpleDateFormat.format(new Date())))
636                 .setSucceeded(succeeded)
637                 .build();
638         try {
639             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, statusEndPath, gatheringStatus);
640         } catch (TransactionChainClosedException e) {
641             LOG.warn("Can't write to transaction, transaction chain probably closed.");
642             LOG.trace("Write to transaction exception: ", e);
643         }
644
645         deviceContext.submitTransaction();
646     }
647 }