2 * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
10 import com.google.common.base.Verify;
11 import com.google.common.collect.ImmutableMap;
12 import java.util.Collection;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.yang.common.QName;
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.IntegerTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
36 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
37 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
50 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
51 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.TypeDefEffectiveStatementImpl;
52 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryTypeEffectiveStatementImpl;
53 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BitsTypeEffectiveStatementImpl;
54 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BooleanTypeEffectiveStatementImpl;
55 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BuiltinEffectiveStatements;
56 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.DecimalTypeEffectiveStatementImpl;
57 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EmptyTypeEffectiveStatementImpl;
58 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EnumTypeEffectiveStatementImpl;
59 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityrefTypeEffectiveStatementImpl;
60 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.InstanceIdentifierTypeEffectiveStatementImpl;
61 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IntegerTypeEffectiveStatementImpl;
62 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LeafrefTypeEffectiveStatementImpl;
63 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.StringTypeEffectiveStatementImpl;
64 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnionTypeEffectiveStatementImpl;
65 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnsignedIntegerTypeEffectiveStatementImpl;
67 public class TypeStatementImpl extends AbstractDeclaredStatement<String>
68 implements TypeStatement {
69 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
71 .addOptional(YangStmtMapping.BASE)
72 .addAny(YangStmtMapping.BIT)
73 .addAny(YangStmtMapping.ENUM)
74 .addOptional(YangStmtMapping.FRACTION_DIGITS)
75 .addOptional(YangStmtMapping.LENGTH)
76 .addOptional(YangStmtMapping.PATH)
77 .addAny(YangStmtMapping.PATTERN)
78 .addOptional(YangStmtMapping.RANGE)
79 .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
80 .addAny(YangStmtMapping.TYPE)
83 protected TypeStatementImpl(final StmtContext<String, TypeStatement, ?> context) {
87 public static class Definition
89 AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
91 private static final Map<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS = ImmutableMap
92 .<String, StatementSupport<?, ?, ?>> builder()
93 .put(TypeUtils.DECIMAL64, new Decimal64SpecificationImpl.Definition())
94 .put(TypeUtils.UNION, new UnionSpecificationImpl.Definition())
95 .put(TypeUtils.ENUMERATION, new EnumSpecificationImpl.Definition())
96 .put(TypeUtils.LEAF_REF, new LeafrefSpecificationImpl.Definition())
97 .put(TypeUtils.BITS, new BitsSpecificationImpl.Definition())
98 .put(TypeUtils.IDENTITY_REF, new IdentityRefSpecificationImpl.Definition())
99 .put(TypeUtils.INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationImpl.Definition()).build();
101 public Definition() {
102 super(YangStmtMapping.TYPE);
106 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
111 public TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
112 return BuiltinTypeStatement.maybeReplace(new TypeStatementImpl(ctx));
116 public TypeEffectiveStatement<TypeStatement> createEffective(
117 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
119 // First look up the proper base type
120 final TypeEffectiveStatement<TypeStatement> typeStmt;
121 switch (ctx.getStatementArgument()) {
122 case TypeUtils.BINARY:
123 typeStmt = BuiltinEffectiveStatements.BINARY;
125 case TypeUtils.BOOLEAN:
126 typeStmt = BuiltinEffectiveStatements.BOOLEAN;
128 case TypeUtils.EMPTY:
129 typeStmt = BuiltinEffectiveStatements.EMPTY;
131 case TypeUtils.INSTANCE_IDENTIFIER:
132 typeStmt = BuiltinEffectiveStatements.INSTANCE_IDENTIFIER;
135 typeStmt = BuiltinEffectiveStatements.INT8;
137 case TypeUtils.INT16:
138 typeStmt = BuiltinEffectiveStatements.INT16;
140 case TypeUtils.INT32:
141 typeStmt = BuiltinEffectiveStatements.INT32;
143 case TypeUtils.INT64:
144 typeStmt = BuiltinEffectiveStatements.INT64;
146 case TypeUtils.STRING:
147 typeStmt = BuiltinEffectiveStatements.STRING;
149 case TypeUtils.UINT8:
150 typeStmt = BuiltinEffectiveStatements.UINT8;
152 case TypeUtils.UINT16:
153 typeStmt = BuiltinEffectiveStatements.UINT16;
155 case TypeUtils.UINT32:
156 typeStmt = BuiltinEffectiveStatements.UINT32;
158 case TypeUtils.UINT64:
159 typeStmt = BuiltinEffectiveStatements.UINT64;
162 final QName qname = StmtContextUtils.qnameFromArgument(ctx, ctx.getStatementArgument());
163 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
164 ctx.getFromNamespace(TypeNamespace.class, qname);
165 SourceException.throwIfNull(typedef, ctx.getStatementSourceReference(), "Type '%s' not found", qname);
167 final TypedefEffectiveStatement effectiveTypedef = typedef.buildEffective();
168 Verify.verify(effectiveTypedef instanceof TypeDefEffectiveStatementImpl);
169 typeStmt = ((TypeDefEffectiveStatementImpl) effectiveTypedef).asTypeEffectiveStatement();
172 if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
176 // Now instantiate the proper effective statement for that type
177 final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
178 if (baseType instanceof BinaryTypeDefinition) {
179 return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType);
180 } else if (baseType instanceof BitsTypeDefinition) {
181 return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
182 } else if (baseType instanceof BooleanTypeDefinition) {
183 return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType);
184 } else if (baseType instanceof DecimalTypeDefinition) {
185 return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
186 } else if (baseType instanceof EmptyTypeDefinition) {
187 return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType);
188 } else if (baseType instanceof EnumTypeDefinition) {
189 return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
190 } else if (baseType instanceof IdentityrefTypeDefinition) {
191 return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType);
192 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
193 return new InstanceIdentifierTypeEffectiveStatementImpl(ctx, (InstanceIdentifierTypeDefinition) baseType);
194 } else if (baseType instanceof IntegerTypeDefinition) {
195 return new IntegerTypeEffectiveStatementImpl(ctx, (IntegerTypeDefinition) baseType);
196 } else if (baseType instanceof LeafrefTypeDefinition) {
197 return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
198 } else if (baseType instanceof StringTypeDefinition) {
199 return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
200 } else if (baseType instanceof UnionTypeDefinition) {
201 return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
202 } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
203 return new UnsignedIntegerTypeEffectiveStatementImpl(ctx, (UnsignedIntegerTypeDefinition) baseType);
205 throw new IllegalStateException("Unhandled base type " + baseType);
210 public void onFullDefinitionDeclared(
211 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
212 super.onFullDefinitionDeclared(stmt);
214 // if it is yang built-in type, no prerequisite is needed, so simply return
215 if (TypeUtils.isYangBuiltInTypeString(stmt.getStatementArgument())) {
219 final QName typeQName = StmtContextUtils.qnameFromArgument(stmt, stmt.getStatementArgument());
220 final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
221 final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
222 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
223 typeAction.mutatesEffectiveCtx(stmt.getParentContext());
226 * If the type does not exist, throw new InferenceException.
227 * Otherwise perform no operation.
229 typeAction.apply(new InferenceAction() {
231 public void apply(final InferenceContext ctx) {
236 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
237 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
238 "Type [%s] was not found.", typeQName);
244 protected SubstatementValidator getSubstatementValidator() {
245 return SUBSTATEMENT_VALIDATOR;
249 public boolean hasArgumentSpecificSupports() {
250 return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
254 public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
255 return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
259 public String internArgument(final String rawArgument) {
260 final String found = TypeUtils.findBuiltinString(rawArgument);
261 return found != null ? found : rawArgument;
267 public String getName() {