2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.client;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableSet;
14 import io.netty.channel.Channel;
15 import io.netty.util.Timer;
16 import io.netty.util.concurrent.Promise;
18 import org.opendaylight.netconf.api.NetconfClientSessionPreferences;
19 import org.opendaylight.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.netconf.api.NetconfMessage;
21 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
22 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
23 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
24 import org.opendaylight.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
25 import org.opendaylight.protocol.framework.SessionListenerFactory;
26 import org.opendaylight.protocol.framework.SessionNegotiator;
27 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
28 import org.openexi.proc.common.AlignmentType;
29 import org.openexi.proc.common.EXIOptions;
30 import org.openexi.proc.common.EXIOptionsException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class NetconfClientSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfMessage,
35 NetconfClientSession, NetconfClientSessionListener> {
37 public static final Set<String> EXI_CLIENT_CAPABILITIES = ImmutableSet.of(
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);
42 public static final Set<String> LEGACY_EXI_CLIENT_CAPABILITIES = ImmutableSet.of(
43 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
44 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0);
46 public static final Set<String> DEFAULT_CLIENT_CAPABILITIES = ImmutableSet.of(
47 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
48 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
50 public static final Set<String> LEGACY_FRAMING_CLIENT_CAPABILITIES = ImmutableSet.of(
51 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0);
53 private static final Logger LOG = LoggerFactory.getLogger(NetconfClientSessionNegotiatorFactory.class);
54 private static final String START_EXI_MESSAGE_ID = "default-start-exi";
55 private static final EXIOptions DEFAULT_OPTIONS;
57 private final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader;
58 private final long connectionTimeoutMillis;
59 private final Timer timer;
60 private final EXIOptions options;
63 final EXIOptions opts = new EXIOptions();
65 opts.setPreserveDTD(true);
66 opts.setPreserveNS(true);
67 opts.setPreserveLexicalValues(true);
68 opts.setAlignmentType(AlignmentType.byteAligned);
69 } catch (EXIOptionsException e) {
70 throw new ExceptionInInitializerError(e);
73 DEFAULT_OPTIONS = opts;
76 private final Set<String> clientCapabilities;
78 public NetconfClientSessionNegotiatorFactory(final Timer timer,
79 final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
80 final long connectionTimeoutMillis) {
81 this(timer, additionalHeader, connectionTimeoutMillis, DEFAULT_OPTIONS);
84 public NetconfClientSessionNegotiatorFactory(final Timer timer,
85 final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
86 final long connectionTimeoutMillis, final Set<String> capabilities) {
87 this(timer, additionalHeader, connectionTimeoutMillis, DEFAULT_OPTIONS, capabilities);
91 public NetconfClientSessionNegotiatorFactory(final Timer timer,
92 final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
93 final long connectionTimeoutMillis, final EXIOptions exiOptions) {
94 this(timer, additionalHeader, connectionTimeoutMillis, exiOptions, EXI_CLIENT_CAPABILITIES);
97 public NetconfClientSessionNegotiatorFactory(final Timer timer,
98 final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
99 final long connectionTimeoutMillis, final EXIOptions exiOptions,
100 final Set<String> capabilities) {
101 this.timer = Preconditions.checkNotNull(timer);
102 this.additionalHeader = additionalHeader;
103 this.connectionTimeoutMillis = connectionTimeoutMillis;
104 this.options = exiOptions;
105 this.clientCapabilities = capabilities;
109 public SessionNegotiator<NetconfClientSession> getSessionNegotiator(
110 final SessionListenerFactory<NetconfClientSessionListener> sessionListenerFactory,
111 final Channel channel, final Promise<NetconfClientSession> promise) {
113 NetconfMessage startExiMessage = NetconfStartExiMessage.create(options, START_EXI_MESSAGE_ID);
114 NetconfHelloMessage helloMessage = null;
116 helloMessage = NetconfHelloMessage.createClientHello(clientCapabilities, additionalHeader);
117 } catch (NetconfDocumentedException e) {
118 LOG.error("Unable to create client hello message with capabilities {} and additional handler {}",
119 clientCapabilities, additionalHeader);
120 throw new IllegalStateException(e);
123 NetconfClientSessionPreferences proposal = new NetconfClientSessionPreferences(helloMessage, startExiMessage);
124 return new NetconfClientSessionNegotiator(proposal, promise, channel, timer,
125 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);