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