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