Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 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.bgpio.types; |
| 17 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.onlab.packet.Ip4Address; |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 21 | import org.onosproject.bgpio.protocol.IgpRouterId; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 22 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 23 | import java.util.Objects; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Provides implementation of OSPFPseudonode Tlv. |
| 27 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 28 | public class OspfPseudonode implements IgpRouterId, BgpValueType { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 29 | public static final short TYPE = 515; |
| 30 | public static final short LENGTH = 8; |
| 31 | |
| 32 | private final int routerID; |
| 33 | private final Ip4Address drInterface; |
| 34 | |
| 35 | /** |
| 36 | * Constructor to initialize parameters. |
| 37 | * |
| 38 | * @param routerID routerID |
| 39 | * @param drInterface IPv4 address of the DR's interface |
| 40 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 41 | public OspfPseudonode(int routerID, Ip4Address drInterface) { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 42 | this.routerID = routerID; |
| 43 | this.drInterface = drInterface; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns object of this class with specified values. |
| 48 | * |
| 49 | * @param routerID routerID |
| 50 | * @param drInterface IPv4 address of the DR's interface |
| 51 | * @return object of OSPFPseudonode |
| 52 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 53 | public static OspfPseudonode of(final int routerID, final Ip4Address drInterface) { |
| 54 | return new OspfPseudonode(routerID, drInterface); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns RouterID. |
| 59 | * |
| 60 | * @return RouterID |
| 61 | */ |
| 62 | public int getrouterID() { |
| 63 | return this.routerID; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public int hashCode() { |
| 68 | return Objects.hash(routerID, drInterface); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public boolean equals(Object obj) { |
| 73 | if (this == obj) { |
| 74 | return true; |
| 75 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 76 | if (obj instanceof OspfPseudonode) { |
| 77 | OspfPseudonode other = (OspfPseudonode) obj; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 78 | return Objects.equals(routerID, other.routerID) && Objects.equals(drInterface, other.drInterface); |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public int write(ChannelBuffer c) { |
| 85 | int iLenStartIndex = c.writerIndex(); |
| 86 | c.writeShort(TYPE); |
| 87 | c.writeShort(LENGTH); |
| 88 | c.writeInt(routerID); |
| 89 | c.writeInt(drInterface.toInt()); |
| 90 | return c.writerIndex() - iLenStartIndex; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Reads the channel buffer and returns object of OSPFPseudonode. |
| 95 | * |
| 96 | * @param cb ChannelBuffer |
| 97 | * @return object of OSPFPseudonode |
| 98 | */ |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 99 | public static OspfPseudonode read(ChannelBuffer cb) { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 100 | int routerID = cb.readInt(); |
| 101 | Ip4Address drInterface = Ip4Address.valueOf(cb.readInt()); |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 102 | return OspfPseudonode.of(routerID, drInterface); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public short getType() { |
| 107 | return TYPE; |
| 108 | } |
| 109 | |
| 110 | @Override |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 111 | public int compareTo(Object o) { |
| 112 | if (this.equals(o)) { |
| 113 | return 0; |
| 114 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 115 | int result = ((Integer) (this.routerID)).compareTo((Integer) (((OspfPseudonode) o).routerID)); |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 116 | if (result != 0) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 117 | return this.drInterface.compareTo(((OspfPseudonode) o).drInterface); |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 118 | } |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | @Override |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 123 | public String toString() { |
| 124 | return MoreObjects.toStringHelper(getClass()) |
| 125 | .add("Type", TYPE) |
| 126 | .add("Length", LENGTH) |
| 127 | .add("RouterID", routerID) |
| 128 | .add("DRInterface", drInterface) |
| 129 | .toString(); |
| 130 | } |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 131 | } |