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