blob: 0839eb54225cd4b923d85568736f47263494ab49 [file] [log] [blame]
tejeshwer degala3fe1ed52016-04-22 17:04:01 +05301/*
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 */
16package org.onosproject.isis.controller.impl;
17
18import org.onlab.packet.Ip4Address;
19import org.onlab.packet.MacAddress;
20import org.onosproject.isis.controller.IsisInterface;
21import org.onosproject.isis.controller.IsisInterfaceState;
22import org.onosproject.isis.controller.IsisNeighbor;
23import org.onosproject.isis.controller.IsisPduType;
24import org.onosproject.isis.controller.IsisRouterType;
25import org.onosproject.isis.io.isispacket.pdu.HelloPdu;
26import org.onosproject.isis.io.isispacket.pdu.L1L2HelloPdu;
27import org.onosproject.isis.io.isispacket.pdu.P2PHelloPdu;
28import org.onosproject.isis.io.util.IsisConstants;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import java.util.List;
33import java.util.concurrent.Executors;
34import java.util.concurrent.ScheduledExecutorService;
35import 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 */
42public 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;
chidambar babu344dc812016-05-02 19:13:10 +053048 private volatile int holdingTime;
49 private int neighborDownInterval;
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053050 private IsisRouterType routerType;
51 private String l1LanId;
52 private String l2LanId;
53 private byte localCircuitId;
54 private int localExtendedCircuitId;
55 private IsisInterfaceState neighborState = IsisInterfaceState.INITIAL;
56 private InternalInactivityTimeCheck inActivityTimeCheckTask;
57 private ScheduledExecutorService exServiceInActivity;
chidambar babu344dc812016-05-02 19:13:10 +053058 private InternalHoldingTimeCheck holdingTimeCheckTask;
59 private ScheduledExecutorService exServiceHoldingTimeCheck;
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053060 private boolean inActivityTimerScheduled = false;
61 private IsisInterface isisInterface;
62
63 /**
64 * Creates an instance of ISIS neighbor.
65 *
66 * @param helloMessage hello message instance
67 * @param isisInterface ISIS interface instance
68 */
69 public DefaultIsisNeighbor(HelloPdu helloMessage, IsisInterface isisInterface) {
70 this.neighborMacAddress = helloMessage.sourceMac();
71 List<String> areaAddresses = helloMessage.areaAddress();
72 this.neighborAreaId = (areaAddresses != null) ? areaAddresses.get(0) : "";
73 this.neighborSystemId = helloMessage.sourceId();
74 List<Ip4Address> interfaceIpAddresses = helloMessage.interfaceIpAddresses();
75 this.interfaceIp = (helloMessage.interfaceIpAddresses() != null) ?
76 interfaceIpAddresses.get(0) : IsisConstants.DEFAULTIP;
77 this.holdingTime = helloMessage.holdingTime();
chidambar babu344dc812016-05-02 19:13:10 +053078 neighborDownInterval = holdingTime;
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053079 this.routerType = IsisRouterType.get(helloMessage.circuitType());
80 if (helloMessage instanceof L1L2HelloPdu) {
81 if (IsisPduType.L1HELLOPDU == helloMessage.isisPduType()) {
82 l1LanId = ((L1L2HelloPdu) helloMessage).lanId();
83 } else if (IsisPduType.L2HELLOPDU == helloMessage.isisPduType()) {
84 l2LanId = ((L1L2HelloPdu) helloMessage).lanId();
85 }
86 } else if (helloMessage instanceof P2PHelloPdu) {
87 this.localCircuitId = ((P2PHelloPdu) helloMessage).localCircuitId();
88 }
89 this.isisInterface = isisInterface;
chidambar babu344dc812016-05-02 19:13:10 +053090 startHoldingTimeCheck();
sunish vk4b5ce002016-05-09 20:18:35 +053091 log.debug("Neighbor added - {}", neighborMacAddress);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053092 }
93
94 /**
95 * Returns local extended circuit ID.
96 *
97 * @return local extended circuit ID
98 */
99 public int localExtendedCircuitId() {
100 return localExtendedCircuitId;
101 }
102
103 /**
104 * Sets local extended circuit ID.
105 *
106 * @param localExtendedCircuitId neighbor extended circuit ID
107 */
108 public void setLocalExtendedCircuitId(int localExtendedCircuitId) {
109 this.localExtendedCircuitId = localExtendedCircuitId;
110 }
111
112 /**
113 * Returns neighbor area ID.
114 *
115 * @return neighbor area ID
116 */
117 public String neighborAreaId() {
118 return neighborAreaId;
119 }
120
121 /**
122 * Sets neighbor area ID.
123 *
124 * @param neighborAreaId neighbor area ID
125 */
126 public void setNeighborAreaId(String neighborAreaId) {
127 this.neighborAreaId = neighborAreaId;
128 }
129
130 /**
131 * Returns neighbor system ID.
132 *
133 * @return neighbor system ID
134 */
135 public String neighborSystemId() {
136 return neighborSystemId;
137 }
138
139 /**
140 * Sets neighbor system ID.
141 *
142 * @param neighborSystemId neighbor system ID
143 */
144 public void setNeighborSystemId(String neighborSystemId) {
145 this.neighborSystemId = neighborSystemId;
146 }
147
148 /**
149 * Returns interface IP.
150 *
151 * @return interface IP
152 */
153 public Ip4Address interfaceIp() {
154 return interfaceIp;
155 }
156
157 /**
158 * Sets interface IP.
159 *
160 * @param interfaceIp IP
161 */
162 public void setInterfaceIp(Ip4Address interfaceIp) {
163 this.interfaceIp = interfaceIp;
164 }
165
166 /**
167 * Returns neighbor mac address.
168 *
169 * @return neighborMacAddress neighbor mac address
170 */
171 public MacAddress neighborMacAddress() {
172 return neighborMacAddress;
173 }
174
175 /**
176 * Sets neighbor mac address.
177 *
178 * @param neighborMacAddress mac address
179 */
180 public void setNeighborMacAddress(MacAddress neighborMacAddress) {
181 this.neighborMacAddress = neighborMacAddress;
182 }
183
184 /**
185 * Returns holding time.
186 *
187 * @return holding time
188 */
189 public int holdingTime() {
190 return holdingTime;
191 }
192
193 /**
194 * Sets holding time.
195 *
196 * @param holdingTime holding time
197 */
198 public void setHoldingTime(int holdingTime) {
199 this.holdingTime = holdingTime;
200 }
201
202 /**
203 * Returns router type.
204 *
205 * @return router type
206 */
207 public IsisRouterType routerType() {
208 return routerType;
209 }
210
211 /**
212 * Sets router type.
213 *
214 * @param routerType router type
215 */
216 public void setRouterType(IsisRouterType routerType) {
217 this.routerType = routerType;
218 }
219
220 /**
221 * Returns L1 lan ID.
222 *
223 * @return L1 lan ID
224 */
225 public String l1LanId() {
226 return l1LanId;
227 }
228
229 /**
230 * Sets L1 lan ID.
231 *
232 * @param l1LanId L1 lan ID
233 */
234 public void setL1LanId(String l1LanId) {
235 this.l1LanId = l1LanId;
236 }
237
238 /**
239 * Returns L2 lan ID.
240 *
241 * @return L2 lan ID
242 */
243 public String l2LanId() {
244 return l2LanId;
245 }
246
247 /**
248 * Sets L2 lan ID.
249 *
250 * @param l2LanId L2 lan ID
251 */
252 public void setL2LanId(String l2LanId) {
chidambar babu344dc812016-05-02 19:13:10 +0530253 this.l2LanId = l2LanId;
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530254 }
255
256 /**
257 * Gets the neighbor interface state.
258 *
259 * @return neighbor interface state
260 */
261 public IsisInterfaceState interfaceState() {
262 return neighborState;
263 }
264
265 /**
266 * Sets the neighbor interface state.
267 *
268 * @param neighborState the neighbor interface state
269 */
270 public void setNeighborState(IsisInterfaceState neighborState) {
271 this.neighborState = neighborState;
272 }
273
274 /**
275 * Returns local circuit ID.
276 *
277 * @return local circuit ID
278 */
279 public byte localCircuitId() {
280 return localCircuitId;
281 }
282
283 /**
284 * Sets local circuit ID.
285 *
286 * @param localCircuitId local circuit ID
287 */
288 public void setLocalCircuitId(byte localCircuitId) {
289 this.localCircuitId = localCircuitId;
290 }
291
292 /**
293 * Returns neighbor state.
294 *
295 * @return neighbor state
296 */
297 public IsisInterfaceState neighborState() {
298 return neighborState;
299 }
300
301 /**
chidambar babu344dc812016-05-02 19:13:10 +0530302 * Starts the holding time check timer.
303 */
304 public void startHoldingTimeCheck() {
305 log.debug("IsisNeighbor::startHoldingTimeCheck");
306 holdingTimeCheckTask = new InternalHoldingTimeCheck();
307 exServiceHoldingTimeCheck = Executors.newSingleThreadScheduledExecutor();
308 exServiceHoldingTimeCheck.scheduleAtFixedRate(holdingTimeCheckTask, 1,
309 1, TimeUnit.SECONDS);
310 }
311
312 /**
313 * Stops the holding time check timer.
314 */
315 public void stopHoldingTimeCheck() {
316 log.debug("IsisNeighbor::stopHoldingTimeCheck ");
317 exServiceHoldingTimeCheck.shutdown();
318 }
319
320 /**
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530321 * Starts the inactivity timer.
322 */
323 public void startInactivityTimeCheck() {
324 if (!inActivityTimerScheduled) {
325 log.debug("IsisNeighbor::startInactivityTimeCheck");
326 inActivityTimeCheckTask = new InternalInactivityTimeCheck();
327 exServiceInActivity = Executors.newSingleThreadScheduledExecutor();
chidambar babu344dc812016-05-02 19:13:10 +0530328 exServiceInActivity.scheduleAtFixedRate(inActivityTimeCheckTask, neighborDownInterval,
329 neighborDownInterval, TimeUnit.SECONDS);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530330 inActivityTimerScheduled = true;
331 }
332 }
333
334 /**
335 * Stops the inactivity timer.
336 */
337 public void stopInactivityTimeCheck() {
338 if (inActivityTimerScheduled) {
339 log.debug("IsisNeighbor::stopInactivityTimeCheck ");
340 exServiceInActivity.shutdown();
341 inActivityTimerScheduled = false;
342 }
343 }
344
345 /**
346 * Called when neighbor is down.
347 */
348 public void neighborDown() {
349 log.debug("Neighbor Down {} and NeighborSystemId {}", neighborMacAddress,
350 neighborSystemId);
351 stopInactivityTimeCheck();
352 isisInterface.setL1LanId(IsisConstants.DEFAULTLANID);
353 isisInterface.setL2LanId(IsisConstants.DEFAULTLANID);
354
355 neighborState = IsisInterfaceState.DOWN;
chidambar babu344dc812016-05-02 19:13:10 +0530356 stopInactivityTimeCheck();
357 stopHoldingTimeCheck();
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530358 isisInterface.removeNeighbor(this);
sunish vk7bdf4d42016-06-24 12:29:43 +0530359
360 isisInterface.isisLsdb().removeTopology(this, isisInterface);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530361 }
362
363 /**
364 * Represents a Task which will do an inactivity time check.
365 */
366 private class InternalInactivityTimeCheck implements Runnable {
367 /**
368 * Creates an instance.
369 */
370 InternalInactivityTimeCheck() {
371 }
372
373 @Override
374 public void run() {
375 log.debug("Neighbor Not Heard till the past router dead interval .");
376 neighborDown();
377 }
378 }
chidambar babu344dc812016-05-02 19:13:10 +0530379
380 /**
381 * Represents a Task which will decrement holding time for this neighbor.
382 */
383 private class InternalHoldingTimeCheck implements Runnable {
384 /**
385 * Creates an instance.
386 */
387 InternalHoldingTimeCheck() {
388 }
389
390 @Override
391 public void run() {
392 holdingTime--;
sunish vk4b5ce002016-05-09 20:18:35 +0530393 if (holdingTime <= 0) {
394 log.debug("Calling neighbor down. Holding time is 0.");
395 neighborDown();
396 }
chidambar babu344dc812016-05-02 19:13:10 +0530397 }
398 }
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530399}