Merge "Remove xtr id mappings on mapping overwrite"
[lispflowmapping.git] / mappingservice / api / src / main / java / org / opendaylight / lispflowmapping / interfaces / dao / SubscriberRLOC.java
1 /*
2  * Copyright (c) 2014 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
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
15
16 /**
17  * Request source RLOC in the mapping service with it's properties.
18  */
19 public class SubscriberRLOC {
20
21     private Rloc rloc;
22     private Eid eid;
23     private Date lastRequestDate;
24     private int subscriberTimeout = DEFAULT_SUBSCRIBER_TIMEOUT;
25
26     private static final int SUBSCRIBER_TIMEOUT_CONSTANT = 10;
27
28     //1 day is default Cisco IOS mapping TTL
29     public static final int DEFAULT_SUBSCRIBER_TIMEOUT = (int) TimeUnit.DAYS.toMinutes(1);
30
31     /**
32      * Constructor.
33      *
34      * @param srcRloc A source RLOC.
35      * @param srcEid  A source EID.
36      * @param subscriberTimeout Subscriber timeout in min(s).
37      */
38     public SubscriberRLOC(Rloc srcRloc, Eid srcEid, int subscriberTimeout) {
39         this(srcRloc, srcEid, subscriberTimeout, new Date(System.currentTimeMillis()));
40     }
41
42     /**
43      * Constructor.
44      *
45      * @param srcRloc A source RLOC.
46      * @param srcEid  A source EID.
47      * @param subscriberTimeout Subscriber timeout in min(s).
48      * @param lastRequestDate Last request date for this subscriber.
49      */
50     public SubscriberRLOC(Rloc srcRloc, Eid srcEid, int subscriberTimeout,
51                           Date lastRequestDate) {
52         super();
53         this.rloc = srcRloc;
54         this.eid = srcEid;
55         this.lastRequestDate = lastRequestDate;
56         this.subscriberTimeout = subscriberTimeout;
57     }
58
59     public Rloc getSrcRloc() {
60         return rloc;
61     }
62
63     public void setSrcRloc(Rloc srcRloc) {
64         this.rloc = srcRloc;
65     }
66
67     public Eid getSrcEid() {
68         return eid;
69     }
70
71     public void setSrcEid(Eid srcEid) {
72         this.eid = srcEid;
73     }
74
75     public Date getLastRequestDate() {
76         return lastRequestDate;
77     }
78
79     public void setLastRequestDate(Date lastRequestDate) {
80         this.lastRequestDate = lastRequestDate;
81     }
82
83     public int getSubscriberTimeout() {
84         return subscriberTimeout;
85     }
86
87     public void setSubscriberTimeout(int subscriberTimeout) {
88         this.subscriberTimeout = subscriberTimeout;
89     }
90
91     /**
92      * Set Subscriber Timeout from a record's ttl.
93      *
94      * @param recordTtl A mapping record's ttl in Minutes. If null, the value is
95      *                  set to default value of 1440 mins (1 days).
96      */
97     public void setSubscriberTimeoutByRecordTtl(Integer recordTtl) {
98         this.subscriberTimeout = recordTtlToSubscriberTime(recordTtl);
99     }
100
101     public static int recordTtlToSubscriberTime(Integer recordTtl) {
102         if (recordTtl != null) {
103             return ( recordTtl + SUBSCRIBER_TIMEOUT_CONSTANT );
104         }
105         return DEFAULT_SUBSCRIBER_TIMEOUT;
106     }
107
108     public boolean timedOut() {
109         return TimeUnit.MILLISECONDS
110                 .toMinutes(System.currentTimeMillis() - lastRequestDate.getTime()) > subscriberTimeout;
111     }
112
113     @Override
114     public int hashCode() {
115         final int prime = 31;
116         int result = 1;
117         result = prime * result + ((rloc == null) ? 0 : rloc.hashCode());
118         result = prime * result + ((eid == null) ? 0 : eid.hashCode());
119         return result;
120     }
121
122     @Override
123     public boolean equals(Object obj) {
124         if (this == obj) {
125             return true;
126         }
127         if (obj == null) {
128             return false;
129         }
130         if (getClass() != obj.getClass()) {
131             return false;
132         }
133         SubscriberRLOC other = (SubscriberRLOC) obj;
134         if (!rloc.equals(other.rloc)) {
135             return false;
136         }
137         return true;
138     }
139
140     @Override
141     public String toString() {
142         return "_rloc=" + rloc.toString() + ", _eid=" + eid.toString()
143                 + ", last request @ " + lastRequestDate.toString();
144     }
145 }