Merge "Fixed for bug 1197"
[controller.git] / opendaylight / netconf / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / commands / input / Input.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.input;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
17 import org.opendaylight.yangtools.yang.data.api.Node;
18 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
19
20 /**
21  * Input arguments for and rpc/command execution
22  */
23 public class Input {
24
25     private final List<Node<?>> args;
26
27     private final Map<String, Node<?>> nameToArg = new HashMap<String, Node<?>>();
28
29     public Input(final List<Node<?>> args) {
30         // FIXME empty Input should be constructed from static factory method
31         if(args.isEmpty()) {
32             this.args = Collections.emptyList();
33             return;
34         }
35
36         final Node<?> input = args.iterator().next();
37         Preconditions
38                 .checkArgument(input instanceof CompositeNode, "Input container has to be of type composite node.");
39         this.args = ((CompositeNode) input).getValue();
40
41         for (final Node<?> arg : this.args) {
42             nameToArg.put(arg.getNodeType().getLocalName(), arg);
43         }
44     }
45
46     public Node<?> getArg(final String name) {
47         return nameToArg.get(name);
48     }
49
50     public CompositeNode wrap(final QName rpcQName) {
51         return new CompositeNodeTOImpl(rpcQName, null, args);
52     }
53 }