Eliminate superfluous onFullDefinitionDeclared overrides
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / TypeStatementImpl.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.Verify;
11 import java.util.Collection;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
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.TypeEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
21 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
34 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
35 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
45 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
46 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.TypeDefEffectiveStatementImpl;
47 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryTypeEffectiveStatementImpl;
48 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BitsTypeEffectiveStatementImpl;
49 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BooleanTypeEffectiveStatementImpl;
50 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BuiltinEffectiveStatements;
51 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.DecimalTypeEffectiveStatementImpl;
52 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EmptyTypeEffectiveStatementImpl;
53 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EnumTypeEffectiveStatementImpl;
54 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityrefTypeEffectiveStatementImpl;
55 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.InstanceIdentifierTypeEffectiveStatementImpl;
56 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IntegerTypeEffectiveStatementImpl;
57 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LeafrefTypeEffectiveStatementImpl;
58 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.StringTypeEffectiveStatementImpl;
59 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnionTypeEffectiveStatementImpl;
60 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnsignedIntegerTypeEffectiveStatementImpl;
61
62 public class TypeStatementImpl extends AbstractDeclaredStatement<String>
63         implements TypeStatement {
64     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
65             .TYPE)
66             .addOptional(YangStmtMapping.BASE)
67             .addAny(YangStmtMapping.BIT)
68             .addAny(YangStmtMapping.ENUM)
69             .addOptional(YangStmtMapping.FRACTION_DIGITS)
70             .addOptional(YangStmtMapping.LENGTH)
71             .addOptional(YangStmtMapping.PATH)
72             .addAny(YangStmtMapping.PATTERN)
73             .addOptional(YangStmtMapping.RANGE)
74             .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
75             .addAny(YangStmtMapping.TYPE)
76             .build();
77
78     protected TypeStatementImpl(final StmtContext<String, TypeStatement, ?> context) {
79         super(context);
80     }
81
82     public static class Definition
83             extends
84             AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
85
86         public Definition() {
87             super(YangStmtMapping.TYPE);
88         }
89
90         @Override
91         public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
92             return value;
93         }
94
95         @Override
96         public TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
97             return BuiltinTypeStatement.maybeReplace(new TypeStatementImpl(ctx));
98         }
99
100         @Override
101         public TypeEffectiveStatement<TypeStatement> createEffective(
102                 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
103
104             // First look up the proper base type
105             final TypeEffectiveStatement<TypeStatement> typeStmt;
106             switch (ctx.getStatementArgument()) {
107                 case TypeUtils.BINARY:
108                     typeStmt = BuiltinEffectiveStatements.BINARY;
109                     break;
110                 case TypeUtils.BOOLEAN:
111                     typeStmt = BuiltinEffectiveStatements.BOOLEAN;
112                     break;
113                 case TypeUtils.EMPTY:
114                     typeStmt = BuiltinEffectiveStatements.EMPTY;
115                     break;
116                 case TypeUtils.INSTANCE_IDENTIFIER:
117                     typeStmt = BuiltinEffectiveStatements.INSTANCE_IDENTIFIER;
118                     break;
119             case TypeUtils.INT8:
120                 typeStmt = BuiltinEffectiveStatements.INT8;
121                 break;
122             case TypeUtils.INT16:
123                 typeStmt = BuiltinEffectiveStatements.INT16;
124                 break;
125             case TypeUtils.INT32:
126                 typeStmt = BuiltinEffectiveStatements.INT32;
127                 break;
128             case TypeUtils.INT64:
129                 typeStmt = BuiltinEffectiveStatements.INT64;
130                 break;
131             case TypeUtils.STRING:
132                 typeStmt = BuiltinEffectiveStatements.STRING;
133                 break;
134             case TypeUtils.UINT8:
135                 typeStmt = BuiltinEffectiveStatements.UINT8;
136                 break;
137             case TypeUtils.UINT16:
138                 typeStmt = BuiltinEffectiveStatements.UINT16;
139                 break;
140             case TypeUtils.UINT32:
141                 typeStmt = BuiltinEffectiveStatements.UINT32;
142                 break;
143             case TypeUtils.UINT64:
144                 typeStmt = BuiltinEffectiveStatements.UINT64;
145                 break;
146             default:
147                 final QName qname = Utils.qNameFromArgument(ctx, ctx.getStatementArgument());
148                 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
149                         ctx.getFromNamespace(TypeNamespace.class, qname);
150                 SourceException.throwIfNull(typedef, ctx.getStatementSourceReference(), "Type '%s' not found", qname);
151
152                 final TypedefEffectiveStatement effectiveTypedef = typedef.buildEffective();
153                 Verify.verify(effectiveTypedef instanceof TypeDefEffectiveStatementImpl);
154                 typeStmt = ((TypeDefEffectiveStatementImpl) effectiveTypedef).asTypeEffectiveStatement();
155             }
156
157             if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
158                 return typeStmt;
159             }
160
161             // Now instantiate the proper effective statement for that type
162             final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
163             if (baseType instanceof BinaryTypeDefinition) {
164                 return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType);
165             } else if (baseType instanceof BitsTypeDefinition) {
166                 return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
167             } else if (baseType instanceof BooleanTypeDefinition) {
168                 return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType);
169             } else if (baseType instanceof DecimalTypeDefinition) {
170                 return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
171             } else if (baseType instanceof EmptyTypeDefinition) {
172                 return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType);
173             } else if (baseType instanceof EnumTypeDefinition) {
174                 return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
175             } else if (baseType instanceof IdentityrefTypeDefinition) {
176                 return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType);
177             } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
178                 return new InstanceIdentifierTypeEffectiveStatementImpl(ctx, (InstanceIdentifierTypeDefinition) baseType);
179             } else if (baseType instanceof IntegerTypeDefinition) {
180                 return new IntegerTypeEffectiveStatementImpl(ctx, (IntegerTypeDefinition) baseType);
181             } else if (baseType instanceof LeafrefTypeDefinition) {
182                 return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
183             } else if (baseType instanceof StringTypeDefinition) {
184                 return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
185             } else if (baseType instanceof UnionTypeDefinition) {
186                 return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
187             } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
188                 return new UnsignedIntegerTypeEffectiveStatementImpl(ctx, (UnsignedIntegerTypeDefinition) baseType);
189             } else {
190                 throw new IllegalStateException("Unhandled base type " + baseType);
191             }
192         }
193
194         @Override
195         public void onFullDefinitionDeclared(
196                 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
197             super.onFullDefinitionDeclared(stmt);
198
199             // if it is yang built-in type, no prerequisite is needed, so simply return
200             if (TypeUtils.isYangBuiltInTypeString(stmt.getStatementArgument())) {
201                 return;
202             }
203
204             final QName typeQName = Utils.qNameFromArgument(stmt, stmt.getStatementArgument());
205             final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
206             final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
207                     typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
208             typeAction.mutatesEffectiveCtx(stmt.getParentContext());
209
210             /*
211              * If the type does not exist, throw new InferenceException.
212              * Otherwise perform no operation.
213              */
214             typeAction.apply(new InferenceAction() {
215                 @Override
216                 public void apply() {
217                     // Intentional NOOP
218                 }
219
220                 @Override
221                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
222                     InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
223                         "Type [%s] was not found.", typeQName);
224                 }
225             });
226         }
227
228         @Override
229         protected SubstatementValidator getSubstatementValidator() {
230             return SUBSTATEMENT_VALIDATOR;
231         }
232     }
233
234     @Nonnull
235     @Override
236     public String getName() {
237         return argument();
238     }
239 }