407fa8e60ee87ab37e7a5d25e067d28eb3f16506
[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.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;
36
37 public class CallHomeMountDispatcher implements NetconfClientDispatcher, CallHomeNetconfSubsystemListener {
38
39     private static final Logger LOG = LoggerFactory.getLogger(CallHomeMountDispatcher.class);
40
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;
50
51     protected CallHomeTopology topology;
52
53     private final CloseCallback onCloseHandler = deviceContext -> {
54         LOG.info("Removing {} from Netconf Topology.", deviceContext.getId());
55         topology.disconnectNode(deviceContext.getId());
56     };
57
58     private final DeviceActionFactory deviceActionFactory;
59     private final BaseNetconfSchemas baseSchemas;
60
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);
69     }
70
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;
87     }
88
89     @Override
90     public Future<NetconfClientSession> createClient(final NetconfClientConfiguration clientConfiguration) {
91         return activateChannel(clientConfiguration);
92     }
93
94     @Override
95     public Future<Void> createReconnectingClient(final NetconfReconnectingClientConfiguration clientConfiguration) {
96         return activateChannel(clientConfiguration);
97     }
98
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());
105         }
106         return 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 }