Use a local for nodeId
[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 package org.opendaylight.netconf.callhome.mount;
9
10 import static java.util.Objects.requireNonNull;
11
12 import io.netty.util.concurrent.EventExecutor;
13 import io.netty.util.concurrent.FailedFuture;
14 import io.netty.util.concurrent.Future;
15 import java.net.InetSocketAddress;
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.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
21 import org.opendaylight.netconf.callhome.mount.CallHomeMountSessionContext.CloseCallback;
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.nettyutil.ReconnectFuture;
30 import org.opendaylight.netconf.sal.connect.api.DeviceActionFactory;
31 import org.opendaylight.netconf.sal.connect.api.SchemaResourceManager;
32 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseNetconfSchemas;
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.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class CallHomeMountDispatcher implements NetconfClientDispatcher, CallHomeNetconfSubsystemListener {
39
40     private static final Logger LOG = LoggerFactory.getLogger(CallHomeMountDispatcher.class);
41
42     private final String topologyId;
43     private final EventExecutor eventExecutor;
44     private final ScheduledThreadPool keepaliveExecutor;
45     private final ThreadPool processingExecutor;
46     private final SchemaResourceManager schemaRepositoryProvider;
47     private final CallHomeMountSessionManager sessionManager;
48     private final DataBroker dataBroker;
49     private final DOMMountPointService mountService;
50     private final AAAEncryptionService encryptionService;
51
52     protected CallHomeTopology topology;
53
54     private final CloseCallback onCloseHandler = deviceContext -> {
55         final var nodeId = deviceContext.getId();
56         LOG.info("Removing {} from Netconf Topology.", nodeId);
57         topology.disconnectNode(nodeId);
58     };
59
60     private final DeviceActionFactory deviceActionFactory;
61     private final BaseNetconfSchemas baseSchemas;
62
63     public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
64                                    final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
65                                    final SchemaResourceManager schemaRepositoryProvider,
66                                    final BaseNetconfSchemas baseSchemas, final DataBroker dataBroker,
67                                    final DOMMountPointService mountService,
68                                    final AAAEncryptionService encryptionService) {
69         this(topologyId, eventExecutor, keepaliveExecutor, processingExecutor, schemaRepositoryProvider, baseSchemas,
70             dataBroker, mountService, encryptionService, null);
71     }
72
73     public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
74             final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
75             final SchemaResourceManager schemaRepositoryProvider, final BaseNetconfSchemas baseSchemas,
76             final DataBroker dataBroker, final DOMMountPointService mountService,
77             final AAAEncryptionService encryptionService, final DeviceActionFactory deviceActionFactory) {
78         this.topologyId = topologyId;
79         this.eventExecutor = eventExecutor;
80         this.keepaliveExecutor = keepaliveExecutor;
81         this.processingExecutor = processingExecutor;
82         this.schemaRepositoryProvider = schemaRepositoryProvider;
83         this.deviceActionFactory = deviceActionFactory;
84         this.sessionManager = new CallHomeMountSessionManager();
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 = getSessionManager().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     void createTopology() {
110         this.topology = new CallHomeTopology(topologyId, this, eventExecutor, keepaliveExecutor, processingExecutor,
111                 schemaRepositoryProvider, dataBroker, mountService, encryptionService, baseSchemas,
112                 deviceActionFactory);
113     }
114
115     @Override
116     public void onNetconfSubsystemOpened(final CallHomeProtocolSessionContext session,
117                                          final CallHomeChannelActivator activator) {
118         final CallHomeMountSessionContext deviceContext =
119                 getSessionManager().createSession(session, activator, onCloseHandler);
120         if (deviceContext != null) {
121             final NodeId nodeId = deviceContext.getId();
122             final Node configNode = deviceContext.getConfigNode();
123             LOG.info("Provisioning fake config {}", configNode);
124             topology.connectNode(nodeId, configNode);
125         }
126     }
127
128     public CallHomeMountSessionManager getSessionManager() {
129         return sessionManager;
130     }
131 }