Merge "Remove xtr id mappings on mapping overwrite"
[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.address.grouping.SubscriberAddress;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.subscriber.address.grouping.SubscriberAddressBuilder;
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 timeout 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 SubscriberAddress address;
27     private Date lastRequestDate;
28     private int subscriberTimeout = DEFAULT_SUBSCRIBER_TIMEOUT;
29
30     /**
31      * Constructor.
32      *
33      * @param srcRloc A source RLOC.
34      * @param srcEid  A source EID.
35      * @param subscriberTimeout Subscriber timeout in min(s).
36      */
37     public Subscriber(Rloc srcRloc, Eid srcEid, int subscriberTimeout) {
38         this(srcRloc, srcEid, subscriberTimeout, new Date(System.currentTimeMillis()));
39     }
40
41     /**
42      * Constructor.
43      *
44      * @param srcRloc A source RLOC.
45      * @param srcEid  A source EID.
46      * @param subscriberTimeout Subscriber timeout in min(s).
47      * @param lastRequestDate Last request date for this subscriber.
48      */
49     public Subscriber(Rloc srcRloc, Eid srcEid, int subscriberTimeout, Date lastRequestDate) {
50         super();
51         this.address = new SubscriberAddressBuilder().setRloc(srcRloc).setEid(srcEid).build();
52         this.lastRequestDate = lastRequestDate;
53         this.subscriberTimeout = subscriberTimeout;
54     }
55
56     public SubscriberAddress getSubscriberAddress() {
57         return address;
58     }
59
60     public Rloc getSrcRloc() {
61         return address.getRloc();
62     }
63
64     public Eid getSrcEid() {
65         return address.getEid();
66     }
67
68     public Date getLastRequestDate() {
69         return lastRequestDate;
70     }
71
72     public void setLastRequestDate(Date lastRequestDate) {
73         this.lastRequestDate = lastRequestDate;
74     }
75
76     public int getSubscriberTimeout() {
77         return subscriberTimeout;
78     }
79
80     public void setSubscriberTimeout(int subscriberTimeout) {
81         this.subscriberTimeout = subscriberTimeout;
82     }
83
84     // Only used in MapResolverTest
85     public void setSubscriberTimeoutByRecordTtl(Integer recordTtl) {
86         this.subscriberTimeout = recordTtlToSubscriberTime(recordTtl);
87     }
88
89     /**
90      * Static method to calculate the subscriber timeout from a mapping record TTL. If a mapping record TTL is not
91      * provided, use the default 1 day TTL. The suscriber timeout is the TTL plus a constant value.
92      *
93      * @param recordTtl The time to live (TTL) value
94      * @return the subscriber timeout
95      */
96     public static int recordTtlToSubscriberTime(Integer recordTtl) {
97         if (recordTtl != null) {
98             return (recordTtl + SUBSCRIBER_TIMEOUT_CONSTANT);
99         }
100         return DEFAULT_SUBSCRIBER_TIMEOUT;
101     }
102
103     public boolean timedOut() {
104         return TimeUnit.MILLISECONDS
105                 .toMinutes(System.currentTimeMillis() - lastRequestDate.getTime()) > subscriberTimeout;
106     }
107
108     @Override
109     public int hashCode() {
110         return address.hashCode();
111     }
112
113     @Override
114     public boolean equals(Object obj) {
115         if (this == obj) {
116             return true;
117         }
118         if (obj == null) {
119             return false;
120         }
121         if (getClass() != obj.getClass()) {
122             return false;
123         }
124         Subscriber other = (Subscriber) obj;
125         if (!address.equals(other.address)) {
126             return false;
127         }
128         return true;
129     }
130
131     @Override
132     public String toString() {
133         return "_rloc=" + address.getRloc().toString() + ", _eid=" + address.getEid().toString()
134                 + ", last request @ " + lastRequestDate.toString();
135     }
136 }