Make netconf utilize encrypted passwords only
[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 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.aaa.encrypt.AAAEncryptionService;
17 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
18 import org.opendaylight.controller.config.threadpool.ThreadPool;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
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.api.SchemaRepositoryProvider;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.opendaylight.yangtools.concepts.ListenerRegistration;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class NetconfTopologyImpl extends AbstractNetconfTopology implements DataTreeChangeListener<Node>, AutoCloseable {
47
48     private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyImpl.class);
49
50     private ListenerRegistration<NetconfTopologyImpl> datastoreListenerRegistration = null;
51
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                                final AAAEncryptionService encryptionService) {
58         super(topologyId, clientDispatcher, eventExecutor, keepaliveExecutor, processingExecutor,
59                 schemaRepositoryProvider, dataBroker, mountPointService, encryptionService);
60     }
61
62     @Override
63     public void close() throws Exception {
64         // close all existing connectors, delete whole topology in datastore?
65         for (final NetconfConnectorDTO connectorDTO : activeConnectors.values()) {
66             connectorDTO.close();
67         }
68         activeConnectors.clear();
69
70         if (datastoreListenerRegistration != null) {
71             datastoreListenerRegistration.close();
72             datastoreListenerRegistration = null;
73         }
74     }
75
76     @Override
77     protected RemoteDeviceHandler<NetconfSessionPreferences> createSalFacade(final RemoteDeviceId id) {
78         return new NetconfDeviceSalFacade(id, mountPointService, dataBroker);
79     }
80
81     /**
82      * Invoke by blueprint
83      */
84     public void init() {
85         final WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
86         initTopology(wtx, LogicalDatastoreType.CONFIGURATION);
87         initTopology(wtx, LogicalDatastoreType.OPERATIONAL);
88         Futures.addCallback(wtx.submit(), new FutureCallback<Void>() {
89             @Override
90             public void onSuccess(final Void result) {
91                 LOG.debug("topology initialization successful");
92             }
93
94             @Override
95             public void onFailure(final Throwable t) {
96                 LOG.error("Unable to initialize netconf-topology, {}", t);
97             }
98         });
99
100         LOG.debug("Registering datastore listener");
101         datastoreListenerRegistration =
102                 dataBroker.registerDataTreeChangeListener(
103                         new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
104                                 TopologyUtil.createTopologyListPath(topologyId).child(Node.class)), this);
105
106
107     }
108
109     @Override
110     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<Node>> collection) {
111         for (final DataTreeModification<Node> change : collection) {
112             final DataObjectModification<Node> rootNode = change.getRootNode();
113             switch (rootNode.getModificationType()) {
114                 case SUBTREE_MODIFIED:
115                     LOG.debug("Config for node {} updated", TopologyUtil.getNodeId(rootNode.getIdentifier()));
116                     disconnectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()));
117                     connectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()), rootNode.getDataAfter());
118                     break;
119                 case WRITE:
120                     LOG.debug("Config for node {} created", TopologyUtil.getNodeId(rootNode.getIdentifier()));
121                     if (activeConnectors.containsKey(TopologyUtil.getNodeId(rootNode.getIdentifier()))) {
122                         LOG.warn("RemoteDevice{{}} was already configured, reconfiguring..", TopologyUtil.getNodeId(rootNode.getIdentifier()));
123                         disconnectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()));
124                     }
125                     connectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()), rootNode.getDataAfter());
126                     break;
127                 case DELETE:
128                     LOG.debug("Config for node {} deleted", TopologyUtil.getNodeId(rootNode.getIdentifier()));
129                     disconnectNode(TopologyUtil.getNodeId(rootNode.getIdentifier()));
130                     break;
131             }
132         }
133     }
134
135     private void initTopology(final WriteTransaction wtx, final LogicalDatastoreType datastoreType) {
136         final NetworkTopology networkTopology = new NetworkTopologyBuilder().build();
137         final InstanceIdentifier<NetworkTopology> networkTopologyId = InstanceIdentifier.builder(NetworkTopology.class).build();
138         wtx.merge(datastoreType, networkTopologyId, networkTopology);
139         final Topology topology = new TopologyBuilder().setTopologyId(new TopologyId(topologyId)).build();
140         wtx.merge(datastoreType, networkTopologyId.child(Topology.class, new TopologyKey(new TopologyId(topologyId))), topology);
141     }
142
143 }