blob: 95bdcdced46b3b9a776bc2444a68d4eb4bee9e2f [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 */
16
17package org.onosproject.ospf.controller.impl;
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.oneone.OneToOneEncoder;
23import org.onosproject.ospf.controller.OspfInterface;
24import org.onosproject.ospf.controller.area.OspfInterfaceImpl;
25import org.onosproject.ospf.protocol.ospfpacket.OspfMessage;
26import org.onosproject.ospf.protocol.ospfpacket.OspfMessageWriter;
27import org.onosproject.ospf.protocol.util.OspfInterfaceState;
28import org.onosproject.ospf.protocol.util.OspfUtil;
29import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32/**
33 * Encodes an OSPF message for output into a ChannelBuffer, for use in a netty pipeline.
34 */
35public class OspfMessageEncoder extends OneToOneEncoder {
36
37 private static final Logger log = LoggerFactory.getLogger(OspfMessageEncoder.class);
38 private OspfInterface ospfInterface;
39
40
41 /**
sunish vka2c5b162016-03-04 14:18:55 +053042 * Creates an instance of OSPF message encoder.
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053043 */
44 OspfMessageEncoder() {
45 }
46
47 /**
sunish vka2c5b162016-03-04 14:18:55 +053048 * Creates an instance of OSPF message encoder.
49 *
50 * @param ospfInterface OSPF interface instance
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053051 */
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 vka2c5b162016-03-04 14:18:55 +053080}