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.NetconfDeviceBuilder;
27 import org.opendaylight.netconf.sal.connect.netconf.NetconfStateSchemasResolverImpl;
28 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
29 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
30 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
31 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
34 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
35 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
38 * Manages connect/disconnect to 1 remote device
40 public class NetconfDeviceConnectionManager implements Closeable {
42 private final CommandDispatcher commandDispatcher;
43 private final SchemaContextRegistry schemaContextRegistry;
44 private final ConsoleIO console;
46 private final ExecutorService executor;
47 private final NioEventLoopGroup nettyThreadGroup;
48 private final NetconfClientDispatcherImpl netconfClientDispatcher;
50 private static final String CACHE = "cache/schema";
51 private static final int RPC_LIMIT = 0;
54 private NetconfDeviceConnectionHandler handler;
55 private NetconfDevice device;
56 private NetconfDeviceCommunicator listener;
58 public NetconfDeviceConnectionManager(final CommandDispatcher commandDispatcher,
59 final CommandArgHandlerRegistry argumentHandlerRegistry, final SchemaContextRegistry schemaContextRegistry,
60 final ConsoleIO consoleIO) {
61 this.commandDispatcher = commandDispatcher;
62 this.schemaContextRegistry = schemaContextRegistry;
63 this.console = consoleIO;
65 executor = Executors.newSingleThreadExecutor();
66 nettyThreadGroup = new NioEventLoopGroup();
67 netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyThreadGroup, nettyThreadGroup,
68 new HashedWheelTimer());
71 // TODO we receive configBuilder in order to add SessionListener, Session
72 // Listener should not be part of config
73 public synchronized void connect(final String name, final InetSocketAddress address, final NetconfClientConfigurationBuilder configBuilder) {
74 // TODO change IllegalState exceptions to custom ConnectionException
75 Preconditions.checkState(listener == null, "Already connected");
77 final RemoteDeviceId deviceId = new RemoteDeviceId(name, address);
79 handler = new NetconfDeviceConnectionHandler(commandDispatcher, schemaContextRegistry,
82 final SharedSchemaRepository repository = new SharedSchemaRepository("repo");
83 final SchemaContextFactory schemaContextFactory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
84 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(repository, YangTextSchemaSource.class, new File(CACHE));
85 repository.registerSchemaSourceListener(cache);
86 repository.registerSchemaSourceListener(TextToASTTransformer.create(repository, repository));
87 final SchemaResourcesDTO schemaResourcesDTO = new SchemaResourcesDTO(repository, schemaContextFactory, new NetconfStateSchemasResolverImpl());
89 device = new NetconfDeviceBuilder()
90 .setReconnectOnSchemasChange(true)
91 .setSchemaResourcesDTO(schemaResourcesDTO)
92 .setGlobalProcessingExecutor(executor)
94 .setSalFacade(handler)
96 listener = new NetconfDeviceCommunicator(deviceId, device, RPC_LIMIT);
98 configBuilder.withSessionListener(listener);
99 listener.initializeRemoteConnection(netconfClientDispatcher, configBuilder.build());
103 * Blocks thread until connection is fully established
105 public synchronized Set<String> connectBlocking(final String name, final InetSocketAddress address, final NetconfClientConfigurationBuilder configBuilder) {
106 this.connect(name, address, configBuilder);
107 synchronized (handler) {
108 while (handler.isUp() == false) {
110 // TODO implement Timeout for unsuccessful connection
112 } catch (final InterruptedException e) {
113 Thread.currentThread().interrupt();
114 throw new IllegalArgumentException(e);
119 return commandDispatcher.getRemoteCommandIds();
122 public synchronized void disconnect() {
123 Preconditions.checkState(listener != null, "Not connected yet");
124 Preconditions.checkState(handler.isUp(), "Not connected yet");
133 public void close() throws IOException {
134 executor.shutdownNow();
135 nettyThreadGroup.shutdownGracefully();