Rework NormalizedNode type hierarchy
[yangtools.git] / attic / yang-data-jaxen / src / main / java / org / opendaylight / yangtools / yang / data / jaxen / NormalizedNodeContext.java
1 /*
2  * Copyright (c) 2015 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.jaxen;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static com.google.common.base.Verify.verifyNotNull;
13 import static java.util.Objects.requireNonNull;
14
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterators;
17 import java.util.Collection;
18 import java.util.Iterator;
19 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.jaxen.Context;
24 import org.jaxen.ContextSupport;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
30 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
37
38 /**
39  * Context wrapper around a {@link NormalizedNode} for use with Jaxen. It tracks the parent node for purposes of
40  * traversing upwards the NormalizedNode tree.
41  */
42 @NonNullByDefault
43 final class NormalizedNodeContext extends Context {
44     private static final long serialVersionUID = 1L;
45     private final @Nullable NormalizedNodeContext parent;
46     private final DataSchemaContextNode<?> schema;
47     private final NormalizedNode node;
48
49     private NormalizedNodeContext(final ContextSupport contextSupport, final DataSchemaContextNode<?> schema,
50         final NormalizedNode node, final @Nullable NormalizedNodeContext parent) {
51         super(contextSupport);
52         this.schema = requireNonNull(schema);
53         this.node = requireNonNull(node);
54         this.parent = parent;
55
56         setNodeSet(ImmutableList.of(this));
57     }
58
59     static NormalizedNodeContext forRoot(final NormalizedNodeContextSupport contextSupport) {
60         final JaxenDocument document = contextSupport.getNavigator().getDocument();
61         return new NormalizedNodeContext(contextSupport, document.getSchema(), document.getRootNode(), null);
62     }
63
64     NormalizedNode getNode() {
65         return node;
66     }
67
68     @Nullable NormalizedNodeContext getParent() {
69         return parent;
70     }
71
72     YangInstanceIdentifier getPath() {
73         return (parent == null ? YangInstanceIdentifier.empty() : parent.getPath()).node(node.getIdentifier());
74     }
75
76     DataSchemaContextNode<?> getSchema() {
77         return schema;
78     }
79
80     NormalizedNodeContext createChild(final NormalizedNode input) {
81         DataSchemaContextNode<?> childSchema = schema.getChild(input.getIdentifier());
82         if (childSchema == null) {
83             /* This feels very much like a hack: but solves lookup of child nodes with predicates.
84              *
85              * What is happening is that a Map context gets queried for its children, which results in contexts being
86              * backed by UnorderedMapMixinContextNode, which requires us to unmask it.
87              *
88              * When the predicate is being evaluated, each child is queried for its child -- but since it is protected,
89              * we cannot find it.
90              */
91             final DataSchemaNode mySchema = schema.getDataSchemaNode();
92             if (mySchema instanceof ListSchemaNode) {
93                 childSchema = verifyNotNull(schema.getChild(mySchema.getQName())).getChild(input.getIdentifier());
94             }
95         }
96
97         checkArgument(childSchema != null, "Failed to find schema for child %s", input);
98         return new NormalizedNodeContext(getContextSupport(), childSchema, input, this);
99     }
100
101     Optional<NormalizedNodeContext> findChild(final PathArgument arg) {
102         return node instanceof DataContainerNode
103             ? ((DataContainerNode<?>)node).findChildByArg(arg).map(this::createChild) : Optional.empty();
104     }
105
106     Optional<NormalizedNodeContext> findDescendant(final YangInstanceIdentifier path) {
107         if (path.isEmpty()) {
108             return Optional.of(this);
109         }
110
111         NormalizedNodeContext ctxWalk = this;
112         NormalizedNode dataWalk = node;
113         for (PathArgument arg : path.getPathArguments()) {
114             checkArgument(dataWalk instanceof DataContainerNode, "Path %s refers beyond node %s", path, dataWalk);
115
116             dataWalk = ((DataContainerNode<?>)dataWalk).childByArg(arg);
117             if (dataWalk == null) {
118                 return Optional.empty();
119             }
120
121             ctxWalk = createChild(dataWalk);
122         }
123
124         return Optional.of(ctxWalk.createChild(dataWalk));
125     }
126
127     Iterator<NormalizedNodeContext> iterateChildren(final DataContainerNode<?> data) {
128         return Iterators.transform(((DataContainerNode<?>) node).body().iterator(), this::createChild);
129     }
130
131     @Nullable Iterator<NormalizedNodeContext> iterateChildrenNamed(final DataContainerNode<?> data, final QName qname) {
132         final DataContainerChild child = data.childByArg(new NodeIdentifier(qname));
133         if (child == null) {
134             return null;
135         }
136
137         final Collection<? extends NormalizedNode> collection;
138         // The child may be a structural node
139         if (child instanceof MapNode) {
140             collection = ((MapNode)child).body();
141         } else if (child instanceof LeafSetNode) {
142             collection = ((LeafSetNode<?>)child).body();
143         } else {
144             return Iterators.singletonIterator(createChild(child));
145         }
146
147         return Iterators.transform(collection.iterator(), this::createChild);
148     }
149
150     static NormalizedNodeContext cast(@Nullable final Context context) {
151         verify(context instanceof NormalizedNodeContext);
152         return (@NonNull NormalizedNodeContext) context;
153     }
154 }