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.commands;
10 import com.google.common.base.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.Lists;
15 import com.google.common.collect.Maps;
16 import java.io.InputStream;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.List;
22 import org.opendaylight.controller.netconf.cli.NetconfDeviceConnectionHandler;
23 import org.opendaylight.controller.netconf.cli.NetconfDeviceConnectionManager;
24 import org.opendaylight.controller.netconf.cli.commands.local.Close;
25 import org.opendaylight.controller.netconf.cli.commands.local.Connect;
26 import org.opendaylight.controller.netconf.cli.commands.local.Disconnect;
27 import org.opendaylight.controller.netconf.cli.commands.local.Help;
28 import org.opendaylight.controller.netconf.cli.commands.remote.RemoteCommand;
29 import org.opendaylight.controller.netconf.cli.io.IOUtil;
30 import org.opendaylight.controller.sal.core.api.RpcImplementation;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
38 * The registry of available commands local + remote. Created from schema contexts.
40 public class CommandDispatcher {
42 // TODO extract interface
44 private final Map<QName, Command> localCommands = Maps.newHashMap();
45 private final Map<String, QName> nameToQNameLocal = Maps.newHashMap();
47 private final Map<QName, Command> remoteCommands = Maps.newHashMap();
48 private final Map<String, QName> nameToQNameRemote = Maps.newHashMap();
50 public synchronized Map<QName, Command> getCommands() {
51 return Collections.unmodifiableMap(mergeCommands());
54 private Map<QName, Command> mergeCommands() {
55 // TODO cache this merged map
56 return mergeMaps(remoteCommands, localCommands);
59 private Map<String, QName> mergeCommandIds() {
60 // TODO cache this merged map
61 return mergeMaps(nameToQNameRemote, nameToQNameLocal);
64 private <K, V> Map<K, V> mergeMaps(final Map<K, V> remoteMap, final Map<K, V> localMap) {
65 final Map<K, V> mergedCommands = Maps.newHashMap();
66 mergedCommands.putAll(remoteMap);
67 mergedCommands.putAll(localMap);
68 return mergedCommands;
71 public synchronized Set<String> getCommandIds() {
72 return mergeCommandIds().keySet();
75 public synchronized Set<String> getRemoteCommandIds() {
76 return nameToQNameRemote.keySet();
79 public synchronized Optional<Command> getCommand(final String nameWithModule) {
80 final QName commandQName = mergeCommandIds().get(nameWithModule);
81 final Map<QName, Command> qNameCommandMap = mergeCommands();
82 if(commandQName == null || qNameCommandMap.containsKey(commandQName) == false) {
83 return Optional.absent();
86 return Optional.of(qNameCommandMap.get(commandQName));
89 public synchronized Optional<Command> getCommand(final QName qName) {
90 return Optional.fromNullable(mergeCommands().get(qName));
93 private static Optional<Command> getCommand(final Map<String, QName> commandNameMap, final Map<QName, Command> commands, final String nameWithModule) {
94 final QName qName = commandNameMap.get(nameWithModule);
96 return Optional.absent();
98 final Command command = commands.get(qName);
100 return Optional.absent();
103 return Optional.of(command);
106 public static final Collection<String> BASE_NETCONF_SCHEMA_PATHS = Lists.newArrayList("/schema/remote/ietf-netconf.yang",
107 "/schema/common/netconf-cli-ext.yang", "/schema/common/ietf-inet-types.yang");
109 public synchronized void addRemoteCommands(final RpcImplementation rpcInvoker, final SchemaContext remoteSchema) {
110 this.addRemoteCommands(rpcInvoker, remoteSchema, parseSchema(BASE_NETCONF_SCHEMA_PATHS));
113 public synchronized void addRemoteCommands(final RpcImplementation rpcInvoker, final SchemaContext remoteSchema, final SchemaContext baseNetconfSchema) {
114 for (final SchemaContext context : Lists.newArrayList(remoteSchema, baseNetconfSchema)) {
115 for (final Module module : context.getModules()) {
116 for (final RpcDefinition rpcDefinition : module.getRpcs()) {
117 final Command command = RemoteCommand.fromRpc(rpcDefinition, rpcInvoker);
118 remoteCommands.put(rpcDefinition.getQName(), command);
119 nameToQNameRemote.put(getCommandName(rpcDefinition, module), rpcDefinition.getQName());
125 public synchronized void removeRemoteCommands() {
126 remoteCommands.clear();
127 nameToQNameRemote.clear();
130 public static final Collection<String> LOCAL_SCHEMA_PATHS = Lists.newArrayList("/schema/local/netconf-cli.yang", "/schema/common/netconf-cli-ext.yang",
131 "/schema/common/ietf-inet-types.yang");
133 public synchronized void addLocalCommands(final NetconfDeviceConnectionManager connectionManager, final SchemaContext localSchema, final Integer connectionTimeout) {
134 for (final Module module : localSchema.getModules()) {
135 for (final RpcDefinition rpcDefinition : module.getRpcs()) {
137 // FIXME make local commands extensible
138 // e.g. by yang extension defining java class to be instantiated
139 // problem is with command specific resources
140 // e.g. Help would need command registry
141 final Command localCommand;
142 if (rpcDefinition.getQName().equals(CommandConstants.HELP_QNAME)) {
143 localCommand = Help.create(rpcDefinition, this);
144 } else if (rpcDefinition.getQName().equals(CommandConstants.CLOSE_QNAME)) {
145 localCommand = Close.create(rpcDefinition);
146 } else if (rpcDefinition.getQName().equals(CommandConstants.CONNECT_QNAME)) {
147 localCommand = Connect.create(rpcDefinition, connectionManager, connectionTimeout);
148 } else if (rpcDefinition.getQName().equals(CommandConstants.DISCONNECT_QNAME)) {
149 localCommand = Disconnect.create(rpcDefinition, connectionManager);
151 throw new IllegalStateException("No command implementation available for local command: " + rpcDefinition.getQName());
154 localCommands.put(localCommand.getCommandId(), localCommand);
155 nameToQNameLocal.put(getCommandName(rpcDefinition, module), localCommand.getCommandId());
160 private static String getCommandName(final RpcDefinition rpcDefinition, final Module module) {
161 return IOUtil.qNameToKeyString(rpcDefinition.getQName(), module.getName());
164 public static SchemaContext parseSchema(final Collection<String> yangPath) {
165 final YangParserImpl yangParserImpl = new YangParserImpl();
166 // TODO change deprecated method
167 final Set<Module> modules = yangParserImpl.parseYangModelsFromStreams(loadYangs(yangPath));
168 return yangParserImpl.resolveSchemaContext(modules);
171 private static List<InputStream> loadYangs(final Collection<String> yangPaths) {
173 return Lists.newArrayList(Collections2.transform(Lists.newArrayList(yangPaths),
174 new Function<String, InputStream>() {
176 public InputStream apply(final String input) {
177 final InputStream resourceAsStream = NetconfDeviceConnectionHandler.class.getResourceAsStream(input);
178 Preconditions.checkNotNull(resourceAsStream, "File %s was null", input);
179 return resourceAsStream;