blob: 479b4da3b28ac45ac9df14de538d9a965c867793 [file] [log] [blame]
Jonathan Hart3930f632015-10-19 12:12:51 -07001/*
Jonathan Hartf4bd0482017-01-27 15:11:18 -08002 * Copyright 2017-present Open Networking Laboratory
Jonathan Hart3930f632015-10-19 12:12:51 -07003 *
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
17package org.onosproject.routing.fpm;
18
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.jboss.netty.channel.Channel;
21import org.jboss.netty.channel.ChannelHandlerContext;
22import org.jboss.netty.handler.codec.frame.FrameDecoder;
23import org.onosproject.routing.fpm.protocol.FpmHeader;
24
25/**
26 * Frame decoder for FPM connections.
27 */
28public class FpmFrameDecoder extends FrameDecoder {
29
30 @Override
31 protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
32 throws Exception {
33
34 if (!channel.isConnected()) {
35 return null;
36 }
37
38 if (buffer.readableBytes() < FpmHeader.FPM_HEADER_LENGTH) {
39 return null;
40 }
41
42 buffer.markReaderIndex();
43
44 short version = buffer.readUnsignedByte();
45 short type = buffer.readUnsignedByte();
46 int length = buffer.readUnsignedShort();
47
48 buffer.resetReaderIndex();
49
50 if (buffer.readableBytes() < length) {
51 // Not enough bytes to read a whole message
52 return null;
53 }
54
55 byte[] fpmMessage = new byte[length];
56 buffer.readBytes(fpmMessage);
57
58 return FpmHeader.decode(fpmMessage, 0, fpmMessage.length);
59 }
60}