0f1f85e410c2755765ba68223c41822f3632a98c
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / AbstractTypeStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.rfc7950.stmt.type;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.util.Collection;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
23 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
41 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
42 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
43 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
52 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
53 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
54 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
55 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
56 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
57
58 abstract class AbstractTypeStatementSupport
59         extends AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
60     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
61         YangStmtMapping.TYPE)
62         .addOptional(YangStmtMapping.BASE)
63         .addAny(YangStmtMapping.BIT)
64         .addAny(YangStmtMapping.ENUM)
65         .addOptional(YangStmtMapping.FRACTION_DIGITS)
66         .addOptional(YangStmtMapping.LENGTH)
67         .addOptional(YangStmtMapping.PATH)
68         .addAny(YangStmtMapping.PATTERN)
69         .addOptional(YangStmtMapping.RANGE)
70         .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
71         .addAny(YangStmtMapping.TYPE)
72         .build();
73
74     static final String BINARY = "binary";
75     static final String BITS = "bits";
76     static final String BOOLEAN = "boolean";
77     static final String DECIMAL64 = "decimal64";
78     static final String EMPTY = "empty";
79     static final String ENUMERATION = "enumeration";
80     static final String IDENTITY_REF = "identityref";
81     static final String INSTANCE_IDENTIFIER = "instance-identifier";
82     static final String INT8 = "int8";
83     static final String INT16 = "int16";
84     static final String INT32 = "int32";
85     static final String INT64 = "int64";
86     static final String LEAF_REF = "leafref";
87     static final String STRING = "string";
88     static final String UINT8 = "uint8";
89     static final String UINT16 = "uint16";
90     static final String UINT32 = "uint32";
91     static final String UINT64 = "uint64";
92     static final String UNION = "union";
93
94     private static final ImmutableMap<String, String> BUILT_IN_TYPES = ImmutableMap.<String, String>builder()
95         .put(BINARY, BINARY)
96         .put(BITS, BITS)
97         .put(BOOLEAN, BOOLEAN)
98         .put(DECIMAL64, DECIMAL64)
99         .put(EMPTY, EMPTY)
100         .put(ENUMERATION, ENUMERATION)
101         .put(IDENTITY_REF,IDENTITY_REF)
102         .put(INSTANCE_IDENTIFIER, INSTANCE_IDENTIFIER)
103         .put(INT8, INT8)
104         .put(INT16, INT16)
105         .put(INT32, INT32)
106         .put(INT64, INT64)
107         .put(LEAF_REF, LEAF_REF)
108         .put(STRING, STRING)
109         .put(UINT8, UINT8)
110         .put(UINT16, UINT16)
111         .put(UINT32, UINT32)
112         .put(UINT64, UINT64)
113         .put(UNION, UNION)
114         .build();
115
116     private static final ImmutableMap<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS =
117             ImmutableMap.<String, StatementSupport<?, ?, ?>>builder()
118             .put(BITS, new BitsSpecificationSupport())
119             .put(DECIMAL64, new Decimal64SpecificationSupport())
120             .put(ENUMERATION, new EnumSpecificationSupport())
121             .put(IDENTITY_REF, new IdentityRefSpecificationRFC6020Support())
122             .put(INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationSupport())
123             .put(LEAF_REF, new LeafrefSpecificationRFC6020Support())
124             .put(UNION, new UnionSpecificationSupport())
125             .build();
126
127     AbstractTypeStatementSupport() {
128         super(YangStmtMapping.TYPE);
129     }
130
131     @Override
132     public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
133         return value;
134     }
135
136     @Override
137     public final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
138         return BuiltinTypeStatement.maybeReplace(new TypeStatementImpl(ctx));
139     }
140
141     @Override
142     public final TypeEffectiveStatement<TypeStatement> createEffective(
143             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
144
145         // First look up the proper base type
146         final TypeEffectiveStatement<TypeStatement> typeStmt;
147         switch (ctx.coerceStatementArgument()) {
148             case BINARY:
149                 typeStmt = BuiltinEffectiveStatement.BINARY;
150                 break;
151             case BOOLEAN:
152                 typeStmt = BuiltinEffectiveStatement.BOOLEAN;
153                 break;
154             case EMPTY:
155                 typeStmt = BuiltinEffectiveStatement.EMPTY;
156                 break;
157             case INSTANCE_IDENTIFIER:
158                 typeStmt = BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
159                 break;
160             case INT8:
161                 typeStmt = BuiltinEffectiveStatement.INT8;
162                 break;
163             case INT16:
164                 typeStmt = BuiltinEffectiveStatement.INT16;
165                 break;
166             case INT32:
167                 typeStmt = BuiltinEffectiveStatement.INT32;
168                 break;
169             case INT64:
170                 typeStmt = BuiltinEffectiveStatement.INT64;
171                 break;
172             case STRING:
173                 typeStmt = BuiltinEffectiveStatement.STRING;
174                 break;
175             case UINT8:
176                 typeStmt = BuiltinEffectiveStatement.UINT8;
177                 break;
178             case UINT16:
179                 typeStmt = BuiltinEffectiveStatement.UINT16;
180                 break;
181             case UINT32:
182                 typeStmt = BuiltinEffectiveStatement.UINT32;
183                 break;
184             case UINT64:
185                 typeStmt = BuiltinEffectiveStatement.UINT64;
186                 break;
187             default:
188                 final QName qname = StmtContextUtils.parseNodeIdentifier(ctx, ctx.getStatementArgument());
189                 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
190                         SourceException.throwIfNull(ctx.getFromNamespace(TypeNamespace.class, qname),
191                             ctx.getStatementSourceReference(), "Type '%s' not found", qname);
192                 typeStmt = typedef.buildEffective().asTypeEffectiveStatement();
193         }
194
195         if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
196             return typeStmt;
197         }
198
199         // Now instantiate the proper effective statement for that type
200         final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
201         if (baseType instanceof BinaryTypeDefinition) {
202             return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType);
203         } else if (baseType instanceof BitsTypeDefinition) {
204             return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
205         } else if (baseType instanceof BooleanTypeDefinition) {
206             return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType);
207         } else if (baseType instanceof DecimalTypeDefinition) {
208             return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
209         } else if (baseType instanceof EmptyTypeDefinition) {
210             return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType);
211         } else if (baseType instanceof EnumTypeDefinition) {
212             return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
213         } else if (baseType instanceof IdentityrefTypeDefinition) {
214             return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType);
215         } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
216             return new InstanceIdentifierTypeEffectiveStatementImpl(ctx,
217                 (InstanceIdentifierTypeDefinition) baseType);
218         } else if (baseType instanceof Int8TypeDefinition) {
219             return new IntegralTypeEffectiveStatementImpl<>(ctx,
220                     RestrictedTypes.newInt8Builder((Int8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
221         } else if (baseType instanceof Int16TypeDefinition) {
222             return new IntegralTypeEffectiveStatementImpl<>(ctx,
223                     RestrictedTypes.newInt16Builder((Int16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
224         } else if (baseType instanceof Int32TypeDefinition) {
225             return new IntegralTypeEffectiveStatementImpl<>(ctx,
226                     RestrictedTypes.newInt32Builder((Int32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
227         } else if (baseType instanceof Int64TypeDefinition) {
228             return new IntegralTypeEffectiveStatementImpl<>(ctx,
229                     RestrictedTypes.newInt64Builder((Int64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
230         } else if (baseType instanceof LeafrefTypeDefinition) {
231             return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
232         } else if (baseType instanceof StringTypeDefinition) {
233             return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
234         } else if (baseType instanceof Uint8TypeDefinition) {
235             return new IntegralTypeEffectiveStatementImpl<>(ctx,
236                     RestrictedTypes.newUint8Builder((Uint8TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
237         } else if (baseType instanceof Uint16TypeDefinition) {
238             return new IntegralTypeEffectiveStatementImpl<>(ctx,
239                     RestrictedTypes.newUint16Builder((Uint16TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
240         } else if (baseType instanceof Uint32TypeDefinition) {
241             return new IntegralTypeEffectiveStatementImpl<>(ctx,
242                     RestrictedTypes.newUint32Builder((Uint32TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
243         } else if (baseType instanceof Uint64TypeDefinition) {
244             return new IntegralTypeEffectiveStatementImpl<>(ctx,
245                     RestrictedTypes.newUint64Builder((Uint64TypeDefinition) baseType, typeEffectiveSchemaPath(ctx)));
246         } else if (baseType instanceof UnionTypeDefinition) {
247             return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
248         } else {
249             throw new IllegalStateException("Unhandled base type " + baseType);
250         }
251     }
252
253     @Override
254     public final void onFullDefinitionDeclared(
255             final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
256         super.onFullDefinitionDeclared(stmt);
257
258         // if it is yang built-in type, no prerequisite is needed, so simply return
259         if (BUILT_IN_TYPES.containsKey(stmt.getStatementArgument())) {
260             return;
261         }
262
263         final QName typeQName = StmtContextUtils.parseNodeIdentifier(stmt, stmt.getStatementArgument());
264         final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
265         final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
266                 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
267         typeAction.mutatesEffectiveCtx(stmt.getParentContext());
268
269         /*
270          * If the type does not exist, throw new InferenceException.
271          * Otherwise perform no operation.
272          */
273         typeAction.apply(new InferenceAction() {
274             @Override
275             public void apply(final InferenceContext ctx) {
276                 // Intentional NOOP
277             }
278
279             @Override
280             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
281                 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
282                     "Type [%s] was not found.", typeQName);
283             }
284         });
285     }
286
287     @Override
288     protected final SubstatementValidator getSubstatementValidator() {
289         return SUBSTATEMENT_VALIDATOR;
290     }
291
292     @Override
293     public final String internArgument(final String rawArgument) {
294         final String found;
295         return (found = BUILT_IN_TYPES.get(rawArgument)) != null ? found : rawArgument;
296     }
297
298     @Override
299     public boolean hasArgumentSpecificSupports() {
300         return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
301     }
302
303     @Override
304     public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
305         return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
306     }
307
308     static final SchemaPath typeEffectiveSchemaPath(final StmtContext<?, ?, ?> stmtCtx) {
309         final SchemaPath path = stmtCtx.getSchemaPath().get();
310         final SchemaPath parent = path.getParent();
311         final QName parentQName = parent.getLastComponent();
312         checkArgument(parentQName != null, "Path %s has an empty parent", path);
313
314         final QName qname = path.getLastComponent().bindTo(parentQName.getModule()).intern();
315         return parent.createChild(qname);
316     }
317 }