2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import java.util.Collection;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
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.Int16TypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
37 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
41 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
42 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
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;
58 abstract class AbstractTypeStatementSupport
59 extends AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
60 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
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)
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";
94 private static final ImmutableMap<String, String> BUILT_IN_TYPES = ImmutableMap.<String, String>builder()
97 .put(BOOLEAN, BOOLEAN)
98 .put(DECIMAL64, DECIMAL64)
100 .put(ENUMERATION, ENUMERATION)
101 .put(IDENTITY_REF,IDENTITY_REF)
102 .put(INSTANCE_IDENTIFIER, INSTANCE_IDENTIFIER)
107 .put(LEAF_REF, LEAF_REF)
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())
127 AbstractTypeStatementSupport() {
128 super(YangStmtMapping.TYPE);
132 public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
137 public final TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
138 return BuiltinTypeStatement.maybeReplace(new TypeStatementImpl(ctx));
142 public final TypeEffectiveStatement<TypeStatement> createEffective(
143 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
145 // First look up the proper base type
146 final TypeEffectiveStatement<TypeStatement> typeStmt;
147 switch (ctx.getStatementArgument()) {
149 typeStmt = BuiltinEffectiveStatement.BINARY;
152 typeStmt = BuiltinEffectiveStatement.BOOLEAN;
155 typeStmt = BuiltinEffectiveStatement.EMPTY;
157 case INSTANCE_IDENTIFIER:
158 typeStmt = BuiltinEffectiveStatement.INSTANCE_IDENTIFIER;
161 typeStmt = BuiltinEffectiveStatement.INT8;
164 typeStmt = BuiltinEffectiveStatement.INT16;
167 typeStmt = BuiltinEffectiveStatement.INT32;
170 typeStmt = BuiltinEffectiveStatement.INT64;
173 typeStmt = BuiltinEffectiveStatement.STRING;
176 typeStmt = BuiltinEffectiveStatement.UINT8;
179 typeStmt = BuiltinEffectiveStatement.UINT16;
182 typeStmt = BuiltinEffectiveStatement.UINT32;
185 typeStmt = BuiltinEffectiveStatement.UINT64;
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();
195 if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
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);
249 throw new IllegalStateException("Unhandled base type " + baseType);
254 public final void onFullDefinitionDeclared(
255 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
256 super.onFullDefinitionDeclared(stmt);
258 // if it is yang built-in type, no prerequisite is needed, so simply return
259 if (BUILT_IN_TYPES.containsKey(stmt.getStatementArgument())) {
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());
270 * If the type does not exist, throw new InferenceException.
271 * Otherwise perform no operation.
273 typeAction.apply(new InferenceAction() {
275 public void apply(final InferenceContext ctx) {
280 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
281 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
282 "Type [%s] was not found.", typeQName);
288 protected final SubstatementValidator getSubstatementValidator() {
289 return SUBSTATEMENT_VALIDATOR;
293 public final String internArgument(final String rawArgument) {
294 final String found = BUILT_IN_TYPES.get(rawArgument);
295 return found != null ? found : rawArgument;
299 public boolean hasArgumentSpecificSupports() {
300 return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
304 public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
305 return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
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 Preconditions.checkArgument(parentQName != null, "Path %s has an empty parent", path);
314 final QName qname = stmtCtx.getFromNamespace(QNameCacheNamespace.class,
315 QName.create(parentQName, path.getLastComponent().getLocalName()));
316 return parent.createChild(qname);