Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / tools / 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.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapNodeBuilder;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32
33 /**
34  * Local Help command. Displays all commands with description.
35  */
36 public class Help extends AbstractCommand {
37
38     private final CommandDispatcher commandDispatcher;
39
40     public Help(final QName qName, final InputDefinition argsDefinition, final OutputDefinition output, final String description, final CommandDispatcher commandDispatcher) {
41         super(qName, argsDefinition, output, description);
42         this.commandDispatcher = commandDispatcher;
43     }
44
45     @Override
46     public Output invoke(final Input inputArgs) {
47         final ArrayList<MapEntryNode> value = Lists.newArrayList();
48
49         for (final String id : commandDispatcher.getCommandIds()) {
50             final Optional<Command> cmd = commandDispatcher.getCommand(id);
51             Preconditions.checkState(cmd.isPresent(), "Command %s has to be present in command dispatcher", id);
52             final Optional<String> description = cmd.get().getCommandDescription();
53             final List<DataContainerChild<?, ?>> nameAndDescription = Lists.newArrayList();
54             nameAndDescription.add(
55                     ImmutableLeafNodeBuilder.create()
56                             .withNodeIdentifier(new NodeIdentifier(QName.create(getCommandId(), "id")))
57                             .withValue(id).build());
58             if(description.isPresent()) {
59                 nameAndDescription.add(
60                         ImmutableLeafNodeBuilder.create()
61                                 .withNodeIdentifier(new NodeIdentifier(QName.create(getCommandId(), "description")))
62                                 .withValue(description.get()).build());
63             }
64             value.add(ImmutableMapEntryNodeBuilder.create()
65                     .withValue(nameAndDescription)
66                     .withNodeIdentifier(
67                             new NodeIdentifierWithPredicates(QName.create(getCommandId(), "commands"),
68                                     QName.create(getCommandId(), "id"), id)).build());
69         }
70         MapNode mappedHelp = ImmutableMapNodeBuilder.create()
71                 .withNodeIdentifier(new NodeIdentifier(QName.create(getCommandId(), "commands")))
72                 .withValue(value).build();
73
74         return new Output(mappedHelp);
75     }
76
77     public static Command create(final RpcDefinition rpcDefinition, final CommandDispatcher commandDispatcher) {
78         return new Help(rpcDefinition.getQName(), getInputDefinition(rpcDefinition), getOutputDefinition(rpcDefinition), rpcDefinition.getDescription(), commandDispatcher);
79     }
80 }