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.controller.netconf.cli;
10 import com.google.common.base.Optional;
11 import jline.console.completer.Completer;
12 import jline.console.completer.NullCompleter;
13 import org.opendaylight.controller.netconf.cli.commands.CommandDispatcher;
14 import org.opendaylight.controller.netconf.cli.io.ConsoleContext;
15 import org.opendaylight.controller.netconf.cli.io.ConsoleIO;
16 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
17 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
18 import org.opendaylight.controller.sal.core.api.RpcImplementation;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 * Implementation of RemoteDeviceHandler. Integrates cli with
24 * sal-netconf-connector.
26 public class NetconfDeviceConnectionHandler implements RemoteDeviceHandler<NetconfSessionPreferences> {
28 private final CommandDispatcher commandDispatcher;
29 private final SchemaContextRegistry schemaContextRegistry;
30 private final ConsoleIO console;
31 private final String deviceId;
33 private boolean up = false;
35 public NetconfDeviceConnectionHandler(final CommandDispatcher commandDispatcher,
36 final SchemaContextRegistry schemaContextRegistry, final ConsoleIO console, final String deviceId) {
37 this.commandDispatcher = commandDispatcher;
38 this.schemaContextRegistry = schemaContextRegistry;
39 this.console = console;
40 this.deviceId = deviceId;
44 public synchronized void onDeviceConnected(final SchemaContext context,
45 final NetconfSessionPreferences preferences, final RpcImplementation rpcImplementation) {
46 console.enterRootContext(new ConsoleContext() {
49 public Optional<String> getPrompt() {
50 return Optional.of(deviceId);
54 public Completer getCompleter() {
55 return new NullCompleter();
59 // TODO Load schemas for base netconf + inet types from remote device if
61 // TODO detect netconf base version
62 // TODO detect inet types version
63 commandDispatcher.addRemoteCommands(rpcImplementation, context);
64 schemaContextRegistry.setRemoteSchemaContext(context);
70 * @return true if connection was fully established
72 public synchronized boolean isUp() {
77 public synchronized void onDeviceDisconnected() {
78 console.leaveRootContext();
79 commandDispatcher.removeRemoteCommands();
80 schemaContextRegistry.setRemoteSchemaContext(null);
85 public void onDeviceFailed(Throwable throwable) {
90 public void onNotification(final CompositeNode compositeNode) {