blob: bbdbe5bb3b8ebd91dac2c04f5834baa6f8fb0127 [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 Be3661782015-11-22 17:52:54 +053018import java.util.Arrays;
Priyanka Bb2988fa2015-10-09 12:45:36 +053019import java.util.Objects;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.bgpio.protocol.IGPRouterID;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import com.google.common.base.MoreObjects;
27
28/**
29 * Provides implementation of IsIsPseudonode Tlv.
30 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053031public class IsIsPseudonode implements IGPRouterID, BgpValueType {
Priyanka B01aefd32015-11-21 21:15:44 +053032 private static final Logger log = LoggerFactory.getLogger(IsIsPseudonode.class);
Priyanka Bb2988fa2015-10-09 12:45:36 +053033
34 public static final short TYPE = 515;
35 public static final short LENGTH = 7;
36
Priyanka Be3661782015-11-22 17:52:54 +053037 private final byte[] isoNodeID;
Priyanka Bb2988fa2015-10-09 12:45:36 +053038 private byte psnIdentifier;
39
40 /**
41 * Constructor to initialize isoNodeID.
42 *
43 * @param isoNodeID ISO system-ID
44 * @param psnIdentifier PSN identifier
45 */
Priyanka Be3661782015-11-22 17:52:54 +053046 public IsIsPseudonode(byte[] isoNodeID, byte psnIdentifier) {
47 this.isoNodeID = Arrays.copyOf(isoNodeID, isoNodeID.length);
Priyanka Bb2988fa2015-10-09 12:45:36 +053048 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 Be3661782015-11-22 17:52:54 +053058 public static IsIsPseudonode of(final byte[] isoNodeID,
Priyanka B01aefd32015-11-21 21:15:44 +053059 final byte psnIdentifier) {
Priyanka Bb2988fa2015-10-09 12:45:36 +053060 return new IsIsPseudonode(isoNodeID, psnIdentifier);
61 }
62
63 /**
64 * Returns ISO NodeID.
65 *
66 * @return ISO NodeID
67 */
Priyanka Be3661782015-11-22 17:52:54 +053068 public byte[] getISONodeID() {
Priyanka Bb2988fa2015-10-09 12:45:36 +053069 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 Be3661782015-11-22 17:52:54 +053083 return Arrays.hashCode(isoNodeID) & Objects.hash(psnIdentifier);
Priyanka Bb2988fa2015-10-09 12:45:36 +053084 }
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 Be3661782015-11-22 17:52:54 +053093 return Arrays.equals(isoNodeID, other.isoNodeID)
94 && Objects.equals(psnIdentifier, other.psnIdentifier);
Priyanka Bb2988fa2015-10-09 12:45:36 +053095 }
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 Be3661782015-11-22 17:52:54 +0530104 c.writeBytes(isoNodeID, 0, LENGTH - 1);
Priyanka Bb2988fa2015-10-09 12:45:36 +0530105 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 Be3661782015-11-22 17:52:54 +0530116 byte[] isoNodeID = new byte[LENGTH - 1];
117 cb.readBytes(isoNodeID);
Priyanka Bb2988fa2015-10-09 12:45:36 +0530118 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}