blob: 35fd2493d2745434481b41e8fb392f741b2643a8 [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 B01aefd32015-11-21 21:15:44 +053018import java.util.ArrayList;
19import java.util.Iterator;
20import java.util.List;
Priyanka Bb2988fa2015-10-09 12:45:36 +053021import java.util.Objects;
22
23import org.jboss.netty.buffer.ChannelBuffer;
24import org.onosproject.bgpio.protocol.IGPRouterID;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import com.google.common.base.MoreObjects;
29
30/**
31 * Provides implementation of IsIsPseudonode Tlv.
32 */
33public class IsIsPseudonode implements IGPRouterID, BGPValueType {
Priyanka B01aefd32015-11-21 21:15:44 +053034 private static final Logger log = LoggerFactory.getLogger(IsIsPseudonode.class);
Priyanka Bb2988fa2015-10-09 12:45:36 +053035
36 public static final short TYPE = 515;
37 public static final short LENGTH = 7;
38
Priyanka B01aefd32015-11-21 21:15:44 +053039 private final List<Byte> isoNodeID;
Priyanka Bb2988fa2015-10-09 12:45:36 +053040 private byte psnIdentifier;
41
42 /**
43 * Constructor to initialize isoNodeID.
44 *
45 * @param isoNodeID ISO system-ID
46 * @param psnIdentifier PSN identifier
47 */
Priyanka B01aefd32015-11-21 21:15:44 +053048 public IsIsPseudonode(List<Byte> isoNodeID, byte psnIdentifier) {
Priyanka Bb2988fa2015-10-09 12:45:36 +053049 this.isoNodeID = isoNodeID;
50 this.psnIdentifier = psnIdentifier;
51 }
52
53 /**
54 * Returns object of this class with specified values.
55 *
56 * @param isoNodeID ISO system-ID
57 * @param psnIdentifier PSN identifier
58 * @return object of IsIsPseudonode
59 */
Priyanka B01aefd32015-11-21 21:15:44 +053060 public static IsIsPseudonode of(final List<Byte> isoNodeID,
61 final byte psnIdentifier) {
Priyanka Bb2988fa2015-10-09 12:45:36 +053062 return new IsIsPseudonode(isoNodeID, psnIdentifier);
63 }
64
65 /**
66 * Returns ISO NodeID.
67 *
68 * @return ISO NodeID
69 */
Priyanka B01aefd32015-11-21 21:15:44 +053070 public List<Byte> getISONodeID() {
Priyanka Bb2988fa2015-10-09 12:45:36 +053071 return isoNodeID;
72 }
73
74 /**
75 * Returns PSN Identifier.
76 *
77 * @return PSN Identifier
78 */
79 public byte getPSNIdentifier() {
80 return this.psnIdentifier;
81 }
82
83 @Override
84 public int hashCode() {
Priyanka B01aefd32015-11-21 21:15:44 +053085 return Objects.hash(isoNodeID) & Objects.hash(psnIdentifier);
Priyanka Bb2988fa2015-10-09 12:45:36 +053086 }
87
88 @Override
89 public boolean equals(Object obj) {
90 if (this == obj) {
91 return true;
92 }
93 if (obj instanceof IsIsPseudonode) {
Priyanka B01aefd32015-11-21 21:15:44 +053094 int countObjSubTlv = 0;
95 int countOtherSubTlv = 0;
96 boolean isCommonSubTlv = true;
Priyanka Bb2988fa2015-10-09 12:45:36 +053097 IsIsPseudonode other = (IsIsPseudonode) obj;
Priyanka B01aefd32015-11-21 21:15:44 +053098 Iterator<Byte> objListIterator = other.isoNodeID.iterator();
99 countOtherSubTlv = other.isoNodeID.size();
100 countObjSubTlv = isoNodeID.size();
101 if (countObjSubTlv != countOtherSubTlv) {
102 return false;
103 } else {
104 while (objListIterator.hasNext() && isCommonSubTlv) {
105 Byte subTlv = objListIterator.next();
106 if (isoNodeID.contains(subTlv) && other.isoNodeID.contains(subTlv)) {
107 isCommonSubTlv = Objects.equals(isoNodeID.get(isoNodeID.indexOf(subTlv)),
108 other.isoNodeID.get(other.isoNodeID.indexOf(subTlv)));
109 } else {
110 isCommonSubTlv = false;
111 }
112 }
113 return isCommonSubTlv && Objects.equals(psnIdentifier, other.psnIdentifier);
114 }
Priyanka Bb2988fa2015-10-09 12:45:36 +0530115 }
116 return false;
117 }
118
119 @Override
120 public int write(ChannelBuffer c) {
121 int iLenStartIndex = c.writerIndex();
122 c.writeShort(TYPE);
123 c.writeShort(LENGTH);
Priyanka B01aefd32015-11-21 21:15:44 +0530124 Iterator<Byte> objListIterator = isoNodeID.iterator();
125 while (objListIterator.hasNext()) {
126 byte value = objListIterator.next();
127 c.writeByte(value);
128 }
Priyanka Bb2988fa2015-10-09 12:45:36 +0530129 c.writeByte(psnIdentifier);
130 return c.writerIndex() - iLenStartIndex;
131 }
132
133 /**
134 * Reads the channel buffer and returns object of IsIsPseudonode.
135 *
136 * @param cb ChannelBuffer
137 * @return object of IsIsPseudonode
138 */
139 public static IsIsPseudonode read(ChannelBuffer cb) {
Priyanka B01aefd32015-11-21 21:15:44 +0530140 List<Byte> isoNodeID = new ArrayList<Byte>();
141 byte value;
142 for (int i = 0; i < LENGTH; i++) {
143 value = cb.readByte();
144 isoNodeID.add(value);
145 }
Priyanka Bb2988fa2015-10-09 12:45:36 +0530146 byte psnIdentifier = cb.readByte();
147 return IsIsPseudonode.of(isoNodeID, psnIdentifier);
148 }
149
150 @Override
151 public short getType() {
152 return TYPE;
153 }
154
155 @Override
156 public String toString() {
157 return MoreObjects.toStringHelper(getClass())
158 .add("Type", TYPE)
159 .add("Length", LENGTH)
160 .add("isoNodeID", isoNodeID)
161 .add("psnIdentifier", psnIdentifier)
162 .toString();
163 }
164}