Merge "Kill Dynamic Actors when we're done with them"
[controller.git] / opendaylight / netconf / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / commands / local / Help.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.local;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Lists;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.controller.netconf.cli.commands.AbstractCommand;
16 import org.opendaylight.controller.netconf.cli.commands.Command;
17 import org.opendaylight.controller.netconf.cli.commands.CommandDispatcher;
18 import org.opendaylight.controller.netconf.cli.commands.input.Input;
19 import org.opendaylight.controller.netconf.cli.commands.input.InputDefinition;
20 import org.opendaylight.controller.netconf.cli.commands.output.Output;
21 import org.opendaylight.controller.netconf.cli.commands.output.OutputDefinition;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.Node;
24 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
25 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
26 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
27 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
28
29 /**
30  * Local Help command. Displays all commands with description.
31  */
32 public class Help extends AbstractCommand {
33
34     private final CommandDispatcher commandDispatcher;
35
36     public Help(final QName qName, final InputDefinition argsDefinition, final OutputDefinition output, final String description, final CommandDispatcher commandDispatcher) {
37         super(qName, argsDefinition, output, description);
38         this.commandDispatcher = commandDispatcher;
39     }
40
41     @Override
42     public Output invoke(final Input inputArgs) {
43         final ArrayList<Node<?>> value = Lists.newArrayList();
44
45         for (final String id : commandDispatcher.getCommandIds()) {
46             final Optional<Command> cmd = commandDispatcher.getCommand(id);
47             Preconditions.checkState(cmd.isPresent(), "Command %s has to be present in command dispatcher", id);
48             final Optional<String> description = cmd.get().getCommandDescription();
49             final List<Node<?>> nameAndDescription = Lists.newArrayList();
50             nameAndDescription.add(NodeFactory.createImmutableSimpleNode(QName.create(getCommandId(), "id"), null, id));
51             if(description.isPresent()) {
52                 nameAndDescription.add(NodeFactory.createImmutableSimpleNode(QName.create(getCommandId(), "description"), null, description.get()));
53             }
54             value.add(ImmutableCompositeNode.create(QName.create(getCommandId(), "commands"), nameAndDescription));
55         }
56
57         return new Output(new CompositeNodeTOImpl(getCommandId(), null, value));
58     }
59
60     public static Command create(final RpcDefinition rpcDefinition, final CommandDispatcher commandDispatcher) {
61         return new Help(rpcDefinition.getQName(), getInputDefinition(rpcDefinition), getOutputDefinition(rpcDefinition), rpcDefinition.getDescription(), commandDispatcher);
62     }
63 }