tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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.isis.controller.impl; |
| 17 | |
| 18 | import org.onlab.packet.Ip4Address; |
| 19 | import org.onlab.packet.MacAddress; |
| 20 | import org.onosproject.isis.controller.IsisInterface; |
| 21 | import org.onosproject.isis.controller.IsisInterfaceState; |
| 22 | import org.onosproject.isis.controller.IsisNeighbor; |
| 23 | import org.onosproject.isis.controller.IsisPduType; |
| 24 | import org.onosproject.isis.controller.IsisRouterType; |
| 25 | import org.onosproject.isis.io.isispacket.pdu.HelloPdu; |
| 26 | import org.onosproject.isis.io.isispacket.pdu.L1L2HelloPdu; |
| 27 | import org.onosproject.isis.io.isispacket.pdu.P2PHelloPdu; |
| 28 | import org.onosproject.isis.io.util.IsisConstants; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.util.List; |
| 33 | import java.util.concurrent.Executors; |
| 34 | import java.util.concurrent.ScheduledExecutorService; |
| 35 | import java.util.concurrent.TimeUnit; |
| 36 | |
| 37 | /** |
| 38 | * Representation of an ISIS neighbor. |
| 39 | * The first thing an ISIS router must do is find its neighbors and form adjacency. |
| 40 | * Each neighbor that the router finds will be represented by this class. |
| 41 | */ |
| 42 | public class DefaultIsisNeighbor implements IsisNeighbor { |
| 43 | private static final Logger log = LoggerFactory.getLogger(DefaultIsisNeighbor.class); |
| 44 | private String neighborAreaId; |
| 45 | private String neighborSystemId; |
| 46 | private Ip4Address interfaceIp; |
| 47 | private MacAddress neighborMacAddress; |
| 48 | private int holdingTime; |
| 49 | private IsisRouterType routerType; |
| 50 | private String l1LanId; |
| 51 | private String l2LanId; |
| 52 | private byte localCircuitId; |
| 53 | private int localExtendedCircuitId; |
| 54 | private IsisInterfaceState neighborState = IsisInterfaceState.INITIAL; |
| 55 | private InternalInactivityTimeCheck inActivityTimeCheckTask; |
| 56 | private ScheduledExecutorService exServiceInActivity; |
| 57 | private boolean inActivityTimerScheduled = false; |
| 58 | private IsisInterface isisInterface; |
| 59 | |
| 60 | /** |
| 61 | * Creates an instance of ISIS neighbor. |
| 62 | * |
| 63 | * @param helloMessage hello message instance |
| 64 | * @param isisInterface ISIS interface instance |
| 65 | */ |
| 66 | public DefaultIsisNeighbor(HelloPdu helloMessage, IsisInterface isisInterface) { |
| 67 | this.neighborMacAddress = helloMessage.sourceMac(); |
| 68 | List<String> areaAddresses = helloMessage.areaAddress(); |
| 69 | this.neighborAreaId = (areaAddresses != null) ? areaAddresses.get(0) : ""; |
| 70 | this.neighborSystemId = helloMessage.sourceId(); |
| 71 | List<Ip4Address> interfaceIpAddresses = helloMessage.interfaceIpAddresses(); |
| 72 | this.interfaceIp = (helloMessage.interfaceIpAddresses() != null) ? |
| 73 | interfaceIpAddresses.get(0) : IsisConstants.DEFAULTIP; |
| 74 | this.holdingTime = helloMessage.holdingTime(); |
| 75 | this.routerType = IsisRouterType.get(helloMessage.circuitType()); |
| 76 | if (helloMessage instanceof L1L2HelloPdu) { |
| 77 | if (IsisPduType.L1HELLOPDU == helloMessage.isisPduType()) { |
| 78 | l1LanId = ((L1L2HelloPdu) helloMessage).lanId(); |
| 79 | } else if (IsisPduType.L2HELLOPDU == helloMessage.isisPduType()) { |
| 80 | l2LanId = ((L1L2HelloPdu) helloMessage).lanId(); |
| 81 | } |
| 82 | } else if (helloMessage instanceof P2PHelloPdu) { |
| 83 | this.localCircuitId = ((P2PHelloPdu) helloMessage).localCircuitId(); |
| 84 | } |
| 85 | this.isisInterface = isisInterface; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns local extended circuit ID. |
| 90 | * |
| 91 | * @return local extended circuit ID |
| 92 | */ |
| 93 | public int localExtendedCircuitId() { |
| 94 | return localExtendedCircuitId; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Sets local extended circuit ID. |
| 99 | * |
| 100 | * @param localExtendedCircuitId neighbor extended circuit ID |
| 101 | */ |
| 102 | public void setLocalExtendedCircuitId(int localExtendedCircuitId) { |
| 103 | this.localExtendedCircuitId = localExtendedCircuitId; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns neighbor area ID. |
| 108 | * |
| 109 | * @return neighbor area ID |
| 110 | */ |
| 111 | public String neighborAreaId() { |
| 112 | return neighborAreaId; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Sets neighbor area ID. |
| 117 | * |
| 118 | * @param neighborAreaId neighbor area ID |
| 119 | */ |
| 120 | public void setNeighborAreaId(String neighborAreaId) { |
| 121 | this.neighborAreaId = neighborAreaId; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns neighbor system ID. |
| 126 | * |
| 127 | * @return neighbor system ID |
| 128 | */ |
| 129 | public String neighborSystemId() { |
| 130 | return neighborSystemId; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Sets neighbor system ID. |
| 135 | * |
| 136 | * @param neighborSystemId neighbor system ID |
| 137 | */ |
| 138 | public void setNeighborSystemId(String neighborSystemId) { |
| 139 | this.neighborSystemId = neighborSystemId; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns interface IP. |
| 144 | * |
| 145 | * @return interface IP |
| 146 | */ |
| 147 | public Ip4Address interfaceIp() { |
| 148 | return interfaceIp; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Sets interface IP. |
| 153 | * |
| 154 | * @param interfaceIp IP |
| 155 | */ |
| 156 | public void setInterfaceIp(Ip4Address interfaceIp) { |
| 157 | this.interfaceIp = interfaceIp; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns neighbor mac address. |
| 162 | * |
| 163 | * @return neighborMacAddress neighbor mac address |
| 164 | */ |
| 165 | public MacAddress neighborMacAddress() { |
| 166 | return neighborMacAddress; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Sets neighbor mac address. |
| 171 | * |
| 172 | * @param neighborMacAddress mac address |
| 173 | */ |
| 174 | public void setNeighborMacAddress(MacAddress neighborMacAddress) { |
| 175 | this.neighborMacAddress = neighborMacAddress; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Returns holding time. |
| 180 | * |
| 181 | * @return holding time |
| 182 | */ |
| 183 | public int holdingTime() { |
| 184 | return holdingTime; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Sets holding time. |
| 189 | * |
| 190 | * @param holdingTime holding time |
| 191 | */ |
| 192 | public void setHoldingTime(int holdingTime) { |
| 193 | this.holdingTime = holdingTime; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Returns router type. |
| 198 | * |
| 199 | * @return router type |
| 200 | */ |
| 201 | public IsisRouterType routerType() { |
| 202 | return routerType; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Sets router type. |
| 207 | * |
| 208 | * @param routerType router type |
| 209 | */ |
| 210 | public void setRouterType(IsisRouterType routerType) { |
| 211 | this.routerType = routerType; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Returns L1 lan ID. |
| 216 | * |
| 217 | * @return L1 lan ID |
| 218 | */ |
| 219 | public String l1LanId() { |
| 220 | return l1LanId; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Sets L1 lan ID. |
| 225 | * |
| 226 | * @param l1LanId L1 lan ID |
| 227 | */ |
| 228 | public void setL1LanId(String l1LanId) { |
| 229 | this.l1LanId = l1LanId; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Returns L2 lan ID. |
| 234 | * |
| 235 | * @return L2 lan ID |
| 236 | */ |
| 237 | public String l2LanId() { |
| 238 | return l2LanId; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Sets L2 lan ID. |
| 243 | * |
| 244 | * @param l2LanId L2 lan ID |
| 245 | */ |
| 246 | public void setL2LanId(String l2LanId) { |
| 247 | this.l1LanId = l1LanId; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Gets the neighbor interface state. |
| 252 | * |
| 253 | * @return neighbor interface state |
| 254 | */ |
| 255 | public IsisInterfaceState interfaceState() { |
| 256 | return neighborState; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Sets the neighbor interface state. |
| 261 | * |
| 262 | * @param neighborState the neighbor interface state |
| 263 | */ |
| 264 | public void setNeighborState(IsisInterfaceState neighborState) { |
| 265 | this.neighborState = neighborState; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Returns local circuit ID. |
| 270 | * |
| 271 | * @return local circuit ID |
| 272 | */ |
| 273 | public byte localCircuitId() { |
| 274 | return localCircuitId; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Sets local circuit ID. |
| 279 | * |
| 280 | * @param localCircuitId local circuit ID |
| 281 | */ |
| 282 | public void setLocalCircuitId(byte localCircuitId) { |
| 283 | this.localCircuitId = localCircuitId; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Returns neighbor state. |
| 288 | * |
| 289 | * @return neighbor state |
| 290 | */ |
| 291 | public IsisInterfaceState neighborState() { |
| 292 | return neighborState; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Starts the inactivity timer. |
| 297 | */ |
| 298 | public void startInactivityTimeCheck() { |
| 299 | if (!inActivityTimerScheduled) { |
| 300 | log.debug("IsisNeighbor::startInactivityTimeCheck"); |
| 301 | inActivityTimeCheckTask = new InternalInactivityTimeCheck(); |
| 302 | exServiceInActivity = Executors.newSingleThreadScheduledExecutor(); |
| 303 | exServiceInActivity.scheduleAtFixedRate(inActivityTimeCheckTask, holdingTime, |
| 304 | holdingTime, TimeUnit.SECONDS); |
| 305 | inActivityTimerScheduled = true; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Stops the inactivity timer. |
| 311 | */ |
| 312 | public void stopInactivityTimeCheck() { |
| 313 | if (inActivityTimerScheduled) { |
| 314 | log.debug("IsisNeighbor::stopInactivityTimeCheck "); |
| 315 | exServiceInActivity.shutdown(); |
| 316 | inActivityTimerScheduled = false; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Called when neighbor is down. |
| 322 | */ |
| 323 | public void neighborDown() { |
| 324 | log.debug("Neighbor Down {} and NeighborSystemId {}", neighborMacAddress, |
| 325 | neighborSystemId); |
| 326 | stopInactivityTimeCheck(); |
| 327 | isisInterface.setL1LanId(IsisConstants.DEFAULTLANID); |
| 328 | isisInterface.setL2LanId(IsisConstants.DEFAULTLANID); |
| 329 | |
| 330 | neighborState = IsisInterfaceState.DOWN; |
| 331 | isisInterface.removeNeighbor(this); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Represents a Task which will do an inactivity time check. |
| 336 | */ |
| 337 | private class InternalInactivityTimeCheck implements Runnable { |
| 338 | /** |
| 339 | * Creates an instance. |
| 340 | */ |
| 341 | InternalInactivityTimeCheck() { |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public void run() { |
| 346 | log.debug("Neighbor Not Heard till the past router dead interval ."); |
| 347 | neighborDown(); |
| 348 | } |
| 349 | } |
| 350 | } |