Merge "Fix for messags at the boot up time This commit proposes following two sets...
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-data-util / src / main / java / org / opendaylight / controller / yang / data / util / Nodes.java
1 /*
2  * Copyright (c) 2013 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.yang.data.util;
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.opendaylight.controller.yang.common.QName;
16 import org.opendaylight.controller.yang.data.api.CompositeNode;
17 import org.opendaylight.controller.yang.data.api.Node;
18 import org.opendaylight.controller.yang.data.api.SimpleNode;
19
20 public class Nodes {
21
22     private Nodes() {
23     }
24
25     public static <T> SimpleNode<T> leafNode(QName name, T value) {
26         return new SimpleNodeTO<T>(name, value, null);
27     }
28
29     public static CompositeNode containerNode(QName name, List<Node<?>> children) {
30         return containerNode(name, children, null);
31     }
32
33     public static CompositeNode containerNode(QName name,
34             List<Node<?>> children, CompositeNode parent) {
35         return new ContainerNodeTO(name, parent, nodeMapFromList(children));
36     }
37
38     public static Map<QName, List<Node<?>>> nodeMapFromList(
39             List<Node<?>> children) {
40         Map<QName, List<Node<?>>> map = new HashMap<QName, List<Node<?>>>();
41         for (Node<?> node : children) {
42
43             QName name = node.getNodeType();
44             List<Node<?>> targetList = map.get(name);
45             if (targetList == null) {
46                 targetList = new ArrayList<Node<?>>();
47                 map.put(name, targetList);
48             }
49             targetList.add(node);
50         }
51         return map;
52     }
53
54     private static class ContainerNodeTO extends AbstractContainerNode {
55
56         private final Map<QName, List<Node<?>>> nodeMap;
57
58         public ContainerNodeTO(QName name, Map<QName, List<Node<?>>> nodeMap) {
59             super(name);
60             this.nodeMap = nodeMap;
61         }
62
63         public ContainerNodeTO(QName name, CompositeNode parent,
64                 Map<QName, List<Node<?>>> nodeMap) {
65             super(name, parent);
66             this.nodeMap = nodeMap;
67         }
68
69         @Override
70         protected Map<QName, List<Node<?>>> getNodeMap() {
71
72             return nodeMap;
73         }
74     }
75
76     private static class SimpleNodeTO<T> extends AbstractNode<T> implements
77             SimpleNode<T> {
78
79         private final T value;
80
81         protected SimpleNodeTO(QName name, T val, CompositeNode parent) {
82             super(name, parent);
83             value = val;
84
85         }
86
87         @Override
88         public T getValue() {
89             return value;
90         }
91
92     }
93
94 }