blob: 630edacd1cabeae72beb850f942e5a9321b8efd8 [file] [log] [blame]
Manicdb26412016-02-16 19:44:34 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Manicdb26412016-02-16 19:44:34 +05303 *
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 vkaa48da82016-03-02 23:17:06 +053016package org.onosproject.ospf.protocol.ospfpacket.types;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.primitives.Bytes;
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onlab.packet.Ip4Address;
22import org.onosproject.ospf.exceptions.OspfParseException;
23import org.onosproject.ospf.protocol.ospfpacket.OspfPacketHeader;
24import org.onosproject.ospf.protocol.ospfpacket.subtype.LsRequestPacket;
25import org.onosproject.ospf.protocol.util.OspfPacketType;
26import org.onosproject.ospf.protocol.util.OspfUtil;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.net.InetAddress;
31import java.util.ArrayList;
32import java.util.List;
Manicdb26412016-02-16 19:44:34 +053033
34/**
sunish vkaa48da82016-03-02 23:17:06 +053035 * 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.
Manicdb26412016-02-16 19:44:34 +053041 */
sunish vkaa48da82016-03-02 23:17:06 +053042public 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 | ... |
Manicdb26412016-02-16 19:44:34 +053066
sunish vkaa48da82016-03-02 23:17:06 +053067 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<>();
Manicdb26412016-02-16 19:44:34 +053072
73 /**
sunish vkaa48da82016-03-02 23:17:06 +053074 * Creates an instance of link state request packet.
Manicdb26412016-02-16 19:44:34 +053075 */
sunish vkaa48da82016-03-02 23:17:06 +053076 public LsRequest() {
Manicdb26412016-02-16 19:44:34 +053077 }
78
79 /**
sunish vkaa48da82016-03-02 23:17:06 +053080 * Creates an instance of link state request packet.
Manicdb26412016-02-16 19:44:34 +053081 *
sunish vkaa48da82016-03-02 23:17:06 +053082 * @param ospfHeader OSPF header instance.
Manicdb26412016-02-16 19:44:34 +053083 */
sunish vkaa48da82016-03-02 23:17:06 +053084 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();
Manicdb26412016-02-16 19:44:34 +0530195 }
196}