Introduce formatting methods for SourceException
[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                 throws SourceException {
94             return value;
95         }
96
97         @Override
98         public TypeStatement createDeclared(
99                 final StmtContext<String, TypeStatement, ?> ctx) {
100             return new TypeStatementImpl(ctx);
101         }
102
103         @Override
104         public TypeEffectiveStatement<TypeStatement> createEffective(
105                 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
106
107             // First look up the proper base type
108             final TypeEffectiveStatement<TypeStatement> typeStmt;
109             switch (ctx.getStatementArgument()) {
110                 case TypeUtils.BINARY:
111                     typeStmt = BuiltinEffectiveStatements.BINARY;
112                     break;
113                 case TypeUtils.BOOLEAN:
114                     typeStmt = BuiltinEffectiveStatements.BOOLEAN;
115                     break;
116                 case TypeUtils.EMPTY:
117                     typeStmt = BuiltinEffectiveStatements.EMPTY;
118                     break;
119                 case TypeUtils.INSTANCE_IDENTIFIER:
120                     typeStmt = BuiltinEffectiveStatements.INSTANCE_IDENTIFIER;
121                     break;
122             case TypeUtils.INT8:
123                 typeStmt = BuiltinEffectiveStatements.INT8;
124                 break;
125             case TypeUtils.INT16:
126                 typeStmt = BuiltinEffectiveStatements.INT16;
127                 break;
128             case TypeUtils.INT32:
129                 typeStmt = BuiltinEffectiveStatements.INT32;
130                 break;
131             case TypeUtils.INT64:
132                 typeStmt = BuiltinEffectiveStatements.INT64;
133                 break;
134             case TypeUtils.STRING:
135                 typeStmt = BuiltinEffectiveStatements.STRING;
136                 break;
137             case TypeUtils.UINT8:
138                 typeStmt = BuiltinEffectiveStatements.UINT8;
139                 break;
140             case TypeUtils.UINT16:
141                 typeStmt = BuiltinEffectiveStatements.UINT16;
142                 break;
143             case TypeUtils.UINT32:
144                 typeStmt = BuiltinEffectiveStatements.UINT32;
145                 break;
146             case TypeUtils.UINT64:
147                 typeStmt = BuiltinEffectiveStatements.UINT64;
148                 break;
149             default:
150                 final QName qname = Utils.qNameFromArgument(ctx, ctx.getStatementArgument());
151                 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
152                         ctx.getFromNamespace(TypeNamespace.class, qname);
153                 SourceException.throwIfNull(typedef, ctx.getStatementSourceReference(), "Type '%s' not found", qname);
154
155                 final TypedefEffectiveStatement effectiveTypedef = typedef.buildEffective();
156                 Verify.verify(effectiveTypedef instanceof TypeDefEffectiveStatementImpl);
157                 typeStmt = ((TypeDefEffectiveStatementImpl) effectiveTypedef).asTypeEffectiveStatement();
158             }
159
160             if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
161                 return typeStmt;
162             }
163
164             // Now instantiate the proper effective statement for that type
165             final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
166             if (baseType instanceof BinaryTypeDefinition) {
167                 return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType);
168             } else if (baseType instanceof BitsTypeDefinition) {
169                 return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
170             } else if (baseType instanceof BooleanTypeDefinition) {
171                 return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType);
172             } else if (baseType instanceof DecimalTypeDefinition) {
173                 return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
174             } else if (baseType instanceof EmptyTypeDefinition) {
175                 return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType);
176             } else if (baseType instanceof EnumTypeDefinition) {
177                 return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
178             } else if (baseType instanceof IdentityrefTypeDefinition) {
179                 return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType);
180             } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
181                 return new InstanceIdentifierTypeEffectiveStatementImpl(ctx, (InstanceIdentifierTypeDefinition) baseType);
182             } else if (baseType instanceof IntegerTypeDefinition) {
183                 return new IntegerTypeEffectiveStatementImpl(ctx, (IntegerTypeDefinition) baseType);
184             } else if (baseType instanceof LeafrefTypeDefinition) {
185                 return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
186             } else if (baseType instanceof StringTypeDefinition) {
187                 return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
188             } else if (baseType instanceof UnionTypeDefinition) {
189                 return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
190             } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
191                 return new UnsignedIntegerTypeEffectiveStatementImpl(ctx, (UnsignedIntegerTypeDefinition) baseType);
192             } else {
193                 throw new IllegalStateException("Unhandled base type " + baseType);
194             }
195         }
196
197         @Override
198         public void onFullDefinitionDeclared(
199                 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt){
200             SUBSTATEMENT_VALIDATOR.validate(stmt);
201
202             // if it is yang built-in type, no prerequisite is needed, so simply return
203             if (TypeUtils.isYangBuiltInTypeString(stmt.getStatementArgument())) {
204                 return;
205             }
206
207             final QName typeQName = Utils.qNameFromArgument(stmt, stmt.getStatementArgument());
208             final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
209             final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
210                     typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
211             typeAction.mutatesEffectiveCtx(stmt.getParentContext());
212
213             /*
214              * If the type does not exist, throw new InferenceException.
215              * Otherwise perform no operation.
216              */
217             typeAction.apply(new InferenceAction() {
218                 @Override
219                 public void apply() {
220                     // Intentional NOOP
221                 }
222
223                 @Override
224                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
225                     InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
226                         "Type [%s] was not found.", typeQName);
227                 }
228             });
229         }
230     }
231
232     @Nonnull
233     @Override
234     public String getName() {
235         return argument();
236     }
237 }