Remove duplicate dependency declaration
[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 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.api.xml.XmlNetconfConstants;
21 import org.opendaylight.controller.netconf.nettyutil.handler.exi.NetconfStartExiMessage;
22 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
23 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
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;
32
33 public class NetconfClientSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfMessage, NetconfClientSession, NetconfClientSessionListener> {
34
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);
39
40     private static final String START_EXI_MESSAGE_ID = "default-start-exi";
41
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 LOG = LoggerFactory.getLogger(NetconfClientSessionNegotiatorFactory.class);
47
48     public NetconfClientSessionNegotiatorFactory(Timer timer,
49                                                  Optional<NetconfHelloMessageAdditionalHeader> additionalHeader,
50                                                  long connectionTimeoutMillis) {
51         this(timer, additionalHeader, connectionTimeoutMillis, DEFAULT_OPTIONS);
52     }
53
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;
61     }
62
63     @Override
64     public SessionNegotiator<NetconfClientSession> getSessionNegotiator(SessionListenerFactory<NetconfClientSessionListener> sessionListenerFactory,
65                                                                         Channel channel,
66             Promise<NetconfClientSession> promise) {
67
68         NetconfMessage startExiMessage = NetconfStartExiMessage.create(options, START_EXI_MESSAGE_ID);
69         NetconfHelloMessage helloMessage = null;
70         try {
71             helloMessage = NetconfHelloMessage.createClientHello(CLIENT_CAPABILITIES, additionalHeader);
72         } catch (NetconfDocumentedException e) {
73             LOG.error("Unable to create client hello message with capabilities {} and additional handler {}",CLIENT_CAPABILITIES,additionalHeader);
74             throw new IllegalStateException(e);
75         }
76
77         NetconfClientSessionPreferences proposal = new NetconfClientSessionPreferences(helloMessage, startExiMessage);
78         return new NetconfClientSessionNegotiator(proposal, promise, channel, timer,
79                 sessionListenerFactory.getSessionListener(),connectionTimeoutMillis);
80     }
81
82     private static final EXIOptions DEFAULT_OPTIONS = new EXIOptions();
83     static {
84         try {
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");
92         }
93     }
94 }