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