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> 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     private static final Logger LOG = LoggerFactory.getLogger(NetconfClientSessionNegotiatorFactory.class);
42     private static final String START_EXI_MESSAGE_ID = "default-start-exi";
43     private static final EXIOptions DEFAULT_OPTIONS;
44
45     private final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader;
46     private final long connectionTimeoutMillis;
47     private final Timer timer;
48     private final EXIOptions options;
49
50     static {
51         final EXIOptions opts = new EXIOptions();
52         try {
53             opts.setPreserveDTD(true);
54             opts.setPreserveNS(true);
55             opts.setPreserveLexicalValues(true);
56             opts.setAlignmentType(AlignmentType.byteAligned);
57         } catch (EXIOptionsException e) {
58             throw new ExceptionInInitializerError(e);
59         }
60
61         DEFAULT_OPTIONS = opts;
62     }
63
64     public NetconfClientSessionNegotiatorFactory(final Timer timer,
65                                                  final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
66                                                  final long connectionTimeoutMillis) {
67         this(timer, additionalHeader, connectionTimeoutMillis, DEFAULT_OPTIONS);
68     }
69
70     public NetconfClientSessionNegotiatorFactory(final Timer timer,
71                                                  final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
72                                                  final long connectionTimeoutMillis, final EXIOptions exiOptions) {
73         this.timer = Preconditions.checkNotNull(timer);
74         this.additionalHeader = additionalHeader;
75         this.connectionTimeoutMillis = connectionTimeoutMillis;
76         this.options = exiOptions;
77     }
78
79     @Override
80     public SessionNegotiator<NetconfClientSession> getSessionNegotiator(final SessionListenerFactory<NetconfClientSessionListener> sessionListenerFactory,
81                                                                         final Channel channel,
82             final Promise<NetconfClientSession> promise) {
83
84         NetconfMessage startExiMessage = NetconfStartExiMessage.create(options, START_EXI_MESSAGE_ID);
85         NetconfHelloMessage helloMessage = null;
86         try {
87             helloMessage = NetconfHelloMessage.createClientHello(CLIENT_CAPABILITIES, additionalHeader);
88         } catch (NetconfDocumentedException e) {
89             LOG.error("Unable to create client hello message with capabilities {} and additional handler {}",CLIENT_CAPABILITIES,additionalHeader);
90             throw new IllegalStateException(e);
91         }
92
93         NetconfClientSessionPreferences proposal = new NetconfClientSessionPreferences(helloMessage, startExiMessage);
94         return new NetconfClientSessionNegotiator(proposal, promise, channel, timer,
95                 sessionListenerFactory.getSessionListener(),connectionTimeoutMillis);
96     }
97 }

©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.