Prepare netconf to support YANG 1.1 actions
[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.sal.connect.api.DeviceActionFactory;
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 public class CallHomeMountDispatcher implements NetconfClientDispatcher, CallHomeNetconfSubsystemListener {
36
37     private static final Logger LOG = LoggerFactory.getLogger(CallHomeMountDispatcher.class);
38
39     private final String topologyId;
40     private final EventExecutor eventExecutor;
41     private final ScheduledThreadPool keepaliveExecutor;
42     private final ThreadPool processingExecutor;
43     private final SchemaRepositoryProvider schemaRepositoryProvider;
44     private final CallHomeMountSessionManager sessionManager;
45     private final DataBroker dataBroker;
46     private final DOMMountPointService mountService;
47     private final AAAEncryptionService encryptionService;
48
49     protected CallHomeTopology topology;
50
51     private final CloseCallback onCloseHandler = new CloseCallback() {
52         @Override
53         public void onClosed(final CallHomeMountSessionContext deviceContext) {
54             LOG.info("Removing {} from Netconf Topology.", deviceContext.getId());
55             topology.disconnectNode(deviceContext.getId());
56         }
57     };
58
59     private DeviceActionFactory deviceActionFactory;
60
61     public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
62                                    final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
63                                    final SchemaRepositoryProvider schemaRepositoryProvider, final DataBroker dataBroker,
64                                    final DOMMountPointService mountService,
65                                    final AAAEncryptionService encryptionService) {
66         this(topologyId, eventExecutor, keepaliveExecutor, processingExecutor, schemaRepositoryProvider, dataBroker,
67                 mountService, encryptionService, null);
68     }
69
70     public CallHomeMountDispatcher(final String topologyId, final EventExecutor eventExecutor,
71             final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor,
72             final SchemaRepositoryProvider schemaRepositoryProvider, final DataBroker dataBroker,
73             final DOMMountPointService mountService,
74             final AAAEncryptionService encryptionService, DeviceActionFactory deviceActionFactory) {
75         this.topologyId = topologyId;
76         this.eventExecutor = eventExecutor;
77         this.keepaliveExecutor = keepaliveExecutor;
78         this.processingExecutor = processingExecutor;
79         this.schemaRepositoryProvider = schemaRepositoryProvider;
80         this.deviceActionFactory = deviceActionFactory;
81         this.sessionManager = new CallHomeMountSessionManager();
82         this.dataBroker = dataBroker;
83         this.mountService = mountService;
84         this.encryptionService = encryptionService;
85     }
86
87     @Override
88     public Future<NetconfClientSession> createClient(final NetconfClientConfiguration clientConfiguration) {
89         return activateChannel(clientConfiguration);
90     }
91
92     @Override
93     public Future<Void> createReconnectingClient(final NetconfReconnectingClientConfiguration clientConfiguration) {
94         return activateChannel(clientConfiguration);
95     }
96
97     private <V> Future<V> activateChannel(final NetconfClientConfiguration conf) {
98         final InetSocketAddress remoteAddr = conf.getAddress();
99         final CallHomeMountSessionContext context = getSessionManager().getByAddress(remoteAddr);
100         LOG.info("Activating NETCONF channel for ip {} device context {}", remoteAddr, context);
101         if (context == null) {
102             return new FailedFuture<>(eventExecutor, new NullPointerException());
103         }
104         return context.activateNetconfChannel(conf.getSessionListener());
105     }
106
107     void createTopology() {
108         this.topology = new CallHomeTopology(topologyId, this, eventExecutor, keepaliveExecutor, processingExecutor,
109                 schemaRepositoryProvider, dataBroker, mountService, encryptionService, deviceActionFactory);
110     }
111
112     @Override
113     public void onNetconfSubsystemOpened(final CallHomeProtocolSessionContext session,
114                                          final CallHomeChannelActivator activator) {
115         final CallHomeMountSessionContext deviceContext =
116                 getSessionManager().createSession(session, activator, onCloseHandler);
117         final NodeId nodeId = deviceContext.getId();
118         final Node configNode = deviceContext.getConfigNode();
119         LOG.info("Provisioning fake config {}", configNode);
120         topology.connectNode(nodeId, configNode);
121     }
122
123     public CallHomeMountSessionManager getSessionManager() {
124         return sessionManager;
125     }
126 }