Add configurable connection timeout to netconf client.
[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 io.netty.channel.Channel;
14 import io.netty.util.Timer;
15 import io.netty.util.concurrent.Promise;
16 import org.opendaylight.controller.netconf.api.NetconfMessage;
17 import org.opendaylight.controller.netconf.api.NetconfSessionPreferences;
18 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
19 import org.opendaylight.protocol.framework.SessionListenerFactory;
20 import org.opendaylight.protocol.framework.SessionNegotiator;
21 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
22 import org.xml.sax.SAXException;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 public class NetconfClientSessionNegotiatorFactory implements SessionNegotiatorFactory {
28
29     private final Timer timer;
30
31     private final Optional<String> additionalHeader;
32     private final long connectionTimeoutMillis;
33
34     public NetconfClientSessionNegotiatorFactory(Timer timer, Optional<String> additionalHeader, long connectionTimeoutMillis) {
35         this.timer = timer;
36         this.additionalHeader = additionalHeader;
37         this.connectionTimeoutMillis = connectionTimeoutMillis;
38     }
39
40     private static NetconfMessage loadHelloMessageTemplate() {
41         final String helloMessagePath = "/client_hello.xml";
42         try (InputStream is = NetconfClientSessionNegotiatorFactory.class.getResourceAsStream(helloMessagePath)) {
43             Preconditions.checkState(is != null, "Input stream from %s was null", helloMessagePath);
44             return new NetconfMessage(XmlUtil.readXmlToDocument(is));
45         } catch (SAXException | IOException e) {
46             throw new RuntimeException("Unable to load hello message", e);
47         }
48     }
49
50     @Override
51     public SessionNegotiator getSessionNegotiator(SessionListenerFactory sessionListenerFactory, Channel channel,
52             Promise promise) {
53         // Hello message needs to be recreated every time
54         NetconfMessage helloMessage = loadHelloMessageTemplate();
55         if(this.additionalHeader.isPresent()) {
56             helloMessage = new NetconfMessage(helloMessage.getDocument(), additionalHeader.get());
57         }
58         NetconfSessionPreferences proposal = new NetconfSessionPreferences(helloMessage);
59         return new NetconfClientSessionNegotiator(proposal, promise, channel, timer,
60                 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
61     }
62
63 }