Merge "Fixed for bug 1168 : Issue while update subnet"
[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.CompositeNode;
17 import org.opendaylight.yangtools.yang.data.api.Node;
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 CompositeNode output;
26
27     public Output(final CompositeNode output) {
28         this.output = output;
29     }
30
31     public Map<DataSchemaNode, List<Node<?>>> unwrap(final OutputDefinition outputDefinition) {
32         Preconditions.checkArgument(outputDefinition.isEmpty() == false);
33
34         final Map<QName, DataSchemaNode> mappedSchemaNodes = mapOutput(outputDefinition);
35         final Map<DataSchemaNode, List<Node<?>>> mappedNodesToSchema = Maps.newHashMap();
36
37         for (final Node<?> node : output.getValue()) {
38             final DataSchemaNode schemaNode = mappedSchemaNodes.get(node.getKey().withoutRevision());
39             final List<Node<?>> list = mappedNodesToSchema.get(schemaNode) == null ? Lists.<Node<?>> newArrayList()
40                     : mappedNodesToSchema.get(schemaNode);
41             list.add(node);
42             mappedNodesToSchema.put(schemaNode, list);
43         }
44
45         return mappedNodesToSchema;
46     }
47
48     public CompositeNode getOutput() {
49         return output;
50     }
51
52     private Map<QName, DataSchemaNode> mapOutput(final OutputDefinition outputDefinition) {
53         final Map<QName, DataSchemaNode> mapped = Maps.newHashMap();
54         for (final DataSchemaNode dataSchemaNode : outputDefinition) {
55             // without revision since data QNames come without revision
56             mapped.put(dataSchemaNode.getQName().withoutRevision(), dataSchemaNode);
57         }
58
59         return mapped;
60     }
61 }