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.rfc6020;
10 import java.util.Collection;
11 import java.util.LinkedList;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
14 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
16 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedIdentitiesNamespace;
20 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BaseEffectiveStatementImpl;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
33 public class BaseStatementImpl extends AbstractDeclaredStatement<QName>
34 implements BaseStatement {
36 protected BaseStatementImpl(StmtContext<QName, BaseStatement, ?> context) {
40 public static class Definition
42 AbstractStatementSupport<QName, BaseStatement, EffectiveStatement<QName, BaseStatement>> {
45 super(Rfc6020Mapping.BASE);
49 public QName parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
50 return Utils.qNameFromArgument(ctx, value);
54 public BaseStatement createDeclared(
55 StmtContext<QName, BaseStatement, ?> ctx) {
56 return new BaseStatementImpl(ctx);
60 public EffectiveStatement<QName, BaseStatement> createEffective(
61 StmtContext<QName, BaseStatement, EffectiveStatement<QName, BaseStatement>> ctx) {
62 return new BaseEffectiveStatementImpl(ctx);
66 public void onStatementDefinitionDeclared(
67 final Mutable<QName, BaseStatement, EffectiveStatement<QName, BaseStatement>> baseStmtCtx)
68 throws SourceException {
69 final Mutable<?, ?, ?> baseParentCtx = baseStmtCtx.getParentContext();
70 if(StmtContextUtils.producesDeclared(baseParentCtx, IdentityStatement.class)) {
72 final QName baseIdentityQName = baseStmtCtx.getStatementArgument();
73 ModelActionBuilder baseIdentityAction = baseStmtCtx.newInferenceAction(ModelProcessingPhase.STATEMENT_DEFINITION);
74 final Prerequisite<StmtContext<?, ?, ?>> requiresPrereq = baseIdentityAction.requiresCtx(baseStmtCtx, IdentityNamespace.class, baseIdentityQName, ModelProcessingPhase.STATEMENT_DEFINITION);
75 final Prerequisite<StmtContext.Mutable<?, ?, ?>> mutatesPrereq = baseIdentityAction.mutatesCtx
76 (baseParentCtx, ModelProcessingPhase.STATEMENT_DEFINITION);
78 baseIdentityAction.apply( new InferenceAction() {
81 public void apply() throws InferenceException {
82 List<StmtContext<?, ?, ?>> derivedIdentities = baseStmtCtx.getFromNamespace(DerivedIdentitiesNamespace.class, baseStmtCtx.getStatementArgument());
83 if(derivedIdentities == null) {
84 derivedIdentities = new LinkedList<>();
85 baseStmtCtx.addToNs(DerivedIdentitiesNamespace.class, baseIdentityQName, derivedIdentities);
87 derivedIdentities.add(baseParentCtx);
91 public void prerequisiteFailed(
92 Collection<? extends Prerequisite<?>> failed)
93 throws InferenceException {
94 throw new InferenceException("Unable to resolve identity "+baseParentCtx.getStatementArgument()+" and base identity " + baseStmtCtx.getStatementArgument(), baseStmtCtx
95 .getStatementSourceReference());
104 public QName getName() {