BUG 2889 : migration of netconf-cli to NormalizedNode api's
[controller.git] / opendaylight / netconf / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / commands / output / Output.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.output;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import com.google.common.collect.Maps;
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.schema.ContainerNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19
20 /**
21  * Output values for and rpc/command execution
22  */
23 public class Output {
24
25     private final NormalizedNode<?, ?> output;
26
27     public Output(final NormalizedNode<?, ?> output) {
28         if (output instanceof ContainerNode && output.getNodeType().getLocalName() == "rpc-reply") {
29             this.output = ((ContainerNode) output).getValue().iterator().next();
30         } else {
31             this.output = output;
32         }
33     }
34
35     public Map<DataSchemaNode, List<NormalizedNode<?, ?>>> unwrap(final OutputDefinition outputDefinition) {
36         Preconditions.checkArgument(outputDefinition.isEmpty() == false);
37
38         final Map<QName, DataSchemaNode> mappedSchemaNodes = mapOutput(outputDefinition);
39         final Map<DataSchemaNode, List<NormalizedNode<?, ?>>> mappedNodesToSchema = Maps.newHashMap();
40
41         final DataSchemaNode schemaNode = mappedSchemaNodes.get(output.getNodeType().withoutRevision());
42         final List<NormalizedNode<?, ?>> list = mappedNodesToSchema.get(schemaNode) == null ? Lists.<NormalizedNode<?, ?>>newArrayList()
43                 : mappedNodesToSchema.get(schemaNode);
44         list.add(output);
45         mappedNodesToSchema.put(schemaNode, list);
46
47         return mappedNodesToSchema;
48     }
49
50     public NormalizedNode<?, ?> getOutput() {
51         return output;
52     }
53
54     private Map<QName, DataSchemaNode> mapOutput(final OutputDefinition outputDefinition) {
55         final Map<QName, DataSchemaNode> mapped = Maps.newHashMap();
56         for (final DataSchemaNode dataSchemaNode : outputDefinition) {
57             // without revision since data QNames come without revision
58             mapped.put(dataSchemaNode.getQName().withoutRevision(), dataSchemaNode);
59         }
60
61         return mapped;
62     }
63 }