Shashikanth VH | 73b1277 | 2016-04-18 14:28:45 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 23 | import org.onosproject.bgpio.util.Validation; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Iterator; |
| 26 | import java.util.List; |
| 27 | import java.util.Objects; |
| 28 | |
| 29 | /** |
| 30 | * Provides implementation of BGP wide community IPV4 neighbour subtlv. |
| 31 | */ |
| 32 | public class WideCommunityIpV4Neighbour implements BgpValueType { |
| 33 | public static final byte TYPE = 8; |
| 34 | private List<IpV4Neighbour> ipv4Neighbour; |
| 35 | public static final byte IPV4_NEIGHBOUR_SIZE = 8; |
| 36 | |
| 37 | /** |
| 38 | * Creates an instance of wide community ipv4 neighbour subtlv. |
| 39 | * |
| 40 | */ |
| 41 | public WideCommunityIpV4Neighbour() { |
| 42 | this.ipv4Neighbour = new ArrayList<>(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Adds local and remote speakers. |
| 47 | * |
| 48 | * @param localSpeaker local speaker |
| 49 | * @param remoteSpeaker remote speaker |
| 50 | */ |
| 51 | public void add(IpAddress localSpeaker, IpAddress remoteSpeaker) { |
| 52 | ipv4Neighbour.add(new IpV4Neighbour(localSpeaker, remoteSpeaker)); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Deletes local and remote speakers. |
| 57 | * |
| 58 | * @param localSpeaker local speaker |
| 59 | * @param remoteSpeaker remote speaker |
| 60 | */ |
| 61 | public void remove(IpAddress localSpeaker, IpAddress remoteSpeaker) { |
| 62 | ipv4Neighbour.remove(new IpV4Neighbour(localSpeaker, remoteSpeaker)); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public int hashCode() { |
| 67 | return Objects.hash(ipv4Neighbour); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public boolean equals(Object obj) { |
| 72 | if (this == obj) { |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | if (obj == null) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | if (obj instanceof WideCommunityIpV4Neighbour) { |
| 81 | WideCommunityIpV4Neighbour other = (WideCommunityIpV4Neighbour) obj; |
| 82 | return Objects.equals(ipv4Neighbour, other.ipv4Neighbour); |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int write(ChannelBuffer c) { |
| 89 | int iLenStartIndex = c.writerIndex(); |
| 90 | |
| 91 | Iterator<IpV4Neighbour> listIterator = ipv4Neighbour.iterator(); |
| 92 | c.writeByte(TYPE); |
| 93 | |
| 94 | int iLengthIndex = c.writerIndex(); |
| 95 | c.writeShort(0); |
| 96 | |
| 97 | while (listIterator.hasNext()) { |
| 98 | IpV4Neighbour speaker = listIterator.next(); |
| 99 | if (speaker instanceof IpV4Neighbour) { |
Shashikanth VH | e38c21e | 2016-05-26 21:37:25 +0530 | [diff] [blame] | 100 | c.writeBytes(speaker.localSpeaker().toOctets()); |
| 101 | c.writeBytes(speaker.remoteSpeaker().toOctets()); |
Shashikanth VH | 73b1277 | 2016-04-18 14:28:45 +0530 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | int length = c.writerIndex() - iLengthIndex; |
| 106 | c.setShort(iLengthIndex, (short) (length - 2)); |
| 107 | |
| 108 | return c.writerIndex() - iLenStartIndex; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Reads the channel buffer and returns object of WideCommunityIpV4Neighbour. |
| 113 | * |
| 114 | * @param c ChannelBuffer |
| 115 | * @return object of WideCommunityIpV4Neighbour |
| 116 | * @throws BgpParseException on read error |
| 117 | */ |
| 118 | public static WideCommunityIpV4Neighbour read(ChannelBuffer c) throws BgpParseException { |
| 119 | WideCommunityIpV4Neighbour wideCommNeighbour = new WideCommunityIpV4Neighbour(); |
| 120 | short length; |
| 121 | |
| 122 | if (c.readableBytes() == 0) { |
| 123 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
| 124 | c.readableBytes()); |
| 125 | } |
| 126 | |
| 127 | length = c.readShort(); |
| 128 | if (c.readableBytes() == 0) { |
| 129 | return wideCommNeighbour; |
| 130 | } |
| 131 | |
| 132 | if (c.readableBytes() < length) { |
| 133 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
| 134 | c.readableBytes()); |
| 135 | } |
| 136 | |
| 137 | while (c.readableBytes() > 0) { |
| 138 | if (c.readableBytes() < IPV4_NEIGHBOUR_SIZE) { |
| 139 | Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, |
| 140 | c.readableBytes()); |
| 141 | } |
| 142 | |
| 143 | IpAddress localSpeaker = IpAddress.valueOf(c.readInt()); |
| 144 | IpAddress remoteSpeaker = IpAddress.valueOf(c.readInt()); |
| 145 | |
| 146 | wideCommNeighbour.add(localSpeaker, remoteSpeaker); |
| 147 | |
| 148 | } |
| 149 | |
| 150 | return wideCommNeighbour; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public String toString() { |
| 155 | return MoreObjects.toStringHelper(getClass()) |
| 156 | .add("ipv4Neighbour", ipv4Neighbour) |
| 157 | .toString(); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public int compareTo(Object o) { |
| 162 | // TODO Auto-generated method stub |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public short getType() { |
| 168 | // TODO Auto-generated method stub |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * IpV4Neighbour class contain remote and local speaker. |
| 174 | */ |
| 175 | private class IpV4Neighbour { |
| 176 | private IpAddress localSpeaker; |
| 177 | private IpAddress remoteSpeaker; |
| 178 | |
| 179 | /** |
| 180 | * Creates an instance of ipv4 neighbour. |
| 181 | * |
| 182 | * @param localSpeaker ip address of local speaker |
| 183 | * @param remoteSpeaker ip address of remote speaker |
| 184 | */ |
| 185 | public IpV4Neighbour(IpAddress localSpeaker, IpAddress remoteSpeaker) { |
| 186 | this.localSpeaker = localSpeaker; |
| 187 | this.remoteSpeaker = remoteSpeaker; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Returns IPV4 neighbour local speaker. |
| 192 | * |
| 193 | * @return IPV4 neighbour local speaker |
| 194 | */ |
| 195 | public IpAddress localSpeaker() { |
| 196 | return localSpeaker; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns IPV4 neighbour remote speaker. |
| 201 | * |
| 202 | * @return IPV4 neighbour remote speaker |
| 203 | */ |
| 204 | public IpAddress remoteSpeaker() { |
| 205 | return remoteSpeaker; |
| 206 | } |
| 207 | } |
| 208 | } |