blob: 0a2ea99a65e8fbc95ae01818699eb59c9c2105a5 [file] [log] [blame]
Dhruv Dhody4d8943a2016-02-17 16:36:08 +05301/*
2 * Copyright 2016 Open Networking Laboratory
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
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 /**
42 * Constructor.
43 */
44 OspfMessageEncoder() {
45 }
46
47 /**
48 * Constructor to initialize instance.
49 */
50 OspfMessageEncoder(OspfInterface ospfInterface) {
51 this.ospfInterface = ospfInterface;
52 }
53
54 @Override
55 protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
56
57 log.debug("Encoding ospfMessage...!!!");
58 if (!(msg instanceof OspfMessage)) {
59 log.debug("Invalid msg.");
60 return msg;
61 }
62
63 OspfMessage ospfMessage = (OspfMessage) msg;
64 OspfMessageWriter messageWriter = new OspfMessageWriter();
65 if (((OspfInterfaceImpl) ospfInterface).state().equals(OspfInterfaceState.POINT2POINT)) {
66 ospfMessage.setDestinationIp(OspfUtil.ALL_SPF_ROUTERS);
67 }
68 ChannelBuffer buf = messageWriter.writeToBuffer(ospfMessage,
69 ((OspfInterfaceImpl) ospfInterface).state().value(),
70 ospfInterface.interfaceType());
71 log.info("OspfMessageEncoder sending packet of lenght {}", buf.readableBytes());
72 log.debug("OspfMessageEncoder sending packet of lenght {}", buf.readableBytes());
73 log.debug("Sending {} Message to {}, Length :: {}, <=> {}", ospfMessage.ospfMessageType(),
74 ospfMessage.destinationIp(), buf.readableBytes(), buf.array());
75
76 return buf;
77 }
78}