Bug 5577 Retry mechanism
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / util / RetryRegistry.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.util;
10
11 import java.util.Date;
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Holder of registration request for fresh operational.
20  */
21 public class RetryRegistry {
22
23     private static final Logger LOG = LoggerFactory.getLogger(RetryRegistry.class);
24     private final Map<NodeId, Date> registration = new ConcurrentHashMap<>();
25     public static final String DATE_AND_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
26
27     public Date register(NodeId nodeId) {
28         Date timestamp = new Date();
29         registration.put(nodeId, timestamp);
30         if (timestamp != null) {
31             LOG.debug("Registered for next consistent operational: {}", nodeId.getValue());
32         }
33         return timestamp;
34     }
35
36     public Date unregisterIfRegistered(NodeId nodeId) {
37         Date timestamp = registration.remove(nodeId);
38         if (timestamp != null) {
39             LOG.debug("Unregistered for next consistent operational: {}", nodeId.getValue());
40         }
41         return timestamp;
42     }
43
44     public boolean isRegistered(NodeId nodeId) {
45         return registration.get(nodeId) != null;
46     }
47
48     public Date getRegistration(NodeId nodeId) {
49         return registration.get(nodeId);
50     }
51
52 }