2 * Copyright (c) 2016 Inocybe Technologies 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
8 package org.opendaylight.netconf.topology.impl;
10 import com.google.common.util.concurrent.CheckedFuture;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import javax.annotation.Nullable;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.netconf.topology.api.NetconfConnectorFactory;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.HostBuilder;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPwBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.login.pw.LoginPasswordBuilder;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Created by adetalhouet on 2016-11-03.
43 public class NetconfConnectorFactoryImpl implements NetconfConnectorFactory {
45 private static final Logger LOG = LoggerFactory.getLogger(NetconfConnectorFactoryImpl.class);
47 private static final InstanceIdentifier<Topology> TOPOLOGY_PATH = InstanceIdentifier.create(NetworkTopology.class)
48 .child(Topology.class, new TopologyKey(new TopologyId("topology-netconf")));
51 public Node newInstance(final DataBroker dataBroker,
52 final String instanceName,
55 final String username,
56 final String password,
57 final Boolean tcpOnly,
58 final Boolean reconnectOnSchemaChange) {
60 final NodeId nodeId = new NodeId(instanceName);
61 final NodeKey nodeKey = new NodeKey(nodeId);
62 final Credentials credentials = new LoginPwBuilder()
64 new LoginPasswordBuilder()
65 .setUsername(username)
66 .setPassword(password)
69 final Host host = HostBuilder.getDefaultInstance(address);
70 final PortNumber portNumber = new PortNumber(port);
71 final NetconfNode netconfNode = new NetconfNodeBuilder()
74 .setCredentials(credentials)
76 .setReconnectOnChangedSchema(reconnectOnSchemaChange)
78 final Node node = new NodeBuilder()
81 .addAugmentation(NetconfNode.class, netconfNode)
84 final InstanceIdentifier<Node> nodePath = TOPOLOGY_PATH.child(Node.class, nodeKey);
85 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
86 transaction.put(LogicalDatastoreType.CONFIGURATION, nodePath, node);
87 final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = transaction.submit();
88 Futures.addCallback(submitFuture, new FutureCallback<Void>() {
90 public void onSuccess(@Nullable final Void result) {
91 LOG.debug("Node {} was successfully added to the topology", instanceName);
95 public void onFailure(final Throwable throwable) {
96 LOG.error("Node {} creation failed: {}", instanceName, throwable);
98 }, MoreExecutors.directExecutor());