Populate xpath/ hierarchy
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / extension / UnrecognizedStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.extension;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableList;
13 import java.util.Optional;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
17 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
20 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
21 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
22 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
23 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.OverrideChildStatementSupport;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
30
31 final class UnrecognizedStatementSupport
32         extends AbstractStatementSupport<Object, UnrecognizedStatement, UnrecognizedEffectiveStatement>
33         implements OverrideChildStatementSupport {
34     private final YangParserConfiguration config;
35
36     UnrecognizedStatementSupport(final StatementDefinition publicDefinition, final YangParserConfiguration config) {
37         // We have no idea about the statement's semantics, hence there should be noone interested in its semantics.
38         // Nevertheless it may be of interest for various hacks to understand there was an extension involved.
39         super(publicDefinition, StatementPolicy.exactReplica(), config, null);
40         this.config = requireNonNull(config);
41     }
42
43     @Override
44     public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
45         return value;
46     }
47
48     @Override
49     public UnrecognizedStatementSupport statementDefinitionOverrideOf(final StatementDefinition childDef) {
50         /*
51          * This code wraps statements encountered inside an extension so they do not get confused with regular
52          * statements.
53          */
54         final QName baseQName = getStatementName();
55         final QName statementName = QName.create(baseQName, childDef.getStatementName().getLocalName());
56
57         final ModelDefinedStatementDefinition def;
58         final Optional<ArgumentDefinition> optArgDef = childDef.getArgumentDefinition();
59         if (optArgDef.isPresent()) {
60             final ArgumentDefinition argDef = optArgDef.get();
61             def = new ModelDefinedStatementDefinition(statementName, argDef.getArgumentName(), argDef.isYinElement());
62         } else {
63             def = new ModelDefinedStatementDefinition(statementName);
64         }
65         return new UnrecognizedStatementSupport(def, config);
66     }
67
68     @Override
69     protected UnrecognizedStatement createDeclared(final StmtContext<Object, UnrecognizedStatement, ?> ctx,
70             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
71         return DeclaredStatements.createUnrecognized(ctx.rawArgument(), ctx.publicDefinition(), substatements);
72     }
73
74     @Override
75     protected UnrecognizedStatement attachDeclarationReference(final UnrecognizedStatement stmt,
76             final DeclarationReference reference) {
77         return DeclaredStatementDecorators.decorateUnrecognized(stmt, reference);
78     }
79
80     // createEffective() should never be called, ensure that for each declared statement
81
82     @Override
83     public void onStatementAdded(final Mutable<Object, UnrecognizedStatement, UnrecognizedEffectiveStatement> stmt) {
84         stmt.setIsSupportedToBuildEffective(false);
85     }
86
87     @Override
88     protected UnrecognizedEffectiveStatement createEffective(final Current<Object, UnrecognizedStatement> stmt,
89             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
90         throw new InferenceException(stmt, "Attempted to instantiate unrecognized effective statement %s",
91             stmt.publicDefinition());
92     }
93 }