Merge "When a node is going down, remove edges in both directions associated with...
[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.ModifyAction;
18 import org.opendaylight.controller.yang.data.api.MutableCompositeNode;
19 import org.opendaylight.controller.yang.data.api.MutableSimpleNode;
20 import org.opendaylight.controller.yang.data.api.Node;
21 import org.opendaylight.controller.yang.data.api.SimpleNode;
22
23 public class Nodes {
24
25     private Nodes() {
26     }
27
28     public static <T> SimpleNode<T> leafNode(QName name, T value) {
29         return new SimpleNodeTO<T>(name, value, null);
30     }
31
32     public static CompositeNode containerNode(QName name, List<Node<?>> children) {
33         return containerNode(name, children, null);
34     }
35
36     public static CompositeNode containerNode(QName name,
37             List<Node<?>> children, CompositeNode parent) {
38         return new ContainerNodeTO(name, parent, nodeMapFromList(children));
39     }
40
41     public static Map<QName, List<Node<?>>> nodeMapFromList(
42             List<Node<?>> children) {
43         Map<QName, List<Node<?>>> map = new HashMap<QName, List<Node<?>>>();
44         for (Node<?> node : children) {
45
46             QName name = node.getNodeType();
47             List<Node<?>> targetList = map.get(name);
48             if (targetList == null) {
49                 targetList = new ArrayList<Node<?>>();
50                 map.put(name, targetList);
51             }
52             targetList.add(node);
53         }
54         return map;
55     }
56
57     private static class ContainerNodeTO extends AbstractContainerNode {
58
59         private final Map<QName, List<Node<?>>> nodeMap;
60
61         public ContainerNodeTO(QName name, Map<QName, List<Node<?>>> nodeMap) {
62             super(name);
63             this.nodeMap = nodeMap;
64         }
65
66         public ContainerNodeTO(QName name, CompositeNode parent,
67                 Map<QName, List<Node<?>>> nodeMap) {
68             super(name, parent);
69             this.nodeMap = nodeMap;
70         }
71
72         @Override
73         protected Map<QName, List<Node<?>>> getNodeMap() {
74
75             return nodeMap;
76         }
77
78         /* (non-Javadoc)
79          * @see org.opendaylight.controller.yang.data.api.CompositeNode#asMutable()
80          */
81         @Override
82         public MutableCompositeNode asMutable() {
83             // TODO Auto-generated method stub
84             return null;
85         }
86
87     }
88
89     private static class SimpleNodeTO<T> extends AbstractNode<T> implements
90             SimpleNode<T> {
91
92         private final T value;
93
94         protected SimpleNodeTO(QName name, T val, CompositeNode parent) {
95             super(name, parent);
96             value = val;
97
98         }
99
100         @Override
101         public T getValue() {
102             return value;
103         }
104
105         /* (non-Javadoc)
106          * @see org.opendaylight.controller.yang.data.api.SimpleNode#asMutable()
107          */
108         @Override
109         public MutableSimpleNode<T> asMutable() {
110             // TODO Auto-generated method stub
111             return null;
112         }
113
114     }
115
116 }