Bug 8153: Enforce check-style rules for netconf - mdsal-netconf-connector
[netconf.git] / netconf / tools / netconf-cli / src / main / java / org / opendaylight / netconf / cli / commands / CommandDispatcher.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.commands;
9
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;
20 import java.util.Map;
21 import java.util.Set;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
23 import org.opendaylight.netconf.cli.NetconfDeviceConnectionHandler;
24 import org.opendaylight.netconf.cli.NetconfDeviceConnectionManager;
25 import org.opendaylight.netconf.cli.commands.local.Close;
26 import org.opendaylight.netconf.cli.commands.local.Connect;
27 import org.opendaylight.netconf.cli.commands.local.Disconnect;
28 import org.opendaylight.netconf.cli.commands.local.Help;
29 import org.opendaylight.netconf.cli.commands.remote.RemoteCommand;
30 import org.opendaylight.netconf.cli.io.IOUtil;
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.spi.meta.ReactorException;
36 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
37
38 /**
39  * The registry of available commands local + remote. Created from schema contexts.
40  */
41 public class CommandDispatcher {
42
43     // TODO extract interface
44
45     private final Map<QName, Command> localCommands = Maps.newHashMap();
46     private final Map<String, QName> nameToQNameLocal = Maps.newHashMap();
47
48     private final Map<QName, Command> remoteCommands = Maps.newHashMap();
49     private final Map<String, QName> nameToQNameRemote = Maps.newHashMap();
50
51     public synchronized Map<QName, Command> getCommands() {
52         return Collections.unmodifiableMap(mergeCommands());
53     }
54
55     private Map<QName, Command> mergeCommands() {
56         // TODO cache this merged map
57         return mergeMaps(remoteCommands, localCommands);
58     }
59
60     private Map<String, QName> mergeCommandIds() {
61         // TODO cache this merged map
62         return mergeMaps(nameToQNameRemote, nameToQNameLocal);
63     }
64
65     private <K, V> Map<K, V> mergeMaps(final Map<K, V> remoteMap, final Map<K, V> localMap) {
66         final Map<K, V> mergedCommands = Maps.newHashMap();
67         mergedCommands.putAll(remoteMap);
68         mergedCommands.putAll(localMap);
69         return mergedCommands;
70     }
71
72     public synchronized Set<String> getCommandIds() {
73         return mergeCommandIds().keySet();
74     }
75
76     public synchronized Set<String> getRemoteCommandIds() {
77         return nameToQNameRemote.keySet();
78     }
79
80     public synchronized Optional<Command> getCommand(final String nameWithModule) {
81         final QName commandQName = mergeCommandIds().get(nameWithModule);
82         final Map<QName, Command> qNameCommandMap = mergeCommands();
83         if(commandQName == null || qNameCommandMap.containsKey(commandQName) == false) {
84             return Optional.absent();
85         }
86
87         return Optional.of(qNameCommandMap.get(commandQName));
88     }
89
90     public synchronized Optional<Command> getCommand(final QName qName) {
91         return Optional.fromNullable(mergeCommands().get(qName));
92     }
93
94     private static Optional<Command> getCommand(final Map<String, QName> commandNameMap, final Map<QName, Command> commands, final String nameWithModule) {
95         final QName qName = commandNameMap.get(nameWithModule);
96         if(qName == null)
97             return Optional.absent();
98
99         final Command command = commands.get(qName);
100         if(command == null) {
101             return Optional.absent();
102         }
103
104         return Optional.of(command);
105     }
106
107     public static final Collection<String> BASE_NETCONF_SCHEMA_PATHS = Lists.newArrayList("/schema/remote/ietf-netconf.yang",
108             "/schema/common/netconf-cli-ext.yang", "/schema/common/ietf-inet-types.yang");
109
110     public synchronized void addRemoteCommands(final DOMRpcService rpcService, final SchemaContext remoteSchema) {
111         this.addRemoteCommands(rpcService, remoteSchema, parseSchema(BASE_NETCONF_SCHEMA_PATHS));
112     }
113
114     public synchronized void addRemoteCommands(final DOMRpcService rpcService, final SchemaContext remoteSchema, final SchemaContext baseNetconfSchema) {
115         for (final SchemaContext context : Lists.newArrayList(remoteSchema, baseNetconfSchema)) {
116             for (final Module module : context.getModules()) {
117                 for (final RpcDefinition rpcDefinition : module.getRpcs()) {
118                     final Command command = RemoteCommand.fromRpc(rpcDefinition, rpcService);
119                     remoteCommands.put(rpcDefinition.getQName(), command);
120                     nameToQNameRemote.put(getCommandName(rpcDefinition, module), rpcDefinition.getQName());
121                 }
122             }
123         }
124     }
125
126     public synchronized void removeRemoteCommands() {
127         remoteCommands.clear();
128         nameToQNameRemote.clear();
129     }
130
131     public static final Collection<String> LOCAL_SCHEMA_PATHS = Lists.newArrayList("/schema/local/netconf-cli.yang", "/schema/common/netconf-cli-ext.yang",
132             "/schema/common/ietf-inet-types.yang");
133
134     public synchronized void addLocalCommands(final NetconfDeviceConnectionManager connectionManager, final SchemaContext localSchema, final Integer connectionTimeout) {
135         for (final Module module : localSchema.getModules()) {
136             for (final RpcDefinition rpcDefinition : module.getRpcs()) {
137
138                 // FIXME make local commands extensible
139                 // e.g. by yang extension defining java class to be instantiated
140                 // problem is with command specific resources
141                 // e.g. Help would need command registry
142                 final Command localCommand;
143                 if (rpcDefinition.getQName().equals(CommandConstants.HELP_QNAME)) {
144                     localCommand = Help.create(rpcDefinition, this);
145                 } else if (rpcDefinition.getQName().equals(CommandConstants.CLOSE_QNAME)) {
146                     localCommand = Close.create(rpcDefinition);
147                 } else if (rpcDefinition.getQName().equals(CommandConstants.CONNECT_QNAME)) {
148                     localCommand = Connect.create(rpcDefinition, connectionManager, connectionTimeout);
149                 } else if (rpcDefinition.getQName().equals(CommandConstants.DISCONNECT_QNAME)) {
150                     localCommand = Disconnect.create(rpcDefinition, connectionManager);
151                 } else {
152                     throw new IllegalStateException("No command implementation available for local command: " + rpcDefinition.getQName());
153                 }
154
155                 localCommands.put(localCommand.getCommandId(), localCommand);
156                 nameToQNameLocal.put(getCommandName(rpcDefinition, module), localCommand.getCommandId());
157             }
158         }
159     }
160
161     private static String getCommandName(final RpcDefinition rpcDefinition, final Module module) {
162         return IOUtil.qNameToKeyString(rpcDefinition.getQName(), module.getName());
163     }
164
165     public static SchemaContext parseSchema(final Collection<String> yangPath) {
166         final List<InputStream> streams = loadYangs(yangPath);
167         final SchemaContext schemaContext;
168         try {
169             schemaContext = YangParserTestUtils.parseYangStreams(streams);
170         } catch (ReactorException e) {
171             throw new RuntimeException("Unable to build schema context from " + streams, e);
172         }
173         return schemaContext;
174     }
175
176     private static List<InputStream> loadYangs(final Collection<String> yangPaths) {
177
178         return Lists.newArrayList(Collections2.transform(Lists.newArrayList(yangPaths),
179                 new Function<String, InputStream>() {
180                     @Override
181                     public InputStream apply(final String input) {
182                         final InputStream resourceAsStream = NetconfDeviceConnectionHandler.class.getResourceAsStream(input);
183                         Preconditions.checkNotNull(resourceAsStream, "File %s was null", input);
184                         return resourceAsStream;
185                     }
186                 }));
187     }
188 }