blob: 094c4382fd173003115d9c8aca9272f08c59b7d4 [file] [log] [blame]
Priyanka Bb2988fa2015-10-09 12:45:36 +05301/*
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 */
16package org.onosproject.bgpio.types;
17
Priyanka B02040732015-11-29 11:30:29 +053018import java.nio.ByteBuffer;
Priyanka Be3661782015-11-22 17:52:54 +053019import java.util.Arrays;
Priyanka Bb2988fa2015-10-09 12:45:36 +053020import java.util.Objects;
21
22import org.jboss.netty.buffer.ChannelBuffer;
23import org.onosproject.bgpio.protocol.IGPRouterID;
Priyanka Bb2988fa2015-10-09 12:45:36 +053024
25import com.google.common.base.MoreObjects;
26
27/**
28 * Provides implementation of IsIsPseudonode Tlv.
29 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053030public class IsIsPseudonode implements IGPRouterID, BgpValueType {
Priyanka Bb2988fa2015-10-09 12:45:36 +053031 public static final short TYPE = 515;
32 public static final short LENGTH = 7;
33
Priyanka Be3661782015-11-22 17:52:54 +053034 private final byte[] isoNodeID;
Priyanka Bb2988fa2015-10-09 12:45:36 +053035 private byte psnIdentifier;
36
37 /**
38 * Constructor to initialize isoNodeID.
39 *
40 * @param isoNodeID ISO system-ID
41 * @param psnIdentifier PSN identifier
42 */
Priyanka Be3661782015-11-22 17:52:54 +053043 public IsIsPseudonode(byte[] isoNodeID, byte psnIdentifier) {
44 this.isoNodeID = Arrays.copyOf(isoNodeID, isoNodeID.length);
Priyanka Bb2988fa2015-10-09 12:45:36 +053045 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 Be3661782015-11-22 17:52:54 +053055 public static IsIsPseudonode of(final byte[] isoNodeID,
Priyanka B01aefd32015-11-21 21:15:44 +053056 final byte psnIdentifier) {
Priyanka Bb2988fa2015-10-09 12:45:36 +053057 return new IsIsPseudonode(isoNodeID, psnIdentifier);
58 }
59
60 /**
61 * Returns ISO NodeID.
62 *
63 * @return ISO NodeID
64 */
Priyanka Be3661782015-11-22 17:52:54 +053065 public byte[] getISONodeID() {
Priyanka Bb2988fa2015-10-09 12:45:36 +053066 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 Be3661782015-11-22 17:52:54 +053080 return Arrays.hashCode(isoNodeID) & Objects.hash(psnIdentifier);
Priyanka Bb2988fa2015-10-09 12:45:36 +053081 }
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 Be3661782015-11-22 17:52:54 +053090 return Arrays.equals(isoNodeID, other.isoNodeID)
91 && Objects.equals(psnIdentifier, other.psnIdentifier);
Priyanka Bb2988fa2015-10-09 12:45:36 +053092 }
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 Be3661782015-11-22 17:52:54 +0530101 c.writeBytes(isoNodeID, 0, LENGTH - 1);
Priyanka Bb2988fa2015-10-09 12:45:36 +0530102 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 Be3661782015-11-22 17:52:54 +0530113 byte[] isoNodeID = new byte[LENGTH - 1];
114 cb.readBytes(isoNodeID);
Priyanka Bb2988fa2015-10-09 12:45:36 +0530115 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 B02040732015-11-29 11:30:29 +0530125 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 Bb2988fa2015-10-09 12:45:36 +0530138 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}