2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.rfc7950.stmt.extension;
10 import com.google.common.collect.ImmutableList;
11 import java.util.Optional;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.common.QNameModule;
14 import org.opendaylight.yangtools.yang.common.UnqualifiedQName;
15 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.meta.ArgumentDefinition;
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.UnrecognizedEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
29 final class UnrecognizedStatementSupport
30 extends AbstractStatementSupport<String, UnrecognizedStatement, UnrecognizedEffectiveStatement> {
31 UnrecognizedStatementSupport(final StatementDefinition publicDefinition) {
32 super(publicDefinition, StatementPolicy.legacyDeclaredCopy());
36 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
41 public Optional<StatementSupport<?, ?, ?>> getUnknownStatementDefinitionOf(
42 final StatementDefinition yangStmtDef) {
43 final QName baseQName = getStatementName();
44 final QName statementName = QName.create(baseQName, yangStmtDef.getStatementName().getLocalName());
46 final ModelDefinedStatementDefinition def;
47 final Optional<ArgumentDefinition> optArgDef = yangStmtDef.getArgumentDefinition();
48 if (optArgDef.isPresent()) {
49 final ArgumentDefinition argDef = optArgDef.get();
50 def = new ModelDefinedStatementDefinition(statementName, argDef.getArgumentName(), argDef.isYinElement());
52 def = new ModelDefinedStatementDefinition(statementName);
54 return Optional.of(new ModelDefinedStatementSupport(def));
58 protected SubstatementValidator getSubstatementValidator() {
63 protected UnrecognizedStatement createDeclared(final StmtContext<String, UnrecognizedStatement, ?> ctx,
64 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
65 return new UnrecognizedStatementImpl(ctx.rawArgument(), ctx.publicDefinition(), substatements);
69 protected UnrecognizedStatement createEmptyDeclared(final StmtContext<String, UnrecognizedStatement, ?> ctx) {
70 return createDeclared(ctx, ImmutableList.of());
74 protected UnrecognizedEffectiveStatement createEffective(final Current<String, UnrecognizedStatement> stmt,
75 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
76 // FIXME: Remove following section after fixing 4380
77 final UnknownSchemaNode original = (UnknownSchemaNode) stmt.original();
78 return new UnrecognizedEffectiveStatementImpl(stmt, substatements,
79 original == null ? qnameFromArgument(stmt) : original.getQName());
82 private static QName qnameFromArgument(final Current<String, UnrecognizedStatement> stmt) {
83 final String value = stmt.argument();
84 if (value == null || value.isEmpty()) {
85 return stmt.publicDefinition().getStatementName();
88 final int colon = value.indexOf(':');
90 final UnqualifiedQName qname = UnqualifiedQName.tryCreate(value);
91 return qname == null ? null : qname.bindTo(stmt.moduleName().getModule()).intern();
94 final QNameModule qnameModule = StmtContextUtils.getModuleQNameByPrefix(stmt.caerbannog(),
95 value.substring(0, colon));
96 if (qnameModule == null) {
100 final int next = value.indexOf(':', colon + 1);
101 final String localName = next == -1 ? value.substring(colon + 1) : value.substring(colon + 1, next);
102 return QName.create(qnameModule, localName).intern();