31c4d4f57e7b65f6f4b9ae5e28376346e3be3600
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / util / DeserializerExceptionHandler.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.netconf.impl.util;
10
11 import com.google.common.collect.Maps;
12 import io.netty.channel.ChannelHandler;
13 import io.netty.channel.ChannelHandlerContext;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import java.util.Map;
20
21 public final class
22         DeserializerExceptionHandler implements ChannelHandler {
23
24     private static final Logger logger = LoggerFactory.getLogger(DeserializerExceptionHandler.class);
25
26
27     @Override
28     public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
29         // NOOP
30     }
31
32     @Override
33     public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
34         // NOOP
35     }
36
37     @Override
38     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
39         logger.warn("An exception occurred during message handling", cause);
40         handleDeserializerException(ctx, cause);
41     }
42
43     private void handleDeserializerException(ChannelHandlerContext ctx, Throwable cause) {
44
45         Map<String, String> info = Maps.newHashMap();
46         info.put("cause", cause.getMessage());
47         NetconfDocumentedException ex = new NetconfDocumentedException(cause.getMessage(),
48                 NetconfDocumentedException.ErrorType.rpc, NetconfDocumentedException.ErrorTag.malformed_message,
49                 NetconfDocumentedException.ErrorSeverity.error, info);
50
51         SendErrorExceptionUtil.sendErrorMessage(ctx.channel(), ex);
52     }
53 }