blob: f0d38c3d363a32d06a3198352ea52ccd2ef17331 [file] [log] [blame]
Satish Ke107e662015-09-21 19:00:17 +05301/*
2 * Copyright 2015 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 */
16package org.onosproject.bgp.controller.impl;
17
18import java.util.List;
19
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.jboss.netty.buffer.ChannelBuffers;
22import org.jboss.netty.channel.Channel;
23import org.jboss.netty.channel.ChannelHandlerContext;
24import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
25import org.onosproject.bgpio.protocol.BGPMessage;
26import org.onlab.util.HexDump;
27
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31/**
32 * Encode an bgp message for output into a ChannelBuffer, for use in a
33 * netty pipeline.
34 */
35public class BGPMessageEncoder extends OneToOneEncoder {
36 protected static final Logger log = LoggerFactory.getLogger(BGPMessageEncoder.class);
37
38 @Override
39 protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
40 log.debug("BGPMessageEncoder::encode");
41 if (!(msg instanceof List)) {
42 log.debug("Invalid msg.");
43 return msg;
44 }
45
46 @SuppressWarnings("unchecked")
47 List<BGPMessage> msglist = (List<BGPMessage>) msg;
48
49 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
50
51 log.debug("SENDING MESSAGE");
52 for (BGPMessage pm : msglist) {
53 pm.writeTo(buf);
54 }
55
56 HexDump.dump(buf);
57
58 return buf;
59 }
60}