2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
11 import com.google.common.annotations.VisibleForTesting;
12 import java.text.ParseException;
13 import java.text.SimpleDateFormat;
14 import java.util.Date;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
16 import org.opendaylight.openflowplugin.applications.frsync.SyncReactor;
17 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeDao;
18 import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
19 import org.opendaylight.openflowplugin.applications.frsync.util.PathUtil;
20 import org.opendaylight.openflowplugin.applications.frsync.util.RetryRegistry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableStatisticsGatheringStatus;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.snapshot.gathering.status.grouping.SnapshotGatheringStatusEnd;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Modified {@link SimplifiedOperationalListener} for usage of retry mechanism.
31 public class SimplifiedOperationalRetryListener extends SimplifiedOperationalListener {
33 private static final Logger LOG = LoggerFactory.getLogger(SimplifiedOperationalRetryListener.class);
34 private final RetryRegistry retryRegistry;
36 public SimplifiedOperationalRetryListener(SyncReactor reactor, FlowCapableNodeSnapshotDao operationalSnapshot,
37 FlowCapableNodeDao configDao, RetryRegistry retryRegistry) {
38 super(reactor, operationalSnapshot, configDao);
39 this.retryRegistry = retryRegistry;
43 * Adding condition check for retry.
45 * @param modification operational datastore modification
46 * @return true if reconciliation is needed, false otherwise
48 protected boolean isReconciliationNeeded(DataTreeModification<Node> modification) {
49 return super.isReconciliationNeeded(modification) || isRegisteredAndConsistentForRetry(modification);
53 * If node is removed unregister for retry in addition.
55 * @param modification operational datastore modification
56 * @return true for cache update, false for cache remove and retry unregister
58 protected boolean updateCache(DataTreeModification<Node> modification) {
59 boolean nodeUpdated = super.updateCache(modification);
60 if (!nodeUpdated) { // node removed if not updated
61 retryRegistry.unregisterIfRegistered(nodeId(modification));
67 * Check if retry should be proceeded.
69 * @param modification operational modification
70 * @return true if device is registered for retry and actual modification is consistent, false otherwise
73 boolean isRegisteredAndConsistentForRetry(DataTreeModification<Node> modification) {
74 final NodeId nodeId = PathUtil.digNodeId(modification.getRootPath().getRootIdentifier());
76 if (!retryRegistry.isRegistered(nodeId)) {
80 final FlowCapableStatisticsGatheringStatus gatheringStatus = modification.getRootNode().getDataAfter()
81 .getAugmentation(FlowCapableStatisticsGatheringStatus.class);
83 if (gatheringStatus == null) {
84 LOG.trace("Statistics gathering never started for: {}", nodeId.getValue());
88 final SnapshotGatheringStatusEnd gatheringStatusEnd = gatheringStatus.getSnapshotGatheringStatusEnd();
90 if (gatheringStatusEnd == null) {
91 LOG.trace("Statistics gathering is not over yet for: {}", nodeId.getValue());
95 if (!gatheringStatusEnd.isSucceeded()) {
96 LOG.debug("Statistics gathering was not successful for: {}", nodeId.getValue());
101 Date timestampOfRegistration = retryRegistry.getRegistration(nodeId);;
102 final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(RetryRegistry.DATE_AND_TIME_FORMAT);
103 Date timestampOfStatistics = simpleDateFormat.parse(gatheringStatusEnd.getEnd().getValue());
104 if (timestampOfStatistics.after(timestampOfRegistration)) {
105 LOG.debug("Fresh operational present for: {} -> going retry!", nodeId.getValue());
108 } catch (ParseException e) {
109 LOG.error("Timestamp parsing error {}", e);
111 LOG.debug("Fresh operational not present for: {}", nodeId.getValue());