Chidambar babu | 86b3b1a | 2016-02-16 17:39:52 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Chidambar babu | 86b3b1a | 2016-02-16 17:39:52 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.ospf.protocol.lsa; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.base.Objects; |
| 21 | import com.google.common.primitives.Bytes; |
| 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onosproject.ospf.controller.OspfLsa; |
| 24 | import org.onosproject.ospf.controller.OspfLsaType; |
| 25 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import java.net.InetAddress; |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * Defines the LSA header, fields and the methods to access them. |
| 35 | */ |
| 36 | public class LsaHeader implements OspfLsa { |
| 37 | |
| 38 | /* |
| 39 | 0 1 2 3 |
| 40 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 42 | | LS age | Options | LS type | |
| 43 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 44 | | Link State ID | |
| 45 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 46 | | Advertising Router | |
| 47 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 48 | | LS sequence number | |
| 49 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 50 | | LS checksum | length | |
| 51 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 52 | LSA header format |
| 53 | REFERENCE : RFC 2328 |
| 54 | */ |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame^] | 55 | public static final Logger log = LoggerFactory.getLogger(LsaHeader.class); |
Chidambar babu | 86b3b1a | 2016-02-16 17:39:52 +0530 | [diff] [blame] | 56 | private int age; |
| 57 | private int options; |
| 58 | private int lsType; |
| 59 | private long lsSequenceNo; |
| 60 | private int lsCheckSum; |
| 61 | private int lsPacketLen; |
| 62 | private String linkStateId; |
| 63 | private Ip4Address advertisingRouter; |
| 64 | |
| 65 | /** |
| 66 | * Gets LSA age. |
| 67 | * |
| 68 | * @return LSA age |
| 69 | */ |
| 70 | public int age() { |
| 71 | return age; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Sets LSA age. |
| 76 | * |
| 77 | * @param age LSA age |
| 78 | */ |
| 79 | public void setAge(int age) { |
| 80 | this.age = age; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Gets options value. |
| 85 | * |
| 86 | * @return options header value |
| 87 | */ |
| 88 | public int options() { |
| 89 | return options; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Sets options header value. |
| 94 | * |
| 95 | * @param options header value |
| 96 | */ |
| 97 | public void setOptions(int options) { |
| 98 | this.options = options; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Gets LSA type. |
| 103 | * |
| 104 | * @return LSA type |
| 105 | */ |
| 106 | public int lsType() { |
| 107 | return lsType; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Sets LSA type. |
| 112 | * |
| 113 | * @param lsType LSA type |
| 114 | */ |
| 115 | public void setLsType(int lsType) { |
| 116 | this.lsType = lsType; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Gets link state id. |
| 121 | * |
| 122 | * @return linkStateId link state id |
| 123 | */ |
| 124 | public String linkStateId() { |
| 125 | return linkStateId; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Sets link state id. |
| 130 | * |
| 131 | * @param linkStateId link state id |
| 132 | */ |
| 133 | public void setLinkStateId(String linkStateId) { |
| 134 | this.linkStateId = linkStateId; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Gets advertising router IP. |
| 139 | * |
| 140 | * @return advertising router |
| 141 | */ |
| 142 | public Ip4Address advertisingRouter() { |
| 143 | return advertisingRouter; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Sets advertising router. |
| 148 | * |
| 149 | * @param advertisingRouter advertising router |
| 150 | */ |
| 151 | public void setAdvertisingRouter(Ip4Address advertisingRouter) { |
| 152 | this.advertisingRouter = advertisingRouter; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Gets LSA sequence number. |
| 157 | * |
| 158 | * @return LSA sequence number |
| 159 | */ |
| 160 | public long lsSequenceNo() { |
| 161 | return lsSequenceNo; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Sets LSA sequence number. |
| 166 | * |
| 167 | * @param lsSequenceNo LSA sequence number |
| 168 | */ |
| 169 | public void setLsSequenceNo(long lsSequenceNo) { |
| 170 | this.lsSequenceNo = lsSequenceNo; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Gets LSA check sum. |
| 175 | * |
| 176 | * @return lsCheckSum LSA checksum |
| 177 | */ |
| 178 | public int lsCheckSum() { |
| 179 | return lsCheckSum; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Sets LSA checksum. |
| 184 | * |
| 185 | * @param lsCheckSum LSA checksum |
| 186 | */ |
| 187 | public void setLsCheckSum(int lsCheckSum) { |
| 188 | this.lsCheckSum = lsCheckSum; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Gets lsa packet length. |
| 193 | * |
| 194 | * @return lsPacketLen LSA packet length |
| 195 | */ |
| 196 | public int lsPacketLen() { |
| 197 | return lsPacketLen; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Sets LSA packet length. |
| 202 | * |
| 203 | * @param lsPacketLen LSA packet length |
| 204 | */ |
| 205 | public void setLsPacketLen(int lsPacketLen) { |
| 206 | this.lsPacketLen = lsPacketLen; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public OspfLsaType getOspfLsaType() { |
| 211 | if (lsType == OspfLsaType.ROUTER.value()) { |
| 212 | return OspfLsaType.ROUTER; |
| 213 | } else if (lsType == OspfLsaType.NETWORK.value()) { |
| 214 | return OspfLsaType.NETWORK; |
| 215 | } else if (lsType == OspfLsaType.SUMMARY.value()) { |
| 216 | return OspfLsaType.SUMMARY; |
| 217 | } else if (lsType == OspfLsaType.ASBR_SUMMARY.value()) { |
| 218 | return OspfLsaType.ASBR_SUMMARY; |
| 219 | } else if (lsType == OspfLsaType.EXTERNAL_LSA.value()) { |
| 220 | return OspfLsaType.EXTERNAL_LSA; |
| 221 | } else if (lsType == OspfLsaType.LINK_LOCAL_OPAQUE_LSA.value()) { |
| 222 | return OspfLsaType.LINK_LOCAL_OPAQUE_LSA; |
| 223 | } else if (lsType == OspfLsaType.AREA_LOCAL_OPAQUE_LSA.value()) { |
| 224 | return OspfLsaType.AREA_LOCAL_OPAQUE_LSA; |
| 225 | } else if (lsType == OspfLsaType.AS_OPAQUE_LSA.value()) { |
| 226 | return OspfLsaType.AS_OPAQUE_LSA; |
| 227 | } |
| 228 | |
| 229 | return OspfLsaType.UNDEFINED; |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public OspfLsa lsaHeader() { |
| 234 | return this; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Gets the LSA header as bytes. |
| 239 | * |
| 240 | * @return LSA header as bytes |
| 241 | */ |
| 242 | public byte[] getLsaHeaderAsByteArray() { |
| 243 | List<Byte> headerLst = new ArrayList<>(); |
| 244 | try { |
| 245 | headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.age()))); |
| 246 | headerLst.add((byte) this.options()); |
| 247 | headerLst.add((byte) this.lsType()); |
| 248 | headerLst.addAll(Bytes.asList(InetAddress.getByName(this.linkStateId()).getAddress())); |
| 249 | headerLst.addAll(Bytes.asList(this.advertisingRouter().toOctets())); |
| 250 | headerLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.lsSequenceNo()))); |
| 251 | headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsCheckSum()))); |
| 252 | headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsPacketLen()))); |
| 253 | } catch (Exception e) { |
| 254 | log.debug("Error::getLsaHeaderAsByteArray {}", e.getMessage()); |
| 255 | return Bytes.toArray(headerLst); |
| 256 | } |
| 257 | return Bytes.toArray(headerLst); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Populates the header from the LSA header instance. |
| 262 | * |
| 263 | * @param lsaHeader LSA header instance |
| 264 | */ |
| 265 | public void populateHeader(LsaHeader lsaHeader) { |
| 266 | //assign all the header values |
| 267 | this.setAge(lsaHeader.age()); |
| 268 | this.setOptions(lsaHeader.options()); |
| 269 | this.setLsType(lsaHeader.lsType()); |
| 270 | this.setLinkStateId(lsaHeader.linkStateId()); |
| 271 | this.setAdvertisingRouter(lsaHeader.advertisingRouter()); |
| 272 | this.setLsSequenceNo(lsaHeader.lsSequenceNo()); |
| 273 | this.setLsCheckSum(lsaHeader.lsCheckSum()); |
| 274 | this.setLsPacketLen(lsaHeader.lsPacketLen()); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public boolean equals(Object o) { |
| 279 | if (this == o) { |
| 280 | return true; |
| 281 | } |
| 282 | if (o == null || getClass() != o.getClass()) { |
| 283 | return false; |
| 284 | } |
| 285 | LsaHeader that = (LsaHeader) o; |
| 286 | return Objects.equal(age, that.age) && |
| 287 | Objects.equal(options, that.options) && |
| 288 | Objects.equal(lsType, that.lsType) && |
| 289 | Objects.equal(lsSequenceNo, that.lsSequenceNo) && |
| 290 | Objects.equal(lsCheckSum, that.lsCheckSum) && |
| 291 | Objects.equal(lsPacketLen, that.lsPacketLen) && |
| 292 | Objects.equal(linkStateId, that.linkStateId) && |
| 293 | Objects.equal(advertisingRouter, that.advertisingRouter); |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public int hashCode() { |
| 298 | return Objects.hashCode(age, options, lsType, lsSequenceNo, lsCheckSum, |
| 299 | lsPacketLen, linkStateId, advertisingRouter); |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public String toString() { |
| 304 | return MoreObjects.toStringHelper(getClass()) |
| 305 | .omitNullValues() |
| 306 | .add("age", age) |
| 307 | .add("options", options) |
| 308 | .add("lsType", lsType) |
| 309 | .add("lsSequenceNo", lsSequenceNo) |
| 310 | .add("lsCheckSum", lsCheckSum) |
| 311 | .add("lsPacketLen", lsPacketLen) |
| 312 | .add("linkStateId;", linkStateId) |
| 313 | .add("advertisingRouter", advertisingRouter) |
| 314 | .toString(); |
| 315 | } |
| 316 | } |