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.parser.stmt.reactor;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.ArrayList;
15 import java.util.Collection;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
24 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.RefineStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
30 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
39 import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
40 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
41 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
42 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
47 StatementContextBase<A, D, E> {
48 private static final Logger LOG = LoggerFactory.getLogger(SubstatementContext.class);
50 private final StatementContextBase<?, ?, ?> parent;
51 private final A argument;
52 private volatile SchemaPath schemaPath;
54 SubstatementContext(final StatementContextBase<?, ?, ?> parent, final ContextBuilder<A, D, E> builder) {
56 this.parent = Preconditions.checkNotNull(parent, "Parent must not be null");
57 this.argument = builder.getDefinition().parseArgumentValue(this, builder.getRawArgument());
60 @SuppressWarnings("unchecked")
61 SubstatementContext(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
62 final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
64 this.parent = newParent;
66 if (newQNameModule != null) {
67 final A originalArg = original.argument;
68 if (originalArg instanceof QName) {
69 final QName originalQName = (QName) originalArg;
70 this.argument = (A) getFromNamespace(QNameCacheNamespace.class,
71 QName.create(newQNameModule, originalQName.getLocalName()));
72 } else if (StmtContextUtils.producesDeclared(original, KeyStatement.class)) {
73 this.argument = (A) StmtContextUtils.replaceModuleQNameForKey(
74 (StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?>) original, newQNameModule);
76 this.argument = original.argument;
79 this.argument = original.argument;
84 public StatementContextBase<?, ?, ?> getParentContext() {
89 public NamespaceStorageNode getParentNamespaceStorage() {
94 public Registry getBehaviourRegistry() {
95 return parent.getBehaviourRegistry();
99 public RootStatementContext<?, ?, ?> getRoot() {
100 return parent.getRoot();
104 public A getStatementArgument() {
109 public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
110 final CopyType typeOfCopy) {
111 return createCopy(null, newParent, typeOfCopy);
115 public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
116 final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
117 Preconditions.checkState(getCompletedPhase() == ModelProcessingPhase.EFFECTIVE_MODEL,
118 "Attempted to copy statement {} which has completed phase {}", this, getCompletedPhase());
120 final SubstatementContext<A, D, E> copy = new SubstatementContext<>(this, newQNameModule, newParent, typeOfCopy);
122 copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
124 if (this.getOriginalCtx() != null) {
125 copy.setOriginalCtx(this.getOriginalCtx());
127 copy.setOriginalCtx(this);
130 definition().onStatementAdded(copy);
132 copy.copyStatements(this, newQNameModule, typeOfCopy);
136 private void copyStatements(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
137 final CopyType typeOfCopy) {
138 final Collection<StatementContextBase<?, ?, ?>> declared = original.declaredSubstatements();
139 final Collection<StatementContextBase<?, ?, ?>> effective = original.effectiveSubstatements();
140 final Collection<StatementContextBase<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
142 for (final StatementContextBase<?, ?, ?> stmtContext : declared) {
143 if (StmtContextUtils.areFeaturesSupported(stmtContext)) {
144 copySubstatement(stmtContext, newQNameModule, typeOfCopy, buffer);
148 for (final StatementContextBase<?, ?, ?> stmtContext : effective) {
149 copySubstatement(stmtContext, newQNameModule, typeOfCopy, buffer);
152 addEffectiveSubstatements(buffer);
155 private void copySubstatement(final StatementContextBase<?, ?, ?> stmtContext,
156 final QNameModule newQNameModule, final CopyType typeOfCopy,
157 final Collection<StatementContextBase<?, ?, ?>> buffer) {
158 if (needToCopyByUses(stmtContext)) {
159 final StatementContextBase<?, ?, ?> copy = stmtContext.createCopy(newQNameModule, this, typeOfCopy);
160 LOG.debug("Copying substatement {} for {} as", stmtContext, this, copy);
162 } else if (isReusedByUses(stmtContext)) {
163 LOG.debug("Reusing substatement {} for {}", stmtContext, this);
164 buffer.add(stmtContext);
166 LOG.debug("Skipping statement {}", stmtContext);
170 // FIXME: revise this, as it seems to be wrong
171 private static final Set<Rfc6020Mapping> NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
172 Rfc6020Mapping.DESCRIPTION,
173 Rfc6020Mapping.REFERENCE,
174 Rfc6020Mapping.STATUS);
175 private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(
177 Rfc6020Mapping.TYPEDEF,
178 Rfc6020Mapping.USES);
180 private static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
181 final StatementDefinition def = stmtContext.getPublicDefinition();
182 if (REUSED_DEF_SET.contains(def)) {
183 LOG.debug("Will reuse {} statement {}", def, stmtContext);
186 if (NOCOPY_FROM_GROUPING_SET.contains(def)) {
187 return !Rfc6020Mapping.GROUPING.equals(stmtContext.getParentContext().getPublicDefinition());
190 LOG.debug("Will copy {} statement {}", def, stmtContext);
194 private static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
195 return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
198 private boolean isSupportedAsShorthandCase() {
199 final Collection<?> supportedCaseShorthands = getFromNamespace(ValidationBundlesNamespace.class,
200 ValidationBundleType.SUPPORTED_CASE_SHORTHANDS);
201 return supportedCaseShorthands == null || supportedCaseShorthands.contains(getPublicDefinition());
204 private SchemaPath createSchemaPath() {
205 final Optional<SchemaPath> maybeParentPath = parent.getSchemaPath();
206 Verify.verify(maybeParentPath.isPresent(), "Parent %s does not have a SchemaPath", parent);
207 final SchemaPath parentPath = maybeParentPath.get();
209 if (argument instanceof QName) {
210 final QName qname = (QName) argument;
211 if (StmtContextUtils.producesDeclared(this, UsesStatement.class)) {
212 return maybeParentPath.orNull();
215 final SchemaPath path;
216 if ((StmtContextUtils.producesDeclared(getParentContext(), ChoiceStatement.class)
217 || Boolean.TRUE.equals(parent.getFromNamespace(AugmentToChoiceNamespace.class, parent)))
218 && isSupportedAsShorthandCase()) {
219 path = parentPath.createChild(qname);
223 return path.createChild(qname);
225 if (argument instanceof String) {
226 // FIXME: This may yield illegal argument exceptions
227 final StatementContextBase<?, ?, ?> originalCtx = getOriginalCtx();
228 final QName qname = (originalCtx != null) ? Utils.qNameFromArgument(originalCtx, (String) argument) : Utils
229 .qNameFromArgument(this, (String) argument);
230 return parentPath.createChild(qname);
232 if (argument instanceof SchemaNodeIdentifier
233 && (StmtContextUtils.producesDeclared(this, AugmentStatement.class) || StmtContextUtils
234 .producesDeclared(this, RefineStatement.class))) {
236 return parentPath.createChild(((SchemaNodeIdentifier) argument).getPathFromRoot());
238 if (Utils.isUnknownNode(this)) {
239 return parentPath.createChild(getPublicDefinition().getStatementName());
242 // FIXME: this does not look right
243 return maybeParentPath.orNull();
247 public Optional<SchemaPath> getSchemaPath() {
248 SchemaPath local = schemaPath;
250 synchronized (this) {
253 local = createSchemaPath();
260 return Optional.fromNullable(local);
264 public boolean isRootContext() {
269 public boolean isConfiguration() {
270 final StmtContext<Boolean, ?, ?> configStatement = StmtContextUtils.findFirstSubstatement(this,
271 ConfigStatement.class);
274 * If "config" statement is not specified, the default is the same as
275 * the parent schema node's "config" value.
277 if (configStatement == null) {
278 return parent.isConfiguration();
282 * If a parent node has "config" set to "true", the node underneath it
283 * can have "config" set to "true" or "false".
285 if (parent.isConfiguration()) {
286 return configStatement.getStatementArgument();
290 * If a parent node has "config" set to "false", no node underneath it
291 * can have "config" set to "true", therefore only "false" is permitted.
293 if (!configStatement.getStatementArgument()) {
297 throw new InferenceException(
298 "Parent node has config statement set to false, therefore no node underneath it can have config set to true",
299 getStatementSourceReference());
303 public boolean isEnabledSemanticVersioning() {
304 return parent.isEnabledSemanticVersioning();