blob: 7c5b14b5da8c14c02c60bed26a1a72e20d3c79ad [file] [log] [blame]
SureshBR25058b72015-08-13 13:05:06 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
SureshBR25058b72015-08-13 13:05:06 +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 */
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +053016package org.onosproject.pcep.server.impl;
SureshBR25058b72015-08-13 13:05:06 +053017
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.pcepio.protocol.PcepMessage;
26import org.onosproject.pcepio.util.HexDump;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30/**
31 * Encode an pcep message for output into a ChannelBuffer, for use in a
32 * netty pipeline.
33 */
34public class PcepMessageEncoder extends OneToOneEncoder {
Ray Milkey9c9cde42018-01-12 14:22:06 -080035 private static final Logger log = LoggerFactory.getLogger(PcepMessageEncoder.class);
SureshBR25058b72015-08-13 13:05:06 +053036
37 @Override
38 protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
39 log.debug("Sending message");
40 if (!(msg instanceof List)) {
41 log.debug("Invalid msg.");
42 return msg;
43 }
44
45 @SuppressWarnings("unchecked")
46 List<PcepMessage> msglist = (List<PcepMessage>) msg;
47
48 ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
49
50 for (PcepMessage pm : msglist) {
51 pm.writeTo(buf);
52 }
53
54 HexDump.pcepHexDump(buf);
55
56 return buf;
57 }
58}