Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +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 | package org.onosproject.isis.io.isispacket; |
| 17 | |
| 18 | import org.jboss.netty.buffer.ChannelBuffer; |
| 19 | import org.onosproject.isis.controller.IsisMessage; |
| 20 | import org.onosproject.isis.exceptions.IsisErrorType; |
| 21 | import org.onosproject.isis.exceptions.IsisParseException; |
| 22 | import org.onosproject.isis.io.isispacket.pdu.Csnp; |
| 23 | import org.onosproject.isis.io.isispacket.pdu.L1L2HelloPdu; |
| 24 | import org.onosproject.isis.io.isispacket.pdu.LsPdu; |
| 25 | import org.onosproject.isis.io.isispacket.pdu.P2PHelloPdu; |
| 26 | import org.onosproject.isis.io.isispacket.pdu.Psnp; |
| 27 | import org.onosproject.isis.io.util.IsisConstants; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | /** |
| 32 | * Represents ISIS message reader. |
| 33 | */ |
| 34 | public class IsisMessageReader { |
| 35 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 36 | private static final Logger log = LoggerFactory.getLogger(IsisMessageReader.class); |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Reads from ISIS packet from buffer. |
| 40 | * |
| 41 | * @param channelBuffer buffer |
| 42 | * @return ISIS message |
Ray Milkey | 2b4958a | 2018-02-06 18:59:06 -0800 | [diff] [blame^] | 43 | * @throws IsisParseException exception |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 44 | */ |
Ray Milkey | 2b4958a | 2018-02-06 18:59:06 -0800 | [diff] [blame^] | 45 | public IsisMessage readFromBuffer(ChannelBuffer channelBuffer) throws IsisParseException { |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 46 | |
| 47 | int dataLength = channelBuffer.readableBytes(); |
| 48 | log.debug("IsisMessageReader::readFromBuffer Data length {}", dataLength); |
| 49 | if (channelBuffer.readableBytes() < IsisConstants.PDU_LENGTH) { |
| 50 | log.debug("Packet should have minimum length..."); |
| 51 | throw new IsisParseException(IsisErrorType.MESSAGE_HEADER_ERROR, IsisErrorType.BAD_MESSAGE_LENGTH); |
| 52 | } |
| 53 | IsisHeader isisHeader = getIsisHeader(channelBuffer); |
| 54 | int totalLength = 0; |
| 55 | IsisMessage isisMessage = null; |
| 56 | switch (isisHeader.isisPduType()) { |
| 57 | case L1HELLOPDU: |
| 58 | case L2HELLOPDU: |
| 59 | isisMessage = new L1L2HelloPdu(isisHeader); |
| 60 | totalLength = channelBuffer.getShort(IsisConstants.PDULENGTHPOSITION); |
| 61 | break; |
| 62 | case P2PHELLOPDU: |
| 63 | isisMessage = new P2PHelloPdu(isisHeader); |
| 64 | totalLength = channelBuffer.getShort(IsisConstants.PDULENGTHPOSITION); |
| 65 | break; |
| 66 | case L1LSPDU: |
| 67 | case L2LSPDU: |
| 68 | isisMessage = new LsPdu(isisHeader); |
| 69 | totalLength = channelBuffer.getShort(8); |
| 70 | break; |
| 71 | case L1CSNP: |
| 72 | case L2CSNP: |
| 73 | isisMessage = new Csnp(isisHeader); |
| 74 | totalLength = channelBuffer.getShort(8); |
| 75 | break; |
| 76 | case L1PSNP: |
| 77 | case L2PSNP: |
| 78 | isisMessage = new Psnp(isisHeader); |
| 79 | totalLength = channelBuffer.getShort(8); |
| 80 | break; |
| 81 | default: |
| 82 | log.debug("Message Reader[Decoder] - Unknown PDU type..!!!"); |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | if (isisMessage != null) { |
| 87 | try { |
| 88 | int bodyLength = totalLength - IsisConstants.COMMONHEADERLENGTH; |
| 89 | isisMessage.readFrom(channelBuffer.readBytes(bodyLength)); |
| 90 | |
| 91 | } catch (Exception e) { |
| 92 | throw new IsisParseException(IsisErrorType.ISIS_MESSAGE_ERROR, |
| 93 | IsisErrorType.BAD_MESSAGE); |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | return isisMessage; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Gets ISIS header. |
| 103 | * |
| 104 | * @param channelBuffer ISIS header |
| 105 | * @return ISIS header |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 106 | */ |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 107 | private IsisHeader getIsisHeader(ChannelBuffer channelBuffer) { |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 108 | |
| 109 | IsisHeader isisHeader = new IsisHeader(); |
| 110 | isisHeader.setIrpDiscriminator(channelBuffer.readByte()); |
| 111 | isisHeader.setPduHeaderLength(channelBuffer.readByte()); |
| 112 | isisHeader.setVersion(channelBuffer.readByte()); |
| 113 | isisHeader.setIdLength(channelBuffer.readByte()); |
| 114 | isisHeader.setIsisPduType(channelBuffer.readByte()); |
| 115 | isisHeader.setVersion2(channelBuffer.readByte()); |
| 116 | isisHeader.setReserved(channelBuffer.readByte()); |
| 117 | isisHeader.setMaximumAreaAddresses(channelBuffer.readByte()); |
| 118 | |
| 119 | return isisHeader; |
| 120 | } |
| 121 | } |