ec6458d1cacc2767fd6e7a20b0d5b842e08717d7
[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  * <p/>
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 com.google.common.base.Optional;
11 import java.net.URI;
12 import java.util.Collection;
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.Rfc6020Mapping;
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.parser.builder.impl.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.ModelProcessingPhase;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToModuleContext;
28 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.BelongsEffectiveToStatementImpl;
31
32 public class BelongsToStatementImpl extends AbstractDeclaredStatement<String>
33         implements BelongsToStatement {
34
35     protected BelongsToStatementImpl(
36             StmtContext<String, BelongsToStatement, ?> context) {
37         super(context);
38     }
39
40     public static class Definition
41             extends
42             AbstractStatementSupport<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> {
43
44         public Definition() {
45             super(Rfc6020Mapping.BELONGS_TO);
46         }
47
48         @Override
49         public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
50             return value;
51         }
52
53         @Override
54         public BelongsToStatement createDeclared(
55                 StmtContext<String, BelongsToStatement, ?> ctx) {
56             return new BelongsToStatementImpl(ctx);
57         }
58
59         @Override
60         public EffectiveStatement<String, BelongsToStatement> createEffective(
61                 StmtContext<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> ctx) {
62             return new BelongsEffectiveToStatementImpl(ctx);
63         }
64
65         @Override
66         public void onLinkageDeclared(final StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) throws SourceException {
67             ModelActionBuilder belongsToAction = belongsToCtx.newInferenceAction(ModelProcessingPhase.SOURCE_LINKAGE);
68
69             final ModuleIdentifier belongsToModuleIdentifier = getModuleIdentifier(belongsToCtx);
70             final ModelActionBuilder.Prerequisite<StmtContext<?, ?, ?>> belongsToPrereq = belongsToAction.requiresCtx(belongsToCtx, ModuleNamespaceForBelongsTo.class, belongsToModuleIdentifier.getName(), ModelProcessingPhase.SOURCE_LINKAGE);
71
72             belongsToAction.apply(new InferenceAction() {
73
74                 @Override
75                 public void apply() throws InferenceException {
76                     StmtContext<?, ?, ?> belongsToModuleCtx = belongsToPrereq.get();
77
78                     belongsToCtx.addToNs(BelongsToModuleContext.class, belongsToModuleIdentifier,
79                             belongsToModuleCtx);
80                 }
81
82                 @Override
83                 public void prerequisiteFailed(Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) throws InferenceException {
84                     if (failed.contains(belongsToPrereq)) {
85                         throw new InferenceException("Module from belongs-to was not found: " + belongsToCtx.getStatementArgument(), belongsToCtx
86                                 .getStatementSourceReference());
87                     }
88                 }
89             });
90         }
91
92         private ModuleIdentifier getModuleIdentifier(StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
93             String moduleName = belongsToCtx.getStatementArgument();
94             return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(), Optional.of(SimpleDateFormatUtil.DEFAULT_BELONGS_TO_DATE));
95         }
96     }
97
98     @Override
99     public String getModule() {
100         return argument();
101     }
102
103     @Override
104     public PrefixStatement getPrefix() {
105         return firstDeclared(PrefixStatement.class);
106     }
107
108 }