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