Adjust test suite parser update to conform with API changes
[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.meta.EffectiveStatement;
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
33         extends AbstractQNameStatementSupport<BaseStatement, EffectiveStatement<QName, BaseStatement>> {
34     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
35             SubstatementValidator.builder(YangStmtMapping.BASE).build();
36
37     public BaseStatementSupport() {
38         super(YangStmtMapping.BASE);
39     }
40
41     @Override
42     public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
43         return StmtContextUtils.qnameFromArgument(ctx, value);
44     }
45
46     @Override
47     public BaseStatement createDeclared(final StmtContext<QName, BaseStatement, ?> ctx) {
48         return new BaseStatementImpl(ctx);
49     }
50
51     @Override
52     public EffectiveStatement<QName, BaseStatement> createEffective(
53             final StmtContext<QName, BaseStatement, EffectiveStatement<QName, BaseStatement>> ctx) {
54         return new BaseEffectiveStatementImpl(ctx);
55     }
56
57     @Override
58     public void onStatementDefinitionDeclared(
59             final Mutable<QName, BaseStatement, EffectiveStatement<QName, BaseStatement>> baseStmtCtx) {
60         final Mutable<?, ?, ?> baseParentCtx = baseStmtCtx.getParentContext();
61         if (StmtContextUtils.producesDeclared(baseParentCtx, IdentityStatement.class)) {
62
63             final QName baseIdentityQName = baseStmtCtx.getStatementArgument();
64             final ModelActionBuilder baseIdentityAction = baseStmtCtx.newInferenceAction(
65                 ModelProcessingPhase.STATEMENT_DEFINITION);
66             final Prerequisite<StmtContext<?, ?, ?>> requiresPrereq = baseIdentityAction.requiresCtx(baseStmtCtx,
67                 IdentityNamespace.class, baseIdentityQName, ModelProcessingPhase.STATEMENT_DEFINITION);
68             final Prerequisite<StmtContext.Mutable<?, ?, ?>> mutatesPrereq = baseIdentityAction.mutatesCtx(
69                 baseParentCtx, ModelProcessingPhase.STATEMENT_DEFINITION);
70
71             baseIdentityAction.apply(new InferenceAction() {
72                 @Override
73                 public void apply(final InferenceContext ctx) {
74                     List<StmtContext<?, ?, ?>> derivedIdentities = baseStmtCtx.getFromNamespace(
75                         DerivedIdentitiesNamespace.class, baseStmtCtx.getStatementArgument());
76                     if (derivedIdentities == null) {
77                         derivedIdentities = new ArrayList<>(1);
78                         baseStmtCtx.addToNs(DerivedIdentitiesNamespace.class, baseIdentityQName, derivedIdentities);
79                     }
80                     derivedIdentities.add(baseParentCtx);
81                 }
82
83                 @Override
84                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
85                     throw new InferenceException(baseStmtCtx.getStatementSourceReference(),
86                         "Unable to resolve identity %s and base identity %s",
87                         baseParentCtx.getStatementArgument(), baseStmtCtx.getStatementArgument());
88                 }
89             });
90         }
91     }
92
93     @Override
94     protected SubstatementValidator getSubstatementValidator() {
95         return SUBSTATEMENT_VALIDATOR;
96     }
97 }