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