2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl;
10 import groovy.util.BuilderSupport;
13 import java.net.URISyntaxException;
14 import java.util.Date;
16 import java.util.Map.Entry;
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;
28 * @author michal.rehak
31 public class MyNodeBuilder extends BuilderSupport {
33 private static final Logger LOG = LoggerFactory.getLogger(MyNodeBuilder.class);
35 private URI qnNamespace;
36 private final String qnPrefix;
37 private final Date qnRevision;
39 private CompositeNode rootNode;
44 private MyNodeBuilder(final QName baseQName) {
45 qnNamespace = baseQName.getNamespace();
46 qnPrefix = baseQName.getPrefix();
47 qnRevision = baseQName.getRevision();
51 * @return initialized singleton instance
53 public static MyNodeBuilder newInstance() {
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);
62 return new MyNodeBuilder(qName);
66 protected void setParent(final Object parent, final Object child) {
68 if (child instanceof AbstractNodeTO<?>) {
69 ((AbstractNodeTO<?>) child).setParent((CompositeNode) parent);
71 LOG.error("PARENTING FAILED: "+parent + " -> " + child);
76 protected Object createNode(final Object name) {
77 MutableCompositeNode newNode = NodeFactory.createMutableCompositeNode(
78 createQName(name), getCurrentNode(), null, null, null);
79 NodeUtils.fixParentRelation(newNode);
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);
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);
106 private ModifyAction processAttributes(@SuppressWarnings("rawtypes") final Map attributes) {
107 LOG.debug("attributes:" + attributes);
108 ModifyAction modAction = null;
110 @SuppressWarnings("unchecked")
111 Map<String, String> attributesSane = attributes;
112 for (Entry<String, String> attr : attributesSane.entrySet()) {
113 switch (attr.getKey()) {
116 qnNamespace = new URI(attr.getValue());
117 } catch (URISyntaxException e) {
118 LOG.error(e.getMessage(), e);
122 modAction = ModifyAction.valueOf(attr.getValue());
126 throw new IllegalArgumentException("Attribute not supported: "+attr.getKey());
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);
139 private QName createQName(final Object localName) {
140 LOG.debug("qname for: "+localName);
141 return new QName(qnNamespace, qnRevision, qnPrefix, (String) localName);
144 protected CompositeNode getCurrentNode() {
145 if (getCurrent() != null) {
146 if (getCurrent() instanceof CompositeNode) {
147 return (CompositeNode) getCurrent();
150 throw new IllegalAccessError("current node is not of type CompositeNode, but: "
151 +getCurrent().getClass().getSimpleName());
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);
167 nodeRevisited = NodeFactory.createImmutableCompositeNode(
168 mutant.getNodeType(), mutant.getParent(), mutant.getValue(), mutant.getModificationAction());
169 NodeUtils.fixChildrenRelation((CompositeNode) nodeRevisited);
171 if (parent == null) {
172 rootNode = (CompositeNode) nodeRevisited;
174 NodeUtils.fixParentRelation(nodeRevisited);
175 nodeRevisited.getParent().getValue().remove(mutant);
180 return nodeRevisited;
186 public CompositeNode getRootNode() {