Remove NetconfTopology
[netconf.git] / apps / 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 package org.opendaylight.netconf.callhome.mount;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import io.netty.util.concurrent.EventExecutor;
14 import io.netty.util.concurrent.FailedFuture;
15 import io.netty.util.concurrent.Future;
16 import java.net.InetSocketAddress;
17 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
18 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
19 import org.opendaylight.controller.config.threadpool.ThreadPool;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
22 import org.opendaylight.netconf.callhome.protocol.CallHomeChannelActivator;
23 import org.opendaylight.netconf.callhome.protocol.CallHomeNetconfSubsystemListener;
24 import org.opendaylight.netconf.callhome.protocol.CallHomeProtocolSessionContext;
25 import org.opendaylight.netconf.client.NetconfClientDispatcher;
26 import org.opendaylight.netconf.client.NetconfClientSession;
27 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
28 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
29 import org.opendaylight.netconf.client.mdsal.api.BaseNetconfSchemas;
30 import org.opendaylight.netconf.client.mdsal.api.CredentialProvider;
31 import org.opendaylight.netconf.client.mdsal.api.DeviceActionFactory;
32 import org.opendaylight.netconf.client.mdsal.api.SchemaResourceManager;
33 import org.opendaylight.netconf.client.mdsal.api.SslHandlerFactoryProvider;
34 import org.opendaylight.netconf.nettyutil.ReconnectFuture;
35 import org.opendaylight.netconf.topology.spi.NetconfNodeUtils;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Reference;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 @Component(service = { CallHomeMountDispatcher.class, CallHomeNetconfSubsystemListener.class }, immediate = true)
44 // Non-final for testing
45 public class CallHomeMountDispatcher implements NetconfClientDispatcher, CallHomeNetconfSubsystemListener {
46     private static final Logger LOG = LoggerFactory.getLogger(CallHomeMountDispatcher.class);
47
48     private final CallHomeMountSessionManager sessionManager = new CallHomeMountSessionManager();
49     private final String topologyId;
50     private final EventExecutor eventExecutor;
51     private final ScheduledThreadPool keepaliveExecutor;
52     private final ThreadPool processingExecutor;
53     private final SchemaResourceManager schemaRepositoryProvider;
54     private final DataBroker dataBroker;
55     private final DOMMountPointService mountService;
56     private final AAAEncryptionService encryptionService;
57     private final CredentialProvider credentialProvider;
58     private final SslHandlerFactoryProvider sslHandlerFactoryProvider;
59
60     protected CallHomeTopology topology;
61
62     private final DeviceActionFactory deviceActionFactory;
63     private final BaseNetconfSchemas baseSchemas;
64
65
66     public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
67             final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
68             final SchemaResourceManager schemaRepositoryProvider, final BaseNetconfSchemas baseSchemas,
69             final DataBroker dataBroker, final DOMMountPointService mountService,
70             final AAAEncryptionService encryptionService, final CredentialProvider credentialProvider,
71             final SslHandlerFactoryProvider sslHandlerFactoryProvider) {
72         this(topologyId, eventExecutor, keepaliveExecutor, processingExecutor, schemaRepositoryProvider, baseSchemas,
73             dataBroker, mountService, encryptionService, credentialProvider, sslHandlerFactoryProvider, null);
74     }
75
76     @Activate
77     public CallHomeMountDispatcher(
78             @Reference(target = "(type=global-event-executor)") final EventExecutor eventExecutor,
79             @Reference(target = "(type=global-netconf-ssh-scheduled-executor)")
80                 final ScheduledThreadPool keepaliveExecutor,
81             @Reference(target = "(type=global-netconf-processing-executor)") final ThreadPool processingExecutor,
82             @Reference final SchemaResourceManager schemaRepositoryProvider,
83             @Reference final BaseNetconfSchemas baseSchemas, @Reference final DataBroker dataBroker,
84             @Reference final DOMMountPointService mountService, @Reference final AAAEncryptionService encryptionService,
85             @Reference final CredentialProvider credentialProvider,
86             @Reference final SslHandlerFactoryProvider sslHandlerFactoryProvider,
87             @Reference final DeviceActionFactory deviceActionFactory) {
88         this(NetconfNodeUtils.DEFAULT_TOPOLOGY_NAME, eventExecutor, keepaliveExecutor, processingExecutor,
89             schemaRepositoryProvider, baseSchemas, dataBroker, mountService, encryptionService, credentialProvider,
90             sslHandlerFactoryProvider, deviceActionFactory);
91     }
92
93     public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
94             final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
95             final SchemaResourceManager schemaRepositoryProvider, final BaseNetconfSchemas baseSchemas,
96             final DataBroker dataBroker, final DOMMountPointService mountService,
97             final AAAEncryptionService encryptionService, final CredentialProvider credentialProvider,
98             final SslHandlerFactoryProvider sslHandlerFactoryProvider, final DeviceActionFactory deviceActionFactory) {
99         this.topologyId = topologyId;
100         this.eventExecutor = eventExecutor;
101         this.keepaliveExecutor = keepaliveExecutor;
102         this.processingExecutor = processingExecutor;
103         this.schemaRepositoryProvider = schemaRepositoryProvider;
104         this.deviceActionFactory = deviceActionFactory;
105         this.baseSchemas = requireNonNull(baseSchemas);
106         this.dataBroker = dataBroker;
107         this.mountService = mountService;
108         this.encryptionService = encryptionService;
109         this.credentialProvider = requireNonNull(credentialProvider);
110         this.sslHandlerFactoryProvider = requireNonNull(sslHandlerFactoryProvider);
111     }
112
113     @Override
114     public Future<NetconfClientSession> createClient(final NetconfClientConfiguration clientConfiguration) {
115         return activateChannel(clientConfiguration);
116     }
117
118     @Override
119     public ReconnectFuture createReconnectingClient(final NetconfReconnectingClientConfiguration clientConfiguration) {
120         return new SingleReconnectFuture(eventExecutor, activateChannel(clientConfiguration));
121     }
122
123     private Future<NetconfClientSession> activateChannel(final NetconfClientConfiguration conf) {
124         final InetSocketAddress remoteAddr = conf.getAddress();
125         final CallHomeMountSessionContext context = sessionManager().getByAddress(remoteAddr);
126         LOG.info("Activating NETCONF channel for ip {} device context {}", remoteAddr, context);
127         return context == null ? new FailedFuture<>(eventExecutor, new NullPointerException())
128             : context.activateNetconfChannel(conf.getSessionListener());
129     }
130
131     @Override
132     public void onNetconfSubsystemOpened(final CallHomeProtocolSessionContext session,
133                                          final CallHomeChannelActivator activator) {
134         final var deviceContext = sessionManager().createSession(session, activator, device -> {
135             final var nodeId = device.getId();
136             LOG.info("Removing {} from Netconf Topology.", nodeId);
137             topology.disconnectNode(nodeId);
138         });
139         if (deviceContext != null) {
140             final Node configNode = deviceContext.getConfigNode();
141             LOG.info("Provisioning fake config {}", configNode);
142             topology.connectNode(configNode);
143         }
144     }
145
146     @VisibleForTesting
147     void createTopology() {
148         topology = new CallHomeTopology(topologyId, this, eventExecutor, keepaliveExecutor, processingExecutor,
149                 schemaRepositoryProvider, dataBroker, mountService, encryptionService, baseSchemas,
150                 deviceActionFactory, credentialProvider, sslHandlerFactoryProvider);
151     }
152
153     @VisibleForTesting
154     CallHomeMountSessionManager sessionManager() {
155         return sessionManager;
156     }
157 }