blob: 1d6ae236cebe2bff0e2b72b085dca78f3b63b363 [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;
sunishvkf7c56552016-07-18 16:02:39 +053022import org.onlab.packet.Ip4Address;
23import org.onosproject.ospf.controller.OspfMessage;
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053024import org.onosproject.ospf.protocol.ospfpacket.OspfMessageReader;
sunishvkf7c56552016-07-18 16:02:39 +053025import org.onosproject.ospf.protocol.util.OspfUtil;
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053026import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import java.util.LinkedList;
30import java.util.List;
31
32/**
33 * Decodes an OSPF message from a Channel, for use in a netty pipeline.
34 */
35public class OspfMessageDecoder extends FrameDecoder {
36
37 private static final Logger log = LoggerFactory.getLogger(OspfMessageDecoder.class);
38
39 @Override
sunishvkf7c56552016-07-18 16:02:39 +053040 protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
41 log.debug("OspfMessageDecoder::Message received <:> length {}", buffer.readableBytes());
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053042 if (!channel.isConnected()) {
43 log.info("Channel is not connected.");
44 return null;
45 }
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053046 OspfMessageReader messageReader = new OspfMessageReader();
47 List<OspfMessage> ospfMessageList = new LinkedList<>();
sunishvkf7c56552016-07-18 16:02:39 +053048 while (buffer.readableBytes() >= OspfUtil.MINIMUM_FRAME_LEN) {
49 ChannelBuffer ospfDataBuffer = buffer.readBytes(OspfUtil.MINIMUM_FRAME_LEN);
50 int readableBytes = ospfDataBuffer.readableBytes();
51 OspfMessage message = messageReader.readFromBuffer(ospfDataBuffer);
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053052 if (message != null) {
sunishvkf7c56552016-07-18 16:02:39 +053053 if (ospfDataBuffer.readableBytes() >= OspfUtil.METADATA_LEN) {
54 ospfDataBuffer.readerIndex(readableBytes - OspfUtil.METADATA_LEN);
55 log.debug("IsisMessageDecoder::Reading metadata <:> length {}", ospfDataBuffer.readableBytes());
56
57
58 int interfaceIndex = ospfDataBuffer.readByte();
59 byte[] sourceIpBytes = new byte[OspfUtil.FOUR_BYTES];
60 ospfDataBuffer.readBytes(sourceIpBytes, 0, OspfUtil.FOUR_BYTES);
61 Ip4Address sourceIP = Ip4Address.valueOf(sourceIpBytes);
62
63 message.setSourceIp(sourceIP);
64 message.setInterfaceIndex(interfaceIndex);
65 }
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053066 ospfMessageList.add(message);
67 }
68 }
sunishvkf7c56552016-07-18 16:02:39 +053069 return (ospfMessageList.size() > 0) ? ospfMessageList : null;
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053070 }
71}