Bug 5577 Retry mechanism
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / SimplifiedOperationalRetryListener.java
1 /**
2  * Copyright (c) 2016 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.applications.frsync.impl;
10
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;
27
28 /**
29  * Modified {@link SimplifiedOperationalListener} for usage of retry mechanism.
30  */
31 public class SimplifiedOperationalRetryListener extends SimplifiedOperationalListener {
32
33     private static final Logger LOG = LoggerFactory.getLogger(SimplifiedOperationalRetryListener.class);
34     private final RetryRegistry retryRegistry;
35
36     public SimplifiedOperationalRetryListener(SyncReactor reactor, FlowCapableNodeSnapshotDao operationalSnapshot,
37                                               FlowCapableNodeDao configDao, RetryRegistry retryRegistry) {
38         super(reactor, operationalSnapshot, configDao);
39         this.retryRegistry = retryRegistry;
40     }
41
42     /**
43      * Adding condition check for retry.
44      *
45      * @param modification operational datastore modification
46      * @return true if reconciliation is needed, false otherwise
47      */
48     protected boolean isReconciliationNeeded(DataTreeModification<Node> modification) {
49         return super.isReconciliationNeeded(modification) || isRegisteredAndConsistentForRetry(modification);
50     }
51
52     /**
53      * If node is removed unregister for retry in addition.
54      *
55      * @param modification operational datastore modification
56      * @return true for cache update, false for cache remove and retry unregister
57      */
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));
62         }
63         return nodeUpdated;
64     }
65
66     /**
67      * Check if retry should be proceeded.
68      *
69      * @param modification operational modification
70      * @return true if device is registered for retry and actual modification is consistent, false otherwise
71      */
72     @VisibleForTesting
73     boolean isRegisteredAndConsistentForRetry(DataTreeModification<Node> modification) {
74         final NodeId nodeId = PathUtil.digNodeId(modification.getRootPath().getRootIdentifier());
75
76         if (!retryRegistry.isRegistered(nodeId)) {
77             return false;
78         }
79
80         final FlowCapableStatisticsGatheringStatus gatheringStatus = modification.getRootNode().getDataAfter()
81                 .getAugmentation(FlowCapableStatisticsGatheringStatus.class);
82
83         if (gatheringStatus == null) {
84             LOG.trace("Statistics gathering never started for: {}", nodeId.getValue());
85             return false;
86         }
87
88         final SnapshotGatheringStatusEnd gatheringStatusEnd = gatheringStatus.getSnapshotGatheringStatusEnd();
89
90         if (gatheringStatusEnd == null) {
91             LOG.trace("Statistics gathering is not over yet for: {}", nodeId.getValue());
92             return false;
93         }
94
95         if (!gatheringStatusEnd.isSucceeded()) {
96             LOG.debug("Statistics gathering was not successful for: {}", nodeId.getValue());
97             return false;
98         }
99
100         try {
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());
106                 return true;
107             }
108         } catch (ParseException e) {
109             LOG.error("Timestamp parsing error {}", e);
110         }
111         LOG.debug("Fresh operational not present for: {}", nodeId.getValue());
112         return false;
113     }
114 }