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