Rusty Eddy | 390498d | 2016-01-15 19:21:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 18 | import org.onlab.packet.IpAddress; |
| 19 | import org.onlab.packet.MacAddress; |
| 20 | import org.onlab.packet.pim.PIMHelloOption; |
| 21 | import org.slf4j.Logger; |
| 22 | |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.util.Calendar; |
| 25 | import java.util.Date; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | import static org.slf4j.LoggerFactory.getLogger; |
| 29 | |
| 30 | public class PIMNeighbor { |
| 31 | |
| 32 | private final Logger log = getLogger(getClass()); |
| 33 | |
| 34 | // IP Address of this neighbor |
| 35 | private IpAddress ipAddr; |
| 36 | |
| 37 | // MAC Address of the neighbor (Need for sending J/P) |
| 38 | private MacAddress macAddr; |
| 39 | |
| 40 | // Hello Options |
| 41 | // Our hello opt holdTime |
| 42 | private short holdTime; |
| 43 | |
| 44 | // Our hello opt prune delay |
| 45 | private int pruneDelay; |
| 46 | |
| 47 | // Neighbor priority |
| 48 | private int priority; |
| 49 | |
| 50 | // Our current genId |
| 51 | private int genId; |
| 52 | |
| 53 | // Our timestamp for this neighbor |
| 54 | private Date lastRefresh; |
| 55 | |
| 56 | /** |
| 57 | * Construct a new PIM Neighbor. |
| 58 | * |
| 59 | * @param ipAddr the IP Address of our new neighbor |
| 60 | */ |
| 61 | public PIMNeighbor(IpAddress ipAddr, Map<Short, PIMHelloOption> opts) { |
| 62 | this.ipAddr = ipAddr; |
| 63 | this.addOptions(opts); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Construct a new PIM neighbor. |
| 68 | * |
| 69 | * @param ipAddr the neighbors IP addr |
| 70 | */ |
| 71 | public PIMNeighbor(IpAddress ipAddr, MacAddress macAddr) { |
| 72 | this.ipAddr = ipAddr; |
| 73 | this.macAddr = macAddr; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get the MAC address of this neighbor. |
| 78 | * |
| 79 | * @return the mac address |
| 80 | */ |
| 81 | public MacAddress getMacaddr() { |
| 82 | return macAddr; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get the IP Address of our neighbor. |
| 87 | * |
| 88 | * @return the IP address of our neighbor |
| 89 | */ |
| 90 | public IpAddress getIpaddr() { |
| 91 | return ipAddr; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Set the IP address of our neighbor. |
| 96 | * |
| 97 | * @param ipAddr our neighbors IP address |
| 98 | */ |
| 99 | public void setIpaddr(IpAddress ipAddr) { |
| 100 | this.ipAddr = ipAddr; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get our neighbors holdTime. |
| 105 | * |
| 106 | * @return the holdTime |
| 107 | */ |
| 108 | public short getHoldtime() { |
| 109 | return holdTime; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Set our neighbors holdTime. |
| 114 | * |
| 115 | * @param holdTime the holdTime |
| 116 | */ |
| 117 | public void setHoldtime(short holdTime) { |
| 118 | this.holdTime = holdTime; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get our neighbors prune delay. |
| 123 | * |
| 124 | * @return our neighbors prune delay |
| 125 | */ |
| 126 | public int getPruneDelay() { |
| 127 | return pruneDelay; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Set our neighbors prune delay. |
| 132 | * |
| 133 | * @param pruneDelay the prune delay |
| 134 | */ |
| 135 | public void setPruneDelay(int pruneDelay) { |
| 136 | this.pruneDelay = pruneDelay; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get our neighbors priority. |
| 141 | * |
| 142 | * @return our neighbors priority |
| 143 | */ |
| 144 | public int getPriority() { |
| 145 | return priority; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Set our neighbors priority. |
| 150 | * |
| 151 | * @param priority our neighbors priority |
| 152 | */ |
| 153 | public void setPriority(int priority) { |
| 154 | this.priority = priority; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get our neighbors Genid. |
| 159 | * |
| 160 | * @return our neighbor Genid |
| 161 | */ |
| 162 | public int getGenid() { |
| 163 | return genId; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Set our neighbors GenId. |
| 168 | * |
| 169 | * @param genId our neighbors GenId |
| 170 | */ |
| 171 | public void setGenid(int genId) { |
| 172 | this.genId = genId; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Add the options for this neighbor if needed. |
| 177 | * |
| 178 | * @param opts the options to be added/modified |
| 179 | * @return true if options changed, false if no option has changed |
| 180 | */ |
| 181 | public boolean addOptions(Map<Short, PIMHelloOption> opts) { |
| 182 | |
| 183 | boolean changed = false; |
| 184 | |
| 185 | for (PIMHelloOption opt : opts.values()) { |
| 186 | Short otype = opt.getOptType(); |
| 187 | ByteBuffer val = ByteBuffer.wrap(opt.getValue()); |
| 188 | |
| 189 | if (otype == PIMHelloOption.OPT_ADDRLIST) { |
| 190 | // TODO: Will implement someday |
| 191 | } else if (otype == PIMHelloOption.OPT_GENID) { |
| 192 | int newval = val.getInt(); |
| 193 | if (newval != genId) { |
| 194 | genId = newval; |
| 195 | changed = true; |
| 196 | } |
| 197 | } else if (otype == PIMHelloOption.OPT_HOLDTIME) { |
| 198 | short newval = val.getShort(); |
| 199 | if (newval != holdTime) { |
| 200 | holdTime = newval; |
| 201 | changed = true; |
| 202 | } |
| 203 | } else if (otype == PIMHelloOption.OPT_PRIORITY) { |
| 204 | int newval = val.getInt(); |
| 205 | if (newval != priority) { |
| 206 | priority = newval; |
| 207 | changed = true; |
| 208 | } |
| 209 | } else if (otype == PIMHelloOption.OPT_PRUNEDELAY) { |
| 210 | int newval = val.getInt(); |
| 211 | if (newval != pruneDelay) { |
| 212 | pruneDelay = newval; |
| 213 | changed = true; |
| 214 | } |
| 215 | } else { |
| 216 | log.warn("received unknown pim hello options" + otype); |
| 217 | } |
| 218 | } |
| 219 | return changed; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Refresh this neighbors timestamp. |
| 224 | */ |
| 225 | public void refreshTimestamp() { |
| 226 | lastRefresh = Calendar.getInstance().getTime(); |
| 227 | } |
| 228 | } |