Bug 2366 - Effective statments impl merge, retest & bugfix
[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 org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
12
13 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
14 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
15 import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedIdentitiesNamespace;
19 import java.util.LinkedList;
20 import java.util.List;
21 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BaseEffectiveStatementImpl;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.BaseStatement;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
33
34 public class BaseStatementImpl extends AbstractDeclaredStatement<QName>
35         implements BaseStatement {
36
37     protected BaseStatementImpl(StmtContext<QName, BaseStatement, ?> context) {
38         super(context);
39     }
40
41     public static class Definition
42             extends
43             AbstractStatementSupport<QName, BaseStatement, EffectiveStatement<QName, BaseStatement>> {
44
45         public Definition() {
46             super(Rfc6020Mapping.BASE);
47         }
48
49         @Override
50         public QName parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
51             return Utils.qNameFromArgument(ctx, value);
52         }
53
54         @Override
55         public BaseStatement createDeclared(
56                 StmtContext<QName, BaseStatement, ?> ctx) {
57             return new BaseStatementImpl(ctx);
58         }
59
60         @Override
61         public EffectiveStatement<QName, BaseStatement> createEffective(
62                 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                 throws SourceException {
70             final Mutable<?, ?, ?> baseParentCtx = baseStmtCtx.getParentContext();
71             if(StmtContextUtils.producesDeclared(baseParentCtx, IdentityStatement.class)) {
72
73                 final QName baseIdentityQName = baseStmtCtx.getStatementArgument();
74                 ModelActionBuilder baseIdentityAction = baseStmtCtx.newInferenceAction(ModelProcessingPhase.STATEMENT_DEFINITION);
75                 final Prerequisite<StmtContext<?, ?, ?>> requiresPrereq = baseIdentityAction.requiresCtx(baseStmtCtx, IdentityNamespace.class, baseIdentityQName, ModelProcessingPhase.STATEMENT_DEFINITION);
76                 final Prerequisite<?> mutatesPrereq = baseIdentityAction.mutatesCtx(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 }