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