e0e227534bd66b9bc4f878a84d59c21da371ed94
[onosfw.git] /
1 /*
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  */
16 package org.onosproject.ovsdb.controller.impl;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.channel.ChannelHandlerContext;
20 import io.netty.handler.codec.ByteToMessageDecoder;
21
22 import java.util.List;
23
24 import org.onosproject.ovsdb.rfc.jsonrpc.JsonReadContext;
25 import org.onosproject.ovsdb.rfc.utils.JsonRpcReaderUtil;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Decoder for inbound messages.
31  */
32 public class MessageDecoder extends ByteToMessageDecoder {
33
34     private final Logger log = LoggerFactory.getLogger(MessageDecoder.class);
35     private final JsonReadContext context = new JsonReadContext();
36
37     /**
38      * Default constructor.
39      */
40     public MessageDecoder() {
41     }
42
43     @Override
44     protected void decode(ChannelHandlerContext ctx, ByteBuf buf,
45                           List<Object> out) throws Exception {
46         log.debug("Message decoder");
47         JsonRpcReaderUtil.readToJsonNode(buf, out, context);
48     }
49
50     @Override
51     public void exceptionCaught(ChannelHandlerContext context, Throwable cause) {
52         log.error("Exception inside channel handling pipeline.", cause);
53         context.close();
54     }
55 }