blob: baeae99511ab653db5cc5efd709c12631ec97fab [file] [log] [blame]
Dhruv Dhody4d8943a2016-02-17 16:36:08 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Dhruv Dhody4d8943a2016-02-17 16:36:08 +05303 *
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.ospf.controller.impl;
17
18import org.jboss.netty.buffer.ChannelBuffer;
19import org.jboss.netty.channel.Channel;
20import org.jboss.netty.channel.ChannelHandlerContext;
21import org.jboss.netty.handler.codec.frame.FrameDecoder;
22import org.onosproject.ospf.protocol.ospfpacket.OspfMessage;
23import org.onosproject.ospf.protocol.ospfpacket.OspfMessageReader;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import java.util.LinkedList;
28import java.util.List;
29
30/**
31 * Decodes an OSPF message from a Channel, for use in a netty pipeline.
32 */
33public class OspfMessageDecoder extends FrameDecoder {
34
35 private static final Logger log = LoggerFactory.getLogger(OspfMessageDecoder.class);
36
37 @Override
38 protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer channelBuffer) throws Exception {
39 log.debug("OspfMessageDecoder::Message received <:> length {}", channelBuffer.readableBytes());
40 log.debug("channelBuffer.readableBytes - decode {}", channelBuffer.readableBytes());
41 if (!channel.isConnected()) {
42 log.info("Channel is not connected.");
43 return null;
44 }
45
46 OspfMessageReader messageReader = new OspfMessageReader();
47 List<OspfMessage> ospfMessageList = new LinkedList<>();
48
49 while (channelBuffer.readableBytes() > 0) {
50 OspfMessage message = messageReader.readFromBuffer(channelBuffer);
51 if (message != null) {
52 ospfMessageList.add(message);
53 }
54 }
55
56 return ospfMessageList;
57 }
58}