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