b8c2ba80905b31af884ecffede57efe211604e4d
[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.aaa.encrypt.AAAEncryptionService;
16 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
17 import org.opendaylight.controller.config.threadpool.ThreadPool;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
20 import org.opendaylight.netconf.callhome.mount.CallHomeMountSessionContext.CloseCallback;
21 import org.opendaylight.netconf.callhome.protocol.CallHomeChannelActivator;
22 import org.opendaylight.netconf.callhome.protocol.CallHomeNetconfSubsystemListener;
23 import org.opendaylight.netconf.callhome.protocol.CallHomeProtocolSessionContext;
24 import org.opendaylight.netconf.client.NetconfClientDispatcher;
25 import org.opendaylight.netconf.client.NetconfClientSession;
26 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
27 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
28 import org.opendaylight.netconf.topology.api.SchemaRepositoryProvider;
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 SchemaRepositoryProvider 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 = new CloseCallback() {
51         @Override
52         public void onClosed(final CallHomeMountSessionContext deviceContext) {
53             LOG.info("Removing {} from Netconf Topology.", deviceContext.getId());
54             topology.disconnectNode(deviceContext.getId());
55         }
56     };
57
58     public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
59             final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
60             final SchemaRepositoryProvider schemaRepositoryProvider, final DataBroker dataBroker,
61             final DOMMountPointService mountService, final AAAEncryptionService encryptionService) {
62         this.topologyId = topologyId;
63         this.eventExecutor = eventExecutor;
64         this.keepaliveExecutor = keepaliveExecutor;
65         this.processingExecutor = processingExecutor;
66         this.schemaRepositoryProvider = schemaRepositoryProvider;
67         this.sessionManager = new CallHomeMountSessionManager();
68         this.dataBroker = dataBroker;
69         this.mountService = mountService;
70         this.encryptionService = encryptionService;
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, keepaliveExecutor, processingExecutor,
95                 schemaRepositoryProvider, dataBroker, mountService, encryptionService);
96     }
97
98     @Override
99     public void onNetconfSubsystemOpened(final CallHomeProtocolSessionContext session,
100             final CallHomeChannelActivator activator) {
101         final CallHomeMountSessionContext deviceContext =
102                 getSessionManager().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 }