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