X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fclient%2FNetconfClientSessionNegotiatorFactory.java;fp=opendaylight%2Fnetconf%2Fnetconf-client%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fclient%2FNetconfClientSessionNegotiatorFactory.java;h=db0b953bddf4f7c730fcd271bb7e5e603bcc0eb1;hb=a92d9d6a21a0f6ca8d2153795721f500eaf29ee9;hp=0000000000000000000000000000000000000000;hpb=fbc3092ca33990f0fc4a47f008786a416c484488;p=controller.git diff --git a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientSessionNegotiatorFactory.java b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientSessionNegotiatorFactory.java new file mode 100644 index 0000000000..db0b953bdd --- /dev/null +++ b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientSessionNegotiatorFactory.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.client; + +import com.google.common.base.Preconditions; +import io.netty.channel.Channel; +import io.netty.util.Timer; +import io.netty.util.concurrent.Promise; +import org.opendaylight.controller.netconf.api.NetconfMessage; +import org.opendaylight.controller.netconf.api.NetconfSessionPreferences; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import org.opendaylight.protocol.framework.SessionListenerFactory; +import org.opendaylight.protocol.framework.SessionNegotiator; +import org.opendaylight.protocol.framework.SessionNegotiatorFactory; +import org.xml.sax.SAXException; + +import java.io.IOException; +import java.io.InputStream; + +public class NetconfClientSessionNegotiatorFactory implements SessionNegotiatorFactory { + + private final Timer timer; + + public NetconfClientSessionNegotiatorFactory(Timer timer) { + this.timer = timer; + } + + private static NetconfMessage loadHelloMessageTemplate() { + final String helloMessagePath = "/client_hello.xml"; + try (InputStream is = NetconfClientSessionNegotiatorFactory.class.getResourceAsStream(helloMessagePath)) { + Preconditions.checkState(is != null, "Input stream from %s was null", helloMessagePath); + return new NetconfMessage(XmlUtil.readXmlToDocument(is)); + } catch (SAXException | IOException e) { + throw new RuntimeException("Unable to load hello message", e); + } + } + + @Override + public SessionNegotiator getSessionNegotiator(SessionListenerFactory sessionListenerFactory, Channel channel, + Promise promise) { + // Hello message needs to be recreated every time + NetconfSessionPreferences proposal = new NetconfSessionPreferences(loadHelloMessageTemplate()); + return new NetconfClientSessionNegotiator(proposal, promise, channel, timer, + sessionListenerFactory.getSessionListener()); + } + +}