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