DeviceState changes
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsGatheringUtils.java
index 05ff9d495afab59dd6eebaf85a3e71ff1b384076..00b824aaf02301b6d45c582e526ecf3bc01b3263 100644 (file)
@@ -8,44 +8,63 @@
 
 package org.opendaylight.openflowplugin.impl.statistics;
 
-import com.google.common.collect.Iterables;
-
-import java.util.Collections;
 import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.collect.Iterables;
+import com.google.common.util.concurrent.AsyncFunction;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.FutureFallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.JdkFutureAdapters;
 import com.google.common.util.concurrent.ListenableFuture;
+import java.text.SimpleDateFormat;
+import java.util.Collections;
+import java.util.Date;
 import java.util.List;
-import java.util.Map;
 import javax.annotation.Nullable;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
+import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
-import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
-import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowHash;
-import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
-import org.opendaylight.openflowplugin.impl.registry.flow.FlowHashFactory;
-import org.opendaylight.openflowplugin.impl.statistics.services.dedicated.StatisticsGatheringService;
-import org.opendaylight.openflowplugin.impl.util.FlowUtil;
+import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
+import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
+import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.StatisticsGatherer;
+import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
+import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.DateAndTime;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableStatisticsGatheringStatus;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableStatisticsGatheringStatusBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.snapshot.gathering.status.grouping.SnapshotGatheringStatusEnd;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.snapshot.gathering.status.grouping.SnapshotGatheringStatusEndBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.snapshot.gathering.status.grouping.SnapshotGatheringStatusStartBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsData;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowStatisticsDataBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.statistics.FlowStatisticsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsData;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.FlowTableStatisticsUpdate;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.and.statistics.map.FlowTableAndStatisticsMap;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatistics;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.flow.table.statistics.FlowTableStatisticsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.Queue;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.queues.QueueKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupDescStatsUpdated;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GroupStatisticsUpdated;
@@ -59,12 +78,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterConfigStatsUpdated;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.MeterStatisticsUpdated;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.NodeMeterStatistics;
@@ -89,7 +105,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.
 import org.opendaylight.yang.gen.v1.urn.opendaylight.queue.statistics.rev131216.queue.id.and.statistics.map.QueueIdAndStatisticsMap;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -99,85 +114,119 @@ import org.slf4j.LoggerFactory;
  */
 public final class StatisticsGatheringUtils {
 
+    public static String DATE_AND_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
+
     private static final Logger LOG = LoggerFactory.getLogger(StatisticsGatheringUtils.class);
     private static final SinglePurposeMultipartReplyTranslator MULTIPART_REPLY_TRANSLATOR = new SinglePurposeMultipartReplyTranslator();
+    public static final String QUEUE2_REQCTX = "QUEUE2REQCTX-";
 
     private StatisticsGatheringUtils() {
         throw new IllegalStateException("This class should not be instantiated.");
     }
 
-    private static KeyedInstanceIdentifier<Node, NodeKey> getInstanceIdentifier(final DeviceContext deviceContext) {
-        return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(deviceContext.getPrimaryConnectionContext().getNodeId()));
-    }
 
-    public static ListenableFuture<Boolean> gatherStatistics(final StatisticsGatheringService statisticsGatheringService,
+    public static ListenableFuture<Boolean> gatherStatistics(final StatisticsGatherer statisticsGatheringService,
                                                              final DeviceContext deviceContext,
                                                              final MultipartType type) {
-        //FIXME : anytype listener must not be send as parameter, it has to be extracted from device context inside service
+        final String deviceId = deviceContext.getPrimaryConnectionContext().getNodeId().toString();
+        EventIdentifier wholeProcessEventIdentifier = null;
+        if (MultipartType.OFPMPFLOW.equals(type)) {
+            wholeProcessEventIdentifier = new EventIdentifier(type.toString(), deviceId);
+            EventsTimeCounter.markStart(wholeProcessEventIdentifier);
+        }
+        final EventIdentifier ofpQueuToRequestContextEventIdentifier = new EventIdentifier(QUEUE2_REQCTX + type.toString(), deviceId);
         final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture =
-                JdkFutureAdapters.listenInPoolThread(statisticsGatheringService.getStatisticsOfType(type));
-        return transformAndStoreStatisticsData(statisticsDataInFuture, deviceContext);
+                JdkFutureAdapters.listenInPoolThread(statisticsGatheringService.getStatisticsOfType(
+                        ofpQueuToRequestContextEventIdentifier, type));
+        return transformAndStoreStatisticsData(statisticsDataInFuture, deviceContext, wholeProcessEventIdentifier, type);
     }
 
     private static ListenableFuture<Boolean> transformAndStoreStatisticsData(final ListenableFuture<RpcResult<List<MultipartReply>>> statisticsDataInFuture,
-                                                                             final DeviceContext deviceContext) {
-        return Futures.transform(statisticsDataInFuture, new Function<RpcResult<List<MultipartReply>>, Boolean>() {
+                                                                             final DeviceContext deviceContext,
+                                                                             final EventIdentifier eventIdentifier, final MultipartType type) {
+        return Futures.transform(statisticsDataInFuture, new AsyncFunction<RpcResult<List<MultipartReply>>, Boolean>() {
             @Nullable
             @Override
-            public Boolean apply(final RpcResult<List<MultipartReply>> rpcResult) {
+            public ListenableFuture<Boolean> apply(final RpcResult<List<MultipartReply>> rpcResult) {
+                boolean isMultipartProcessed = Boolean.TRUE;
                 if (rpcResult.isSuccessful()) {
-                    boolean isMultipartProcessed = Boolean.TRUE;
-                    Iterable<? extends DataObject> allMultipartData = Collections.emptyList();
-                    DataObject multipartData = null;
-                    for (final MultipartReply singleReply : rpcResult.getResult()) {
-                        final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply);
-                        multipartData = multipartDataList.get(0);
-                        allMultipartData = Iterables.concat(allMultipartData, multipartDataList);
-                    }
+                    LOG.debug("Stats reply successfully received for node {} of type {}", deviceContext.getDeviceInfo().getNodeId(), type);
 
-                    if (deviceContext.getDeviceState().isValid()) {
-                        deviceContext.startGatheringOperationsToOneTransaction();
-                    }
+                    // TODO: in case the result value is null then multipart data probably got processed on the fly -
+                    // TODO: this contract should by clearly stated and enforced - now simple true value is returned
+                    if (null != rpcResult.getResult()) {
+                        Iterable<? extends DataObject> allMultipartData = Collections.emptyList();
+                        DataObject multipartData = null;
+
+
+                        try {
+                            for (final MultipartReply singleReply : rpcResult.getResult()) {
+                                final List<? extends DataObject> multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(
+                                        deviceContext.getPrimaryConnectionContext().getFeatures().getDatapathId(),
+                                        deviceContext.getPrimaryConnectionContext().getFeatures().getVersion(), singleReply);
+                                multipartData = multipartDataList.get(0);
+                                allMultipartData = Iterables.concat(allMultipartData, multipartDataList);
+                            }
+                        } catch (final Exception e) {
+                            LOG.warn("stats processing of type {} for node {} failed during transfomation step",
+                                    type, deviceContext.getDeviceInfo().getNodeId(), e);
+                            return Futures.immediateFailedFuture(e);
+                        }
+
+
+                        try {
+                            if (multipartData instanceof GroupStatisticsUpdated) {
+                                processGroupStatistics((Iterable<GroupStatisticsUpdated>) allMultipartData, deviceContext);
+                            } else if (multipartData instanceof MeterStatisticsUpdated) {
+                                processMetersStatistics((Iterable<MeterStatisticsUpdated>) allMultipartData, deviceContext);
+                            } else if (multipartData instanceof NodeConnectorStatisticsUpdate) {
+                                processNodeConnectorStatistics((Iterable<NodeConnectorStatisticsUpdate>) allMultipartData, deviceContext);
+                            } else if (multipartData instanceof FlowTableStatisticsUpdate) {
+                                processFlowTableStatistics((Iterable<FlowTableStatisticsUpdate>) allMultipartData, deviceContext);
+                            } else if (multipartData instanceof QueueStatisticsUpdate) {
+                                processQueueStatistics((Iterable<QueueStatisticsUpdate>) allMultipartData, deviceContext);
+                            } else if (multipartData instanceof FlowsStatisticsUpdate) {
+                                /* FlowStat Processing is realized by NettyThread only by initPhase, otherwise it is realized
+                                 * by MD-SAL thread */
+                                return processFlowStatistics((Iterable<FlowsStatisticsUpdate>) allMultipartData, deviceContext, eventIdentifier);
 
-                    if (multipartData instanceof GroupStatisticsUpdated) {
-                        processGroupStatistics((Iterable<GroupStatisticsUpdated>)allMultipartData, deviceContext);
-                    } else if (multipartData instanceof MeterStatisticsUpdated) {
-                        processMetersStatistics((Iterable<MeterStatisticsUpdated>)allMultipartData, deviceContext);
-                    } else if (multipartData instanceof NodeConnectorStatisticsUpdate) {
-                        processNodeConnectorStatistics((Iterable<NodeConnectorStatisticsUpdate>) allMultipartData, deviceContext);
-                    } else if (multipartData instanceof FlowTableStatisticsUpdate) {
-                        processFlowTableStatistics((Iterable<FlowTableStatisticsUpdate>) allMultipartData, deviceContext);
-                    } else if (multipartData instanceof QueueStatisticsUpdate) {
-                        processQueueStatistics((Iterable<QueueStatisticsUpdate>) allMultipartData, deviceContext);
-                    } else if (multipartData instanceof FlowsStatisticsUpdate) {
-                        processFlowStatistics((Iterable<FlowsStatisticsUpdate>)allMultipartData, deviceContext);
-                    } else if (multipartData instanceof GroupDescStatsUpdated) {
-                        processGroupDescStats((Iterable<GroupDescStatsUpdated>) allMultipartData, deviceContext);
-                    } else if (multipartData instanceof MeterConfigStatsUpdated) {
-                        processMeterConfigStatsUpdated((Iterable<MeterConfigStatsUpdated>) allMultipartData, deviceContext);
+                            } else if (multipartData instanceof GroupDescStatsUpdated) {
+                                processGroupDescStats((Iterable<GroupDescStatsUpdated>) allMultipartData, deviceContext);
+                            } else if (multipartData instanceof MeterConfigStatsUpdated) {
+                                processMeterConfigStatsUpdated((Iterable<MeterConfigStatsUpdated>) allMultipartData, deviceContext);
+                            } else {
+                                isMultipartProcessed = Boolean.FALSE;
+                            }
+                        } catch (final Exception e) {
+                            LOG.warn("stats processing of type {} for node {} failed during write-to-tx step",
+                                    type, deviceContext.getDeviceInfo().getNodeId(), e);
+                            return Futures.immediateFailedFuture(e);
+                        }
+
+                        LOG.debug("Stats reply added to transaction for node {} of type {}", deviceContext.getDeviceInfo().getNodeId(), type);
+
+                        //TODO : implement experimenter
                     } else {
-                        isMultipartProcessed = Boolean.FALSE;
+                        LOG.debug("Stats reply was empty for node {} of type {}", deviceContext.getDeviceInfo().getNodeId(), type);
                     }
-                    //TODO : implement experimenter
 
-                    if (deviceContext.getDeviceState().isValid()) {
-                        deviceContext.commitOperationsGatheredInOneTransaction();
-                    }
-                    return isMultipartProcessed;
+                } else {
+                    LOG.debug("Stats reply FAILED for node {} of type {}: {}", deviceContext.getDeviceInfo().getNodeId(), type, rpcResult.getErrors());
+                    isMultipartProcessed = Boolean.FALSE;
                 }
-                return Boolean.FALSE;
+                return Futures.immediateFuture(isMultipartProcessed);
             }
         });
     }
 
-    private static void processMeterConfigStatsUpdated(final Iterable<MeterConfigStatsUpdated> data, final DeviceContext deviceContext) {
+    private static void processMeterConfigStatsUpdated(final Iterable<MeterConfigStatsUpdated> data, final DeviceContext deviceContext) throws Exception {
+        final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext.getDeviceInfo());
+        deleteAllKnownMeters(deviceContext, fNodeIdent);
         for (final MeterConfigStatsUpdated meterConfigStatsUpdated : data) {
-            NodeId nodeId = meterConfigStatsUpdated.getId();
-            final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(nodeId);
-            for (MeterConfigStats meterConfigStats : meterConfigStatsUpdated.getMeterConfigStats()) {
+            for (final MeterConfigStats meterConfigStats : meterConfigStatsUpdated.getMeterConfigStats()) {
                 final MeterId meterId = meterConfigStats.getMeterId();
                 final InstanceIdentifier<Meter> meterInstanceIdentifier = fNodeIdent.child(Meter.class, new MeterKey(meterId));
-    
+
                 final MeterBuilder meterBuilder = new MeterBuilder(meterConfigStats);
                 meterBuilder.setKey(new MeterKey(meterId));
                 meterBuilder.addAugmentation(NodeMeterStatistics.class, new NodeMeterStatisticsBuilder().build());
@@ -185,66 +234,115 @@ public final class StatisticsGatheringUtils {
                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, meterInstanceIdentifier, meterBuilder.build());
             }
         }
+        deviceContext.submitTransaction();
     }
 
-    private static void processFlowStatistics(final Iterable<FlowsStatisticsUpdate> data, final DeviceContext deviceContext) {
-        boolean isFirstLoop = true;
-        for (final FlowsStatisticsUpdate flowsStatistics : data) {
-            if (isFirstLoop) {
-                final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
-                        .child(Node.class, new NodeKey(flowsStatistics.getId()));
-                deleteAllKnownFlows(deviceContext, nodeIdent);
-                isFirstLoop = false;
+    private static ListenableFuture<Boolean> processFlowStatistics(final Iterable<FlowsStatisticsUpdate> data,
+                                                                   final DeviceContext deviceContext, final EventIdentifier eventIdentifier) {
+        final ListenableFuture<Void> deleFuture = deleteAllKnownFlows(deviceContext.getDeviceInfo(),
+                deviceContext.getDeviceFlowRegistry(), deviceContext, deviceContext.getDeviceState());
+        return Futures.transform(deleFuture, new Function<Void, Boolean>() {
+
+            @Override
+            public Boolean apply(final Void input) {
+                writeFlowStatistics(data, deviceContext.getDeviceInfo(), deviceContext.getDeviceFlowRegistry(),
+                        deviceContext);
+                deviceContext.submitTransaction();
+                EventsTimeCounter.markEnd(eventIdentifier);
+                return Boolean.TRUE;
             }
-            for (final FlowAndStatisticsMapList flowStat : flowsStatistics.getFlowAndStatisticsMapList()) {
-                final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
-                FlowId flowId = null;
-                FlowHash flowHash = FlowHashFactory.create(flowBuilder.build(), deviceContext.getPrimaryConnectionContext().getFeatures().getVersion());
-                short tableId = flowStat.getTableId();
-                final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
-                FlowDescriptor flowDescriptor;
-                flowDescriptor = deviceFlowRegistry.retrieveIdForFlow(flowHash);
-                if (null != flowDescriptor) {
-                    flowId = flowDescriptor.getFlowId();
-                } else {
-                    LOG.trace("Flow descriptor for flow hash {} wasn't found.", flowHash.hashCode());
-                    flowId = FlowUtil.createAlienFlowId(tableId);
-                    flowDescriptor = FlowDescriptorFactory.create(tableId, flowId);
-                    deviceFlowRegistry.store(flowHash, flowDescriptor);
+        });
+    }
+
+    public static void writeFlowStatistics(final Iterable<FlowsStatisticsUpdate> data,
+                                           final DeviceInfo deviceInfo,
+                                           final DeviceFlowRegistry registry,
+                                           final TxFacade txFacade) {
+        final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
+        try {
+            for (final FlowsStatisticsUpdate flowsStatistics : data) {
+                for (final FlowAndStatisticsMapList flowStat : flowsStatistics.getFlowAndStatisticsMapList()) {
+                    final FlowBuilder flowBuilder = new FlowBuilder(flowStat);
+                    flowBuilder.addAugmentation(FlowStatisticsData.class, refineFlowStatisticsAugmentation(flowStat).build());
+
+                    final short tableId = flowStat.getTableId();
+                    final FlowRegistryKey flowRegistryKey = FlowRegistryKeyFactory.create(flowBuilder.build());
+                    final FlowId flowId = registry.storeIfNecessary(flowRegistryKey, tableId);
+
+                    final FlowKey flowKey = new FlowKey(flowId);
+                    flowBuilder.setKey(flowKey);
+                    final TableKey tableKey = new TableKey(tableId);
+                    final InstanceIdentifier<Flow> flowIdent = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey);
+                    txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
                 }
-                FlowKey flowKey = new FlowKey(flowId);
-                flowBuilder.setKey(flowKey);
-                final TableKey tableKey = new TableKey(tableId);
-                final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(flowsStatistics.getId());
-                final InstanceIdentifier<Flow> flowIdent = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey);
-                deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build());
             }
+        } catch (Exception e) {
+            LOG.warn("Not able to write to transaction: {}", e.getMessage());
         }
     }
 
-    private static void deleteAllKnownFlows(final DeviceContext deviceContext, final InstanceIdentifier<Node> nodeIdent) {
-        if (deviceContext.getDeviceState().deviceSynchronized()) {
-            for (Map.Entry<FlowHash, FlowDescriptor> registryEntry : deviceContext.getDeviceFlowRegistry().getAllFlowDescriptors().entrySet()) {
-                FlowDescriptor flowDescriptor = registryEntry.getValue();
+    /**
+     * Method extracts flow statistics out of flowAndStatistics model
+     *
+     * @param flowAndStats
+     */
+    private static FlowStatisticsDataBuilder refineFlowStatisticsAugmentation(final FlowAndStatisticsMapList flowAndStats) {
+        final FlowStatisticsBuilder flowStatisticsBuilder = new FlowStatisticsBuilder(flowAndStats);
+        final FlowStatisticsDataBuilder flowStatisticsDataBld = new FlowStatisticsDataBuilder();
+        flowStatisticsDataBld.setFlowStatistics(flowStatisticsBuilder.build());
+        return flowStatisticsDataBld;
+    }
 
-                FlowId flowId = flowDescriptor.getFlowId();
-                FlowKey flowKey = new FlowKey(flowId);
-                final InstanceIdentifier<Flow> flowInstanceIdentifier = nodeIdent
-                        .augmentation(FlowCapableNode.class)
-                        .child(Table.class, flowDescriptor.getTableKey())
-                        .child(Flow.class, flowKey);
+    public static ListenableFuture<Void> deleteAllKnownFlows(final DeviceInfo deviceInfo,
+                                                             final DeviceFlowRegistry registry,
+                                                             final TxFacade txFacade,
+                                                             final DeviceState deviceState) {
+        //TODO:Make check for phase from enum
+        /* DeviceState.deviceSynchronized is a marker for actual phase - false means initPhase, true means noInitPhase */
+        if (deviceState.deviceSynchronized()) {
+            final InstanceIdentifier<FlowCapableNode> flowCapableNodePath = assembleFlowCapableNodeInstanceIdentifier(deviceInfo);
+            final ReadOnlyTransaction readTx = txFacade.getReadTransaction();
+            final CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> flowCapableNodeFuture = readTx.read(
+                    LogicalDatastoreType.OPERATIONAL, flowCapableNodePath);
 
-                LOG.trace("Deleting flow with id {}", flowInstanceIdentifier);
-                deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, flowInstanceIdentifier);
-            }
+            /* we wish to close readTx for fallBack */
+            Futures.withFallback(flowCapableNodeFuture, new FutureFallback<Optional<FlowCapableNode>>() {
+
+                @Override
+                public ListenableFuture<Optional<FlowCapableNode>> create(final Throwable t) throws Exception {
+                    readTx.close();
+                    return Futures.immediateFailedFuture(t);
+                }
+            });
+            /*
+             * we have to read actual tables with all information before we set empty Flow list, merge is expensive and
+             * not applicable for lists
+             */
+            return Futures.transform(flowCapableNodeFuture, new AsyncFunction<Optional<FlowCapableNode>, Void>() {
+
+                @Override
+                public ListenableFuture<Void> apply(final Optional<FlowCapableNode> flowCapNodeOpt) throws Exception {
+                    if (flowCapNodeOpt.isPresent()) {
+                        for (final Table tableData : flowCapNodeOpt.get().getTable()) {
+                            final Table table = new TableBuilder(tableData).setFlow(Collections.<Flow>emptyList()).build();
+                            final InstanceIdentifier<Table> iiToTable = flowCapableNodePath.child(Table.class, tableData.getKey());
+                            txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToTable, table);
+                        }
+                    }
+                    registry.removeMarked();
+                    readTx.close();
+                    return Futures.immediateFuture(null);
+                }
+
+            });
         }
-        deviceContext.getDeviceFlowRegistry().removeMarked();
+        return Futures.immediateFuture(null);
     }
 
-    private static void processQueueStatistics(final Iterable<QueueStatisticsUpdate> data, final DeviceContext deviceContext) {
+    private static void processQueueStatistics(final Iterable<QueueStatisticsUpdate> data, final DeviceContext deviceContext) throws Exception {
+        // TODO: clean all queues of all node-connectors before writing up-to-date stats
+        final InstanceIdentifier<Node> nodeIdent = deviceContext.getDeviceInfo().getNodeInstanceIdentifier();
         for (final QueueStatisticsUpdate queueStatisticsUpdate : data) {
-            final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
-                    .child(Node.class, new NodeKey(queueStatisticsUpdate.getId()));
             for (final QueueIdAndStatisticsMap queueStat : queueStatisticsUpdate.getQueueIdAndStatisticsMap()) {
                 if (queueStat.getQueueId() != null) {
                     final FlowCapableNodeConnectorQueueStatistics statChild =
@@ -257,17 +355,22 @@ public final class StatisticsGatheringUtils {
                             .child(NodeConnector.class, new NodeConnectorKey(queueStat.getNodeConnectorId()))
                             .augmentation(FlowCapableNodeConnector.class)
                             .child(Queue.class, qKey);
-                    final InstanceIdentifier<FlowCapableNodeConnectorQueueStatisticsData> queueStatIdent = queueIdent.augmentation(FlowCapableNodeConnectorQueueStatisticsData.class);
-                    deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueStatIdent, statBuild.build());
+                    final QueueBuilder queueBuilder = new QueueBuilder()
+                            .setKey(qKey)
+                            .setQueueId(queueStat.getQueueId())
+                            // node-connector-id is already contained in parent node and the port-id here is of incompatible format
+                            .addAugmentation(FlowCapableNodeConnectorQueueStatisticsData.class, statBuild.build());
+                    deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, queueIdent, queueBuilder.build());
                 }
             }
         }
+        deviceContext.submitTransaction();
     }
 
-    private static void processFlowTableStatistics(final Iterable<FlowTableStatisticsUpdate> data, final DeviceContext deviceContext) {
-        for(final FlowTableStatisticsUpdate flowTableStatisticsUpdate : data) {
-            final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(flowTableStatisticsUpdate.getId());
-    
+    private static void processFlowTableStatistics(final Iterable<FlowTableStatisticsUpdate> data, final DeviceContext deviceContext) throws Exception {
+        final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext.getDeviceInfo());
+        for (final FlowTableStatisticsUpdate flowTableStatisticsUpdate : data) {
+
             for (final FlowTableAndStatisticsMap tableStat : flowTableStatisticsUpdate.getFlowTableAndStatisticsMap()) {
                 final InstanceIdentifier<FlowTableStatistics> tStatIdent = fNodeIdent.child(Table.class, new TableKey(tableStat.getTableId().getValue()))
                         .augmentation(FlowTableStatisticsData.class).child(FlowTableStatistics.class);
@@ -275,12 +378,12 @@ public final class StatisticsGatheringUtils {
                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, tStatIdent, stats);
             }
         }
+        deviceContext.submitTransaction();
     }
 
-    private static void processNodeConnectorStatistics(final Iterable<NodeConnectorStatisticsUpdate> data, final DeviceContext deviceContext) {
+    private static void processNodeConnectorStatistics(final Iterable<NodeConnectorStatisticsUpdate> data, final DeviceContext deviceContext) throws Exception {
+        final InstanceIdentifier<Node> nodeIdent = deviceContext.getDeviceInfo().getNodeInstanceIdentifier();
         for (final NodeConnectorStatisticsUpdate nodeConnectorStatisticsUpdate : data) {
-            final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class)
-                    .child(Node.class, new NodeKey(nodeConnectorStatisticsUpdate.getId()));
             for (final NodeConnectorStatisticsAndPortNumberMap nConnectPort : nodeConnectorStatisticsUpdate.getNodeConnectorStatisticsAndPortNumberMap()) {
                 final FlowCapableNodeConnectorStatistics stats = new FlowCapableNodeConnectorStatisticsBuilder(nConnectPort).build();
                 final NodeConnectorKey key = new NodeConnectorKey(nConnectPort.getNodeConnectorId());
@@ -292,17 +395,13 @@ public final class StatisticsGatheringUtils {
                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowCapNodeConnStatIdent, stats);
             }
         }
+        deviceContext.submitTransaction();
     }
 
     private static void processMetersStatistics(final Iterable<MeterStatisticsUpdated> data,
-                                                final DeviceContext deviceContext) {
-        boolean isFirstLoop = true;
-        for (MeterStatisticsUpdated meterStatisticsUpdated : data) {
-            final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(meterStatisticsUpdated.getId());
-            if (isFirstLoop) {
-                deleteAllKnownMeters(deviceContext, fNodeIdent);
-                isFirstLoop = false;
-            }
+                                                final DeviceContext deviceContext) throws Exception {
+        final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext.getDeviceInfo());
+        for (final MeterStatisticsUpdated meterStatisticsUpdated : data) {
             for (final MeterStats mStat : meterStatisticsUpdated.getMeterStats()) {
                 final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build();
                 final MeterId meterId = mStat.getMeterId();
@@ -313,70 +412,115 @@ public final class StatisticsGatheringUtils {
                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, msIdent, stats);
             }
         }
+        deviceContext.submitTransaction();
     }
 
-    private static void deleteAllKnownMeters(final DeviceContext deviceContext, final InstanceIdentifier<FlowCapableNode> fNodeIdent) {
-        for (MeterId meterId : deviceContext.getDeviceMeterRegistry().getAllMeterIds()) {
+    private static void deleteAllKnownMeters(final DeviceContext deviceContext, final InstanceIdentifier<FlowCapableNode> fNodeIdent) throws Exception {
+        for (final MeterId meterId : deviceContext.getDeviceMeterRegistry().getAllMeterIds()) {
             final InstanceIdentifier<Meter> meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId));
             deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, meterIdent);
         }
         deviceContext.getDeviceMeterRegistry().removeMarked();
     }
 
-    private static void processGroupDescStats(final Iterable<GroupDescStatsUpdated> data, final DeviceContext deviceContext) {
-        for ( GroupDescStatsUpdated groupDescStatsUpdated : data) {
-            NodeId nodeId = groupDescStatsUpdated.getId();
-            final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(nodeId);
-    
-            for (GroupDescStats groupDescStats : groupDescStatsUpdated.getGroupDescStats()) {
+    private static void processGroupDescStats(final Iterable<GroupDescStatsUpdated> data, final DeviceContext deviceContext) throws Exception {
+        final InstanceIdentifier<FlowCapableNode> fNodeIdent =
+                deviceContext.getDeviceInfo().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
+        deleteAllKnownGroups(deviceContext, fNodeIdent);
+
+        for (final GroupDescStatsUpdated groupDescStatsUpdated : data) {
+            for (final GroupDescStats groupDescStats : groupDescStatsUpdated.getGroupDescStats()) {
                 final GroupId groupId = groupDescStats.getGroupId();
-    
+
                 final GroupBuilder groupBuilder = new GroupBuilder(groupDescStats);
                 groupBuilder.setKey(new GroupKey(groupId));
                 groupBuilder.addAugmentation(NodeGroupStatistics.class, new NodeGroupStatisticsBuilder().build());
-    
+
                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
-    
+
                 deviceContext.getDeviceGroupRegistry().store(groupId);
                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, groupIdent, groupBuilder.build());
             }
         }
+        deviceContext.submitTransaction();
     }
 
-    private static void deleteAllKnownGroups(final DeviceContext deviceContext, final InstanceIdentifier<FlowCapableNode> fNodeIdent) {
-        for (GroupId groupId : deviceContext.getDeviceGroupRegistry().getAllGroupIds()) {
+    private static void deleteAllKnownGroups(final DeviceContext deviceContext, final InstanceIdentifier<FlowCapableNode> fNodeIdent) throws Exception {
+        for (final GroupId groupId : deviceContext.getDeviceGroupRegistry().getAllGroupIds()) {
             final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId));
             deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, groupIdent);
         }
         deviceContext.getDeviceGroupRegistry().removeMarked();
     }
 
-    private static void processGroupStatistics(final Iterable<GroupStatisticsUpdated> data, final DeviceContext deviceContext) {
-        boolean isFirstLoop = true; 
-        for (GroupStatisticsUpdated groupStatistics : data) {
-            NodeId nodeId = groupStatistics.getId();
-            final InstanceIdentifier<FlowCapableNode> fNodeIdent = getFlowCapableNodeInstanceIdentifier(nodeId);
-            if (isFirstLoop) {
-                deleteAllKnownGroups(deviceContext, fNodeIdent);
-                isFirstLoop = false;
-            }
-    
+    private static void processGroupStatistics(final Iterable<GroupStatisticsUpdated> data, final DeviceContext deviceContext) throws Exception {
+        final InstanceIdentifier<FlowCapableNode> fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext.getDeviceInfo());
+        for (final GroupStatisticsUpdated groupStatistics : data) {
             for (final GroupStats groupStats : groupStatistics.getGroupStats()) {
-    
+
                 final InstanceIdentifier<Group> groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupStats.getGroupId()));
                 final InstanceIdentifier<NodeGroupStatistics> nGroupStatIdent = groupIdent
                         .augmentation(NodeGroupStatistics.class);
-    
+
                 final InstanceIdentifier<GroupStatistics> gsIdent = nGroupStatIdent.child(GroupStatistics.class);
                 final GroupStatistics stats = new GroupStatisticsBuilder(groupStats).build();
                 deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, gsIdent, stats);
             }
         }
+        deviceContext.submitTransaction();
     }
 
-    private static InstanceIdentifier<FlowCapableNode> getFlowCapableNodeInstanceIdentifier(final NodeId nodeId) {
-        final InstanceIdentifier<Node> nodeIdent = InstanceIdentifier
-                .create(Nodes.class).child(Node.class, new NodeKey(nodeId));
-        return nodeIdent.augmentation(FlowCapableNode.class);
+    private static InstanceIdentifier<FlowCapableNode> assembleFlowCapableNodeInstanceIdentifier(final DeviceInfo deviceInfo) {
+        return deviceInfo.getNodeInstanceIdentifier().augmentation(FlowCapableNode.class);
+    }
+
+    /**
+     * Writes snapshot gathering start timestamp + cleans end mark
+     *
+     * @param deviceContext txManager + node path keeper
+     */
+    static void markDeviceStateSnapshotStart(final DeviceContext deviceContext) {
+        final InstanceIdentifier<FlowCapableStatisticsGatheringStatus> statusPath = deviceContext.getDeviceInfo()
+                .getNodeInstanceIdentifier().augmentation(FlowCapableStatisticsGatheringStatus.class);
+
+        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_AND_TIME_FORMAT);
+        final FlowCapableStatisticsGatheringStatus gatheringStatus = new FlowCapableStatisticsGatheringStatusBuilder()
+                .setSnapshotGatheringStatusStart(new SnapshotGatheringStatusStartBuilder()
+                        .setBegin(new DateAndTime(simpleDateFormat.format(new Date())))
+                        .build())
+                .setSnapshotGatheringStatusEnd(null) // TODO: reconsider if really need to clean end mark here
+                .build();
+        try {
+            deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, statusPath, gatheringStatus);
+        } catch (final Exception e) {
+            LOG.warn("Can't write to transaction: {}", e);
+        }
+
+        deviceContext.submitTransaction();
+    }
+
+    /**
+     * Writes snapshot gathering end timestamp + outcome
+     *
+     * @param deviceContext txManager + node path keeper
+     * @param succeeded     outcome of currently finished gathering
+     */
+    static void markDeviceStateSnapshotEnd(final DeviceContext deviceContext, final boolean succeeded) {
+        final InstanceIdentifier<SnapshotGatheringStatusEnd> statusEndPath = deviceContext.getDeviceInfo()
+                .getNodeInstanceIdentifier().augmentation(FlowCapableStatisticsGatheringStatus.class)
+                .child(SnapshotGatheringStatusEnd.class);
+
+        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_AND_TIME_FORMAT);
+        final SnapshotGatheringStatusEnd gatheringStatus = new SnapshotGatheringStatusEndBuilder()
+                .setEnd(new DateAndTime(simpleDateFormat.format(new Date())))
+                .setSucceeded(succeeded)
+                .build();
+        try {
+            deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, statusEndPath, gatheringStatus);
+        } catch (Exception e) {
+            LOG.warn("Can't write to transaction: {}", e);
+        }
+
+        deviceContext.submitTransaction();
     }
 }