blob: 75d061b7e7745d12bd56a820490cefb47dc6f950 [file] [log] [blame]
Priyanka Bb2988fa2015-10-09 12:45:36 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Priyanka Bb2988fa2015-10-09 12:45:36 +05303 *
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
Jonathan Hart51539b82015-10-29 09:53:04 -070018import com.google.common.base.MoreObjects;
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.onosproject.bgpio.protocol.IgpRouterId;
21
Priyanka B02040732015-11-29 11:30:29 +053022import java.nio.ByteBuffer;
Priyanka Be3661782015-11-22 17:52:54 +053023import java.util.Arrays;
Priyanka Bb2988fa2015-10-09 12:45:36 +053024import java.util.Objects;
25
Priyanka Bb2988fa2015-10-09 12:45:36 +053026/**
27 * Provides implementation of IsIsPseudonode Tlv.
28 */
Jonathan Hart51539b82015-10-29 09:53:04 -070029public class IsIsPseudonode implements IgpRouterId, BgpValueType {
Priyanka Bb2988fa2015-10-09 12:45:36 +053030 public static final short TYPE = 515;
31 public static final short LENGTH = 7;
32
Priyanka Be3661782015-11-22 17:52:54 +053033 private final byte[] isoNodeID;
Priyanka Bb2988fa2015-10-09 12:45:36 +053034 private byte psnIdentifier;
35
36 /**
37 * Constructor to initialize isoNodeID.
38 *
39 * @param isoNodeID ISO system-ID
40 * @param psnIdentifier PSN identifier
41 */
Priyanka Be3661782015-11-22 17:52:54 +053042 public IsIsPseudonode(byte[] isoNodeID, byte psnIdentifier) {
43 this.isoNodeID = Arrays.copyOf(isoNodeID, isoNodeID.length);
Priyanka Bb2988fa2015-10-09 12:45:36 +053044 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 Be3661782015-11-22 17:52:54 +053054 public static IsIsPseudonode of(final byte[] isoNodeID,
Priyanka B01aefd32015-11-21 21:15:44 +053055 final byte psnIdentifier) {
Priyanka Bb2988fa2015-10-09 12:45:36 +053056 return new IsIsPseudonode(isoNodeID, psnIdentifier);
57 }
58
59 /**
60 * Returns ISO NodeID.
61 *
62 * @return ISO NodeID
63 */
Jonathan Hart51539b82015-10-29 09:53:04 -070064 public byte[] getIsoNodeId() {
Priyanka Bb2988fa2015-10-09 12:45:36 +053065 return isoNodeID;
66 }
67
68 /**
69 * Returns PSN Identifier.
70 *
71 * @return PSN Identifier
72 */
Jonathan Hart51539b82015-10-29 09:53:04 -070073 public byte getPsnIdentifier() {
Priyanka Bb2988fa2015-10-09 12:45:36 +053074 return this.psnIdentifier;
75 }
76
77 @Override
78 public int hashCode() {
Priyanka Be3661782015-11-22 17:52:54 +053079 return Arrays.hashCode(isoNodeID) & Objects.hash(psnIdentifier);
Priyanka Bb2988fa2015-10-09 12:45:36 +053080 }
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 Be3661782015-11-22 17:52:54 +053089 return Arrays.equals(isoNodeID, other.isoNodeID)
90 && Objects.equals(psnIdentifier, other.psnIdentifier);
Priyanka Bb2988fa2015-10-09 12:45:36 +053091 }
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 Be3661782015-11-22 17:52:54 +0530100 c.writeBytes(isoNodeID, 0, LENGTH - 1);
Priyanka Bb2988fa2015-10-09 12:45:36 +0530101 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 Be3661782015-11-22 17:52:54 +0530112 byte[] isoNodeID = new byte[LENGTH - 1];
113 cb.readBytes(isoNodeID);
Priyanka Bb2988fa2015-10-09 12:45:36 +0530114 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 B02040732015-11-29 11:30:29 +0530124 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 Bb2988fa2015-10-09 12:45:36 +0530137 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 Hart51539b82015-10-29 09:53:04 -0700145}