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 io.netty.channel.Channel;
12 import io.netty.util.Timer;
13 import io.netty.util.concurrent.Promise;
15 import java.io.IOException;
16 import java.io.InputStream;
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;
28 import com.google.common.base.Optional;
29 import com.google.common.base.Preconditions;
31 public class NetconfClientSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfMessage, NetconfClientSession, NetconfClientSessionListener> {
33 private final Optional<NetconfHelloMessageAdditionalHeader> additionalHeader;
34 private final long connectionTimeoutMillis;
35 private final Timer timer;
37 public NetconfClientSessionNegotiatorFactory(Timer timer, Optional<NetconfHelloMessageAdditionalHeader> additionalHeader, long connectionTimeoutMillis) {
38 this.timer = Preconditions.checkNotNull(timer);
39 this.additionalHeader = additionalHeader;
40 this.connectionTimeoutMillis = connectionTimeoutMillis;
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);
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();
59 if(this.additionalHeader.isPresent()) {
60 helloMessage = new NetconfHelloMessage(helloMessage.getDocument(), additionalHeader.get());
62 helloMessage = new NetconfHelloMessage(helloMessage.getDocument());
64 NetconfSessionPreferences proposal = new NetconfSessionPreferences(helloMessage);
65 return new NetconfClientSessionNegotiator(proposal, promise, channel, timer,
66 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);