Merge "Distributed Datastore integration with config subsystem Updated with the usage...
[controller.git] / opendaylight / netconf / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / commands / AbstractCommand.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.commands;
9
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.input.InputDefinition;
14 import org.opendaylight.controller.netconf.cli.commands.output.OutputDefinition;
15 import org.opendaylight.controller.netconf.cli.io.ConsoleContext;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
19
20 public abstract class AbstractCommand implements Command {
21
22     private final QName qName;
23     private final InputDefinition args;
24     private final OutputDefinition output;
25     private final String description;
26
27     public AbstractCommand(final QName qName, final InputDefinition args, final OutputDefinition output,
28             final String description) {
29         this.qName = qName;
30         this.args = args;
31         this.output = output;
32         this.description = description;
33     }
34
35     protected static OutputDefinition getOutputDefinition(final RpcDefinition rpcDefinition) {
36         final ContainerSchemaNode output = rpcDefinition.getOutput();
37         return output != null ? OutputDefinition.fromOutput(output) : OutputDefinition.empty();
38     }
39
40     protected static InputDefinition getInputDefinition(final RpcDefinition rpcDefinition) {
41         final ContainerSchemaNode input = rpcDefinition.getInput();
42         return InputDefinition.fromInput(input);
43     }
44
45     @Override
46     public InputDefinition getInputDefinition() {
47         return args;
48     }
49
50     @Override
51     public OutputDefinition getOutputDefinition() {
52         return output;
53     }
54
55     @Override
56     public QName getCommandId() {
57         return qName;
58     }
59
60     @Override
61     public ConsoleContext getConsoleContext() {
62         return new ConsoleContext() {
63
64             @Override
65             public Completer getCompleter() {
66                 return new NullCompleter();
67             }
68
69             @Override
70             public Optional<String> getPrompt() {
71                 return Optional.of(qName.getLocalName());
72             }
73         };
74     }
75
76     @Override
77     public Optional<String> getCommandDescription() {
78         return Optional.fromNullable(description);
79     }
80 }