RemoteDeviceDataBroker proxy
[netconf.git] / opendaylight / 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 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.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
26 import org.opendaylight.controller.sal.core.api.Broker;
27 import org.opendaylight.netconf.client.NetconfClientDispatcher;
28 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
29 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
30 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalFacade;
31 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
32 import org.opendaylight.netconf.topology.AbstractNetconfTopology;
33 import org.opendaylight.netconf.topology.SchemaRepositoryProvider;
34 import org.opendaylight.netconf.topology.pipeline.TopologyMountPointFacade.ConnectionStatusListenerRegistration;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yangtools.concepts.ListenerRegistration;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class NetconfTopologyImpl extends AbstractNetconfTopology implements DataTreeChangeListener<Node>, AutoCloseable {
42
43     private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyImpl.class);
44
45         private ListenerRegistration<NetconfTopologyImpl> datastoreListenerRegistration = null;
46
47     public NetconfTopologyImpl(final String topologyId, final NetconfClientDispatcher clientDispatcher,
48                                final BindingAwareBroker bindingAwareBroker, final Broker domBroker,
49                                final EventExecutor eventExecutor, final ScheduledThreadPool keepaliveExecutor,
50                                final ThreadPool processingExecutor, final SchemaRepositoryProvider schemaRepositoryProvider) {
51         super(topologyId, clientDispatcher,
52                 bindingAwareBroker, domBroker, eventExecutor,
53                 keepaliveExecutor, processingExecutor, schemaRepositoryProvider);
54         registerToSal(this, this);
55     }
56
57     @Override
58     public void close() throws Exception {
59         // close all existing connectors, delete whole topology in datastore?
60         for (NetconfConnectorDTO connectorDTO : activeConnectors.values()) {
61             connectorDTO.getCommunicator().close();
62         }
63         activeConnectors.clear();
64
65         if (datastoreListenerRegistration != null) {
66             datastoreListenerRegistration.close();
67             datastoreListenerRegistration = null;
68         }
69     }
70
71     @Override
72     protected RemoteDeviceHandler<NetconfSessionPreferences> createSalFacade(RemoteDeviceId id, Broker domBroker, BindingAwareBroker bindingBroker, long defaultRequestTimeoutMillis) {
73         return new NetconfDeviceSalFacade(id, domBroker, bindingAwareBroker, defaultRequestTimeoutMillis);
74     }
75
76     @Override
77     public void registerMountPoint(ActorContext context, NodeId nodeId) {
78         throw new UnsupportedOperationException("MountPoint registration is not supported in regular topology, this happens automaticaly in the netconf pipeline");
79     }
80
81     @Override
82     public void registerMountPoint(ActorContext context, NodeId nodeId, ActorRef masterRef) {
83         throw new UnsupportedOperationException("MountPoint registration is not supported in regular topology, this happens automaticaly in the netconf pipeline");
84     }
85
86     @Override
87     public void unregisterMountPoint(NodeId nodeId) {
88         throw new UnsupportedOperationException("MountPoint registration is not supported in regular topology, this happens automaticaly in the netconf pipeline");
89     }
90
91     @Override
92     public ConnectionStatusListenerRegistration registerConnectionStatusListener(NodeId node, RemoteDeviceHandler<NetconfSessionPreferences> listener) {
93         throw new UnsupportedOperationException("Registering a listener on a regular netconf device is not supported(supported only in clustered netconf topology)");
94     }
95
96     @Override
97     public void onSessionInitiated(ProviderContext session) {
98         dataBroker = session.getSALService(DataBroker.class);
99
100         LOG.warn("Registering datastore listener");
101         datastoreListenerRegistration =
102                 dataBroker.registerDataTreeChangeListener(
103                         new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, createTopologyId(topologyId).child(Node.class)), this);
104     }
105
106     @Override
107     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> collection) {
108         for (DataTreeModification<Node> change : collection) {
109             final DataObjectModification<Node> rootNode = change.getRootNode();
110             switch (rootNode.getModificationType()) {
111                 case SUBTREE_MODIFIED:
112                     LOG.debug("Config for node {} updated", getNodeId(rootNode.getIdentifier()));
113                     disconnectNode(getNodeId(rootNode.getIdentifier()));
114                     connectNode(getNodeId(rootNode.getIdentifier()), rootNode.getDataAfter());
115                     break;
116                 case WRITE:
117                     LOG.debug("Config for node {} created", getNodeId(rootNode.getIdentifier()));
118                     if (activeConnectors.containsKey(getNodeId(rootNode.getIdentifier()))) {
119                         LOG.warn("RemoteDevice{{}} was already configured, reconfiguring..", getNodeId(rootNode.getIdentifier()));
120                         disconnectNode(getNodeId(rootNode.getIdentifier()));
121                     }
122                     connectNode(getNodeId(rootNode.getIdentifier()), rootNode.getDataAfter());
123                     break;
124                 case DELETE:
125                     LOG.debug("Config for node {} deleted", getNodeId(rootNode.getIdentifier()));
126                     disconnectNode(getNodeId(rootNode.getIdentifier()));
127                     break;
128             }
129         }
130     }
131
132 }