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; |
Priyanka B | e366178 | 2015-11-22 17:52:54 +0530 | [diff] [blame] | 19 | import java.util.Arrays; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
| 22 | import org.jboss.netty.buffer.ChannelBuffer; |
| 23 | import org.onosproject.bgpio.protocol.IGPRouterID; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 24 | |
| 25 | import com.google.common.base.MoreObjects; |
| 26 | |
| 27 | /** |
| 28 | * Provides implementation of IsIsPseudonode Tlv. |
| 29 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 30 | public class IsIsPseudonode implements IGPRouterID, BgpValueType { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 31 | public static final short TYPE = 515; |
| 32 | public static final short LENGTH = 7; |
| 33 | |
Priyanka B | e366178 | 2015-11-22 17:52:54 +0530 | [diff] [blame] | 34 | private final byte[] isoNodeID; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 35 | private byte psnIdentifier; |
| 36 | |
| 37 | /** |
| 38 | * Constructor to initialize isoNodeID. |
| 39 | * |
| 40 | * @param isoNodeID ISO system-ID |
| 41 | * @param psnIdentifier PSN identifier |
| 42 | */ |
Priyanka B | e366178 | 2015-11-22 17:52:54 +0530 | [diff] [blame] | 43 | public IsIsPseudonode(byte[] isoNodeID, byte psnIdentifier) { |
| 44 | this.isoNodeID = Arrays.copyOf(isoNodeID, isoNodeID.length); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 45 | this.psnIdentifier = psnIdentifier; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns object of this class with specified values. |
| 50 | * |
| 51 | * @param isoNodeID ISO system-ID |
| 52 | * @param psnIdentifier PSN identifier |
| 53 | * @return object of IsIsPseudonode |
| 54 | */ |
Priyanka B | e366178 | 2015-11-22 17:52:54 +0530 | [diff] [blame] | 55 | public static IsIsPseudonode of(final byte[] isoNodeID, |
Priyanka B | 01aefd3 | 2015-11-21 21:15:44 +0530 | [diff] [blame] | 56 | final byte psnIdentifier) { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 57 | return new IsIsPseudonode(isoNodeID, psnIdentifier); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns ISO NodeID. |
| 62 | * |
| 63 | * @return ISO NodeID |
| 64 | */ |
Priyanka B | e366178 | 2015-11-22 17:52:54 +0530 | [diff] [blame] | 65 | public byte[] getISONodeID() { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 66 | return isoNodeID; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns PSN Identifier. |
| 71 | * |
| 72 | * @return PSN Identifier |
| 73 | */ |
| 74 | public byte getPSNIdentifier() { |
| 75 | return this.psnIdentifier; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public int hashCode() { |
Priyanka B | e366178 | 2015-11-22 17:52:54 +0530 | [diff] [blame] | 80 | return Arrays.hashCode(isoNodeID) & Objects.hash(psnIdentifier); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean equals(Object obj) { |
| 85 | if (this == obj) { |
| 86 | return true; |
| 87 | } |
| 88 | if (obj instanceof IsIsPseudonode) { |
| 89 | IsIsPseudonode other = (IsIsPseudonode) obj; |
Priyanka B | e366178 | 2015-11-22 17:52:54 +0530 | [diff] [blame] | 90 | return Arrays.equals(isoNodeID, other.isoNodeID) |
| 91 | && Objects.equals(psnIdentifier, other.psnIdentifier); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public int write(ChannelBuffer c) { |
| 98 | int iLenStartIndex = c.writerIndex(); |
| 99 | c.writeShort(TYPE); |
| 100 | c.writeShort(LENGTH); |
Priyanka B | e366178 | 2015-11-22 17:52:54 +0530 | [diff] [blame] | 101 | c.writeBytes(isoNodeID, 0, LENGTH - 1); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 102 | c.writeByte(psnIdentifier); |
| 103 | return c.writerIndex() - iLenStartIndex; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Reads the channel buffer and returns object of IsIsPseudonode. |
| 108 | * |
| 109 | * @param cb ChannelBuffer |
| 110 | * @return object of IsIsPseudonode |
| 111 | */ |
| 112 | public static IsIsPseudonode read(ChannelBuffer cb) { |
Priyanka B | e366178 | 2015-11-22 17:52:54 +0530 | [diff] [blame] | 113 | byte[] isoNodeID = new byte[LENGTH - 1]; |
| 114 | cb.readBytes(isoNodeID); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 115 | byte psnIdentifier = cb.readByte(); |
| 116 | return IsIsPseudonode.of(isoNodeID, psnIdentifier); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public short getType() { |
| 121 | return TYPE; |
| 122 | } |
| 123 | |
| 124 | @Override |
Priyanka B | 0204073 | 2015-11-29 11:30:29 +0530 | [diff] [blame] | 125 | public int compareTo(Object o) { |
| 126 | if (this.equals(o)) { |
| 127 | return 0; |
| 128 | } |
| 129 | ByteBuffer value1 = ByteBuffer.wrap(this.isoNodeID); |
| 130 | ByteBuffer value2 = ByteBuffer.wrap(((IsIsPseudonode) o).isoNodeID); |
| 131 | if (value1.compareTo(value2) != 0) { |
| 132 | return value1.compareTo(value2); |
| 133 | } |
| 134 | return ((Byte) (this.psnIdentifier)).compareTo((Byte) (((IsIsPseudonode) o).psnIdentifier)); |
| 135 | } |
| 136 | |
| 137 | @Override |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 138 | public String toString() { |
| 139 | return MoreObjects.toStringHelper(getClass()) |
| 140 | .add("Type", TYPE) |
| 141 | .add("Length", LENGTH) |
| 142 | .add("isoNodeID", isoNodeID) |
| 143 | .add("psnIdentifier", psnIdentifier) |
| 144 | .toString(); |
| 145 | } |
| 146 | } |