2 * Copyright (c) 2015 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.model.util;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.HashSet;
18 import java.util.Optional;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
26 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
27 import org.opendaylight.yangtools.yang.model.api.Status;
28 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.UsesNode;
31 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
32 import org.opendaylight.yangtools.yang.xpath.api.YangXPathExpression.QualifiedBound;
35 * Proxy for AugmentationSchema. Child node schemas are replaced with actual schemas from parent. This is needed to
36 * correctly interpret constructs like this:
50 * The {@link AugmentationSchemaNode} returned for {@code augment /foo} contains bare {@code container bar}, e.g. it
51 * does not show {@code augment /foo/bar} as an available augmentation -- this is only visible in {@code foo}'s schema
55 * Note this class only handles {@link DataSchemaNode}s, not all {@code schema tree} statements, as it strictly should.
57 // FIXME: YANGTOOLS-1403: this functionality should be integrated into EffectiveAugmentStatement/AugmentationSchemaNode
58 public final class EffectiveAugmentationSchema implements AugmentationSchemaNode {
59 private final AugmentationSchemaNode delegate;
60 private final ImmutableSet<DataSchemaNode> realChildSchemas;
61 private final ImmutableMap<QName, DataSchemaNode> mappedChildSchemas;
63 public EffectiveAugmentationSchema(final AugmentationSchemaNode augmentSchema,
64 final Collection<? extends DataSchemaNode> realChildSchemas) {
65 delegate = requireNonNull(augmentSchema);
66 this.realChildSchemas = ImmutableSet.copyOf(realChildSchemas);
68 final Map<QName, DataSchemaNode> m = new HashMap<>(realChildSchemas.size());
69 for (DataSchemaNode realChildSchema : realChildSchemas) {
70 m.put(realChildSchema.getQName(), realChildSchema);
73 mappedChildSchemas = ImmutableMap.copyOf(m);
77 * Returns an AugmentationSchemaNode as effective in a parent node.
79 * @param schema Augmentation schema
80 * @param parent Parent schema
81 * @return Adjusted Augmentation schema
82 * @throws NullPointerException if any of the arguments is null
84 // FIXME: 8.0.0: integrate this method into the constructor
85 public static AugmentationSchemaNode create(final AugmentationSchemaNode schema, final DataNodeContainer parent) {
86 final Set<DataSchemaNode> children = new HashSet<>();
87 for (DataSchemaNode augNode : schema.getChildNodes()) {
88 // parent may have the corresponding child removed via 'deviate unsupported', i.e. the child is effectively
89 // not present at the target site
90 final DataSchemaNode child = parent.dataChildByName(augNode.getQName());
95 return new EffectiveAugmentationSchema(schema, children);
99 public Optional<? extends QualifiedBound> getWhenCondition() {
100 return delegate.getWhenCondition();
104 public Optional<String> getDescription() {
105 return delegate.getDescription();
109 public Optional<String> getReference() {
110 return delegate.getReference();
114 public Status getStatus() {
115 return delegate.getStatus();
119 public Collection<? extends UnknownSchemaNode> getUnknownSchemaNodes() {
120 return delegate.getUnknownSchemaNodes();
124 public Collection<? extends TypeDefinition<?>> getTypeDefinitions() {
125 return delegate.getTypeDefinitions();
129 public Collection<? extends DataSchemaNode> getChildNodes() {
130 return realChildSchemas;
134 public Collection<? extends GroupingDefinition> getGroupings() {
135 return delegate.getGroupings();
139 public DataSchemaNode dataChildByName(final QName name) {
140 return mappedChildSchemas.get(requireNonNull(name));
144 public Collection<? extends UsesNode> getUses() {
145 return delegate.getUses();
149 public Optional<AugmentationSchemaNode> getOriginalDefinition() {
150 return delegate.getOriginalDefinition();
154 public Collection<? extends ActionDefinition> getActions() {
155 return delegate.getActions();
159 public Optional<ActionDefinition> findAction(final QName qname) {
160 return delegate.findAction(qname);
164 public Collection<? extends NotificationDefinition> getNotifications() {
165 return delegate.getNotifications();
169 public Optional<NotificationDefinition> findNotification(final QName qname) {
170 return delegate.findNotification(qname);
174 public AugmentEffectiveStatement asEffectiveStatement() {
175 return delegate.asEffectiveStatement();