2 * Copyright (c) 2016 Brocade Communication Systems and others. All rights reserved.
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
8 package org.opendaylight.netconf.callhome.mount;
10 import static java.util.Objects.requireNonNull;
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.sal.connect.api.DeviceActionFactory;
30 import org.opendaylight.netconf.sal.connect.api.SchemaResourceManager;
31 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseNetconfSchemas;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class CallHomeMountDispatcher implements NetconfClientDispatcher, CallHomeNetconfSubsystemListener {
39 private static final Logger LOG = LoggerFactory.getLogger(CallHomeMountDispatcher.class);
41 private final String topologyId;
42 private final EventExecutor eventExecutor;
43 private final ScheduledThreadPool keepaliveExecutor;
44 private final ThreadPool processingExecutor;
45 private final SchemaResourceManager schemaRepositoryProvider;
46 private final CallHomeMountSessionManager sessionManager;
47 private final DataBroker dataBroker;
48 private final DOMMountPointService mountService;
49 private final AAAEncryptionService encryptionService;
51 protected CallHomeTopology topology;
53 private final CloseCallback onCloseHandler = deviceContext -> {
54 LOG.info("Removing {} from Netconf Topology.", deviceContext.getId());
55 topology.disconnectNode(deviceContext.getId());
58 private final DeviceActionFactory deviceActionFactory;
59 private final BaseNetconfSchemas baseSchemas;
61 public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
62 final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
63 final SchemaResourceManager schemaRepositoryProvider,
64 final BaseNetconfSchemas baseSchemas, final DataBroker dataBroker,
65 final DOMMountPointService mountService,
66 final AAAEncryptionService encryptionService) {
67 this(topologyId, eventExecutor, keepaliveExecutor, processingExecutor, schemaRepositoryProvider, baseSchemas,
68 dataBroker, mountService, encryptionService, null);
71 public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
72 final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
73 final SchemaResourceManager schemaRepositoryProvider, final BaseNetconfSchemas baseSchemas,
74 final DataBroker dataBroker, final DOMMountPointService mountService,
75 final AAAEncryptionService encryptionService, final DeviceActionFactory deviceActionFactory) {
76 this.topologyId = topologyId;
77 this.eventExecutor = eventExecutor;
78 this.keepaliveExecutor = keepaliveExecutor;
79 this.processingExecutor = processingExecutor;
80 this.schemaRepositoryProvider = schemaRepositoryProvider;
81 this.deviceActionFactory = deviceActionFactory;
82 this.sessionManager = new CallHomeMountSessionManager();
83 this.baseSchemas = requireNonNull(baseSchemas);
84 this.dataBroker = dataBroker;
85 this.mountService = mountService;
86 this.encryptionService = encryptionService;
90 public Future<NetconfClientSession> createClient(final NetconfClientConfiguration clientConfiguration) {
91 return activateChannel(clientConfiguration);
95 public Future<Void> createReconnectingClient(final NetconfReconnectingClientConfiguration clientConfiguration) {
96 return activateChannel(clientConfiguration);
99 private <V> Future<V> activateChannel(final NetconfClientConfiguration conf) {
100 final InetSocketAddress remoteAddr = conf.getAddress();
101 final CallHomeMountSessionContext context = getSessionManager().getByAddress(remoteAddr);
102 LOG.info("Activating NETCONF channel for ip {} device context {}", remoteAddr, context);
103 if (context == null) {
104 return new FailedFuture<>(eventExecutor, new NullPointerException());
106 return context.activateNetconfChannel(conf.getSessionListener());
109 void createTopology() {
110 this.topology = new CallHomeTopology(topologyId, this, eventExecutor, keepaliveExecutor, processingExecutor,
111 schemaRepositoryProvider, dataBroker, mountService, encryptionService, baseSchemas,
112 deviceActionFactory);
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);
128 public CallHomeMountSessionManager getSessionManager() {
129 return sessionManager;