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