X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflowplugin-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fimpl%2Fstatistics%2FStatisticsGatheringUtils.java;h=4933f28374008e089049c67978ba84ae55fe0756;hb=85a27decf79695a8a0a2dcd21325dc5336398860;hp=b6ce236a5e9fff4086ddd3e47847c5c54fdc5499;hpb=db2e8814774e513f2cd632ea58abb3613538f2c6;p=openflowplugin.git diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsGatheringUtils.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsGatheringUtils.java index b6ce236a5e..4933f28374 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsGatheringUtils.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsGatheringUtils.java @@ -11,29 +11,41 @@ package org.opendaylight.openflowplugin.impl.statistics; 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.concurrent.ExecutionException; import javax.annotation.Nullable; -import org.opendaylight.controller.md.sal.binding.api.ReadTransaction; +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.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.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; @@ -92,7 +104,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; @@ -102,6 +113,8 @@ 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-"; @@ -114,73 +127,99 @@ public final class StatisticsGatheringUtils { public static ListenableFuture 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); } - EventIdentifier ofpQueuToRequestContextEventIdentifier = new EventIdentifier(QUEUE2_REQCTX + type.toString(), deviceId); + final EventIdentifier ofpQueuToRequestContextEventIdentifier = new EventIdentifier(QUEUE2_REQCTX + type.toString(), deviceId); final ListenableFuture>> statisticsDataInFuture = JdkFutureAdapters.listenInPoolThread(statisticsGatheringService.getStatisticsOfType( ofpQueuToRequestContextEventIdentifier, type)); - return transformAndStoreStatisticsData(statisticsDataInFuture, deviceContext, wholeProcessEventIdentifier); + return transformAndStoreStatisticsData(statisticsDataInFuture, deviceContext, wholeProcessEventIdentifier, type); } private static ListenableFuture transformAndStoreStatisticsData(final ListenableFuture>> statisticsDataInFuture, final DeviceContext deviceContext, - final EventIdentifier eventIdentifier) { - return Futures.transform(statisticsDataInFuture, new Function>, Boolean>() { + final EventIdentifier eventIdentifier, final MultipartType type) { + return Futures.transform(statisticsDataInFuture, new AsyncFunction>, Boolean>() { @Nullable @Override - public Boolean apply(final RpcResult> rpcResult) { + public ListenableFuture apply(final RpcResult> rpcResult) { + boolean isMultipartProcessed = Boolean.TRUE; if (rpcResult.isSuccessful()) { - boolean isMultipartProcessed = Boolean.TRUE; + LOG.debug("Stats reply successfully received for node {} of type {}", deviceContext.getDeviceState().getNodeId(), type); // 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 allMultipartData = Collections.emptyList(); DataObject multipartData = null; - for (final MultipartReply singleReply : rpcResult.getResult()) { - final List multipartDataList = MULTIPART_REPLY_TRANSLATOR.translate(deviceContext, singleReply); - multipartData = multipartDataList.get(0); - allMultipartData = Iterables.concat(allMultipartData, multipartDataList); + + + try { + for (final MultipartReply singleReply : rpcResult.getResult()) { + final List 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.getDeviceState().getNodeId(), e); + return Futures.immediateFailedFuture(e); } - if (multipartData instanceof GroupStatisticsUpdated) { - processGroupStatistics((Iterable) allMultipartData, deviceContext); - } else if (multipartData instanceof MeterStatisticsUpdated) { - processMetersStatistics((Iterable) allMultipartData, deviceContext); - } else if (multipartData instanceof NodeConnectorStatisticsUpdate) { - processNodeConnectorStatistics((Iterable) allMultipartData, deviceContext); - } else if (multipartData instanceof FlowTableStatisticsUpdate) { - processFlowTableStatistics((Iterable) allMultipartData, deviceContext); - } else if (multipartData instanceof QueueStatisticsUpdate) { - processQueueStatistics((Iterable) allMultipartData, deviceContext); - } else if (multipartData instanceof FlowsStatisticsUpdate) { - processFlowStatistics((Iterable) allMultipartData, deviceContext); - EventsTimeCounter.markEnd(eventIdentifier); - } else if (multipartData instanceof GroupDescStatsUpdated) { - processGroupDescStats((Iterable) allMultipartData, deviceContext); - } else if (multipartData instanceof MeterConfigStatsUpdated) { - processMeterConfigStatsUpdated((Iterable) allMultipartData, deviceContext); - } else { - isMultipartProcessed = Boolean.FALSE; + + try { + if (multipartData instanceof GroupStatisticsUpdated) { + processGroupStatistics((Iterable) allMultipartData, deviceContext); + } else if (multipartData instanceof MeterStatisticsUpdated) { + processMetersStatistics((Iterable) allMultipartData, deviceContext); + } else if (multipartData instanceof NodeConnectorStatisticsUpdate) { + processNodeConnectorStatistics((Iterable) allMultipartData, deviceContext); + } else if (multipartData instanceof FlowTableStatisticsUpdate) { + processFlowTableStatistics((Iterable) allMultipartData, deviceContext); + } else if (multipartData instanceof QueueStatisticsUpdate) { + processQueueStatistics((Iterable) 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) allMultipartData, deviceContext, eventIdentifier); + + } else if (multipartData instanceof GroupDescStatsUpdated) { + processGroupDescStats((Iterable) allMultipartData, deviceContext); + } else if (multipartData instanceof MeterConfigStatsUpdated) { + processMeterConfigStatsUpdated((Iterable) 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.getDeviceState().getNodeId(), e); + return Futures.immediateFailedFuture(e); } + + LOG.debug("Stats reply added to transaction for node {} of type {}", deviceContext.getDeviceState().getNodeId(), type); + //TODO : implement experimenter + } else { + LOG.debug("Stats reply was empty for node {} of type {}", deviceContext.getDeviceState().getNodeId(), type); } - return isMultipartProcessed; + } else { + LOG.debug("Stats reply FAILED for node {} of type {}: {}", deviceContext.getDeviceState().getNodeId(), type, rpcResult.getErrors()); + isMultipartProcessed = Boolean.FALSE; } - return Boolean.FALSE; + return Futures.immediateFuture(isMultipartProcessed); } }); } - private static void processMeterConfigStatsUpdated(final Iterable data, final DeviceContext deviceContext) { - final InstanceIdentifier fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext); + private static void processMeterConfigStatsUpdated(final Iterable data, final DeviceContext deviceContext) throws Exception { + final InstanceIdentifier fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext.getDeviceState()); deleteAllKnownMeters(deviceContext, fNodeIdent); for (final MeterConfigStatsUpdated meterConfigStatsUpdated : data) { for (final MeterConfigStats meterConfigStats : meterConfigStatsUpdated.getMeterConfigStats()) { @@ -197,29 +236,47 @@ public final class StatisticsGatheringUtils { deviceContext.submitTransaction(); } - private static void processFlowStatistics(final Iterable data, final DeviceContext deviceContext) { - deleteAllKnownFlows(deviceContext); - writeFlowStatistics(data, deviceContext); - deviceContext.submitTransaction(); + private static ListenableFuture processFlowStatistics(final Iterable data, + final DeviceContext deviceContext, final EventIdentifier eventIdentifier) { + final ListenableFuture deleFuture = deleteAllKnownFlows(deviceContext.getDeviceState(), + deviceContext.getDeviceFlowRegistry(), deviceContext); + return Futures.transform(deleFuture, new Function() { + + @Override + public Boolean apply(final Void input) { + writeFlowStatistics(data, deviceContext.getDeviceState(), deviceContext.getDeviceFlowRegistry(), + deviceContext); + deviceContext.submitTransaction(); + EventsTimeCounter.markEnd(eventIdentifier); + return Boolean.TRUE; + } + }); } - public static void writeFlowStatistics(Iterable data, DeviceContext deviceContext) { - final InstanceIdentifier fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext); - 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 = deviceContext.getDeviceFlowRegistry().storeIfNecessary(flowRegistryKey, tableId); - - final FlowKey flowKey = new FlowKey(flowId); - flowBuilder.setKey(flowKey); - final TableKey tableKey = new TableKey(tableId); - final InstanceIdentifier flowIdent = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey); - deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build()); + public static void writeFlowStatistics(final Iterable data, + final DeviceState deviceState, + final DeviceFlowRegistry registry, + final TxFacade txFacade) { + final InstanceIdentifier fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceState); + 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 flowIdent = fNodeIdent.child(Table.class, tableKey).child(Flow.class, flowKey); + txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, flowIdent, flowBuilder.build()); + } } + } catch (Exception e) { + LOG.warn("Not able to write to transaction: {}", e.getMessage()); } } @@ -235,31 +292,51 @@ public final class StatisticsGatheringUtils { return flowStatisticsDataBld; } - public static void deleteAllKnownFlows(final DeviceContext deviceContext) { - if (deviceContext.getDeviceState().deviceSynchronized()) { - InstanceIdentifier flowCapableNodePath = assembleFlowCapableNodeInstanceIdentifier(deviceContext); - final Short numOfTablesOnDevice = deviceContext.getDeviceState().getFeatures().getTables(); - for (short i = 0; i < numOfTablesOnDevice; i++) { - final KeyedInstanceIdentifier iiToTable = flowCapableNodePath.child(Table.class, new TableKey(i)); - final ReadTransaction readTx = deviceContext.getReadTransaction(); - final CheckedFuture, ReadFailedException> tableDataFuture = readTx.read(LogicalDatastoreType.OPERATIONAL, iiToTable); - try { - final Optional tableDataOpt = tableDataFuture.get(); - if (tableDataOpt.isPresent()) { - final Table tableData = tableDataOpt.get(); - final Table table = new TableBuilder(tableData).setFlow(Collections.emptyList()).build(); - deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToTable, table); + public static ListenableFuture deleteAllKnownFlows(final DeviceState deviceState, + final DeviceFlowRegistry registry, + final TxFacade txFacade) { + /* DeviceState.deviceSynchronized is a marker for actual phase - false means initPhase, true means noInitPhase */ + if (deviceState.deviceSynchronized()) { + final InstanceIdentifier flowCapableNodePath = assembleFlowCapableNodeInstanceIdentifier(deviceState); + final ReadOnlyTransaction readTx = txFacade.getReadTransaction(); + final CheckedFuture, ReadFailedException> flowCapableNodeFuture = readTx.read( + LogicalDatastoreType.OPERATIONAL, flowCapableNodePath); + + /* we wish to close readTx for fallBack */ + Futures.withFallback(flowCapableNodeFuture, new FutureFallback>() { + + @Override + public ListenableFuture> 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, Void>() { + + @Override + public ListenableFuture apply(final Optional flowCapNodeOpt) throws Exception { + if (flowCapNodeOpt.isPresent()) { + for (final Table tableData : flowCapNodeOpt.get().getTable()) { + final Table table = new TableBuilder(tableData).setFlow(Collections.emptyList()).build(); + final InstanceIdentifier
iiToTable = flowCapableNodePath.child(Table.class, tableData.getKey()); + txFacade.writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToTable, table); + } } - } catch (final InterruptedException e) { - LOG.trace("Reading of table features for table wit ID {} was interrputed.", i); - } catch (final ExecutionException e) { - LOG.trace("Reading of table features for table wit ID {} encountered execution exception {}.", i, e); + registry.removeMarked(); + readTx.close(); + return Futures.immediateFuture(null); } - } + + }); } + return Futures.immediateFuture(null); } - private static void processQueueStatistics(final Iterable data, final DeviceContext deviceContext) { + private static void processQueueStatistics(final Iterable data, final DeviceContext deviceContext) throws Exception { // TODO: clean all queues of all node-connectors before writing up-to-date stats final InstanceIdentifier nodeIdent = deviceContext.getDeviceState().getNodeInstanceIdentifier(); for (final QueueStatisticsUpdate queueStatisticsUpdate : data) { @@ -287,8 +364,8 @@ public final class StatisticsGatheringUtils { deviceContext.submitTransaction(); } - private static void processFlowTableStatistics(final Iterable data, final DeviceContext deviceContext) { - final InstanceIdentifier fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext); + private static void processFlowTableStatistics(final Iterable data, final DeviceContext deviceContext) throws Exception { + final InstanceIdentifier fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext.getDeviceState()); for (final FlowTableStatisticsUpdate flowTableStatisticsUpdate : data) { for (final FlowTableAndStatisticsMap tableStat : flowTableStatisticsUpdate.getFlowTableAndStatisticsMap()) { @@ -301,7 +378,7 @@ public final class StatisticsGatheringUtils { deviceContext.submitTransaction(); } - private static void processNodeConnectorStatistics(final Iterable data, final DeviceContext deviceContext) { + private static void processNodeConnectorStatistics(final Iterable data, final DeviceContext deviceContext) throws Exception { final InstanceIdentifier nodeIdent = deviceContext.getDeviceState().getNodeInstanceIdentifier(); for (final NodeConnectorStatisticsUpdate nodeConnectorStatisticsUpdate : data) { for (final NodeConnectorStatisticsAndPortNumberMap nConnectPort : nodeConnectorStatisticsUpdate.getNodeConnectorStatisticsAndPortNumberMap()) { @@ -319,8 +396,8 @@ public final class StatisticsGatheringUtils { } private static void processMetersStatistics(final Iterable data, - final DeviceContext deviceContext) { - final InstanceIdentifier fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext); + final DeviceContext deviceContext) throws Exception { + final InstanceIdentifier fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext.getDeviceState()); for (final MeterStatisticsUpdated meterStatisticsUpdated : data) { for (final MeterStats mStat : meterStatisticsUpdated.getMeterStats()) { final MeterStatistics stats = new MeterStatisticsBuilder(mStat).build(); @@ -335,7 +412,7 @@ public final class StatisticsGatheringUtils { deviceContext.submitTransaction(); } - private static void deleteAllKnownMeters(final DeviceContext deviceContext, final InstanceIdentifier fNodeIdent) { + private static void deleteAllKnownMeters(final DeviceContext deviceContext, final InstanceIdentifier fNodeIdent) throws Exception { for (final MeterId meterId : deviceContext.getDeviceMeterRegistry().getAllMeterIds()) { final InstanceIdentifier meterIdent = fNodeIdent.child(Meter.class, new MeterKey(meterId)); deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, meterIdent); @@ -343,7 +420,7 @@ public final class StatisticsGatheringUtils { deviceContext.getDeviceMeterRegistry().removeMarked(); } - private static void processGroupDescStats(final Iterable data, final DeviceContext deviceContext) { + private static void processGroupDescStats(final Iterable data, final DeviceContext deviceContext) throws Exception { final InstanceIdentifier fNodeIdent = deviceContext.getDeviceState().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class); deleteAllKnownGroups(deviceContext, fNodeIdent); @@ -365,7 +442,7 @@ public final class StatisticsGatheringUtils { deviceContext.submitTransaction(); } - private static void deleteAllKnownGroups(final DeviceContext deviceContext, final InstanceIdentifier fNodeIdent) { + private static void deleteAllKnownGroups(final DeviceContext deviceContext, final InstanceIdentifier fNodeIdent) throws Exception { for (final GroupId groupId : deviceContext.getDeviceGroupRegistry().getAllGroupIds()) { final InstanceIdentifier groupIdent = fNodeIdent.child(Group.class, new GroupKey(groupId)); deviceContext.addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, groupIdent); @@ -373,8 +450,8 @@ public final class StatisticsGatheringUtils { deviceContext.getDeviceGroupRegistry().removeMarked(); } - private static void processGroupStatistics(final Iterable data, final DeviceContext deviceContext) { - final InstanceIdentifier fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext); + private static void processGroupStatistics(final Iterable data, final DeviceContext deviceContext) throws Exception { + final InstanceIdentifier fNodeIdent = assembleFlowCapableNodeInstanceIdentifier(deviceContext.getDeviceState()); for (final GroupStatisticsUpdated groupStatistics : data) { for (final GroupStats groupStats : groupStatistics.getGroupStats()) { @@ -390,7 +467,57 @@ public final class StatisticsGatheringUtils { deviceContext.submitTransaction(); } - private static InstanceIdentifier assembleFlowCapableNodeInstanceIdentifier(final DeviceContext deviceContext) { - return deviceContext.getDeviceState().getNodeInstanceIdentifier().augmentation(FlowCapableNode.class); + private static InstanceIdentifier assembleFlowCapableNodeInstanceIdentifier(final DeviceState deviceState) { + return deviceState.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 statusPath = deviceContext.getDeviceState() + .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 statusEndPath = deviceContext.getDeviceState() + .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(); } }