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.xml.XmlUtil;
21 import org.opendaylight.protocol.framework.SessionListenerFactory;
22 import org.opendaylight.protocol.framework.SessionNegotiator;
23 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
24 import org.xml.sax.SAXException;
26 import com.google.common.base.Optional;
27 import com.google.common.base.Preconditions;
29 public class NetconfClientSessionNegotiatorFactory implements SessionNegotiatorFactory {
31 private final Timer timer;
33 private final Optional<String> additionalHeader;
35 public NetconfClientSessionNegotiatorFactory(Timer timer, Optional<String> additionalHeader) {
37 this.additionalHeader = additionalHeader;
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);
51 public SessionNegotiator getSessionNegotiator(SessionListenerFactory sessionListenerFactory, Channel channel,
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());
58 NetconfSessionPreferences proposal = new NetconfSessionPreferences(helloMessage);
59 return new NetconfClientSessionNegotiator(proposal, promise, channel, timer,
60 sessionListenerFactory.getSessionListener());