blob: 92bbe9e3b7ff2a703b1054f7487855d8e994fcd2 [file] [log] [blame]
Rusty Eddy390498d2016-01-15 19:21:32 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Rusty Eddy390498d2016-01-15 19:21:32 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.pim.impl;
17
Jonathan Hart54119bb2016-02-06 18:48:27 -080018import com.google.common.base.MoreObjects;
Rusty Eddy390498d2016-01-15 19:21:32 -080019import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onlab.packet.pim.PIMHelloOption;
Rusty Eddy390498d2016-01-15 19:21:32 -080022
23import java.nio.ByteBuffer;
Jonathan Hart54119bb2016-02-06 18:48:27 -080024import java.util.Collection;
25import java.util.Objects;
26import java.util.concurrent.TimeUnit;
Rusty Eddy390498d2016-01-15 19:21:32 -080027
Jonathan Hart54119bb2016-02-06 18:48:27 -080028import static com.google.common.base.Preconditions.checkNotNull;
Rusty Eddy390498d2016-01-15 19:21:32 -080029
Jonathan Hart54119bb2016-02-06 18:48:27 -080030/**
31 * Represents a PIM neighbor.
32 */
Jonathan Hartfbfe2a82016-03-29 11:36:33 -070033public class PimNeighbor {
Rusty Eddy390498d2016-01-15 19:21:32 -080034
Rusty Eddy390498d2016-01-15 19:21:32 -080035 // IP Address of this neighbor
Jonathan Hart54119bb2016-02-06 18:48:27 -080036 private final IpAddress ipAddr;
Rusty Eddy390498d2016-01-15 19:21:32 -080037
38 // MAC Address of the neighbor (Need for sending J/P)
Jonathan Hart54119bb2016-02-06 18:48:27 -080039 private final MacAddress macAddr;
Rusty Eddy390498d2016-01-15 19:21:32 -080040
41 // Hello Options
42 // Our hello opt holdTime
Jonathan Hart54119bb2016-02-06 18:48:27 -080043 private final short holdTime;
Rusty Eddy390498d2016-01-15 19:21:32 -080044
45 // Our hello opt prune delay
Jonathan Hart54119bb2016-02-06 18:48:27 -080046 private final int pruneDelay;
Rusty Eddy390498d2016-01-15 19:21:32 -080047
48 // Neighbor priority
Jonathan Hart54119bb2016-02-06 18:48:27 -080049 private final int priority;
Rusty Eddy390498d2016-01-15 19:21:32 -080050
51 // Our current genId
Jonathan Hart54119bb2016-02-06 18:48:27 -080052 private final int genId;
53
54 private final long upTime;
Rusty Eddy390498d2016-01-15 19:21:32 -080055
56 // Our timestamp for this neighbor
Jonathan Hart54119bb2016-02-06 18:48:27 -080057 private long lastRefresh;
Rusty Eddy390498d2016-01-15 19:21:32 -080058
59 /**
Jonathan Hart54119bb2016-02-06 18:48:27 -080060 * Class constructor.
Rusty Eddy390498d2016-01-15 19:21:32 -080061 *
Jonathan Hart54119bb2016-02-06 18:48:27 -080062 * @param ipAddress neighbor IP address
63 * @param macAddress neighbor MAC address
64 * @param holdTime hold time
65 * @param pruneDelay prune delay
66 * @param priority priority
67 * @param genId generation ID
Rusty Eddy390498d2016-01-15 19:21:32 -080068 */
Jonathan Hartfbfe2a82016-03-29 11:36:33 -070069 public PimNeighbor(IpAddress ipAddress, MacAddress macAddress,
Jonathan Hart54119bb2016-02-06 18:48:27 -080070 short holdTime, int pruneDelay, int priority, int genId) {
71 this.ipAddr = checkNotNull(ipAddress);
72 this.macAddr = checkNotNull(macAddress);
73 this.holdTime = holdTime;
74 this.pruneDelay = pruneDelay;
75 this.priority = priority;
76 this.genId = genId;
77
78 this.upTime = System.currentTimeMillis();
Rusty Eddy390498d2016-01-15 19:21:32 -080079 }
80
81 /**
Jonathan Hart54119bb2016-02-06 18:48:27 -080082 * Gets the IP address of our neighbor.
Rusty Eddy390498d2016-01-15 19:21:32 -080083 *
84 * @return the IP address of our neighbor
85 */
Jonathan Hart54119bb2016-02-06 18:48:27 -080086 public IpAddress ipAddress() {
Rusty Eddy390498d2016-01-15 19:21:32 -080087 return ipAddr;
88 }
89
90 /**
Jonathan Hart54119bb2016-02-06 18:48:27 -080091 * Gets the MAC address of this neighbor.
Rusty Eddy390498d2016-01-15 19:21:32 -080092 *
Jonathan Hart54119bb2016-02-06 18:48:27 -080093 * @return the mac address
Rusty Eddy390498d2016-01-15 19:21:32 -080094 */
Jonathan Hart54119bb2016-02-06 18:48:27 -080095 public MacAddress macAddress() {
96 return macAddr;
Rusty Eddy390498d2016-01-15 19:21:32 -080097 }
98
99 /**
Jonathan Hart54119bb2016-02-06 18:48:27 -0800100 * Gets our neighbor's hold time.
Rusty Eddy390498d2016-01-15 19:21:32 -0800101 *
Jonathan Hart54119bb2016-02-06 18:48:27 -0800102 * @return the hold time
Rusty Eddy390498d2016-01-15 19:21:32 -0800103 */
Jonathan Hart54119bb2016-02-06 18:48:27 -0800104 public short holdtime() {
Rusty Eddy390498d2016-01-15 19:21:32 -0800105 return holdTime;
106 }
107
108 /**
Jonathan Hart54119bb2016-02-06 18:48:27 -0800109 * Gets our neighbor's prune delay.
Rusty Eddy390498d2016-01-15 19:21:32 -0800110 *
Jonathan Hart54119bb2016-02-06 18:48:27 -0800111 * @return our neighbor's prune delay
Rusty Eddy390498d2016-01-15 19:21:32 -0800112 */
Jonathan Hart54119bb2016-02-06 18:48:27 -0800113 public int pruneDelay() {
Rusty Eddy390498d2016-01-15 19:21:32 -0800114 return pruneDelay;
115 }
116
117 /**
Jonathan Hart54119bb2016-02-06 18:48:27 -0800118 * Gets our neighbor's priority.
Rusty Eddy390498d2016-01-15 19:21:32 -0800119 *
Jonathan Hart54119bb2016-02-06 18:48:27 -0800120 * @return our neighbor's priority
Rusty Eddy390498d2016-01-15 19:21:32 -0800121 */
Jonathan Hart54119bb2016-02-06 18:48:27 -0800122 public int priority() {
Rusty Eddy390498d2016-01-15 19:21:32 -0800123 return priority;
124 }
125
126 /**
Jonathan Hart54119bb2016-02-06 18:48:27 -0800127 * Gets our neighbor's generation ID.
Rusty Eddy390498d2016-01-15 19:21:32 -0800128 *
Jonathan Hart54119bb2016-02-06 18:48:27 -0800129 * @return our neighbor's generation ID
Rusty Eddy390498d2016-01-15 19:21:32 -0800130 */
Jonathan Hart54119bb2016-02-06 18:48:27 -0800131 public int generationId() {
Rusty Eddy390498d2016-01-15 19:21:32 -0800132 return genId;
133 }
134
135 /**
Jonathan Hart54119bb2016-02-06 18:48:27 -0800136 * Gets the last time we heard a HELLO from this neighbor.
Rusty Eddy390498d2016-01-15 19:21:32 -0800137 *
Jonathan Hart54119bb2016-02-06 18:48:27 -0800138 * @return last refresh time
Rusty Eddy390498d2016-01-15 19:21:32 -0800139 */
Jonathan Hart54119bb2016-02-06 18:48:27 -0800140 public long lastRefresh() {
141 return lastRefresh;
Rusty Eddy390498d2016-01-15 19:21:32 -0800142 }
143
144 /**
Jonathan Hart54119bb2016-02-06 18:48:27 -0800145 * Gets the time that we first learnt of this neighbor.
Rusty Eddy390498d2016-01-15 19:21:32 -0800146 *
Jonathan Hart54119bb2016-02-06 18:48:27 -0800147 * @return up time
Rusty Eddy390498d2016-01-15 19:21:32 -0800148 */
Jonathan Hart54119bb2016-02-06 18:48:27 -0800149 public long upTime() {
150 return upTime;
Rusty Eddy390498d2016-01-15 19:21:32 -0800151 }
152
153 /**
Jonathan Hart54119bb2016-02-06 18:48:27 -0800154 * Refreshes this neighbor's last seen timestamp.
Rusty Eddy390498d2016-01-15 19:21:32 -0800155 */
156 public void refreshTimestamp() {
Jonathan Hart54119bb2016-02-06 18:48:27 -0800157 lastRefresh = System.currentTimeMillis();
Rusty Eddy390498d2016-01-15 19:21:32 -0800158 }
Jonathan Hart54119bb2016-02-06 18:48:27 -0800159
160 /**
161 * Returns whether this neighbor is expired or not.
162 *
163 * @return true if the neighbor is expired, otherwise false
164 */
165 public boolean isExpired() {
166 return lastRefresh + TimeUnit.SECONDS.toMillis(holdTime)
167 < System.currentTimeMillis();
168 }
169
170 /**
171 * Creates a PIM neighbor based on an IP, MAC, and collection of PIM HELLO
172 * options.
173 *
174 * @param ipAddress neighbor IP address
175 * @param macAddress neighbor MAC address
176 * @param opts options from the PIM HELLO packet
177 * @return new PIM neighbor
178 */
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700179 public static PimNeighbor createPimNeighbor(IpAddress ipAddress,
Jonathan Hart54119bb2016-02-06 18:48:27 -0800180 MacAddress macAddress,
181 Collection<PIMHelloOption> opts) {
182
183 int generationID = PIMHelloOption.DEFAULT_GENID;
184 short holdTime = PIMHelloOption.DEFAULT_HOLDTIME;
185 int priority = PIMHelloOption.DEFAULT_PRIORITY;
186 int pruneDelay = PIMHelloOption.DEFAULT_PRUNEDELAY;
187
188 for (PIMHelloOption opt : opts) {
189 short type = opt.getOptType();
190 ByteBuffer value = ByteBuffer.wrap(opt.getValue());
191
192 if (type == PIMHelloOption.OPT_GENID) {
193 generationID = value.getInt();
194 } else if (type == PIMHelloOption.OPT_HOLDTIME) {
195 holdTime = value.getShort();
196 } else if (type == PIMHelloOption.OPT_PRIORITY) {
197 priority = value.getInt();
198 } else if (type == PIMHelloOption.OPT_PRUNEDELAY) {
199 pruneDelay = value.getInt();
200 } else if (type == PIMHelloOption.OPT_ADDRLIST) {
201 // TODO: Will implement someday
202 }
203 }
204
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700205 return new PimNeighbor(ipAddress, macAddress, holdTime, pruneDelay, priority, generationID);
Jonathan Hart54119bb2016-02-06 18:48:27 -0800206 }
207
208 @Override
209 public boolean equals(Object other) {
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700210 if (!(other instanceof PimNeighbor)) {
Jonathan Hart54119bb2016-02-06 18:48:27 -0800211 return false;
212 }
213
Jonathan Hartfbfe2a82016-03-29 11:36:33 -0700214 PimNeighbor that = (PimNeighbor) other;
Jonathan Hart54119bb2016-02-06 18:48:27 -0800215
216 return this.ipAddr.equals(that.ipAddress()) &&
217 this.macAddr.equals(that.macAddress()) &&
218 this.genId == that.genId &&
219 this.holdTime == that.holdTime &&
220 this.priority == that.priority;
221 }
222
223 @Override
224 public int hashCode() {
225 return Objects.hash(ipAddr, macAddr, genId, holdTime, priority);
226 }
227
228 @Override
229 public String toString() {
230 return MoreObjects.toStringHelper(getClass())
231 .add("ipAddress", ipAddr)
232 .add("macAddress", macAddr)
233 .add("generationId", genId)
234 .add("holdTime", holdTime)
235 .add("priority", priority)
236 .add("pruneDelay", pruneDelay)
237 .toString();
238 }
239
Rusty Eddy390498d2016-01-15 19:21:32 -0800240}