Remove BaseCallHomeTopology
[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.mount.CallHomeMountSessionContext.CloseCallback;
23 import org.opendaylight.netconf.callhome.protocol.CallHomeChannelActivator;
24 import org.opendaylight.netconf.callhome.protocol.CallHomeNetconfSubsystemListener;
25 import org.opendaylight.netconf.callhome.protocol.CallHomeProtocolSessionContext;
26 import org.opendaylight.netconf.client.NetconfClientDispatcher;
27 import org.opendaylight.netconf.client.NetconfClientSession;
28 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
29 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
30 import org.opendaylight.netconf.nettyutil.ReconnectFuture;
31 import org.opendaylight.netconf.sal.connect.api.DeviceActionFactory;
32 import org.opendaylight.netconf.sal.connect.api.SchemaResourceManager;
33 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseNetconfSchemas;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 // Non-final for testing
40 public class CallHomeMountDispatcher implements NetconfClientDispatcher, CallHomeNetconfSubsystemListener {
41     private static final Logger LOG = LoggerFactory.getLogger(CallHomeMountDispatcher.class);
42
43     private final CallHomeMountSessionManager sessionManager = new CallHomeMountSessionManager();
44     private final String topologyId;
45     private final EventExecutor eventExecutor;
46     private final ScheduledThreadPool keepaliveExecutor;
47     private final ThreadPool processingExecutor;
48     private final SchemaResourceManager schemaRepositoryProvider;
49     private final DataBroker dataBroker;
50     private final DOMMountPointService mountService;
51     private final AAAEncryptionService encryptionService;
52
53     protected CallHomeTopology topology;
54
55     private final CloseCallback onCloseHandler = deviceContext -> {
56         final var nodeId = deviceContext.getId();
57         LOG.info("Removing {} from Netconf Topology.", nodeId);
58         topology.disconnectNode(nodeId);
59     };
60
61     private final DeviceActionFactory deviceActionFactory;
62     private final BaseNetconfSchemas baseSchemas;
63
64     public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
65                                    final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
66                                    final SchemaResourceManager schemaRepositoryProvider,
67                                    final BaseNetconfSchemas baseSchemas, final DataBroker dataBroker,
68                                    final DOMMountPointService mountService,
69                                    final AAAEncryptionService encryptionService) {
70         this(topologyId, eventExecutor, keepaliveExecutor, processingExecutor, schemaRepositoryProvider, baseSchemas,
71             dataBroker, mountService, encryptionService, null);
72     }
73
74     public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
75             final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
76             final SchemaResourceManager schemaRepositoryProvider, final BaseNetconfSchemas baseSchemas,
77             final DataBroker dataBroker, final DOMMountPointService mountService,
78             final AAAEncryptionService encryptionService, final DeviceActionFactory deviceActionFactory) {
79         this.topologyId = topologyId;
80         this.eventExecutor = eventExecutor;
81         this.keepaliveExecutor = keepaliveExecutor;
82         this.processingExecutor = processingExecutor;
83         this.schemaRepositoryProvider = schemaRepositoryProvider;
84         this.deviceActionFactory = deviceActionFactory;
85         this.baseSchemas = requireNonNull(baseSchemas);
86         this.dataBroker = dataBroker;
87         this.mountService = mountService;
88         this.encryptionService = encryptionService;
89     }
90
91     @Override
92     public Future<NetconfClientSession> createClient(final NetconfClientConfiguration clientConfiguration) {
93         return activateChannel(clientConfiguration);
94     }
95
96     @Override
97     public ReconnectFuture createReconnectingClient(final NetconfReconnectingClientConfiguration clientConfiguration) {
98         return new SingleReconnectFuture(eventExecutor, activateChannel(clientConfiguration));
99     }
100
101     private Future<NetconfClientSession> activateChannel(final NetconfClientConfiguration conf) {
102         final InetSocketAddress remoteAddr = conf.getAddress();
103         final CallHomeMountSessionContext context = sessionManager().getByAddress(remoteAddr);
104         LOG.info("Activating NETCONF channel for ip {} device context {}", remoteAddr, context);
105         return context == null ? new FailedFuture<>(eventExecutor, new NullPointerException())
106             : context.activateNetconfChannel(conf.getSessionListener());
107     }
108
109     @Override
110     public void onNetconfSubsystemOpened(final CallHomeProtocolSessionContext session,
111                                          final CallHomeChannelActivator activator) {
112         final var deviceContext = sessionManager().createSession(session, activator, onCloseHandler);
113         if (deviceContext != null) {
114             final NodeId nodeId = deviceContext.getId();
115             final Node configNode = deviceContext.getConfigNode();
116             LOG.info("Provisioning fake config {}", configNode);
117             topology.connectNode(nodeId, configNode);
118         }
119     }
120
121     @VisibleForTesting
122     void createTopology() {
123         topology = new CallHomeTopology(topologyId, this, eventExecutor, keepaliveExecutor, processingExecutor,
124                 schemaRepositoryProvider, dataBroker, mountService, encryptionService, baseSchemas,
125                 deviceActionFactory);
126     }
127
128     @VisibleForTesting
129     CallHomeMountSessionManager sessionManager() {
130         return sessionManager;
131     }
132 }