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 Date qnRevision;
38 private CompositeNode rootNode;
43 private MyNodeBuilder(final QName baseQName) {
44 qnNamespace = baseQName.getNamespace();
45 qnRevision = baseQName.getRevision();
49 * @return initialized singleton instance
51 public static MyNodeBuilder newInstance() {
55 new URI("urn:opendaylight:controller:network"),
56 new Date(42), "node");
57 } catch (URISyntaxException e) {
58 LOG.error(e.getMessage(), e);
60 return new MyNodeBuilder(qName);
64 protected void setParent(final Object parent, final Object child) {
66 if (child instanceof AbstractNodeTO<?>) {
67 ((AbstractNodeTO<?>) child).setParent((CompositeNode) parent);
69 LOG.error("PARENTING FAILED: "+parent + " -> " + child);
74 protected Object createNode(final Object name) {
75 MutableCompositeNode newNode = NodeFactory.createMutableCompositeNode(
76 createQName(name), getCurrentNode(), null, null, null);
77 NodeUtils.fixParentRelation(newNode);
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);
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);
104 private ModifyAction processAttributes(@SuppressWarnings("rawtypes") final Map attributes) {
105 LOG.debug("attributes:" + attributes);
106 ModifyAction modAction = null;
108 @SuppressWarnings("unchecked")
109 Map<String, String> attributesSane = attributes;
110 for (Entry<String, String> attr : attributesSane.entrySet()) {
111 switch (attr.getKey()) {
114 qnNamespace = new URI(attr.getValue());
115 } catch (URISyntaxException e) {
116 LOG.error(e.getMessage(), e);
120 modAction = ModifyAction.valueOf(attr.getValue());
124 throw new IllegalArgumentException("Attribute not supported: "+attr.getKey());
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);
137 private QName createQName(final Object localName) {
138 LOG.debug("qname for: "+localName);
139 return QName.create(qnNamespace, qnRevision, (String) localName);
142 protected CompositeNode getCurrentNode() {
143 if (getCurrent() != null) {
144 if (getCurrent() instanceof CompositeNode) {
145 return (CompositeNode) getCurrent();
148 throw new IllegalAccessError("current node is not of type CompositeNode, but: "
149 +getCurrent().getClass().getSimpleName());
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);
165 nodeRevisited = NodeFactory.createImmutableCompositeNode(
166 mutant.getNodeType(), mutant.getParent(), mutant.getValue(), mutant.getModificationAction());
167 NodeUtils.fixChildrenRelation((CompositeNode) nodeRevisited);
169 if (parent == null) {
170 rootNode = (CompositeNode) nodeRevisited;
172 NodeUtils.fixParentRelation(nodeRevisited);
173 nodeRevisited.getParent().getValue().remove(mutant);
178 return nodeRevisited;
184 public CompositeNode getRootNode() {