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