2a73412d2c20166e51166f1cbb2701761aeeaef0
[netconf.git] / netconf / tools / netconf-cli / src / main / java / org / opendaylight / netconf / cli / NetconfDeviceConnectionManager.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 package org.opendaylight.netconf.cli;
9
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;
14 import java.io.File;
15 import java.io.IOException;
16 import java.net.InetSocketAddress;
17 import java.util.Set;
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.NetconfStateSchemas.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;
36
37 /**
38  * Manages connect/disconnect to 1 remote device
39  */
40 public class NetconfDeviceConnectionManager implements Closeable {
41
42     private final CommandDispatcher commandDispatcher;
43     private final SchemaContextRegistry schemaContextRegistry;
44     private final ConsoleIO console;
45
46     private final ExecutorService executor;
47     private final NioEventLoopGroup nettyThreadGroup;
48     private final NetconfClientDispatcherImpl netconfClientDispatcher;
49
50     private static final String CACHE = "cache/schema";
51
52     // Connection
53     private NetconfDeviceConnectionHandler handler;
54     private NetconfDevice device;
55     private NetconfDeviceCommunicator listener;
56
57     public NetconfDeviceConnectionManager(final CommandDispatcher commandDispatcher,
58             final CommandArgHandlerRegistry argumentHandlerRegistry, final SchemaContextRegistry schemaContextRegistry,
59             final ConsoleIO consoleIO) {
60         this.commandDispatcher = commandDispatcher;
61         this.schemaContextRegistry = schemaContextRegistry;
62         this.console = consoleIO;
63
64         executor = Executors.newSingleThreadExecutor();
65         nettyThreadGroup = new NioEventLoopGroup();
66         netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyThreadGroup, nettyThreadGroup,
67                 new HashedWheelTimer());
68     }
69
70     // TODO we receive configBuilder in order to add SessionListener, Session
71     // Listener should not be part of config
72     public synchronized void connect(final String name, final InetSocketAddress address, final NetconfClientConfigurationBuilder configBuilder) {
73         // TODO change IllegalState exceptions to custom ConnectionException
74         Preconditions.checkState(listener == null, "Already connected");
75
76         final RemoteDeviceId deviceId = new RemoteDeviceId(name, address);
77
78         handler = new NetconfDeviceConnectionHandler(commandDispatcher, schemaContextRegistry,
79                 console, name);
80
81         final SharedSchemaRepository repository = new SharedSchemaRepository("repo");
82         final SchemaContextFactory schemaContextFactory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
83         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(repository, YangTextSchemaSource.class, new File(CACHE));
84         repository.registerSchemaSourceListener(cache);
85         repository.registerSchemaSourceListener(TextToASTTransformer.create(repository, repository));
86         final SchemaResourcesDTO schemaResourcesDTO = new SchemaResourcesDTO(repository, schemaContextFactory, new NetconfStateSchemasResolverImpl());
87
88         device = new NetconfDeviceBuilder()
89                 .setReconnectOnSchemasChange(true)
90                 .setSchemaResourcesDTO(schemaResourcesDTO)
91                 .setGlobalProcessingExecutor(executor)
92                 .setId(deviceId)
93                 .setSalFacade(handler)
94                 .build();
95         listener = new NetconfDeviceCommunicator(deviceId, device);
96         configBuilder.withSessionListener(listener);
97         listener.initializeRemoteConnection(netconfClientDispatcher, configBuilder.build());
98     }
99
100     /**
101      * Blocks thread until connection is fully established
102      */
103     public synchronized Set<String> connectBlocking(final String name, final InetSocketAddress address, final NetconfClientConfigurationBuilder configBuilder) {
104         this.connect(name, address, configBuilder);
105         synchronized (handler) {
106             while (handler.isUp() == false) {
107                 try {
108                     // TODO implement Timeout for unsuccessful connection
109                     handler.wait();
110                 } catch (final InterruptedException e) {
111                     Thread.currentThread().interrupt();
112                     throw new IllegalArgumentException(e);
113                 }
114             }
115         }
116
117         return commandDispatcher.getRemoteCommandIds();
118     }
119
120     public synchronized void disconnect() {
121         Preconditions.checkState(listener != null, "Not connected yet");
122         Preconditions.checkState(handler.isUp(), "Not connected yet");
123         listener.close();
124         listener = null;
125         device = null;
126         handler.close();
127         handler = null;
128     }
129
130     @Override
131     public void close() throws IOException {
132         executor.shutdownNow();
133         nettyThreadGroup.shutdownGracefully();
134     }
135 }