Merge "Bug-915: Adding static document generation. Currently the API Explorer can...
[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 java.util.Map;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public final class DeserializerExceptionHandler implements ChannelHandler {
21
22     private static final Logger LOG = LoggerFactory.getLogger(DeserializerExceptionHandler.class);
23
24     @Override
25     public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
26         // NOOP
27     }
28
29     @Override
30     public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
31         // NOOP
32     }
33
34     @Override
35     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
36         LOG.warn("An exception occurred during message handling", cause);
37         handleDeserializerException(ctx, cause);
38     }
39
40     private void handleDeserializerException(ChannelHandlerContext ctx, Throwable cause) {
41
42         Map<String, String> info = Maps.newHashMap();
43         info.put("cause", cause.getMessage());
44         NetconfDocumentedException ex = new NetconfDocumentedException(cause.getMessage(),
45                 NetconfDocumentedException.ErrorType.rpc, NetconfDocumentedException.ErrorTag.malformed_message,
46                 NetconfDocumentedException.ErrorSeverity.error, info);
47
48         SendErrorExceptionUtil.sendErrorMessage(ctx.channel(), ex);
49     }
50 }