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