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