1. Corrected Ip Protocol match field.
[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 java.util.Map;
12
13 import org.opendaylight.controller.netconf.api.NetconfDeserializerException;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.util.messages.SendErrorExceptionUtil;
16
17 import com.google.common.collect.Maps;
18
19 import io.netty.channel.ChannelHandler;
20 import io.netty.channel.ChannelHandlerContext;
21
22 public final class DeserializerExceptionHandler implements ChannelHandler {
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         if (cause instanceof NetconfDeserializerException) {
37             handleDeserializerException(ctx, cause);
38         }
39     }
40
41     private void handleDeserializerException(ChannelHandlerContext ctx, Throwable cause) {
42
43         Map<String, String> info = Maps.newHashMap();
44         info.put("cause", cause.getMessage());
45         NetconfDocumentedException ex = new NetconfDocumentedException(cause.getMessage(),
46                 NetconfDocumentedException.ErrorType.rpc, NetconfDocumentedException.ErrorTag.malformed_message,
47                 NetconfDocumentedException.ErrorSeverity.error, info);
48
49         SendErrorExceptionUtil.sendErrorMessage(ctx.channel(), ex);
50     }
51 }