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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.