Mani | cdb2641 | 2016-02-16 19:44:34 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Mani | cdb2641 | 2016-02-16 19:44:34 +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 | */ |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 16 | package org.onosproject.ospf.protocol.ospfpacket.types; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.primitives.Bytes; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onosproject.ospf.exceptions.OspfParseException; |
| 23 | import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader; |
| 24 | import org.onosproject.ospf.protocol.ospfpacket.subtype.LsRequestPacket; |
| 25 | import org.onosproject.ospf.protocol.util.OspfPacketType; |
| 26 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import java.net.InetAddress; |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.List; |
Mani | cdb2641 | 2016-02-16 19:44:34 +0530 | [diff] [blame] | 33 | |
| 34 | /** |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 35 | * Representation of an OSPF Link State Request packet. |
| 36 | * Link State Request packets are OSPF packet type 3. After exchanging |
| 37 | * database description packets with a neighboring router, a router may |
| 38 | * find that parts of its link-state database are out-of-date. The |
| 39 | * Link State Request packet is used to request the pieces of the |
| 40 | * neighbor's database that are more up-to-date. |
Mani | cdb2641 | 2016-02-16 19:44:34 +0530 | [diff] [blame] | 41 | */ |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 42 | public class LsRequest extends OspfPacketHeader { |
| 43 | /* |
| 44 | 0 1 2 3 |
| 45 | 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 |
| 46 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 47 | | Version # | 3 | Packet length | |
| 48 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 49 | | Router ID | |
| 50 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 51 | | Area ID | |
| 52 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 53 | | Checksum | AuType | |
| 54 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 55 | | Authentication | |
| 56 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 57 | | Authentication | |
| 58 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 59 | | LS type | |
| 60 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 61 | | Link State ID | |
| 62 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 63 | | Advertising Router | |
| 64 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 65 | | ... | |
Mani | cdb2641 | 2016-02-16 19:44:34 +0530 | [diff] [blame] | 66 | |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 67 | LsRequest Message Format |
| 68 | REFERENCE : RFC 2328 |
| 69 | */ |
| 70 | private static final Logger log = LoggerFactory.getLogger(LsRequest.class); |
| 71 | private List<LsRequestPacket> linkStateRequests = new ArrayList<>(); |
Mani | cdb2641 | 2016-02-16 19:44:34 +0530 | [diff] [blame] | 72 | |
| 73 | /** |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 74 | * Creates an instance of link state request packet. |
Mani | cdb2641 | 2016-02-16 19:44:34 +0530 | [diff] [blame] | 75 | */ |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 76 | public LsRequest() { |
Mani | cdb2641 | 2016-02-16 19:44:34 +0530 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 80 | * Creates an instance of link state request packet. |
Mani | cdb2641 | 2016-02-16 19:44:34 +0530 | [diff] [blame] | 81 | * |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 82 | * @param ospfHeader OSPF header instance. |
Mani | cdb2641 | 2016-02-16 19:44:34 +0530 | [diff] [blame] | 83 | */ |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 84 | public LsRequest(OspfPacketHeader ospfHeader) { |
| 85 | populateHeader(ospfHeader); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Adds link state request. |
| 90 | * |
| 91 | * @param lsRequestPacket ls request packet instance |
| 92 | */ |
| 93 | public void addLinkStateRequests(LsRequestPacket lsRequestPacket) { |
| 94 | if (!linkStateRequests.contains(lsRequestPacket)) { |
| 95 | linkStateRequests.add(lsRequestPacket); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Gets link state request packet instance. |
| 101 | * |
| 102 | * @return link state request packet instance |
| 103 | */ |
| 104 | public List<LsRequestPacket> getLinkStateRequests() { |
| 105 | return linkStateRequests; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public OspfPacketType ospfMessageType() { |
| 110 | return OspfPacketType.LSREQUEST; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException { |
| 115 | |
| 116 | while (channelBuffer.readableBytes() >= OspfUtil.LSREQUEST_LENGTH) { |
| 117 | LsRequestPacket lsRequestPacket = new LsRequestPacket(); |
| 118 | lsRequestPacket.setLsType(channelBuffer.readInt()); |
| 119 | byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; |
| 120 | channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); |
| 121 | lsRequestPacket.setLinkStateId(Ip4Address.valueOf(tempByteArray).toString()); |
| 122 | tempByteArray = new byte[OspfUtil.FOUR_BYTES]; |
| 123 | channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); |
| 124 | lsRequestPacket.setOwnRouterId(Ip4Address.valueOf(tempByteArray).toString()); |
| 125 | |
| 126 | this.addLinkStateRequests(lsRequestPacket); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public byte[] asBytes() { |
| 132 | byte[] lsrMessage = null; |
| 133 | byte[] lsrHeader = getLsrHeaderAsByteArray(); |
| 134 | byte[] lsrBody = getLsrBodyAsByteArray(); |
| 135 | lsrMessage = Bytes.concat(lsrHeader, lsrBody); |
| 136 | |
| 137 | return lsrMessage; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Gets LS request packet header as byte array. |
| 142 | * |
| 143 | * @return LS request packet header as byte array |
| 144 | */ |
| 145 | public byte[] getLsrHeaderAsByteArray() { |
| 146 | List<Byte> headerLst = new ArrayList<>(); |
| 147 | |
| 148 | try { |
| 149 | headerLst.add((byte) this.ospfVersion()); |
| 150 | headerLst.add((byte) this.ospfType()); |
| 151 | headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.ospfPacLength()))); |
| 152 | headerLst.addAll(Bytes.asList(this.routerId().toOctets())); |
| 153 | headerLst.addAll(Bytes.asList(this.areaId().toOctets())); |
| 154 | headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.checksum()))); |
| 155 | headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.authType()))); |
| 156 | //Authentication is 0 always. Total 8 bytes consist of zero |
| 157 | byte[] auth = new byte[OspfUtil.EIGHT_BYTES]; |
| 158 | headerLst.addAll(Bytes.asList(auth)); |
| 159 | } catch (Exception e) { |
| 160 | log.debug("Error::getLsrBodyAsByteArray {}", e.getMessage()); |
| 161 | return Bytes.toArray(headerLst); |
| 162 | } |
| 163 | |
| 164 | return Bytes.toArray(headerLst); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Gets LS request packet body as byte array. |
| 169 | * |
| 170 | * @return LS request packet body as byte array |
| 171 | */ |
| 172 | public byte[] getLsrBodyAsByteArray() { |
| 173 | List<Byte> bodyLst = new ArrayList<>(); |
| 174 | |
| 175 | try { |
| 176 | for (LsRequestPacket lsrPacket : linkStateRequests) { |
| 177 | bodyLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(lsrPacket.lsType()))); |
| 178 | bodyLst.addAll(Bytes.asList(InetAddress.getByName(lsrPacket.linkStateId()).getAddress())); |
| 179 | bodyLst.addAll(Bytes.asList(InetAddress.getByName(lsrPacket.ownRouterId()).getAddress())); |
| 180 | } |
| 181 | } catch (Exception e) { |
| 182 | log.debug("Error::getLsrBodyAsByteArray {}", e.getMessage()); |
| 183 | return Bytes.toArray(bodyLst); |
| 184 | } |
| 185 | |
| 186 | return Bytes.toArray(bodyLst); |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public String toString() { |
| 191 | return MoreObjects.toStringHelper(getClass()) |
| 192 | .omitNullValues() |
| 193 | .add("linkStateRequests", linkStateRequests) |
| 194 | .toString(); |
Mani | cdb2641 | 2016-02-16 19:44:34 +0530 | [diff] [blame] | 195 | } |
| 196 | } |