2 * Copyright (c) 2015 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.netconf.topology.impl;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import io.netty.util.concurrent.EventExecutor;
14 import java.util.Collection;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
17 import org.opendaylight.controller.config.threadpool.ThreadPool;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
23 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
26 import org.opendaylight.netconf.client.NetconfClientDispatcher;
27 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
28 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
29 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalFacade;
30 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
31 import org.opendaylight.netconf.topology.AbstractNetconfTopology;
32 import org.opendaylight.netconf.topology.api.SchemaRepositoryProvider;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
40 import org.opendaylight.yangtools.concepts.ListenerRegistration;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class NetconfTopologyImpl extends AbstractNetconfTopology
46 implements DataTreeChangeListener<Node>, AutoCloseable {
48 private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyImpl.class);
50 private ListenerRegistration<NetconfTopologyImpl> datastoreListenerRegistration = null;
52 public NetconfTopologyImpl(final String topologyId, final NetconfClientDispatcher clientDispatcher,
53 final EventExecutor eventExecutor, final ScheduledThreadPool keepaliveExecutor,
54 final ThreadPool processingExecutor,
55 final SchemaRepositoryProvider schemaRepositoryProvider,
56 final DataBroker dataBroker, final DOMMountPointService mountPointService) {
57 super(topologyId, clientDispatcher, eventExecutor, keepaliveExecutor, processingExecutor,
58 schemaRepositoryProvider, dataBroker, mountPointService);
62 public void close() throws Exception {
63 // close all existing connectors, delete whole topology in datastore?
64 for (final NetconfConnectorDTO connectorDTO : activeConnectors.values()) {
67 activeConnectors.clear();
69 if (datastoreListenerRegistration != null) {
70 datastoreListenerRegistration.close();
71 datastoreListenerRegistration = null;
76 protected RemoteDeviceHandler<NetconfSessionPreferences> createSalFacade(final RemoteDeviceId id) {
77 return new NetconfDeviceSalFacade(id, mountPointService, dataBroker);
81 * Invoked by blueprint.
84 final WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
85 initTopology(wtx, LogicalDatastoreType.CONFIGURATION);
86 initTopology(wtx, LogicalDatastoreType.OPERATIONAL);
87 Futures.addCallback(wtx.submit(), new FutureCallback<Void>() {
89 public void onSuccess(final Void result) {
90 LOG.debug("topology initialization successful");
94 public void onFailure(final Throwable throwable) {
95 LOG.error("Unable to initialize netconf-topology, {}", throwable);
99 LOG.debug("Registering datastore listener");
100 datastoreListenerRegistration =
101 dataBroker.registerDataTreeChangeListener(
102 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
103 TopologyUtil.createTopologyListPath(topologyId).child(Node.class)), this);
109 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<Node>> collection) {
110 for (final DataTreeModification<Node> change : collection) {
111 final DataObjectModification<Node> rootNode = change.getRootNode();
112 switch (rootNode.getModificationType()) {
113 case SUBTREE_MODIFIED:
114 LOG.debug("Config for node {} updated", TopologyUtil.getNodeId(rootNode.getIdentifier()));
115 disconnectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()));
116 connectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()), rootNode.getDataAfter());
119 LOG.debug("Config for node {} created", TopologyUtil.getNodeId(rootNode.getIdentifier()));
120 if (activeConnectors.containsKey(TopologyUtil.getNodeId(rootNode.getIdentifier()))) {
121 LOG.warn("RemoteDevice{{}} was already configured, reconfiguring..",
122 TopologyUtil.getNodeId(rootNode.getIdentifier()));
123 disconnectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()));
125 connectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()), rootNode.getDataAfter());
128 LOG.debug("Config for node {} deleted", TopologyUtil.getNodeId(rootNode.getIdentifier()));
129 disconnectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()));
132 LOG.debug("Unsupported modification type: {}.", rootNode.getModificationType());
137 private void initTopology(final WriteTransaction wtx, final LogicalDatastoreType datastoreType) {
138 final NetworkTopology networkTopology = new NetworkTopologyBuilder().build();
139 final InstanceIdentifier<NetworkTopology> networkTopologyId =
140 InstanceIdentifier.builder(NetworkTopology.class).build();
141 wtx.merge(datastoreType, networkTopologyId, networkTopology);
142 final Topology topology = new TopologyBuilder().setTopologyId(new TopologyId(topologyId)).build();
143 wtx.merge(datastoreType,
144 networkTopologyId.child(Topology.class, new TopologyKey(new TopologyId(topologyId))), topology);