blob: fe149c609664c5890aa6750a5513ccb8763130d6 [file] [log] [blame]
Dhruv Dhodye64b93e2016-04-20 19:26:55 +05301/*
2 * Copyright 2016-present 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 */
16package org.onosproject.isis.io.isispacket;
17
18import org.jboss.netty.buffer.ChannelBuffer;
19import org.onosproject.isis.controller.IsisMessage;
20import org.onosproject.isis.exceptions.IsisErrorType;
21import org.onosproject.isis.exceptions.IsisParseException;
22import org.onosproject.isis.io.isispacket.pdu.Csnp;
23import org.onosproject.isis.io.isispacket.pdu.L1L2HelloPdu;
24import org.onosproject.isis.io.isispacket.pdu.LsPdu;
25import org.onosproject.isis.io.isispacket.pdu.P2PHelloPdu;
26import org.onosproject.isis.io.isispacket.pdu.Psnp;
27import org.onosproject.isis.io.util.IsisConstants;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31/**
32 * Represents ISIS message reader.
33 */
34public class IsisMessageReader {
35
36 protected static final Logger log = LoggerFactory.getLogger(IsisMessageReader.class);
37
38 /**
39 * Reads from ISIS packet from buffer.
40 *
41 * @param channelBuffer buffer
42 * @return ISIS message
43 * @throws Exception exception
44 */
45 public IsisMessage readFromBuffer(ChannelBuffer channelBuffer) throws Exception {
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
106 * @throws Exception
107 */
108 private IsisHeader getIsisHeader(ChannelBuffer channelBuffer) throws Exception {
109
110 IsisHeader isisHeader = new IsisHeader();
111 isisHeader.setIrpDiscriminator(channelBuffer.readByte());
112 isisHeader.setPduHeaderLength(channelBuffer.readByte());
113 isisHeader.setVersion(channelBuffer.readByte());
114 isisHeader.setIdLength(channelBuffer.readByte());
115 isisHeader.setIsisPduType(channelBuffer.readByte());
116 isisHeader.setVersion2(channelBuffer.readByte());
117 isisHeader.setReserved(channelBuffer.readByte());
118 isisHeader.setMaximumAreaAddresses(channelBuffer.readByte());
119
120 return isisHeader;
121 }
122}