Deprecate CopyableNode at al.
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / AbstractEffectiveSimpleDataNodeContainer.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.parser.rfc7950.stmt;
9
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.ArrayList;
13 import java.util.LinkedHashSet;
14 import java.util.List;
15 import java.util.Optional;
16 import java.util.Set;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
21 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.WhenEffectiveStatement;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
32
33 public abstract class AbstractEffectiveSimpleDataNodeContainer<D extends DeclaredStatement<QName>> extends
34         AbstractEffectiveDocumentedDataNodeContainer<QName, D> implements DataNodeContainer, AugmentationTarget,
35         DataSchemaNode {
36
37     private final Set<AugmentationSchemaNode> augmentations;
38     private final List<UnknownSchemaNode> unknownNodes;
39     private final RevisionAwareXPath whenCondition;
40     private final SchemaPath path;
41     private final boolean configuration;
42     private final boolean addedByUses;
43
44     // FIXME: YANGTOOLS-724: this field should be final
45     private boolean augmenting;
46
47     protected AbstractEffectiveSimpleDataNodeContainer(final StmtContext<QName, D, ?> ctx) {
48         super(ctx);
49
50         this.path = ctx.getSchemaPath().get();
51         this.configuration = ctx.isConfiguration();
52
53         whenCondition = findFirstEffectiveSubstatementArgument(WhenEffectiveStatement.class).orElse(null);
54
55         // initSubstatementCollectionsAndFields
56
57         List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
58         Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
59         for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
60             if (effectiveStatement instanceof UnknownSchemaNode) {
61                 unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
62             }
63             if (effectiveStatement instanceof AugmentationSchemaNode) {
64                 augmentationsInit.add((AugmentationSchemaNode) effectiveStatement);
65             }
66         }
67         this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
68         this.augmentations = ImmutableSet.copyOf(augmentationsInit);
69
70         // initCopyType
71         final CopyHistory copyTypesFromOriginal = ctx.getCopyHistory();
72         if (copyTypesFromOriginal.contains(CopyType.ADDED_BY_USES_AUGMENTATION)) {
73             this.augmenting = true;
74             this.addedByUses = true;
75         } else {
76             this.augmenting = copyTypesFromOriginal.contains(CopyType.ADDED_BY_AUGMENTATION);
77             this.addedByUses = copyTypesFromOriginal.contains(CopyType.ADDED_BY_USES);
78         }
79     }
80
81     @Nonnull
82     @Override
83     public QName getQName() {
84         return path.getLastComponent();
85     }
86
87     @Nonnull
88     @Override
89     public SchemaPath getPath() {
90         return path;
91     }
92
93     @Deprecated
94     @Override
95     public boolean isAugmenting() {
96         return augmenting;
97     }
98
99     @Deprecated
100     @Override
101     public boolean isAddedByUses() {
102         return addedByUses;
103     }
104
105     @Override
106     public boolean isConfiguration() {
107         return configuration;
108     }
109
110     @Override
111     public Set<AugmentationSchemaNode> getAvailableAugmentations() {
112         return augmentations;
113     }
114
115     @Nonnull
116     @Override
117     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
118         return unknownNodes;
119     }
120
121     @Override
122     public final Optional<RevisionAwareXPath> getWhenCondition() {
123         return Optional.ofNullable(whenCondition);
124     }
125
126     /**
127      * Reset {@link #isAugmenting()} to false.
128      *
129      * @deprecated This method is a violation of immutable contract and is a side-effect of bad/incomplete lifecycle,
130      *             which needs to be fixed. Do not introduce new callers. This deficiency is tracked in YANGTOOLS-724.
131      */
132     @Deprecated
133     public final void resetAugmenting() {
134         augmenting = false;
135     }
136 }