Merge "Add timeout to subscriber data"
[lispflowmapping.git] / mappingservice / api / src / main / java / org / opendaylight / lispflowmapping / interfaces / dao / Subscriber.java
1 /*
2  * Copyright (c) 2014, 2017 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 package org.opendaylight.lispflowmapping.interfaces.dao;
9
10 import java.util.Date;
11 import java.util.concurrent.TimeUnit;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.subscriber.data.grouping.SubscriberData;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.subscriber.data.grouping.SubscriberDataBuilder;
16
17 /**
18  * Request source RLOC in the mapping service with it's properties.
19  */
20 public class Subscriber {
21     // 1 day is default Cisco IOS mapping TTL
22     public static final int DEFAULT_SUBSCRIBER_TIMEOUT = (int) TimeUnit.DAYS.toMinutes(1);
23     // Subscriber TTL should be slightly higher than the mapping TTL. Arbitrary value is set to 10 minutes
24     private static final int SUBSCRIBER_TIMEOUT_CONSTANT = 10;
25
26     private SubscriberData data;
27     private Date lastRequestDate;
28
29     /**
30      * Constructor.
31      *
32      * @param srcRloc A source RLOC.
33      * @param srcEid  A source EID.
34      * @param subscriberTtl Subscriber TTL in min(s).
35      */
36     public Subscriber(Rloc srcRloc, Eid srcEid, int subscriberTtl) {
37         this(srcRloc, srcEid, subscriberTtl, new Date(System.currentTimeMillis()));
38     }
39
40     /**
41      * Constructor.
42      *
43      * @param srcRloc A source RLOC.
44      * @param srcEid  A source EID.
45      * @param subscriberTtl Subscriber TTL in min(s).
46      * @param lastRequestDate Last request date for this subscriber.
47      */
48     public Subscriber(Rloc srcRloc, Eid srcEid, int subscriberTtl, Date lastRequestDate) {
49         super();
50         this.data = new SubscriberDataBuilder().setRloc(srcRloc).setEid(srcEid).setTtl(subscriberTtl).build();
51         this.lastRequestDate = lastRequestDate;
52     }
53
54     public SubscriberData getSubscriberData() {
55         return data;
56     }
57
58     public Rloc getSrcRloc() {
59         return data.getRloc();
60     }
61
62     public Eid getSrcEid() {
63         return data.getEid();
64     }
65
66     public Date getLastRequestDate() {
67         return lastRequestDate;
68     }
69
70     public void setLastRequestDate(Date lastRequestDate) {
71         this.lastRequestDate = lastRequestDate;
72     }
73
74     public int getSubscriberTtl() {
75         return data.getTtl();
76     }
77
78     // Only used in MapResolverTest
79     public void setSubscriberTtlByRecordTtl(Integer recordTtl) {
80         SubscriberDataBuilder sdb = new SubscriberDataBuilder(this.data);
81         sdb.setTtl(recordTtlToSubscriberTime(recordTtl));
82         this.data = sdb.build();
83     }
84
85     /**
86      * Static method to calculate the subscriber TTL from a mapping record TTL. If a mapping record TTL is not
87      * provided, use the default 1 day TTL. The subscriber TTL is the TTL plus a constant value.
88      *
89      * @param recordTtl The time to live (TTL) value
90      * @return the subscriber TTL
91      */
92     public static int recordTtlToSubscriberTime(Integer recordTtl) {
93         if (recordTtl != null) {
94             return (recordTtl + SUBSCRIBER_TIMEOUT_CONSTANT);
95         }
96         return DEFAULT_SUBSCRIBER_TIMEOUT;
97     }
98
99     public boolean timedOut() {
100         return TimeUnit.MILLISECONDS
101                 .toMinutes(System.currentTimeMillis() - lastRequestDate.getTime()) > data.getTtl();
102     }
103
104     @Override
105     public int hashCode() {
106         return data.hashCode();
107     }
108
109     @Override
110     public boolean equals(Object obj) {
111         if (this == obj) {
112             return true;
113         }
114         if (obj == null) {
115             return false;
116         }
117         if (getClass() != obj.getClass()) {
118             return false;
119         }
120         Subscriber other = (Subscriber) obj;
121         if (!data.equals(other.data)) {
122             return false;
123         }
124         return true;
125     }
126
127     @Override
128     public String toString() {
129         return "_rloc=" + data.getRloc().toString() + ", _eid=" + data.getEid().toString()
130                 + ", _ttl=" + data.getTtl().toString() + ", last request @ " + lastRequestDate.toString();
131     }
132 }