blob: 19f8ef50275ea052e9b212e2fb5135934849f75f [file] [log] [blame]
Kalyankumar Asangi27728f22016-02-17 15:46:28 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Kalyankumar Asangi27728f22016-02-17 15:46:28 +05303 *
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
17package org.onosproject.ospf.controller.area;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.base.Objects;
21import org.onlab.packet.Ip4Address;
22import org.onosproject.ospf.controller.OspfInterface;
23import org.onosproject.ospf.controller.OspfNbr;
24import org.onosproject.ospf.protocol.lsa.LsaHeader;
25import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader;
26import org.onosproject.ospf.protocol.util.OspfInterfaceState;
27import org.onosproject.ospf.protocol.util.OspfParameters;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import java.util.ArrayList;
32import java.util.HashMap;
33import java.util.List;
34import java.util.Set;
35
36/**
37 * Representation of an OSPF interface.
38 */
39public class OspfInterfaceImpl implements OspfInterface {
40 private static final Logger log = LoggerFactory.getLogger(OspfInterfaceImpl.class);
41 private Ip4Address ipAddress;
42 private Ip4Address ipNetworkMask;
43 private int areaId;
44 private int helloIntervalTime;
45 private int routerDeadIntervalTime;
46 private int transmitDelay;
47 private int routerPriority;
48 private int systemInterfaceType;
49 private int interfaceType;
50 private int interfaceCost;
51 private String authType;
52 private String authKey;
53 private int pollInterval;
54 private int mtu;
55 private int reTransmitInterval;
56 private Ip4Address dr;
57 private Ip4Address bdr;
58 private OspfInterfaceState state;
59 private List<LsaHeader> linkStateHeaders = new ArrayList<>();
60 private HashMap<String, OspfNbr> listOfNeighbors = new HashMap<>();
61 private HashMap<String, LsaHeader> listOfNeighborMap = new HashMap<>();
62
63 /**
64 * Gets the interface state.
65 *
66 * @return interfaceState state of the interface
67 */
68 public OspfInterfaceState state() {
69 return state;
70 }
71
72 /**
73 * Sets the interface state.
74 *
75 * @param ospfInterfaceState interface state enum instance
76 */
77 public void setState(OspfInterfaceState ospfInterfaceState) {
78 this.state = ospfInterfaceState;
79 }
80
81 /**
82 * Gets link state headers.
83 *
84 * @return get the list of lsa headers
85 */
86 public List<LsaHeader> linkStateHeaders() {
87 Set<String> key = listOfNeighborMap.keySet();
88 for (String keys : key) {
89 LsaHeader lsaHeader = listOfNeighborMap.get(keys);
90 linkStateHeaders.add(lsaHeader);
91 }
92 return linkStateHeaders;
93 }
94
95 /**
96 * Gets IP network mask.
97 *
98 * @return network mask
99 */
100 public Ip4Address ipNetworkMask() {
101 return ipNetworkMask;
102 }
103
104 /**
105 * Sets IP network mask.
106 *
107 * @param ipNetworkMask network mask
108 */
109 @Override
110 public void setIpNetworkMask(Ip4Address ipNetworkMask) {
111 this.ipNetworkMask = ipNetworkMask;
112 }
113
114 /**
115 * Adds neighboring router to list.
116 *
117 * @param ospfNbr ospfNbr instance
118 */
119 public void addNeighbouringRouter(OspfNbr ospfNbr) {
120 listOfNeighbors.put(ospfNbr.neighborId().toString(), ospfNbr);
121 }
122
123 /**
124 * Gets the neighbour details from listOfNeighbors map.
125 *
126 * @param neighborId neighbors id
127 * @return ospfNbr neighbor instance
128 */
129 public OspfNbr neighbouringRouter(String neighborId) {
130 return listOfNeighbors.get(neighborId);
131 }
132
133
134 /**
135 * Adds LSAHeader to map.
136 *
137 * @param lsaHeader LSA header instance
138 */
139 public void addLsaHeaderForDelayAck(LsaHeader lsaHeader) {
140 String key = lsaHeader.lsType() + "-" + lsaHeader.linkStateId() + "-" +
141 lsaHeader.advertisingRouter();
142 if (lsaHeader.lsType() == OspfParameters.LINK_LOCAL_OPAQUE_LSA ||
143 lsaHeader.lsType() == OspfParameters.AREA_LOCAL_OPAQUE_LSA ||
144 lsaHeader.lsType() == OspfParameters.AS_OPAQUE_LSA) {
145 OpaqueLsaHeader header = (OpaqueLsaHeader) lsaHeader;
146 key = lsaHeader.lsType() + "-" + header.opaqueType() + header.opaqueId()
147 + "-" + lsaHeader.advertisingRouter();
148 }
149
150 log.debug("Adding LSA key {} for delayed Ack", key);
151 listOfNeighborMap.put(key, lsaHeader);
152 }
153
154 /**
155 * Removes LSA header from map.
156 *
157 * @param lsaKey key used to store LSA in map
158 */
159 public void removeLsaFromNeighborMap(String lsaKey) {
160 listOfNeighborMap.remove(lsaKey);
161 }
162
163 /**
164 * Checks neighbor is in the list or not.
165 *
166 * @param neighborId neighbors id
167 * @return true if neighbor in list else false
168 */
169 public boolean isNeighborInList(String neighborId) {
170 return listOfNeighbors.containsKey(neighborId);
171 }
172
173 /**
174 * Gets the list of neighbors.
175 *
176 * @return listOfNeighbors as key value pair
177 */
178 public HashMap<String, OspfNbr> listOfNeighbors() {
179 return listOfNeighbors;
180 }
181
182 /**
183 * Sets the list of neighbors.
184 *
185 * @param listOfNeighbors as key value pair
186 */
187 public void setListOfNeighbors(HashMap<String, OspfNbr> listOfNeighbors) {
188 this.listOfNeighbors = listOfNeighbors;
189 }
190
191 /**
192 * Gets the IP address.
193 *
194 * @return IP address
195 */
196 public Ip4Address ipAddress() {
197 return ipAddress;
198 }
199
200 /**
201 * Sets the interface IP address.
202 *
203 * @param ipAddress interface IP address
204 */
205 public void setIpAddress(Ip4Address ipAddress) {
206 this.ipAddress = ipAddress;
207 }
208
209 /**
210 * Gets router priority.
211 *
212 * @return routerPriority value
213 */
214 public int routerPriority() {
215 return routerPriority;
216 }
217
218 /**
219 * Sets router priority.
220 *
221 * @param routerPriority value
222 */
223 public void setRouterPriority(int routerPriority) {
224 this.routerPriority = routerPriority;
225 }
226
227 /**
228 * Gets the area id this interface belongs.
229 *
230 * @return area id this interface belongs
231 */
232 public int areaId() {
233 return areaId;
234 }
235
236
237 /**
238 * Sets the area id this interface belongs.
239 *
240 * @param areaId the area id this interface belongs
241 */
242 public void setAreaId(int areaId) {
243 this.areaId = areaId;
244 }
245
246 /**
247 * Gets hello interval time.
248 *
249 * @return hello interval time
250 */
251 public int helloIntervalTime() {
252 return helloIntervalTime;
253 }
254
255 /**
256 * Sets hello interval time.
257 *
258 * @param helloIntervalTime an integer interval time
259 */
260 public void setHelloIntervalTime(int helloIntervalTime) {
261 this.helloIntervalTime = helloIntervalTime;
262 }
263
264 /**
265 * Gets router dead interval time.
266 *
267 * @return router dead interval time
268 */
269 public int routerDeadIntervalTime() {
270 return routerDeadIntervalTime;
271 }
272
273 /**
274 * Sets router dead interval time.
275 *
276 * @param routerDeadIntervalTime router dead interval time
277 */
278 public void setRouterDeadIntervalTime(int routerDeadIntervalTime) {
279 this.routerDeadIntervalTime = routerDeadIntervalTime;
280 }
281
282 /**
283 * Gets interface type.
284 *
285 * @return interfaceType an integer represents interface type
286 */
287 public int interfaceType() {
288 return interfaceType;
289 }
290
291 /**
292 * Sets interface type.
293 *
294 * @param interfaceType interface type
295 */
296 public void setInterfaceType(int interfaceType) {
297 this.interfaceType = interfaceType;
298 }
299
300 /**
301 * Gets interface cost.
302 *
303 * @return interface cost
304 */
305 public int interfaceCost() {
306 return interfaceCost;
307 }
308
309 /**
310 * Sets interface cost.
311 *
312 * @param interfaceCost interface cost
313 */
314 public void setInterfaceCost(int interfaceCost) {
315 this.interfaceCost = interfaceCost;
316 }
317
318 /**
319 * Gets authentication type.
320 *
321 * @return authType represents authentication type
322 */
323 public String authType() {
324 return authType;
325 }
326
327 /**
328 * Sets authentication type.
329 *
330 * @param authType authType represents authentication type
331 */
332 public void setAuthType(String authType) {
333 this.authType = authType;
334 }
335
336 /**
337 * Gets authentication key.
338 *
339 * @return authKey represents authentication key
340 */
341 public String authKey() {
342 return authKey;
343 }
344
345 /**
346 * Sets authentication key.
347 *
348 * @param authKey represents authentication key
349 */
350 public void setAuthKey(String authKey) {
351 this.authKey = authKey;
352 }
353
354 /**
355 * Gets poll interval.
356 *
357 * @return pollInterval an integer represents poll interval
358 */
359 public int pollInterval() {
360 return pollInterval;
361 }
362
363 /**
364 * Sets poll interval.
365 *
366 * @param pollInterval an integer represents poll interval
367 */
368 public void setPollInterval(int pollInterval) {
369 this.pollInterval = pollInterval;
370 }
371
372 /**
373 * Gets max transfer unit.
374 *
375 * @return mtu an integer represents max transfer unit
376 */
377 public int mtu() {
378 return mtu;
379 }
380
381 /**
382 * Sets max transfer unit.
383 *
384 * @param mtu max transfer unit
385 */
386 public void setMtu(int mtu) {
387 this.mtu = mtu;
388 }
389
390 /**
391 * Gets retransmit interval.
392 *
393 * @return retransmit interval
394 */
395 public int reTransmitInterval() {
396 return reTransmitInterval;
397 }
398
399 /**
400 * Sets retransmit interval.
401 *
402 * @param reTransmitInterval retransmit interval
403 */
404 public void setReTransmitInterval(int reTransmitInterval) {
405 this.reTransmitInterval = reTransmitInterval;
406 }
407
408 /**
409 * Gets designated routers IP address.
410 *
411 * @return dr designated routers IP address
412 */
413 public Ip4Address dr() {
414 return dr;
415 }
416
417 /**
418 * Sets designated routers IP address.
419 *
420 * @param dr designated routers IP address
421 */
422 public void setDr(Ip4Address dr) {
423 this.dr = dr;
424 }
425
426 /**
427 * Gets backup designated routers IP address.
428 *
429 * @return bdr backup designated routers IP address
430 */
431 public Ip4Address bdr() {
432 return bdr;
433 }
434
435 /**
436 * Sets backup designated routers IP address.
437 *
438 * @param bdr backup designated routers IP address
439 */
440 public void setBdr(Ip4Address bdr) {
441 this.bdr = bdr;
442 }
443
444 /**
445 * Get transmission delay.
446 *
447 * @return transmission delay
448 */
449 public int transmitDelay() {
450 return transmitDelay;
451 }
452
453 /**
454 * Sets transmission delay.
455 *
456 * @param transmitDelay transmission delay
457 */
458 public void setTransmitDelay(int transmitDelay) {
459 this.transmitDelay = transmitDelay;
460 }
461
462 @Override
463 public boolean equals(Object o) {
464 if (this == o) {
465 return true;
466 }
467 if (o == null || getClass() != o.getClass()) {
468 return false;
469 }
470 OspfInterfaceImpl that = (OspfInterfaceImpl) o;
471 return Objects.equal(areaId, that.areaId) &&
472 Objects.equal(helloIntervalTime, that.helloIntervalTime) &&
473 Objects.equal(routerDeadIntervalTime, that.routerDeadIntervalTime) &&
474 Objects.equal(transmitDelay, that.transmitDelay) &&
475 Objects.equal(routerPriority, that.routerPriority) &&
476 Objects.equal(systemInterfaceType, that.systemInterfaceType) &&
477 Objects.equal(interfaceType, that.interfaceType) &&
478 Objects.equal(interfaceCost, that.interfaceCost) &&
479 Objects.equal(pollInterval, that.pollInterval) &&
480 Objects.equal(mtu, that.mtu) &&
481 Objects.equal(reTransmitInterval, that.reTransmitInterval) &&
482 Objects.equal(ipAddress, that.ipAddress) &&
483 Objects.equal(ipNetworkMask, that.ipNetworkMask) &&
484 Objects.equal(listOfNeighbors, that.listOfNeighbors) &&
485 Objects.equal(authType, that.authType) &&
486 Objects.equal(authKey, that.authKey) &&
487 Objects.equal(dr, that.dr) &&
488 Objects.equal(bdr, that.bdr);
489 }
490
491 @Override
492 public int hashCode() {
493 return Objects.hashCode(ipAddress, ipNetworkMask, areaId, helloIntervalTime,
494 routerDeadIntervalTime, transmitDelay, routerPriority, listOfNeighbors,
495 systemInterfaceType, interfaceType, interfaceCost, authType, authKey,
496 pollInterval, mtu, reTransmitInterval, dr, bdr);
497 }
498
499 @Override
500 public String toString() {
501 return MoreObjects.toStringHelper(getClass())
502 .omitNullValues()
503 .add("ipAddress", ipAddress)
504 .add("routerPriority", routerPriority)
505 .add("areaID", areaId)
506 .add("helloIntervalTime", helloIntervalTime)
507 .add("routerDeadIntervalTime", routerDeadIntervalTime)
508 .add("interfaceType", interfaceType)
509 .add("interfaceCost", interfaceCost)
510 .add("authType", authType)
511 .add("authKey", authKey)
512 .add("pollInterval", pollInterval)
513 .add("mtu", mtu)
514 .add("reTransmitInterval", reTransmitInterval)
515 .add("dr", dr)
516 .add("bdr", bdr)
517 .add("transmitDelay", transmitDelay)
518 .toString();
519 }
520}