blob: 7fac0cb99c6c678d1ea272f3164bd0b76b7134e7 [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();
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053091 }
92
93 /**
94 * Returns local extended circuit ID.
95 *
96 * @return local extended circuit ID
97 */
98 public int localExtendedCircuitId() {
99 return localExtendedCircuitId;
100 }
101
102 /**
103 * Sets local extended circuit ID.
104 *
105 * @param localExtendedCircuitId neighbor extended circuit ID
106 */
107 public void setLocalExtendedCircuitId(int localExtendedCircuitId) {
108 this.localExtendedCircuitId = localExtendedCircuitId;
109 }
110
111 /**
112 * Returns neighbor area ID.
113 *
114 * @return neighbor area ID
115 */
116 public String neighborAreaId() {
117 return neighborAreaId;
118 }
119
120 /**
121 * Sets neighbor area ID.
122 *
123 * @param neighborAreaId neighbor area ID
124 */
125 public void setNeighborAreaId(String neighborAreaId) {
126 this.neighborAreaId = neighborAreaId;
127 }
128
129 /**
130 * Returns neighbor system ID.
131 *
132 * @return neighbor system ID
133 */
134 public String neighborSystemId() {
135 return neighborSystemId;
136 }
137
138 /**
139 * Sets neighbor system ID.
140 *
141 * @param neighborSystemId neighbor system ID
142 */
143 public void setNeighborSystemId(String neighborSystemId) {
144 this.neighborSystemId = neighborSystemId;
145 }
146
147 /**
148 * Returns interface IP.
149 *
150 * @return interface IP
151 */
152 public Ip4Address interfaceIp() {
153 return interfaceIp;
154 }
155
156 /**
157 * Sets interface IP.
158 *
159 * @param interfaceIp IP
160 */
161 public void setInterfaceIp(Ip4Address interfaceIp) {
162 this.interfaceIp = interfaceIp;
163 }
164
165 /**
166 * Returns neighbor mac address.
167 *
168 * @return neighborMacAddress neighbor mac address
169 */
170 public MacAddress neighborMacAddress() {
171 return neighborMacAddress;
172 }
173
174 /**
175 * Sets neighbor mac address.
176 *
177 * @param neighborMacAddress mac address
178 */
179 public void setNeighborMacAddress(MacAddress neighborMacAddress) {
180 this.neighborMacAddress = neighborMacAddress;
181 }
182
183 /**
184 * Returns holding time.
185 *
186 * @return holding time
187 */
188 public int holdingTime() {
189 return holdingTime;
190 }
191
192 /**
193 * Sets holding time.
194 *
195 * @param holdingTime holding time
196 */
197 public void setHoldingTime(int holdingTime) {
198 this.holdingTime = holdingTime;
199 }
200
201 /**
202 * Returns router type.
203 *
204 * @return router type
205 */
206 public IsisRouterType routerType() {
207 return routerType;
208 }
209
210 /**
211 * Sets router type.
212 *
213 * @param routerType router type
214 */
215 public void setRouterType(IsisRouterType routerType) {
216 this.routerType = routerType;
217 }
218
219 /**
220 * Returns L1 lan ID.
221 *
222 * @return L1 lan ID
223 */
224 public String l1LanId() {
225 return l1LanId;
226 }
227
228 /**
229 * Sets L1 lan ID.
230 *
231 * @param l1LanId L1 lan ID
232 */
233 public void setL1LanId(String l1LanId) {
234 this.l1LanId = l1LanId;
235 }
236
237 /**
238 * Returns L2 lan ID.
239 *
240 * @return L2 lan ID
241 */
242 public String l2LanId() {
243 return l2LanId;
244 }
245
246 /**
247 * Sets L2 lan ID.
248 *
249 * @param l2LanId L2 lan ID
250 */
251 public void setL2LanId(String l2LanId) {
chidambar babu344dc812016-05-02 19:13:10 +0530252 this.l2LanId = l2LanId;
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530253 }
254
255 /**
256 * Gets the neighbor interface state.
257 *
258 * @return neighbor interface state
259 */
260 public IsisInterfaceState interfaceState() {
261 return neighborState;
262 }
263
264 /**
265 * Sets the neighbor interface state.
266 *
267 * @param neighborState the neighbor interface state
268 */
269 public void setNeighborState(IsisInterfaceState neighborState) {
270 this.neighborState = neighborState;
271 }
272
273 /**
274 * Returns local circuit ID.
275 *
276 * @return local circuit ID
277 */
278 public byte localCircuitId() {
279 return localCircuitId;
280 }
281
282 /**
283 * Sets local circuit ID.
284 *
285 * @param localCircuitId local circuit ID
286 */
287 public void setLocalCircuitId(byte localCircuitId) {
288 this.localCircuitId = localCircuitId;
289 }
290
291 /**
292 * Returns neighbor state.
293 *
294 * @return neighbor state
295 */
296 public IsisInterfaceState neighborState() {
297 return neighborState;
298 }
299
300 /**
chidambar babu344dc812016-05-02 19:13:10 +0530301 * Starts the holding time check timer.
302 */
303 public void startHoldingTimeCheck() {
304 log.debug("IsisNeighbor::startHoldingTimeCheck");
305 holdingTimeCheckTask = new InternalHoldingTimeCheck();
306 exServiceHoldingTimeCheck = Executors.newSingleThreadScheduledExecutor();
307 exServiceHoldingTimeCheck.scheduleAtFixedRate(holdingTimeCheckTask, 1,
308 1, TimeUnit.SECONDS);
309 }
310
311 /**
312 * Stops the holding time check timer.
313 */
314 public void stopHoldingTimeCheck() {
315 log.debug("IsisNeighbor::stopHoldingTimeCheck ");
316 exServiceHoldingTimeCheck.shutdown();
317 }
318
319 /**
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530320 * Starts the inactivity timer.
321 */
322 public void startInactivityTimeCheck() {
323 if (!inActivityTimerScheduled) {
324 log.debug("IsisNeighbor::startInactivityTimeCheck");
325 inActivityTimeCheckTask = new InternalInactivityTimeCheck();
326 exServiceInActivity = Executors.newSingleThreadScheduledExecutor();
chidambar babu344dc812016-05-02 19:13:10 +0530327 exServiceInActivity.scheduleAtFixedRate(inActivityTimeCheckTask, neighborDownInterval,
328 neighborDownInterval, TimeUnit.SECONDS);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530329 inActivityTimerScheduled = true;
330 }
331 }
332
333 /**
334 * Stops the inactivity timer.
335 */
336 public void stopInactivityTimeCheck() {
337 if (inActivityTimerScheduled) {
338 log.debug("IsisNeighbor::stopInactivityTimeCheck ");
339 exServiceInActivity.shutdown();
340 inActivityTimerScheduled = false;
341 }
342 }
343
344 /**
345 * Called when neighbor is down.
346 */
347 public void neighborDown() {
348 log.debug("Neighbor Down {} and NeighborSystemId {}", neighborMacAddress,
349 neighborSystemId);
350 stopInactivityTimeCheck();
351 isisInterface.setL1LanId(IsisConstants.DEFAULTLANID);
352 isisInterface.setL2LanId(IsisConstants.DEFAULTLANID);
353
354 neighborState = IsisInterfaceState.DOWN;
chidambar babu344dc812016-05-02 19:13:10 +0530355 stopInactivityTimeCheck();
356 stopHoldingTimeCheck();
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530357 isisInterface.removeNeighbor(this);
358 }
359
360 /**
361 * Represents a Task which will do an inactivity time check.
362 */
363 private class InternalInactivityTimeCheck implements Runnable {
364 /**
365 * Creates an instance.
366 */
367 InternalInactivityTimeCheck() {
368 }
369
370 @Override
371 public void run() {
372 log.debug("Neighbor Not Heard till the past router dead interval .");
373 neighborDown();
374 }
375 }
chidambar babu344dc812016-05-02 19:13:10 +0530376
377 /**
378 * Represents a Task which will decrement holding time for this neighbor.
379 */
380 private class InternalHoldingTimeCheck implements Runnable {
381 /**
382 * Creates an instance.
383 */
384 InternalHoldingTimeCheck() {
385 }
386
387 @Override
388 public void run() {
389 holdingTime--;
390 }
391 }
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530392}