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