Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +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.ospfpacket; |
| 18 | |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.onlab.packet.Ip4Address; |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 21 | import org.onosproject.ospf.controller.OspfMessage; |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 22 | import org.onosproject.ospf.exceptions.OspfErrorType; |
| 23 | import org.onosproject.ospf.exceptions.OspfParseException; |
| 24 | import org.onosproject.ospf.protocol.ospfpacket.types.DdPacket; |
| 25 | import org.onosproject.ospf.protocol.ospfpacket.types.HelloPacket; |
| 26 | import org.onosproject.ospf.protocol.ospfpacket.types.LsAcknowledge; |
| 27 | import org.onosproject.ospf.protocol.ospfpacket.types.LsRequest; |
| 28 | import org.onosproject.ospf.protocol.ospfpacket.types.LsUpdate; |
| 29 | import org.onosproject.ospf.protocol.util.OspfParameters; |
| 30 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | /** |
| 35 | * A message reader which reads OSPF messages from ChannelBuffer and converts to OspfMessage instances. |
| 36 | */ |
| 37 | public class OspfMessageReader { |
| 38 | private static final Logger log = LoggerFactory.getLogger(OspfMessageReader.class); |
| 39 | |
| 40 | /** |
| 41 | * Reads and Converts the channel buffer to OspfMessage instance. |
| 42 | * |
| 43 | * @param channelBuffer channel buffer instance. |
| 44 | * @return OSPF message instance. |
| 45 | * @throws Exception might throws exception while parsing buffer |
| 46 | */ |
| 47 | public OspfMessage readFromBuffer(ChannelBuffer channelBuffer) |
| 48 | throws Exception { |
| 49 | |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 50 | try { |
| 51 | OspfPacketHeader ospfHeader = getOspfHeader(channelBuffer); |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 52 | OspfMessage ospfMessage = null; |
| 53 | switch (ospfHeader.ospfType()) { |
| 54 | case OspfParameters.HELLO: |
| 55 | ospfMessage = new HelloPacket(ospfHeader); |
| 56 | break; |
| 57 | case OspfParameters.DD: |
| 58 | ospfMessage = new DdPacket(ospfHeader); |
| 59 | break; |
| 60 | case OspfParameters.LSREQUEST: |
| 61 | ospfMessage = new LsRequest(ospfHeader); |
| 62 | break; |
| 63 | case OspfParameters.LSUPDATE: |
| 64 | ospfMessage = new LsUpdate(ospfHeader); |
| 65 | break; |
| 66 | case OspfParameters.LSACK: |
| 67 | ospfMessage = new LsAcknowledge(ospfHeader); |
| 68 | break; |
| 69 | default: |
| 70 | log.debug("Message Reader[Decoder] - Unknown LSA type..!!!"); |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | if (ospfMessage != null) { |
| 75 | try { |
| 76 | log.debug("{} Received::Message Length :: {} ", ospfMessage.ospfMessageType(), |
| 77 | ospfHeader.ospfPacLength()); |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 78 | ospfMessage.readFrom(channelBuffer.readBytes(ospfHeader.ospfPacLength() - |
| 79 | OspfUtil.OSPF_HEADER_LENGTH)); |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 80 | } catch (Exception e) { |
| 81 | throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, |
| 82 | OspfErrorType.BAD_MESSAGE); |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
| 87 | return ospfMessage; |
| 88 | } catch (Exception e) { |
| 89 | throw new OspfParseException(OspfErrorType.OSPF_MESSAGE_ERROR, |
| 90 | OspfErrorType.BAD_MESSAGE); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Gets the OSPF packet Header. |
| 96 | * |
| 97 | * @param channelBuffer channel buffer instance. |
| 98 | * @return Ospf Header instance. |
| 99 | */ |
| 100 | private OspfPacketHeader getOspfHeader(ChannelBuffer channelBuffer) throws Exception { |
| 101 | OspfPacketHeader ospfPacketHeader = new OspfPacketHeader(); |
| 102 | |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 103 | // Determine OSPF version & Packet Type |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 104 | int version = channelBuffer.readByte(); //byte 1 is ospf version |
| 105 | int packetType = channelBuffer.readByte(); //byte 2 is ospf packet type |
| 106 | |
| 107 | // byte 3 & 4 combine is packet length. |
| 108 | int packetLength = channelBuffer.readShort(); |
| 109 | |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 110 | byte[] tempByteArray = new byte[OspfUtil.FOUR_BYTES]; |
| 111 | channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); |
| 112 | Ip4Address routerId = Ip4Address.valueOf(tempByteArray); |
| 113 | |
| 114 | tempByteArray = new byte[OspfUtil.FOUR_BYTES]; |
| 115 | channelBuffer.readBytes(tempByteArray, 0, OspfUtil.FOUR_BYTES); |
| 116 | Ip4Address areaId = Ip4Address.valueOf(tempByteArray); |
| 117 | |
| 118 | int checkSum = channelBuffer.readUnsignedShort(); |
| 119 | int auType = channelBuffer.readUnsignedShort(); |
| 120 | int authentication = (int) channelBuffer.readLong(); |
| 121 | |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 122 | ospfPacketHeader.setOspfVer(version); |
| 123 | ospfPacketHeader.setOspftype(packetType); |
| 124 | ospfPacketHeader.setOspfPacLength(packetLength); |
| 125 | ospfPacketHeader.setRouterId(routerId); |
| 126 | ospfPacketHeader.setAreaId(areaId); |
| 127 | ospfPacketHeader.setChecksum(checkSum); |
| 128 | ospfPacketHeader.setAuthType(auType); |
| 129 | ospfPacketHeader.setAuthentication(authentication); |
| 130 | |
| 131 | return ospfPacketHeader; |
| 132 | } |
| 133 | } |