Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.pim.impl; |
| 17 | |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.pim.PIMHelloOption; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 22 | |
| 23 | import java.nio.ByteBuffer; |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 24 | import java.util.Collection; |
| 25 | import java.util.Objects; |
| 26 | import java.util.concurrent.TimeUnit; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 27 | |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 29 | |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 30 | /** |
| 31 | * Represents a PIM neighbor. |
| 32 | */ |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 33 | public class PimNeighbor { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 34 | |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 35 | // IP Address of this neighbor |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 36 | private final IpAddress ipAddr; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 37 | |
| 38 | // MAC Address of the neighbor (Need for sending J/P) |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 39 | private final MacAddress macAddr; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 40 | |
| 41 | // Hello Options |
| 42 | // Our hello opt holdTime |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 43 | private final short holdTime; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 44 | |
| 45 | // Our hello opt prune delay |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 46 | private final int pruneDelay; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 47 | |
| 48 | // Neighbor priority |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 49 | private final int priority; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 50 | |
| 51 | // Our current genId |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 52 | private final int genId; |
| 53 | |
| 54 | private final long upTime; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 55 | |
| 56 | // Our timestamp for this neighbor |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 57 | private long lastRefresh; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 60 | * Class constructor. |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 61 | * |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 62 | * @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 Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 68 | */ |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 69 | public PimNeighbor(IpAddress ipAddress, MacAddress macAddress, |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 70 | 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 Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 82 | * Gets the IP address of our neighbor. |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 83 | * |
| 84 | * @return the IP address of our neighbor |
| 85 | */ |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 86 | public IpAddress ipAddress() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 87 | return ipAddr; |
| 88 | } |
| 89 | |
| 90 | /** |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 91 | * Gets the MAC address of this neighbor. |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 92 | * |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 93 | * @return the mac address |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 94 | */ |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 95 | public MacAddress macAddress() { |
| 96 | return macAddr; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 100 | * Gets our neighbor's hold time. |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 101 | * |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 102 | * @return the hold time |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 103 | */ |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 104 | public short holdtime() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 105 | return holdTime; |
| 106 | } |
| 107 | |
| 108 | /** |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 109 | * Gets our neighbor's prune delay. |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 110 | * |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 111 | * @return our neighbor's prune delay |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 112 | */ |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 113 | public int pruneDelay() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 114 | return pruneDelay; |
| 115 | } |
| 116 | |
| 117 | /** |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 118 | * Gets our neighbor's priority. |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 119 | * |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 120 | * @return our neighbor's priority |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 121 | */ |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 122 | public int priority() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 123 | return priority; |
| 124 | } |
| 125 | |
| 126 | /** |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 127 | * Gets our neighbor's generation ID. |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 128 | * |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 129 | * @return our neighbor's generation ID |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 130 | */ |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 131 | public int generationId() { |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 132 | return genId; |
| 133 | } |
| 134 | |
| 135 | /** |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 136 | * Gets the last time we heard a HELLO from this neighbor. |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 137 | * |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 138 | * @return last refresh time |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 139 | */ |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 140 | public long lastRefresh() { |
| 141 | return lastRefresh; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 145 | * Gets the time that we first learnt of this neighbor. |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 146 | * |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 147 | * @return up time |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 148 | */ |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 149 | public long upTime() { |
| 150 | return upTime; |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /** |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 154 | * Refreshes this neighbor's last seen timestamp. |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 155 | */ |
| 156 | public void refreshTimestamp() { |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 157 | lastRefresh = System.currentTimeMillis(); |
Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 158 | } |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 159 | |
| 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 Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 179 | public static PimNeighbor createPimNeighbor(IpAddress ipAddress, |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 180 | 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 Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 205 | return new PimNeighbor(ipAddress, macAddress, holdTime, pruneDelay, priority, generationID); |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public boolean equals(Object other) { |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 210 | if (!(other instanceof PimNeighbor)) { |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | |
Jonathan Hart | fbfe2a8 | 2016-03-29 11:36:33 -0700 | [diff] [blame] | 214 | PimNeighbor that = (PimNeighbor) other; |
Jonathan Hart | 54119bb | 2016-02-06 18:48:27 -0800 | [diff] [blame] | 215 | |
| 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 Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 240 | } |