Merge "Cleanup: Remove passing around of DataPersistenceProvider"
[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.ImmutableSet;
14 import io.netty.channel.Channel;
15 import io.netty.util.Timer;
16 import io.netty.util.concurrent.Promise;
17 import java.util.Set;
18 import org.opendaylight.controller.netconf.api.NetconfClientSessionPreferences;
19 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
20 import org.opendaylight.controller.netconf.api.NetconfMessage;
21 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
22 import org.opendaylight.controller.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
23 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
24 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
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;
33
34 public class NetconfClientSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfMessage, NetconfClientSession, NetconfClientSessionListener> {
35
36     public static final Set<String> EXI_CLIENT_CAPABILITIES = ImmutableSet.of(
37             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
38             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1,
39             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0);
40
41     public static final Set<String> LEGACY_EXI_CLIENT_CAPABILITIES = ImmutableSet.of(
42             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
43             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0);
44
45     public static final Set<String> DEFAULT_CLIENT_CAPABILITIES = ImmutableSet.of(
46             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
47             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
48
49     public static final Set<String> LEGACY_FRAMING_CLIENT_CAPABILITIES = ImmutableSet.of(
50             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0);
51
52     private static final Logger LOG = LoggerFactory.getLogger(NetconfClientSessionNegotiatorFactory.class);
53     private static final String START_EXI_MESSAGE_ID = "default-start-exi";
54     private static final EXIOptions DEFAULT_OPTIONS;
55
56     private final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader;
57     private final long connectionTimeoutMillis;
58     private final Timer timer;
59     private final EXIOptions options;
60
61     static {
62         final EXIOptions opts = new EXIOptions();
63         try {
64             opts.setPreserveDTD(true);
65             opts.setPreserveNS(true);
66             opts.setPreserveLexicalValues(true);
67             opts.setAlignmentType(AlignmentType.byteAligned);
68         } catch (EXIOptionsException e) {
69             throw new ExceptionInInitializerError(e);
70         }
71
72         DEFAULT_OPTIONS = opts;
73     }
74
75     private final Set<String> clientCapabilities;
76
77     public NetconfClientSessionNegotiatorFactory(final Timer timer,
78                                                  final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
79                                                  final long connectionTimeoutMillis) {
80         this(timer, additionalHeader, connectionTimeoutMillis, DEFAULT_OPTIONS);
81     }
82
83     public NetconfClientSessionNegotiatorFactory(final Timer timer,
84                                                  final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
85                                                  final long connectionTimeoutMillis, final Set<String> capabilities) {
86         this(timer, additionalHeader, connectionTimeoutMillis, DEFAULT_OPTIONS, capabilities);
87
88     }
89
90     public NetconfClientSessionNegotiatorFactory(final Timer timer,
91                                                  final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
92                                                  final long connectionTimeoutMillis, final EXIOptions exiOptions) {
93         this(timer, additionalHeader, connectionTimeoutMillis, exiOptions, EXI_CLIENT_CAPABILITIES);
94     }
95
96     public NetconfClientSessionNegotiatorFactory(final Timer timer,
97                                                  final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
98                                                  final long connectionTimeoutMillis, final EXIOptions exiOptions, final Set<String> capabilities) {
99         this.timer = Preconditions.checkNotNull(timer);
100         this.additionalHeader = additionalHeader;
101         this.connectionTimeoutMillis = connectionTimeoutMillis;
102         this.options = exiOptions;
103         this.clientCapabilities = capabilities;
104     }
105
106     @Override
107     public SessionNegotiator<NetconfClientSession> getSessionNegotiator(final SessionListenerFactory<NetconfClientSessionListener> sessionListenerFactory,
108                                                                         final Channel channel,
109             final Promise<NetconfClientSession> promise) {
110
111         NetconfMessage startExiMessage = NetconfStartExiMessage.create(options, START_EXI_MESSAGE_ID);
112         NetconfHelloMessage helloMessage = null;
113         try {
114             helloMessage = NetconfHelloMessage.createClientHello(clientCapabilities, additionalHeader);
115         } catch (NetconfDocumentedException e) {
116             LOG.error("Unable to create client hello message with capabilities {} and additional handler {}", clientCapabilities,additionalHeader);
117             throw new IllegalStateException(e);
118         }
119
120         NetconfClientSessionPreferences proposal = new NetconfClientSessionPreferences(helloMessage, startExiMessage);
121         return new NetconfClientSessionNegotiator(proposal, promise, channel, timer,
122                 sessionListenerFactory.getSessionListener(),connectionTimeoutMillis);
123     }
124 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.