blob: 3e56d6ff56e5ae6c075e542a22bb17d20e344f8a [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;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053025import org.onosproject.bgpio.protocol.BgpMessage;
Satish Ke107e662015-09-21 19:00:17 +053026import 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 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053035public class BgpMessageEncoder extends OneToOneEncoder {
36 protected static final Logger log = LoggerFactory.getLogger(BgpMessageEncoder.class);
Satish Ke107e662015-09-21 19:00:17 +053037
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")
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053047 List<BgpMessage> msglist = (List<BgpMessage>) msg;
Satish Ke107e662015-09-21 19:00:17 +053048
49 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
50
51 log.debug("SENDING MESSAGE");
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053052 for (BgpMessage pm : msglist) {
Satish Ke107e662015-09-21 19:00:17 +053053 pm.writeTo(buf);
54 }
55
56 HexDump.dump(buf);
57
58 return buf;
59 }
60}