blob: 5c742d0f9acf2542f2c767ad1af2f44d71179ded [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
18import java.util.Objects;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.bgpio.protocol.IGPRouterID;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import com.google.common.base.MoreObjects;
26
27/**
28 * Provides implementation of IsIsPseudonode Tlv.
29 */
30public class IsIsPseudonode implements IGPRouterID, BGPValueType {
31 protected static final Logger log = LoggerFactory.getLogger(IsIsPseudonode.class);
32
33 public static final short TYPE = 515;
34 public static final short LENGTH = 7;
35
36 private final byte[] isoNodeID;
37 private byte psnIdentifier;
38
39 /**
40 * Constructor to initialize isoNodeID.
41 *
42 * @param isoNodeID ISO system-ID
43 * @param psnIdentifier PSN identifier
44 */
45 public IsIsPseudonode(byte[] isoNodeID, byte psnIdentifier) {
46 this.isoNodeID = isoNodeID;
47 this.psnIdentifier = psnIdentifier;
48 }
49
50 /**
51 * Returns object of this class with specified values.
52 *
53 * @param isoNodeID ISO system-ID
54 * @param psnIdentifier PSN identifier
55 * @return object of IsIsPseudonode
56 */
57 public static IsIsPseudonode of(final byte[] isoNodeID, final byte psnIdentifier) {
58 return new IsIsPseudonode(isoNodeID, psnIdentifier);
59 }
60
61 /**
62 * Returns ISO NodeID.
63 *
64 * @return ISO NodeID
65 */
66 public byte[] getISONodeID() {
67 return isoNodeID;
68 }
69
70 /**
71 * Returns PSN Identifier.
72 *
73 * @return PSN Identifier
74 */
75 public byte getPSNIdentifier() {
76 return this.psnIdentifier;
77 }
78
79 @Override
80 public int hashCode() {
81 return Objects.hash(isoNodeID, psnIdentifier);
82 }
83
84 @Override
85 public boolean equals(Object obj) {
86 if (this == obj) {
87 return true;
88 }
89 if (obj instanceof IsIsPseudonode) {
90 IsIsPseudonode other = (IsIsPseudonode) obj;
91 return Objects.equals(isoNodeID, other.isoNodeID) && Objects.equals(psnIdentifier, other.psnIdentifier);
92 }
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);
101 c.writeBytes(isoNodeID);
102 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) {
113 byte[] isoNodeID = new byte[LENGTH - 1];
114 cb.readBytes(isoNodeID, 0, LENGTH - 1);
115 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
125 public String toString() {
126 return MoreObjects.toStringHelper(getClass())
127 .add("Type", TYPE)
128 .add("Length", LENGTH)
129 .add("isoNodeID", isoNodeID)
130 .add("psnIdentifier", psnIdentifier)
131 .toString();
132 }
133}