Upstream StmtContextUtils.producesDeclared()
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / base / BaseStatementSupport.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.base;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
15 import org.opendaylight.yangtools.yang.model.api.stmt.BaseEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedIdentitiesNamespace;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
31
32 public final class BaseStatementSupport extends AbstractQNameStatementSupport<BaseStatement, BaseEffectiveStatement> {
33     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
34             SubstatementValidator.builder(YangStmtMapping.BASE).build();
35     private static final BaseStatementSupport INSTANCE = new BaseStatementSupport();
36
37     private BaseStatementSupport() {
38         super(YangStmtMapping.BASE);
39     }
40
41     public static BaseStatementSupport getInstance() {
42         return INSTANCE;
43     }
44
45     @Override
46     public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
47         return StmtContextUtils.parseNodeIdentifier(ctx, value);
48     }
49
50     @Override
51     public BaseStatement createDeclared(final StmtContext<QName, BaseStatement, ?> ctx) {
52         return new BaseStatementImpl(ctx);
53     }
54
55     @Override
56     public BaseEffectiveStatement createEffective(final StmtContext<QName, BaseStatement, BaseEffectiveStatement> ctx) {
57         return new BaseEffectiveStatementImpl(ctx);
58     }
59
60     @Override
61     public void onStatementDefinitionDeclared(final Mutable<QName, BaseStatement, BaseEffectiveStatement> baseStmtCtx) {
62         final Mutable<?, ?, ?> baseParentCtx = baseStmtCtx.getParentContext();
63         if (baseParentCtx.producesDeclared(IdentityStatement.class)) {
64
65             final QName baseIdentityQName = baseStmtCtx.coerceStatementArgument();
66             final ModelActionBuilder baseIdentityAction = baseStmtCtx.newInferenceAction(
67                 ModelProcessingPhase.STATEMENT_DEFINITION);
68             final Prerequisite<StmtContext<?, ?, ?>> requiresPrereq = baseIdentityAction.requiresCtx(baseStmtCtx,
69                 IdentityNamespace.class, baseIdentityQName, ModelProcessingPhase.STATEMENT_DEFINITION);
70             final Prerequisite<StmtContext.Mutable<?, ?, ?>> mutatesPrereq = baseIdentityAction.mutatesCtx(
71                 baseParentCtx, ModelProcessingPhase.STATEMENT_DEFINITION);
72
73             baseIdentityAction.apply(new InferenceAction() {
74                 @Override
75                 public void apply(final InferenceContext ctx) {
76                     List<StmtContext<?, ?, ?>> derivedIdentities = baseStmtCtx.getFromNamespace(
77                         DerivedIdentitiesNamespace.class, baseIdentityQName);
78                     if (derivedIdentities == null) {
79                         derivedIdentities = new ArrayList<>(1);
80                         baseStmtCtx.addToNs(DerivedIdentitiesNamespace.class, baseIdentityQName, derivedIdentities);
81                     }
82                     derivedIdentities.add(baseParentCtx);
83                 }
84
85                 @Override
86                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
87                     throw new InferenceException(baseStmtCtx.getStatementSourceReference(),
88                         "Unable to resolve identity %s and base identity %s",
89                         baseParentCtx.getStatementArgument(), baseStmtCtx.getStatementArgument());
90                 }
91             });
92         }
93     }
94
95     @Override
96     protected SubstatementValidator getSubstatementValidator() {
97         return SUBSTATEMENT_VALIDATOR;
98     }
99 }