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