blob: 0446e09fa563f640382de0e54801653569eab073 [file] [log] [blame]
Dhruv Dhody4d8943a2016-02-17 16:36:08 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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;
Ray Milkey019fba42018-01-31 14:07:47 -080024import org.onosproject.ospf.exceptions.OspfParseException;
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053025import org.onosproject.ospf.protocol.ospfpacket.OspfMessageReader;
sunishvkf7c56552016-07-18 16:02:39 +053026import org.onosproject.ospf.protocol.util.OspfUtil;
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.util.LinkedList;
31import java.util.List;
32
33/**
34 * Decodes an OSPF message from a Channel, for use in a netty pipeline.
35 */
36public class OspfMessageDecoder extends FrameDecoder {
37
38 private static final Logger log = LoggerFactory.getLogger(OspfMessageDecoder.class);
39
40 @Override
Ray Milkey019fba42018-01-31 14:07:47 -080041 protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
42 throws OspfParseException {
sunishvkf7c56552016-07-18 16:02:39 +053043 log.debug("OspfMessageDecoder::Message received <:> length {}", buffer.readableBytes());
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053044 if (!channel.isConnected()) {
45 log.info("Channel is not connected.");
46 return null;
47 }
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053048 OspfMessageReader messageReader = new OspfMessageReader();
49 List<OspfMessage> ospfMessageList = new LinkedList<>();
sunishvkf7c56552016-07-18 16:02:39 +053050 while (buffer.readableBytes() >= OspfUtil.MINIMUM_FRAME_LEN) {
51 ChannelBuffer ospfDataBuffer = buffer.readBytes(OspfUtil.MINIMUM_FRAME_LEN);
52 int readableBytes = ospfDataBuffer.readableBytes();
53 OspfMessage message = messageReader.readFromBuffer(ospfDataBuffer);
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053054 if (message != null) {
sunishvkf7c56552016-07-18 16:02:39 +053055 if (ospfDataBuffer.readableBytes() >= OspfUtil.METADATA_LEN) {
56 ospfDataBuffer.readerIndex(readableBytes - OspfUtil.METADATA_LEN);
57 log.debug("IsisMessageDecoder::Reading metadata <:> length {}", ospfDataBuffer.readableBytes());
58
59
60 int interfaceIndex = ospfDataBuffer.readByte();
61 byte[] sourceIpBytes = new byte[OspfUtil.FOUR_BYTES];
62 ospfDataBuffer.readBytes(sourceIpBytes, 0, OspfUtil.FOUR_BYTES);
63 Ip4Address sourceIP = Ip4Address.valueOf(sourceIpBytes);
64
65 message.setSourceIp(sourceIP);
66 message.setInterfaceIndex(interfaceIndex);
67 }
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053068 ospfMessageList.add(message);
69 }
70 }
Jon Hallcbd1b392017-01-18 20:15:44 -080071 return (!ospfMessageList.isEmpty()) ? ospfMessageList : null;
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053072 }
73}