Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.bgpio.types; |
| 17 | |
Priyanka B | 01aefd3 | 2015-11-21 21:15:44 +0530 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.Iterator; |
| 20 | import java.util.List; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
| 23 | import org.jboss.netty.buffer.ChannelBuffer; |
| 24 | import org.onosproject.bgpio.protocol.IGPRouterID; |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import com.google.common.base.MoreObjects; |
| 29 | |
| 30 | /** |
| 31 | * Provides implementation of IsIsPseudonode Tlv. |
| 32 | */ |
| 33 | public class IsIsPseudonode implements IGPRouterID, BGPValueType { |
Priyanka B | 01aefd3 | 2015-11-21 21:15:44 +0530 | [diff] [blame] | 34 | private static final Logger log = LoggerFactory.getLogger(IsIsPseudonode.class); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 35 | |
| 36 | public static final short TYPE = 515; |
| 37 | public static final short LENGTH = 7; |
| 38 | |
Priyanka B | 01aefd3 | 2015-11-21 21:15:44 +0530 | [diff] [blame] | 39 | private final List<Byte> isoNodeID; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 40 | private byte psnIdentifier; |
| 41 | |
| 42 | /** |
| 43 | * Constructor to initialize isoNodeID. |
| 44 | * |
| 45 | * @param isoNodeID ISO system-ID |
| 46 | * @param psnIdentifier PSN identifier |
| 47 | */ |
Priyanka B | 01aefd3 | 2015-11-21 21:15:44 +0530 | [diff] [blame] | 48 | public IsIsPseudonode(List<Byte> isoNodeID, byte psnIdentifier) { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 49 | 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 B | 01aefd3 | 2015-11-21 21:15:44 +0530 | [diff] [blame] | 60 | public static IsIsPseudonode of(final List<Byte> isoNodeID, |
| 61 | final byte psnIdentifier) { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 62 | return new IsIsPseudonode(isoNodeID, psnIdentifier); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns ISO NodeID. |
| 67 | * |
| 68 | * @return ISO NodeID |
| 69 | */ |
Priyanka B | 01aefd3 | 2015-11-21 21:15:44 +0530 | [diff] [blame] | 70 | public List<Byte> getISONodeID() { |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 71 | 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 B | 01aefd3 | 2015-11-21 21:15:44 +0530 | [diff] [blame] | 85 | return Objects.hash(isoNodeID) & Objects.hash(psnIdentifier); |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean equals(Object obj) { |
| 90 | if (this == obj) { |
| 91 | return true; |
| 92 | } |
| 93 | if (obj instanceof IsIsPseudonode) { |
Priyanka B | 01aefd3 | 2015-11-21 21:15:44 +0530 | [diff] [blame] | 94 | int countObjSubTlv = 0; |
| 95 | int countOtherSubTlv = 0; |
| 96 | boolean isCommonSubTlv = true; |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 97 | IsIsPseudonode other = (IsIsPseudonode) obj; |
Priyanka B | 01aefd3 | 2015-11-21 21:15:44 +0530 | [diff] [blame] | 98 | 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 B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 115 | } |
| 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 B | 01aefd3 | 2015-11-21 21:15:44 +0530 | [diff] [blame] | 124 | Iterator<Byte> objListIterator = isoNodeID.iterator(); |
| 125 | while (objListIterator.hasNext()) { |
| 126 | byte value = objListIterator.next(); |
| 127 | c.writeByte(value); |
| 128 | } |
Priyanka B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 129 | 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 B | 01aefd3 | 2015-11-21 21:15:44 +0530 | [diff] [blame] | 140 | 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 B | b2988fa | 2015-10-09 12:45:36 +0530 | [diff] [blame] | 146 | 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 | } |