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 | |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 18 | import java.nio.ByteBuffer; |
Satish K | e70d88a | 2015-11-24 21:47:51 +0530 | [diff] [blame] | 19 | import java.util.Arrays; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 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 IsIsNonPseudonode Tlv. |
| 28 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 29 | public class IsIsNonPseudonode 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 = 6; |
| 32 | |
| 33 | private final byte[] isoNodeID; |
| 34 | |
| 35 | /** |
| 36 | * Constructor to initialize isoNodeID. |
| 37 | * |
| 38 | * @param isoNodeID ISO system-ID |
| 39 | */ |
| 40 | public IsIsNonPseudonode(byte[] isoNodeID) { |
Priyanka B | e366178 | 2015-11-22 17:52:54 +0530 | [diff] [blame] | 41 | this.isoNodeID = Arrays.copyOf(isoNodeID, isoNodeID.length); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Returns object of this class with specified isoNodeID. |
| 46 | * |
| 47 | * @param isoNodeID ISO system-ID |
| 48 | * @return object of IsIsNonPseudonode |
| 49 | */ |
| 50 | public static IsIsNonPseudonode of(final byte[] isoNodeID) { |
| 51 | return new IsIsNonPseudonode(isoNodeID); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns ISO NodeID. |
| 56 | * |
| 57 | * @return ISO NodeID |
| 58 | */ |
| 59 | public byte[] getISONodeID() { |
| 60 | return isoNodeID; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int hashCode() { |
Satish K | e70d88a | 2015-11-24 21:47:51 +0530 | [diff] [blame] | 65 | return Arrays.hashCode(isoNodeID); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean equals(Object obj) { |
| 70 | if (this == obj) { |
| 71 | return true; |
| 72 | } |
| 73 | if (obj instanceof IsIsNonPseudonode) { |
| 74 | IsIsNonPseudonode other = (IsIsNonPseudonode) obj; |
Satish K | e70d88a | 2015-11-24 21:47:51 +0530 | [diff] [blame] | 75 | return Arrays.equals(isoNodeID, other.isoNodeID); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 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.writeBytes(isoNodeID); |
| 86 | return c.writerIndex() - iLenStartIndex; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Reads the channel buffer and returns object of IsIsNonPseudonode. |
| 91 | * |
| 92 | * @param cb ChannelBuffer |
| 93 | * @return object of IsIsNonPseudonode |
| 94 | */ |
| 95 | public static IsIsNonPseudonode read(ChannelBuffer cb) { |
| 96 | byte[] isoNodeID = new byte[LENGTH]; |
Priyanka B | e366178 | 2015-11-22 17:52:54 +0530 | [diff] [blame] | 97 | cb.readBytes(isoNodeID); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 98 | return IsIsNonPseudonode.of(isoNodeID); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public short getType() { |
| 103 | return TYPE; |
| 104 | } |
| 105 | |
| 106 | @Override |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 107 | public int compareTo(Object o) { |
| 108 | if (this.equals(o)) { |
| 109 | return 0; |
| 110 | } |
| 111 | ByteBuffer value1 = ByteBuffer.wrap(this.isoNodeID); |
| 112 | ByteBuffer value2 = ByteBuffer.wrap(((IsIsNonPseudonode) o).isoNodeID); |
| 113 | return value1.compareTo(value2); |
| 114 | } |
| 115 | |
| 116 | @Override |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 117 | public String toString() { |
| 118 | return MoreObjects.toStringHelper(getClass()) |
| 119 | .add("Type", TYPE) |
| 120 | .add("Length", LENGTH) |
| 121 | .add("ISONodeID", isoNodeID) |
| 122 | .toString(); |
| 123 | } |
| 124 | } |