Refactor InferenceAction
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / BelongsToStatementImpl.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.Optional;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
14 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
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.BelongsToStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
19 import org.opendaylight.yangtools.yang.model.util.ModuleIdentifierImpl;
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.InferenceException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
30 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToModuleContext;
31 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleIdentifier;
32 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BelongsEffectiveToStatementImpl;
34
35 public class BelongsToStatementImpl extends AbstractDeclaredStatement<String>
36         implements BelongsToStatement {
37     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
38             SubstatementValidator.builder(YangStmtMapping.BELONGS_TO).addMandatory(YangStmtMapping.PREFIX).build();
39
40     protected BelongsToStatementImpl(final StmtContext<String, BelongsToStatement, ?> context) {
41         super(context);
42     }
43
44     public static class Definition
45             extends
46             AbstractStatementSupport<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> {
47
48         public Definition() {
49             super(YangStmtMapping.BELONGS_TO);
50         }
51
52         @Override
53         public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
54             return value;
55         }
56
57         @Override
58         public BelongsToStatement createDeclared(
59                 final StmtContext<String, BelongsToStatement, ?> ctx) {
60             return new BelongsToStatementImpl(ctx);
61         }
62
63         @Override
64         public EffectiveStatement<String, BelongsToStatement> createEffective(
65                 final StmtContext<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> ctx) {
66             return new BelongsEffectiveToStatementImpl(ctx);
67         }
68
69         @Override
70         public void onLinkageDeclared(
71                 final StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
72             ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
73
74             final ModuleIdentifier belongsToModuleIdentifier = getModuleIdentifier(belongsToCtx);
75             final ModelActionBuilder.Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(
76                 belongsToCtx, ModuleNamespaceForBelongsTo.class, belongsToModuleIdentifier.getName(),
77                 ModelProcessingPhase.SOURCE_LINKAGE);
78
79             belongsToAction.apply(new InferenceAction() {
80                 @Override
81                 public void apply(final InferenceContext ctx) {
82                     StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.resolve(ctx);
83
84                     belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToModuleIdentifier, belongsToModuleCtx);
85                     belongsToCtx.addToNs(BelongsToPrefixToModuleIdentifier.class,
86                         StmtContextUtils.findFirstDeclaredSubstatement(belongsToCtx, PrefixStatement.class)
87                         .getStatementArgument(), belongsToModuleIdentifier);
88                 }
89
90                 @Override
91                 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
92                     if (failed.contains(belongsToPrereq)) {
93                         throw new InferenceException(belongsToCtx.getStatementSourceReference(),
94                             "Module '%s' from belongs-to was not found", belongsToCtx.getStatementArgument());
95                     }
96                 }
97             });
98         }
99
100         private static ModuleIdentifier getModuleIdentifier(
101                 final StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
102             String moduleName = belongsToCtx.getStatementArgument();
103             return ModuleIdentifierImpl.create(moduleName, Optional.empty(),
104                 Optional.of(SimpleDateFormatUtil.DEFAULT_BELONGS_TO_DATE));
105         }
106
107         @Override
108         protected SubstatementValidator getSubstatementValidator() {
109             return SUBSTATEMENT_VALIDATOR;
110         }
111     }
112
113     @Nonnull
114     @Override
115     public String getModule() {
116         return argument();
117     }
118
119     @Nonnull
120     @Override
121     public PrefixStatement getPrefix() {
122         return firstDeclared(PrefixStatement.class);
123     }
124
125 }