a3e98b31dad5fc96c2ede181fbe14290cbe1f7e5
[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.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     private static final int RPC_LIMIT = 0;
52
53     // Connection
54     private NetconfDeviceConnectionHandler handler;
55     private NetconfDevice device;
56     private NetconfDeviceCommunicator listener;
57
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;
64
65         executor = Executors.newSingleThreadExecutor();
66         nettyThreadGroup = new NioEventLoopGroup();
67         netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyThreadGroup, nettyThreadGroup,
68                 new HashedWheelTimer());
69     }
70
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");
76
77         final RemoteDeviceId deviceId = new RemoteDeviceId(name, address);
78
79         handler = new NetconfDeviceConnectionHandler(commandDispatcher, schemaContextRegistry,
80                 console, name);
81
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());
88
89         device = new NetconfDeviceBuilder()
90                 .setReconnectOnSchemasChange(true)
91                 .setSchemaResourcesDTO(schemaResourcesDTO)
92                 .setGlobalProcessingExecutor(executor)
93                 .setId(deviceId)
94                 .setSalFacade(handler)
95                 .build();
96         listener = new NetconfDeviceCommunicator(deviceId, device, RPC_LIMIT);
97
98         configBuilder.withSessionListener(listener);
99         listener.initializeRemoteConnection(netconfClientDispatcher, configBuilder.build());
100     }
101
102     /**
103      * Blocks thread until connection is fully established
104      */
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) {
109                 try {
110                     // TODO implement Timeout for unsuccessful connection
111                     handler.wait();
112                 } catch (final InterruptedException e) {
113                     Thread.currentThread().interrupt();
114                     throw new IllegalArgumentException(e);
115                 }
116             }
117         }
118
119         return commandDispatcher.getRemoteCommandIds();
120     }
121
122     public synchronized void disconnect() {
123         Preconditions.checkState(listener != null, "Not connected yet");
124         Preconditions.checkState(handler.isUp(), "Not connected yet");
125         listener.close();
126         listener = null;
127         device = null;
128         handler.close();
129         handler = null;
130     }
131
132     @Override
133     public void close() throws IOException {
134         executor.shutdownNow();
135         nettyThreadGroup.shutdownGracefully();
136     }
137 }