2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.cli;
10 import com.google.common.base.Preconditions;
11 import io.netty.channel.nio.NioEventLoopGroup;
12 import io.netty.util.HashedWheelTimer;
13 import java.io.Closeable;
15 import java.io.IOException;
16 import java.io.InputStream;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import org.opendaylight.controller.netconf.cli.commands.CommandDispatcher;
21 import org.opendaylight.controller.netconf.cli.io.ConsoleIO;
22 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
23 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
24 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
25 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
26 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
27 import org.opendaylight.yangtools.yang.model.util.repo.AbstractCachingSchemaSourceProvider;
28 import org.opendaylight.yangtools.yang.model.util.repo.FilesystemSchemaCachingProvider;
29 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
30 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProviders;
33 * Manages connect/disconnect to 1 remote device
35 public class NetconfDeviceConnectionManager implements Closeable {
37 private final CommandDispatcher commandDispatcher;
38 private final SchemaContextRegistry schemaContextRegistry;
39 private final ConsoleIO console;
41 private final ExecutorService executor;
42 private final NioEventLoopGroup nettyThreadGroup;
43 private final NetconfClientDispatcherImpl netconfClientDispatcher;
46 private NetconfDeviceConnectionHandler handler;
47 private NetconfDevice device;
48 private NetconfDeviceCommunicator listener;
50 public NetconfDeviceConnectionManager(final CommandDispatcher commandDispatcher,
51 final CommandArgHandlerRegistry argumentHandlerRegistry, final SchemaContextRegistry schemaContextRegistry,
52 final ConsoleIO consoleIO) {
53 this.commandDispatcher = commandDispatcher;
54 this.schemaContextRegistry = schemaContextRegistry;
55 this.console = consoleIO;
57 executor = Executors.newSingleThreadExecutor();
58 nettyThreadGroup = new NioEventLoopGroup();
59 netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyThreadGroup, nettyThreadGroup,
60 new HashedWheelTimer());
63 // TODO we receive configBuilder in order to add SessionListener, Session
64 // Listener should not be part of config
65 public synchronized void connect(final String name, final NetconfClientConfigurationBuilder configBuilder) {
66 // TODO change IllegalState exceptions to custom ConnectionException
67 Preconditions.checkState(listener == null, "Already connected");
69 final RemoteDeviceId deviceId = new RemoteDeviceId(name);
71 handler = new NetconfDeviceConnectionHandler(commandDispatcher, schemaContextRegistry,
73 device = NetconfDevice.createNetconfDevice(deviceId, getGlobalNetconfSchemaProvider(), executor, handler);
74 listener = new NetconfDeviceCommunicator(deviceId, device);
75 configBuilder.withSessionListener(listener);
76 listener.initializeRemoteConnection(netconfClientDispatcher, configBuilder.build());
80 * Blocks thread until connection is fully established
82 public synchronized Set<String> connectBlocking(final String name, final NetconfClientConfigurationBuilder configBuilder) {
83 this.connect(name, configBuilder);
84 synchronized (handler) {
85 while (handler.isUp() == false) {
87 // TODO implement Timeout for unsuccessful connection
89 } catch (final InterruptedException e) {
90 Thread.currentThread().interrupt();
91 throw new IllegalArgumentException(e);
96 return commandDispatcher.getRemoteCommandIds();
99 public synchronized void disconnect() {
100 Preconditions.checkState(listener != null, "Not connected yet");
101 Preconditions.checkState(handler.isUp(), "Not connected yet");
109 private static AbstractCachingSchemaSourceProvider<String, InputStream> getGlobalNetconfSchemaProvider() {
110 // FIXME move to args
111 final String storageFile = "cache/schema";
112 final File directory = new File(storageFile);
113 final SchemaSourceProvider<String> defaultProvider = SchemaSourceProviders.noopProvider();
114 return FilesystemSchemaCachingProvider.createFromStringSourceProvider(defaultProvider, directory);
118 public void close() throws IOException {
119 executor.shutdownNow();
120 nettyThreadGroup.shutdownGracefully();