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.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.net.InetSocketAddress;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import org.opendaylight.netconf.cli.commands.CommandDispatcher;
21 import org.opendaylight.netconf.cli.io.ConsoleIO;
22 import org.opendaylight.netconf.client.NetconfClientDispatcherImpl;
23 import org.opendaylight.netconf.client.conf.NetconfClientConfigurationBuilder;
24 import org.opendaylight.netconf.sal.connect.netconf.NetconfDevice;
25 import org.opendaylight.netconf.sal.connect.netconf.NetconfDevice.SchemaResourcesDTO;
26 import org.opendaylight.netconf.sal.connect.netconf.NetconfStateSchemas.NetconfStateSchemasResolverImpl;
27 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
28 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
29 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
30 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
31 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
32 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
33 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
34 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
37 * Manages connect/disconnect to 1 remote device
39 public class NetconfDeviceConnectionManager implements Closeable {
41 private final CommandDispatcher commandDispatcher;
42 private final SchemaContextRegistry schemaContextRegistry;
43 private final ConsoleIO console;
45 private final ExecutorService executor;
46 private final NioEventLoopGroup nettyThreadGroup;
47 private final NetconfClientDispatcherImpl netconfClientDispatcher;
49 private static final String CACHE = "cache/schema";
52 private NetconfDeviceConnectionHandler handler;
53 private NetconfDevice device;
54 private NetconfDeviceCommunicator listener;
56 public NetconfDeviceConnectionManager(final CommandDispatcher commandDispatcher,
57 final CommandArgHandlerRegistry argumentHandlerRegistry, final SchemaContextRegistry schemaContextRegistry,
58 final ConsoleIO consoleIO) {
59 this.commandDispatcher = commandDispatcher;
60 this.schemaContextRegistry = schemaContextRegistry;
61 this.console = consoleIO;
63 executor = Executors.newSingleThreadExecutor();
64 nettyThreadGroup = new NioEventLoopGroup();
65 netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyThreadGroup, nettyThreadGroup,
66 new HashedWheelTimer());
69 // TODO we receive configBuilder in order to add SessionListener, Session
70 // Listener should not be part of config
71 public synchronized void connect(final String name, final InetSocketAddress address, final NetconfClientConfigurationBuilder configBuilder) {
72 // TODO change IllegalState exceptions to custom ConnectionException
73 Preconditions.checkState(listener == null, "Already connected");
75 final RemoteDeviceId deviceId = new RemoteDeviceId(name, address);
77 handler = new NetconfDeviceConnectionHandler(commandDispatcher, schemaContextRegistry,
80 final SharedSchemaRepository repository = new SharedSchemaRepository("repo");
81 final SchemaContextFactory schemaContextFactory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
82 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(repository, YangTextSchemaSource.class, new File(CACHE));
83 repository.registerSchemaSourceListener(cache);
84 repository.registerSchemaSourceListener(TextToASTTransformer.create(repository, repository));
86 device = new NetconfDevice(new SchemaResourcesDTO(repository, schemaContextFactory, new NetconfStateSchemasResolverImpl()),
87 deviceId, handler, executor, true);
88 listener = new NetconfDeviceCommunicator(deviceId, device);
89 configBuilder.withSessionListener(listener);
90 listener.initializeRemoteConnection(netconfClientDispatcher, configBuilder.build());
94 * Blocks thread until connection is fully established
96 public synchronized Set<String> connectBlocking(final String name, final InetSocketAddress address, final NetconfClientConfigurationBuilder configBuilder) {
97 this.connect(name, address, configBuilder);
98 synchronized (handler) {
99 while (handler.isUp() == false) {
101 // TODO implement Timeout for unsuccessful connection
103 } catch (final InterruptedException e) {
104 Thread.currentThread().interrupt();
105 throw new IllegalArgumentException(e);
110 return commandDispatcher.getRemoteCommandIds();
113 public synchronized void disconnect() {
114 Preconditions.checkState(listener != null, "Not connected yet");
115 Preconditions.checkState(handler.isUp(), "Not connected yet");
124 public void close() throws IOException {
125 executor.shutdownNow();
126 nettyThreadGroup.shutdownGracefully();