Merge "add partial match for non-module capabilities - to enable deviations in base...
[netconf.git] / netconf / netconf-topology / src / main / java / org / opendaylight / netconf / topology / impl / NetconfTopologyImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.topology.impl;
10
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.netconf.topology.util.TopologyUtil;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
47 import org.opendaylight.yangtools.concepts.ListenerRegistration;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public class NetconfTopologyImpl extends AbstractNetconfTopology implements DataTreeChangeListener<Node>, AutoCloseable {
53
54     private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyImpl.class);
55
56         private ListenerRegistration<NetconfTopologyImpl> datastoreListenerRegistration = null;
57
58     public NetconfTopologyImpl(final String topologyId, final NetconfClientDispatcher clientDispatcher,
59                                final BindingAwareBroker bindingAwareBroker, final Broker domBroker,
60                                final EventExecutor eventExecutor, final ScheduledThreadPool keepaliveExecutor,
61                                final ThreadPool processingExecutor, final SchemaRepositoryProvider schemaRepositoryProvider) {
62         super(topologyId, clientDispatcher,
63                 bindingAwareBroker, domBroker, eventExecutor,
64                 keepaliveExecutor, processingExecutor, schemaRepositoryProvider);
65         registerToSal(this, this);
66     }
67
68     @Override
69     public void close() throws Exception {
70         // close all existing connectors, delete whole topology in datastore?
71         for (NetconfConnectorDTO connectorDTO : activeConnectors.values()) {
72             connectorDTO.getCommunicator().close();
73         }
74         activeConnectors.clear();
75
76         if (datastoreListenerRegistration != null) {
77             datastoreListenerRegistration.close();
78             datastoreListenerRegistration = null;
79         }
80     }
81
82     @Override
83     protected RemoteDeviceHandler<NetconfSessionPreferences> createSalFacade(RemoteDeviceId id, Broker domBroker, BindingAwareBroker bindingBroker) {
84         return new NetconfDeviceSalFacade(id, domBroker, bindingAwareBroker);
85     }
86
87     @Override
88     public void registerMountPoint(ActorContext context, NodeId nodeId) {
89         throw new UnsupportedOperationException("MountPoint registration is not supported in regular topology, this happens automaticaly in the netconf pipeline");
90     }
91
92     @Override
93     public void registerMountPoint(ActorContext context, NodeId nodeId, ActorRef masterRef) {
94         throw new UnsupportedOperationException("MountPoint registration is not supported in regular topology, this happens automaticaly in the netconf pipeline");
95     }
96
97     @Override
98     public void unregisterMountPoint(NodeId nodeId) {
99         throw new UnsupportedOperationException("MountPoint registration is not supported in regular topology, this happens automaticaly in the netconf pipeline");
100     }
101
102     @Override
103     public ConnectionStatusListenerRegistration registerConnectionStatusListener(NodeId node, RemoteDeviceHandler<NetconfSessionPreferences> listener) {
104         throw new UnsupportedOperationException("Registering a listener on a regular netconf device is not supported(supported only in clustered netconf topology)");
105     }
106
107     @Override
108     public void onSessionInitiated(ProviderContext session) {
109         dataBroker = session.getSALService(DataBroker.class);
110
111         final WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
112         initTopology(wtx, LogicalDatastoreType.CONFIGURATION);
113         initTopology(wtx, LogicalDatastoreType.OPERATIONAL);
114         Futures.addCallback(wtx.submit(), new FutureCallback<Void>() {
115             @Override
116             public void onSuccess(Void result) {
117                 LOG.debug("topology initialization successful");
118             }
119
120             @Override
121             public void onFailure(Throwable t) {
122                 LOG.error("Unable to initialize netconf-topology, {}", t);
123             }
124         });
125
126         LOG.debug("Registering datastore listener");
127         datastoreListenerRegistration =
128                 dataBroker.registerDataTreeChangeListener(
129                         new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
130                                 TopologyUtil.createTopologyListPath(topologyId).child(Node.class)), this);
131
132
133     }
134
135     @Override
136     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> collection) {
137         for (DataTreeModification<Node> change : collection) {
138             final DataObjectModification<Node> rootNode = change.getRootNode();
139             switch (rootNode.getModificationType()) {
140                 case SUBTREE_MODIFIED:
141                     LOG.debug("Config for node {} updated", TopologyUtil.getNodeId(rootNode.getIdentifier()));
142                     disconnectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()));
143                     connectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()), rootNode.getDataAfter());
144                     break;
145                 case WRITE:
146                     LOG.debug("Config for node {} created", TopologyUtil.getNodeId(rootNode.getIdentifier()));
147                     if (activeConnectors.containsKey(TopologyUtil.getNodeId(rootNode.getIdentifier()))) {
148                         LOG.warn("RemoteDevice{{}} was already configured, reconfiguring..", TopologyUtil.getNodeId(rootNode.getIdentifier()));
149                         disconnectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()));
150                     }
151                     connectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()), rootNode.getDataAfter());
152                     break;
153                 case DELETE:
154                     LOG.debug("Config for node {} deleted", TopologyUtil.getNodeId(rootNode.getIdentifier()));
155                     disconnectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()));
156                     break;
157             }
158         }
159     }
160
161     private void initTopology(final WriteTransaction wtx, final LogicalDatastoreType datastoreType) {
162         final NetworkTopology networkTopology = new NetworkTopologyBuilder().build();
163         final InstanceIdentifier<NetworkTopology> networkTopologyId = InstanceIdentifier.builder(NetworkTopology.class).build();
164         wtx.merge(datastoreType, networkTopologyId, networkTopology);
165         final Topology topology = new TopologyBuilder().setTopologyId(new TopologyId(topologyId)).build();
166         wtx.merge(datastoreType, networkTopologyId.child(Topology.class, new TopologyKey(new TopologyId(topologyId))), topology);
167     }
168
169 }