2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.client;
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;
23 import java.io.IOException;
24 import java.io.InputStream;
26 public class NetconfClientSessionNegotiatorFactory implements SessionNegotiatorFactory {
28 private final Timer timer;
30 public NetconfClientSessionNegotiatorFactory(Timer timer) {
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);
45 public SessionNegotiator getSessionNegotiator(SessionListenerFactory sessionListenerFactory, Channel channel,
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());