Chidambar babu | 86b3b1a | 2016-02-16 17:39:52 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 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.ospf.protocol.ospfpacket; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onosproject.ospf.exceptions.OspfParseException; |
| 22 | import org.onosproject.ospf.protocol.util.OspfPacketType; |
| 23 | |
| 24 | /** |
| 25 | * Defines the OSPF Packet Header, fields and access methods. |
| 26 | * Every OSPF packet starts with a standard 24 byte header. |
| 27 | * This header contains all the information necessary to determine whether |
| 28 | * the packet should be accepted for further processing |
| 29 | */ |
| 30 | public class OspfPacketHeader implements OspfMessage { |
| 31 | |
| 32 | /* |
| 33 | 0 1 2 3 |
| 34 | 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 |
| 35 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 36 | | Version # | Type | Packet length | |
| 37 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 38 | | Router ID | |
| 39 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 40 | | Area ID | |
| 41 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 42 | | Checksum | AuType | |
| 43 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 44 | | Authentication | |
| 45 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 46 | | Authentication | |
| 47 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 48 | */ |
| 49 | |
| 50 | private int ospfVer; |
| 51 | private int ospfType; |
| 52 | private int ospfPackLength; |
| 53 | private Ip4Address routerId; |
| 54 | private Ip4Address areaId; |
| 55 | private int checkSum; |
| 56 | private int auType; |
| 57 | private int authentication; |
| 58 | private Ip4Address destinationIp; |
| 59 | private Ip4Address sourceIp; |
| 60 | |
| 61 | /** |
| 62 | * Gets the source IP. |
| 63 | * |
| 64 | * @return source IP address |
| 65 | */ |
| 66 | public Ip4Address sourceIp() { |
| 67 | return sourceIp; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Sets the source IP address. |
| 72 | * |
| 73 | * @param sourceIp source IP address |
| 74 | */ |
| 75 | public void setSourceIp(Ip4Address sourceIp) { |
| 76 | this.sourceIp = sourceIp; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public OspfPacketType ospfMessageType() { |
| 81 | //default impl |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void readFrom(ChannelBuffer channelBuffer) throws OspfParseException { |
| 87 | //default impl |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public byte[] asBytes() { |
| 92 | //default impl |
| 93 | return new byte[0]; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Gets OSPF version. |
| 98 | * |
| 99 | * @return OSPF version |
| 100 | */ |
| 101 | public int ospfVersion() { |
| 102 | return ospfVer; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Sets OSPF version. |
| 107 | * |
| 108 | * @param ospfVer OSPF version |
| 109 | */ |
| 110 | public void setOspfVer(int ospfVer) { |
| 111 | this.ospfVer = ospfVer; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Gets OSPF packet type. |
| 116 | * |
| 117 | * @return OSPF packet type |
| 118 | */ |
| 119 | public int ospfType() { |
| 120 | return ospfType; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Sets OSPF packet type. |
| 125 | * |
| 126 | * @param ospfType packet type |
| 127 | */ |
| 128 | public void setOspftype(int ospfType) { |
| 129 | this.ospfType = ospfType; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Gets ospf packet length. |
| 134 | * |
| 135 | * @return OSPF packet length |
| 136 | */ |
| 137 | public int ospfPacLength() { |
| 138 | return ospfPackLength; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Sets OSPF packet length. |
| 143 | * |
| 144 | * @param ospfPacLength packet length |
| 145 | */ |
| 146 | public void setOspfPacLength(int ospfPacLength) { |
| 147 | this.ospfPackLength = ospfPacLength; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Gets router id. |
| 152 | * |
| 153 | * @return routerId |
| 154 | */ |
| 155 | public Ip4Address routerId() { |
| 156 | return routerId; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sets router id. |
| 161 | * |
| 162 | * @param routerId router id |
| 163 | */ |
| 164 | public void setRouterId(Ip4Address routerId) { |
| 165 | this.routerId = routerId; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Gets area id. |
| 170 | * |
| 171 | * @return areaId area id |
| 172 | */ |
| 173 | public Ip4Address areaId() { |
| 174 | return areaId; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Sets area id. |
| 179 | * |
| 180 | * @param areaId area id |
| 181 | */ |
| 182 | public void setAreaId(Ip4Address areaId) { |
| 183 | this.areaId = areaId; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Gets checksum value. |
| 188 | * |
| 189 | * @return checkSum check sum value |
| 190 | */ |
| 191 | public int checksum() { |
| 192 | return checkSum; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Sets checksum. |
| 197 | * |
| 198 | * @param checkSum check sum value |
| 199 | */ |
| 200 | public void setChecksum(int checkSum) { |
| 201 | this.checkSum = checkSum; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Gets auth type. |
| 206 | * |
| 207 | * @return authType authentication type |
| 208 | */ |
| 209 | public int authType() { |
| 210 | return auType; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Sets auth Type. |
| 215 | * |
| 216 | * @param auType authentication type |
| 217 | */ |
| 218 | public void setAuthType(int auType) { |
| 219 | this.auType = auType; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Gets authentication. |
| 224 | * |
| 225 | * @return authentication |
| 226 | */ |
| 227 | public int authentication() { |
| 228 | return authentication; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Sets authentication. |
| 233 | * |
| 234 | * @param authentication authentication |
| 235 | */ |
| 236 | public void setAuthentication(int authentication) { |
| 237 | this.authentication = authentication; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Gets destination IP. |
| 242 | * |
| 243 | * @return destination IP |
| 244 | */ |
| 245 | public Ip4Address destinationIp() { |
| 246 | return destinationIp; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Sets destination IP. |
| 251 | * |
| 252 | * @param destinationIp destination IP |
| 253 | */ |
| 254 | public void setDestinationIp(Ip4Address destinationIp) { |
| 255 | this.destinationIp = destinationIp; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Populates the header from the packetHeader instance. |
| 260 | * |
| 261 | * @param ospfPacketHeader packet header instance. |
| 262 | */ |
| 263 | public void populateHeader(OspfPacketHeader ospfPacketHeader) { |
| 264 | this.setSourceIp(ospfPacketHeader.sourceIp()); |
| 265 | this.setOspfVer(ospfPacketHeader.ospfVersion()); |
| 266 | this.setOspftype(ospfPacketHeader.ospfType()); |
| 267 | this.setOspfPacLength(ospfPacketHeader.ospfPacLength()); |
| 268 | this.setRouterId(ospfPacketHeader.routerId()); |
| 269 | this.setAreaId(ospfPacketHeader.areaId()); |
| 270 | this.setChecksum(ospfPacketHeader.checksum()); |
| 271 | this.setAuthType(ospfPacketHeader.authType()); |
| 272 | this.setAuthentication(ospfPacketHeader.authentication()); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public String toString() { |
| 277 | return MoreObjects.toStringHelper(getClass()) |
| 278 | .omitNullValues() |
| 279 | .add("ospfVersion", ospfVer) |
| 280 | .add("ospfType", ospfType) |
| 281 | .add("ospfPackLength", ospfPackLength) |
| 282 | .add("routerId", routerId) |
| 283 | .add("areaId", areaId) |
| 284 | .add("checkSum", checkSum) |
| 285 | .add("auType", auType) |
| 286 | .add("authentication", authentication) |
| 287 | .add("destinationIP", destinationIp) |
| 288 | .toString(); |
| 289 | } |
| 290 | } |