Bug 3670 (part 1/5): Use of new statement parser in yang-maven-plugin
[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.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;
32
33 public class BaseStatementImpl extends AbstractDeclaredStatement<QName>
34         implements BaseStatement {
35
36     protected BaseStatementImpl(StmtContext<QName, BaseStatement, ?> context) {
37         super(context);
38     }
39
40     public static class Definition
41             extends
42             AbstractStatementSupport<QName, BaseStatement, EffectiveStatement<QName, BaseStatement>> {
43
44         public Definition() {
45             super(Rfc6020Mapping.BASE);
46         }
47
48         @Override
49         public QName parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
50             return Utils.qNameFromArgument(ctx, value);
51         }
52
53         @Override
54         public BaseStatement createDeclared(
55                 StmtContext<QName, BaseStatement, ?> ctx) {
56             return new BaseStatementImpl(ctx);
57         }
58
59         @Override
60         public EffectiveStatement<QName, BaseStatement> createEffective(
61                 StmtContext<QName, BaseStatement, EffectiveStatement<QName, BaseStatement>> ctx) {
62             return new BaseEffectiveStatementImpl(ctx);
63         }
64
65         @Override
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)) {
71
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);
77
78                 baseIdentityAction.apply( new InferenceAction() {
79
80                     @Override
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);
86                         }
87                         derivedIdentities.add(baseParentCtx);
88                     }
89
90                     @Override
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());
96                     }
97
98                 });
99             }
100         }
101     }
102
103     @Override
104     public QName getName() {
105         return argument();
106     }
107
108 }