BUG 8839: Revert "Make netconf utilize encrypted passwords only"
[netconf.git] / netconf / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / mount / CallHomeMountDispatcher.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems 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.callhome.mount;
10
11 import io.netty.util.concurrent.EventExecutor;
12 import io.netty.util.concurrent.FailedFuture;
13 import io.netty.util.concurrent.Future;
14 import java.net.InetSocketAddress;
15 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
16 import org.opendaylight.controller.config.threadpool.ThreadPool;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
19 import org.opendaylight.netconf.callhome.mount.CallHomeMountSessionContext.CloseCallback;
20 import org.opendaylight.netconf.callhome.protocol.CallHomeChannelActivator;
21 import org.opendaylight.netconf.callhome.protocol.CallHomeNetconfSubsystemListener;
22 import org.opendaylight.netconf.callhome.protocol.CallHomeProtocolSessionContext;
23 import org.opendaylight.netconf.client.NetconfClientDispatcher;
24 import org.opendaylight.netconf.client.NetconfClientSession;
25 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
26 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
27 import org.opendaylight.netconf.topology.api.SchemaRepositoryProvider;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class CallHomeMountDispatcher implements NetconfClientDispatcher, CallHomeNetconfSubsystemListener {
34
35     private static final Logger LOG = LoggerFactory.getLogger(CallHomeMountDispatcher.class);
36
37     private final String topologyId;
38     private final EventExecutor eventExecutor;
39     private final ScheduledThreadPool keepaliveExecutor;
40     private final ThreadPool processingExecutor;
41     private final SchemaRepositoryProvider schemaRepositoryProvider;
42     private final CallHomeMountSessionManager sessionManager;
43     private final DataBroker dataBroker;
44     private final DOMMountPointService mountService;
45
46     protected CallHomeTopology topology;
47
48     private final CloseCallback onCloseHandler = new CloseCallback() {
49         @Override
50         public void onClosed(final CallHomeMountSessionContext deviceContext) {
51             LOG.info("Removing {} from Netconf Topology.", deviceContext.getId());
52             topology.disconnectNode(deviceContext.getId());
53         }
54     };
55
56     public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
57                                    final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
58                                    final SchemaRepositoryProvider schemaRepositoryProvider, final DataBroker dataBroker,
59                                    final DOMMountPointService mountService) {
60         this.topologyId = topologyId;
61         this.eventExecutor = eventExecutor;
62         this.keepaliveExecutor = keepaliveExecutor;
63         this.processingExecutor = processingExecutor;
64         this.schemaRepositoryProvider = schemaRepositoryProvider;
65         this.sessionManager = new CallHomeMountSessionManager();
66         this.dataBroker = dataBroker;
67         this.mountService = mountService;
68     }
69
70     @Override
71     public Future<NetconfClientSession> createClient(final NetconfClientConfiguration clientConfiguration) {
72         return activateChannel(clientConfiguration);
73     }
74
75     @Override
76     public Future<Void> createReconnectingClient(final NetconfReconnectingClientConfiguration clientConfiguration) {
77         return activateChannel(clientConfiguration);
78     }
79
80     private <V> Future<V> activateChannel(final NetconfClientConfiguration conf) {
81         final InetSocketAddress remoteAddr = conf.getAddress();
82         final CallHomeMountSessionContext context = getSessionManager().getByAddress(remoteAddr);
83         LOG.info("Activating NETCONF channel for ip {} device context {}", remoteAddr, context);
84         if (context == null) {
85             return new FailedFuture<>(eventExecutor, new NullPointerException());
86         }
87         return context.activateNetconfChannel(conf.getSessionListener());
88     }
89
90     void createTopology() {
91         this.topology = new CallHomeTopology(topologyId, this, eventExecutor, keepaliveExecutor, processingExecutor,
92                 schemaRepositoryProvider, dataBroker, mountService);
93     }
94
95     @Override
96     public void onNetconfSubsystemOpened(final CallHomeProtocolSessionContext session,
97                                          final CallHomeChannelActivator activator) {
98         final CallHomeMountSessionContext deviceContext =
99                 getSessionManager().createSession(session, activator, onCloseHandler);
100         final NodeId nodeId = deviceContext.getId();
101         final Node configNode = deviceContext.getConfigNode();
102         LOG.info("Provisioning fake config {}", configNode);
103         topology.connectNode(nodeId, configNode);
104     }
105
106     public CallHomeMountSessionManager getSessionManager() {
107         return sessionManager;
108     }
109 }