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