blob: 01bd81d891b21d46c33cc8f4ad4ba2e991c2ec99 [file] [log] [blame]
Jian Lie4f12162016-09-13 00:09:09 +09001/*
2 * Copyright 2016-present 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 */
Jian Li5e505c62016-12-05 02:44:24 +090016package org.onosproject.lisp.ctl.impl;
Jian Lie4f12162016-09-13 00:09:09 +090017
18
19import io.netty.buffer.ByteBuf;
Jian Liafe2d3f2016-11-01 02:49:07 +090020import io.netty.buffer.Unpooled;
Jian Lie4f12162016-09-13 00:09:09 +090021import io.netty.channel.ChannelHandlerContext;
Jian Liafe2d3f2016-11-01 02:49:07 +090022import io.netty.channel.socket.DatagramPacket;
23import io.netty.handler.codec.MessageToMessageEncoder;
Jian Lie4f12162016-09-13 00:09:09 +090024import org.onosproject.lisp.msg.protocols.LispMessage;
25
26import java.util.List;
27
28/**
29 * Encode a LISP message for output into a ByteBuffer,
30 * for use in a netty pipeline.
31 */
Jian Liafe2d3f2016-11-01 02:49:07 +090032public class LispMessageEncoder extends MessageToMessageEncoder {
Jian Lie4f12162016-09-13 00:09:09 +090033
34 @Override
Jian Liafe2d3f2016-11-01 02:49:07 +090035 protected void encode(ChannelHandlerContext ctx, Object msg, List out) throws Exception {
Jian Lie4f12162016-09-13 00:09:09 +090036 if (!(msg instanceof List)) {
Jian Liafe2d3f2016-11-01 02:49:07 +090037 ByteBuf byteBuf = Unpooled.buffer();
38 ((LispMessage) msg).writeTo(byteBuf);
39 out.add(new DatagramPacket(byteBuf, ((LispMessage) msg).getSender()));
Jian Lie4f12162016-09-13 00:09:09 +090040 return;
41 }
42
43 List<LispMessage> msgList = (List<LispMessage>) msg;
44
45 for (LispMessage message : msgList) {
46 if (message != null) {
Jian Liafe2d3f2016-11-01 02:49:07 +090047 ByteBuf byteBuf = Unpooled.buffer();
48 message.writeTo(byteBuf);
Jian Li2174e322016-12-14 03:28:59 +090049 out.add(new DatagramPacket(byteBuf, message.getSender()));
Jian Lie4f12162016-09-13 00:09:09 +090050 }
51 }
52 }
53}