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 akka.actor.ActorContext;
12 import akka.actor.ActorRef;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import io.netty.util.concurrent.EventExecutor;
16 import java.util.Collection;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
19 import org.opendaylight.controller.config.threadpool.ThreadPool;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
28 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
29 import org.opendaylight.controller.sal.core.api.Broker;
30 import org.opendaylight.netconf.client.NetconfClientDispatcher;
31 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
32 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
33 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalFacade;
34 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
35 import org.opendaylight.netconf.topology.AbstractNetconfTopology;
36 import org.opendaylight.netconf.topology.SchemaRepositoryProvider;
37 import org.opendaylight.netconf.topology.pipeline.TopologyMountPointFacade.ConnectionStatusListenerRegistration;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yangtools.concepts.ListenerRegistration;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class NetconfTopologyImpl extends AbstractNetconfTopology implements DataTreeChangeListener<Node>, AutoCloseable {
53 private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyImpl.class);
55 private ListenerRegistration<NetconfTopologyImpl> datastoreListenerRegistration = null;
57 public NetconfTopologyImpl(final String topologyId, final NetconfClientDispatcher clientDispatcher,
58 final BindingAwareBroker bindingAwareBroker, final Broker domBroker,
59 final EventExecutor eventExecutor, final ScheduledThreadPool keepaliveExecutor,
60 final ThreadPool processingExecutor, final SchemaRepositoryProvider schemaRepositoryProvider) {
61 super(topologyId, clientDispatcher,
62 bindingAwareBroker, domBroker, eventExecutor,
63 keepaliveExecutor, processingExecutor, schemaRepositoryProvider);
64 registerToSal(this, this);
68 public void close() throws Exception {
69 // close all existing connectors, delete whole topology in datastore?
70 for (NetconfConnectorDTO connectorDTO : activeConnectors.values()) {
71 connectorDTO.getCommunicator().close();
73 activeConnectors.clear();
75 if (datastoreListenerRegistration != null) {
76 datastoreListenerRegistration.close();
77 datastoreListenerRegistration = null;
82 protected RemoteDeviceHandler<NetconfSessionPreferences> createSalFacade(RemoteDeviceId id, Broker domBroker, BindingAwareBroker bindingBroker) {
83 return new NetconfDeviceSalFacade(id, domBroker, bindingAwareBroker);
87 public void registerMountPoint(ActorContext context, NodeId nodeId) {
88 throw new UnsupportedOperationException("MountPoint registration is not supported in regular topology, this happens automaticaly in the netconf pipeline");
92 public void registerMountPoint(ActorContext context, NodeId nodeId, ActorRef masterRef) {
93 throw new UnsupportedOperationException("MountPoint registration is not supported in regular topology, this happens automaticaly in the netconf pipeline");
97 public void unregisterMountPoint(NodeId nodeId) {
98 throw new UnsupportedOperationException("MountPoint registration is not supported in regular topology, this happens automaticaly in the netconf pipeline");
102 public ConnectionStatusListenerRegistration registerConnectionStatusListener(NodeId node, RemoteDeviceHandler<NetconfSessionPreferences> listener) {
103 throw new UnsupportedOperationException("Registering a listener on a regular netconf device is not supported(supported only in clustered netconf topology)");
107 public void onSessionInitiated(ProviderContext session) {
108 dataBroker = session.getSALService(DataBroker.class);
110 final WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
111 initTopology(wtx, LogicalDatastoreType.CONFIGURATION);
112 initTopology(wtx, LogicalDatastoreType.OPERATIONAL);
113 Futures.addCallback(wtx.submit(), new FutureCallback<Void>() {
115 public void onSuccess(Void result) {
116 LOG.debug("topology initialization successful");
120 public void onFailure(Throwable t) {
121 LOG.error("Unable to initialize netconf-topology, {}", t);
125 LOG.debug("Registering datastore listener");
126 datastoreListenerRegistration =
127 dataBroker.registerDataTreeChangeListener(
128 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, createTopologyId(topologyId).child(Node.class)), this);
134 public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> collection) {
135 for (DataTreeModification<Node> change : collection) {
136 final DataObjectModification<Node> rootNode = change.getRootNode();
137 switch (rootNode.getModificationType()) {
138 case SUBTREE_MODIFIED:
139 LOG.debug("Config for node {} updated", getNodeId(rootNode.getIdentifier()));
140 disconnectNode(getNodeId(rootNode.getIdentifier()));
141 connectNode(getNodeId(rootNode.getIdentifier()), rootNode.getDataAfter());
144 LOG.debug("Config for node {} created", getNodeId(rootNode.getIdentifier()));
145 if (activeConnectors.containsKey(getNodeId(rootNode.getIdentifier()))) {
146 LOG.warn("RemoteDevice{{}} was already configured, reconfiguring..", getNodeId(rootNode.getIdentifier()));
147 disconnectNode(getNodeId(rootNode.getIdentifier()));
149 connectNode(getNodeId(rootNode.getIdentifier()), rootNode.getDataAfter());
152 LOG.debug("Config for node {} deleted", getNodeId(rootNode.getIdentifier()));
153 disconnectNode(getNodeId(rootNode.getIdentifier()));
159 private void initTopology(final WriteTransaction wtx, final LogicalDatastoreType datastoreType) {
160 final NetworkTopology networkTopology = new NetworkTopologyBuilder().build();
161 final InstanceIdentifier<NetworkTopology> networkTopologyId = InstanceIdentifier.builder(NetworkTopology.class).build();
162 wtx.merge(datastoreType, networkTopologyId, networkTopology);
163 final Topology topology = new TopologyBuilder().setTopologyId(new TopologyId(topologyId)).build();
164 wtx.merge(datastoreType, networkTopologyId.child(Topology.class, new TopologyKey(new TopologyId(topologyId))), topology);