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