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