Merge "Fix AugmentationSchemaProxy.getDataChildByName()"
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / MyNodeBuilder.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.yangtools.yang.data.impl;
9
10 import groovy.util.BuilderSupport;
11
12 import java.net.URI;
13 import java.net.URISyntaxException;
14 import java.util.Date;
15 import java.util.Map;
16 import java.util.Map.Entry;
17
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
20 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
21 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
22 import org.opendaylight.yangtools.yang.data.api.Node;
23 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * @author michal.rehak
29  */
30 @Deprecated
31 public class MyNodeBuilder extends BuilderSupport {
32
33     private static final Logger LOG = LoggerFactory.getLogger(MyNodeBuilder.class);
34
35     private URI qnNamespace;
36     private final String qnPrefix;
37     private final Date qnRevision;
38
39     private CompositeNode rootNode;
40
41     /**
42      * @param baseQName
43      */
44     private MyNodeBuilder(final QName baseQName) {
45         qnNamespace = baseQName.getNamespace();
46         qnPrefix = baseQName.getPrefix();
47         qnRevision = baseQName.getRevision();
48     }
49
50     /**
51      * @return initialized singleton instance
52      */
53     public static MyNodeBuilder newInstance() {
54         QName qName = null;
55         try {
56             qName = new QName(
57                     new URI("urn:opendaylight:controller:network"),
58                     new Date(42), "yang-data-impl-groovyTest_", "node");
59         } catch (URISyntaxException e) {
60             LOG.error(e.getMessage(), e);
61         }
62         return new MyNodeBuilder(qName);
63     }
64
65     @Override
66     protected void setParent(final Object parent, final Object child) {
67         // do nothing
68         if (child instanceof AbstractNodeTO<?>) {
69             ((AbstractNodeTO<?>) child).setParent((CompositeNode) parent);
70         } else {
71             LOG.error("PARENTING FAILED: "+parent + " -> " + child);
72         }
73     }
74
75     @Override
76     protected Object createNode(final Object name) {
77         MutableCompositeNode newNode = NodeFactory.createMutableCompositeNode(
78                 createQName(name), getCurrentNode(), null, null, null);
79         NodeUtils.fixParentRelation(newNode);
80         return newNode;
81     }
82
83     @Override
84     protected Object createNode(final Object name, @SuppressWarnings("rawtypes") final Map attributes) {
85         ModifyAction modifyAction = processAttributes(attributes);
86         MutableCompositeNode newNode = NodeFactory.createMutableCompositeNode(
87                 createQName(name), getCurrentNode(), null, modifyAction, null);
88         NodeUtils.fixParentRelation(newNode);
89         return newNode;
90     }
91
92
93     @Override
94     protected Object createNode(final Object name, @SuppressWarnings("rawtypes") final Map attributes, final Object value) {
95         ModifyAction modifyAction = processAttributes(attributes);
96         SimpleNode<Object> newNode = NodeFactory.createImmutableSimpleNode(
97                 createQName(name), (CompositeNode) getCurrent(), value, modifyAction);
98         NodeUtils.fixParentRelation(newNode);
99         return newNode;
100     }
101
102     /**
103      * @param attributes
104      * @return
105      */
106     private ModifyAction processAttributes(@SuppressWarnings("rawtypes") final Map attributes) {
107         LOG.debug("attributes:" + attributes);
108         ModifyAction modAction = null;
109
110         @SuppressWarnings("unchecked")
111         Map<String, String> attributesSane = attributes;
112         for (Entry<String, String> attr : attributesSane.entrySet()) {
113             switch (attr.getKey()) {
114             case "xmlns":
115                 try {
116                     qnNamespace = new URI(attr.getValue());
117                 } catch (URISyntaxException e) {
118                     LOG.error(e.getMessage(), e);
119                 }
120                 break;
121             case "modifyAction":
122                 modAction = ModifyAction.valueOf(attr.getValue());
123                 break;
124
125             default:
126                 throw new IllegalArgumentException("Attribute not supported: "+attr.getKey());
127             }
128         }
129         return modAction;
130     }
131
132     @Override
133     protected Object createNode(final Object name, final Object value) {
134         SimpleNode<Object> newNode = NodeFactory.createImmutableSimpleNode(createQName(name), (CompositeNode) getCurrent(), value);
135         NodeUtils.fixParentRelation(newNode);
136         return newNode;
137     }
138
139     private QName createQName(final Object localName) {
140         LOG.debug("qname for: "+localName);
141         return new QName(qnNamespace, qnRevision, qnPrefix, (String) localName);
142     }
143
144     protected CompositeNode getCurrentNode() {
145         if (getCurrent() != null) {
146             if (getCurrent() instanceof CompositeNode) {
147                 return (CompositeNode) getCurrent();
148
149             } else {
150                 throw new IllegalAccessError("current node is not of type CompositeNode, but: "
151                         +getCurrent().getClass().getSimpleName());
152             }
153         }
154
155         return null;
156     }
157
158     @Override
159     protected Object postNodeCompletion(final Object parent, final Object node) {
160         Node<?> nodeRevisited = (Node<?>) node;
161         LOG.debug("postNodeCompletion at: \n  "+ nodeRevisited+"\n  "+parent);
162         if (nodeRevisited instanceof MutableCompositeNode) {
163             MutableCompositeNode mutant = (MutableCompositeNode) nodeRevisited;
164             if (mutant.getValue().isEmpty()) {
165                 LOG.error("why is it having empty value? -- " + mutant);
166             }
167             nodeRevisited = NodeFactory.createImmutableCompositeNode(
168                     mutant.getNodeType(), mutant.getParent(), mutant.getValue(), mutant.getModificationAction());
169             NodeUtils.fixChildrenRelation((CompositeNode) nodeRevisited);
170
171             if (parent == null) {
172                 rootNode = (CompositeNode) nodeRevisited;
173             } else {
174                 NodeUtils.fixParentRelation(nodeRevisited);
175                 nodeRevisited.getParent().getValue().remove(mutant);
176             }
177         }
178
179
180         return nodeRevisited;
181     }
182
183     /**
184      * @return tree root
185      */
186     public CompositeNode getRootNode() {
187         return rootNode;
188     }
189 }