Factor out SchemaResourceManager
[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 io.netty.util.concurrent.EventExecutor;
11 import io.netty.util.concurrent.FailedFuture;
12 import io.netty.util.concurrent.Future;
13 import java.net.InetSocketAddress;
14 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
15 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
16 import org.opendaylight.controller.config.threadpool.ThreadPool;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
19 import org.opendaylight.netconf.callhome.mount.CallHomeMountSessionContext.CloseCallback;
20 import org.opendaylight.netconf.callhome.protocol.CallHomeChannelActivator;
21 import org.opendaylight.netconf.callhome.protocol.CallHomeNetconfSubsystemListener;
22 import org.opendaylight.netconf.callhome.protocol.CallHomeProtocolSessionContext;
23 import org.opendaylight.netconf.client.NetconfClientDispatcher;
24 import org.opendaylight.netconf.client.NetconfClientSession;
25 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
26 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
27 import org.opendaylight.netconf.sal.connect.api.DeviceActionFactory;
28 import org.opendaylight.netconf.sal.connect.api.SchemaResourceManager;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class CallHomeMountDispatcher implements NetconfClientDispatcher, CallHomeNetconfSubsystemListener {
35
36     private static final Logger LOG = LoggerFactory.getLogger(CallHomeMountDispatcher.class);
37
38     private final String topologyId;
39     private final EventExecutor eventExecutor;
40     private final ScheduledThreadPool keepaliveExecutor;
41     private final ThreadPool processingExecutor;
42     private final SchemaResourceManager schemaRepositoryProvider;
43     private final CallHomeMountSessionManager sessionManager;
44     private final DataBroker dataBroker;
45     private final DOMMountPointService mountService;
46     private final AAAEncryptionService encryptionService;
47
48     protected CallHomeTopology topology;
49
50     private final CloseCallback onCloseHandler = deviceContext -> {
51         LOG.info("Removing {} from Netconf Topology.", deviceContext.getId());
52         topology.disconnectNode(deviceContext.getId());
53     };
54
55     private final DeviceActionFactory deviceActionFactory;
56
57     public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
58                                    final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
59                                    final SchemaResourceManager schemaRepositoryProvider, final DataBroker dataBroker,
60                                    final DOMMountPointService mountService,
61                                    final AAAEncryptionService encryptionService) {
62         this(topologyId, eventExecutor, keepaliveExecutor, processingExecutor, schemaRepositoryProvider, dataBroker,
63                 mountService, encryptionService, null);
64     }
65
66     public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
67             final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
68             final SchemaResourceManager schemaRepositoryProvider, final DataBroker dataBroker,
69             final DOMMountPointService mountService,
70             final AAAEncryptionService encryptionService, final DeviceActionFactory deviceActionFactory) {
71         this.topologyId = topologyId;
72         this.eventExecutor = eventExecutor;
73         this.keepaliveExecutor = keepaliveExecutor;
74         this.processingExecutor = processingExecutor;
75         this.schemaRepositoryProvider = schemaRepositoryProvider;
76         this.deviceActionFactory = deviceActionFactory;
77         this.sessionManager = new CallHomeMountSessionManager();
78         this.dataBroker = dataBroker;
79         this.mountService = mountService;
80         this.encryptionService = encryptionService;
81     }
82
83     @Override
84     public Future<NetconfClientSession> createClient(final NetconfClientConfiguration clientConfiguration) {
85         return activateChannel(clientConfiguration);
86     }
87
88     @Override
89     public Future<Void> createReconnectingClient(final NetconfReconnectingClientConfiguration clientConfiguration) {
90         return activateChannel(clientConfiguration);
91     }
92
93     private <V> Future<V> activateChannel(final NetconfClientConfiguration conf) {
94         final InetSocketAddress remoteAddr = conf.getAddress();
95         final CallHomeMountSessionContext context = getSessionManager().getByAddress(remoteAddr);
96         LOG.info("Activating NETCONF channel for ip {} device context {}", remoteAddr, context);
97         if (context == null) {
98             return new FailedFuture<>(eventExecutor, new NullPointerException());
99         }
100         return context.activateNetconfChannel(conf.getSessionListener());
101     }
102
103     void createTopology() {
104         this.topology = new CallHomeTopology(topologyId, this, eventExecutor, keepaliveExecutor, processingExecutor,
105                 schemaRepositoryProvider, dataBroker, mountService, encryptionService, deviceActionFactory);
106     }
107
108     @Override
109     public void onNetconfSubsystemOpened(final CallHomeProtocolSessionContext session,
110                                          final CallHomeChannelActivator activator) {
111         final CallHomeMountSessionContext deviceContext =
112                 getSessionManager().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     public CallHomeMountSessionManager getSessionManager() {
122         return sessionManager;
123     }
124 }