Bug 977: Return RpcError result on neconf failure
[controller.git] / opendaylight / netconf / netconf-client / src / main / java / org / opendaylight / controller / netconf / client / NetconfClientSessionNegotiatorFactory.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.client;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Sets;
14
15 import io.netty.channel.Channel;
16 import io.netty.util.Timer;
17 import io.netty.util.concurrent.Promise;
18
19 import org.opendaylight.controller.netconf.api.NetconfClientSessionPreferences;
20 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
21 import org.opendaylight.controller.netconf.api.NetconfMessage;
22 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
23 import org.opendaylight.controller.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
24 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
25 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
26 import org.opendaylight.protocol.framework.SessionListenerFactory;
27 import org.opendaylight.protocol.framework.SessionNegotiator;
28 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
29 import org.openexi.proc.common.AlignmentType;
30 import org.openexi.proc.common.EXIOptions;
31 import org.openexi.proc.common.EXIOptionsException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class NetconfClientSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfMessage, NetconfClientSession, NetconfClientSessionListener> {
36
37     public static final java.util.Set<String> CLIENT_CAPABILITIES = Sets.newHashSet(
38             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
39             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1,
40             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0);
41
42     private static final String START_EXI_MESSAGE_ID = "default-start-exi";
43
44     private final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader;
45     private final long connectionTimeoutMillis;
46     private final Timer timer;
47     private final EXIOptions options;
48     private static final Logger logger = LoggerFactory.getLogger(NetconfClientSessionNegotiatorFactory.class);
49
50     public NetconfClientSessionNegotiatorFactory(Timer timer,
51                                                  Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
52                                                  long connectionTimeoutMillis) {
53         this(timer, additionalHeader, connectionTimeoutMillis, DEFAULT_OPTIONS);
54     }
55
56     public NetconfClientSessionNegotiatorFactory(Timer timer,
57                                                  Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
58                                                  long connectionTimeoutMillis, EXIOptions exiOptions) {
59         this.timer = Preconditions.checkNotNull(timer);
60         this.additionalHeader = additionalHeader;
61         this.connectionTimeoutMillis = connectionTimeoutMillis;
62         this.options = exiOptions;
63     }
64
65     @Override
66     public SessionNegotiator<NetconfClientSession> getSessionNegotiator(SessionListenerFactory<NetconfClientSessionListener> sessionListenerFactory,
67                                                                         Channel channel,
68             Promise<NetconfClientSession> promise) {
69
70         NetconfMessage startExiMessage = NetconfStartExiMessage.create(options, START_EXI_MESSAGE_ID);
71         NetconfHelloMessage helloMessage = null;
72         try {
73             helloMessage = NetconfHelloMessage.createClientHello(CLIENT_CAPABILITIES, additionalHeader);
74         } catch (NetconfDocumentedException e) {
75             logger.error("Unable to create client hello message with capabilities {} and additional handler {}",CLIENT_CAPABILITIES,additionalHeader);
76             throw new IllegalStateException(e);
77         }
78
79         NetconfClientSessionPreferences proposal = new NetconfClientSessionPreferences(helloMessage, startExiMessage);
80         return new NetconfClientSessionNegotiator(proposal, promise, channel, timer,
81                 sessionListenerFactory.getSessionListener(),connectionTimeoutMillis);
82     }
83
84     private static final EXIOptions DEFAULT_OPTIONS = new EXIOptions();
85     static {
86         try {
87             DEFAULT_OPTIONS.setPreserveDTD(true);
88             DEFAULT_OPTIONS.setPreserveNS(true);
89             DEFAULT_OPTIONS.setPreserveLexicalValues(true);
90             DEFAULT_OPTIONS.setAlignmentType(AlignmentType.preCompress);
91         } catch (EXIOptionsException e) {
92             // Should not happen since DEFAULT_OPTIONS are still the same
93             throw new IllegalStateException("Unable to create EXI DEFAULT_OPTIONS");
94         }
95     }
96 }