Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.onosproject.ospf.controller.impl; |
| 18 | |
| 19 | import org.jboss.netty.buffer.ChannelBuffer; |
| 20 | import org.jboss.netty.channel.Channel; |
| 21 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 22 | import org.jboss.netty.handler.codec.oneone.OneToOneEncoder; |
| 23 | import org.onosproject.ospf.controller.OspfInterface; |
| 24 | import org.onosproject.ospf.controller.area.OspfInterfaceImpl; |
| 25 | import org.onosproject.ospf.protocol.ospfpacket.OspfMessage; |
| 26 | import org.onosproject.ospf.protocol.ospfpacket.OspfMessageWriter; |
| 27 | import org.onosproject.ospf.protocol.util.OspfInterfaceState; |
| 28 | import org.onosproject.ospf.protocol.util.OspfUtil; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | /** |
| 33 | * Encodes an OSPF message for output into a ChannelBuffer, for use in a netty pipeline. |
| 34 | */ |
| 35 | public class OspfMessageEncoder extends OneToOneEncoder { |
| 36 | |
| 37 | private static final Logger log = LoggerFactory.getLogger(OspfMessageEncoder.class); |
| 38 | private OspfInterface ospfInterface; |
| 39 | |
| 40 | |
| 41 | /** |
sunish vk | a2c5b16 | 2016-03-04 14:18:55 +0530 | [diff] [blame] | 42 | * Creates an instance of OSPF message encoder. |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 43 | */ |
| 44 | OspfMessageEncoder() { |
| 45 | } |
| 46 | |
| 47 | /** |
sunish vk | a2c5b16 | 2016-03-04 14:18:55 +0530 | [diff] [blame] | 48 | * Creates an instance of OSPF message encoder. |
| 49 | * |
| 50 | * @param ospfInterface OSPF interface instance |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 51 | */ |
| 52 | OspfMessageEncoder(OspfInterface ospfInterface) { |
| 53 | this.ospfInterface = ospfInterface; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception { |
| 58 | |
| 59 | log.debug("Encoding ospfMessage...!!!"); |
| 60 | if (!(msg instanceof OspfMessage)) { |
| 61 | log.debug("Invalid msg."); |
| 62 | return msg; |
| 63 | } |
| 64 | |
| 65 | OspfMessage ospfMessage = (OspfMessage) msg; |
| 66 | OspfMessageWriter messageWriter = new OspfMessageWriter(); |
| 67 | if (((OspfInterfaceImpl) ospfInterface).state().equals(OspfInterfaceState.POINT2POINT)) { |
| 68 | ospfMessage.setDestinationIp(OspfUtil.ALL_SPF_ROUTERS); |
| 69 | } |
| 70 | ChannelBuffer buf = messageWriter.writeToBuffer(ospfMessage, |
| 71 | ((OspfInterfaceImpl) ospfInterface).state().value(), |
| 72 | ospfInterface.interfaceType()); |
| 73 | log.info("OspfMessageEncoder sending packet of lenght {}", buf.readableBytes()); |
| 74 | log.debug("OspfMessageEncoder sending packet of lenght {}", buf.readableBytes()); |
| 75 | log.debug("Sending {} Message to {}, Length :: {}, <=> {}", ospfMessage.ospfMessageType(), |
| 76 | ospfMessage.destinationIp(), buf.readableBytes(), buf.array()); |
| 77 | |
| 78 | return buf; |
| 79 | } |
sunish vk | a2c5b16 | 2016-03-04 14:18:55 +0530 | [diff] [blame] | 80 | } |