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 static org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator.MAX;
12 import com.google.common.base.Verify;
13 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.Rfc6020Mapping;
17 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
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.Prerequisite;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
47 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
48 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.TypeDefEffectiveStatementImpl;
49 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryTypeEffectiveStatementImpl;
50 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BitsTypeEffectiveStatementImpl;
51 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BooleanTypeEffectiveStatementImpl;
52 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BuiltinEffectiveStatements;
53 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.DecimalTypeEffectiveStatementImpl;
54 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EmptyTypeEffectiveStatementImpl;
55 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EnumTypeEffectiveStatementImpl;
56 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityrefTypeEffectiveStatementImpl;
57 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.InstanceIdentifierTypeEffectiveStatementImpl;
58 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IntegerTypeEffectiveStatementImpl;
59 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LeafrefTypeEffectiveStatementImpl;
60 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.StringTypeEffectiveStatementImpl;
61 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnionTypeEffectiveStatementImpl;
62 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnsignedIntegerTypeEffectiveStatementImpl;
64 public class TypeStatementImpl extends AbstractDeclaredStatement<String>
65 implements TypeStatement {
66 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
68 .add(Rfc6020Mapping.BASE, 0, 1)
69 .add(Rfc6020Mapping.BIT, 0, MAX)
70 .add(Rfc6020Mapping.ENUM, 0, MAX)
71 .add(Rfc6020Mapping.FRACTION_DIGITS, 0, 1)
72 .add(Rfc6020Mapping.LENGTH, 0, 1)
73 .add(Rfc6020Mapping.PATH, 0, 1)
74 .add(Rfc6020Mapping.PATTERN, 0, MAX)
75 .add(Rfc6020Mapping.RANGE, 0, 1)
76 .add(Rfc6020Mapping.REQUIRE_INSTANCE, 0, 1)
77 .add(Rfc6020Mapping.TYPE, 0, MAX)
80 protected TypeStatementImpl(final StmtContext<String, TypeStatement, ?> context) {
84 public static class Definition
86 AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
89 super(Rfc6020Mapping.TYPE);
93 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value)
94 throws SourceException {
99 public TypeStatement createDeclared(
100 final StmtContext<String, TypeStatement, ?> ctx) {
101 return new TypeStatementImpl(ctx);
105 public TypeEffectiveStatement<TypeStatement> createEffective(
106 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
108 // First look up the proper base type
109 final TypeEffectiveStatement<TypeStatement> typeStmt;
110 switch (ctx.getStatementArgument()) {
111 case TypeUtils.BINARY:
112 typeStmt = BuiltinEffectiveStatements.BINARY;
114 case TypeUtils.BOOLEAN:
115 typeStmt = BuiltinEffectiveStatements.BOOLEAN;
117 case TypeUtils.EMPTY:
118 typeStmt = BuiltinEffectiveStatements.EMPTY;
120 case TypeUtils.INSTANCE_IDENTIFIER:
121 typeStmt = BuiltinEffectiveStatements.INSTANCE_IDENTIFIER;
124 typeStmt = BuiltinEffectiveStatements.INT8;
126 case TypeUtils.INT16:
127 typeStmt = BuiltinEffectiveStatements.INT16;
129 case TypeUtils.INT32:
130 typeStmt = BuiltinEffectiveStatements.INT32;
132 case TypeUtils.INT64:
133 typeStmt = BuiltinEffectiveStatements.INT64;
135 case TypeUtils.STRING:
136 typeStmt = BuiltinEffectiveStatements.STRING;
138 case TypeUtils.UINT8:
139 typeStmt = BuiltinEffectiveStatements.UINT8;
141 case TypeUtils.UINT16:
142 typeStmt = BuiltinEffectiveStatements.UINT16;
144 case TypeUtils.UINT32:
145 typeStmt = BuiltinEffectiveStatements.UINT32;
147 case TypeUtils.UINT64:
148 typeStmt = BuiltinEffectiveStatements.UINT64;
151 final QName qname = Utils.qNameFromArgument(ctx, ctx.getStatementArgument());
152 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
153 ctx.getFromNamespace(TypeNamespace.class, qname);
154 if (typedef == null) {
155 throw new SourceException("Type '" + qname + "' not found", ctx.getStatementSourceReference());
158 final TypedefEffectiveStatement effectiveTypedef = typedef.buildEffective();
159 Verify.verify(effectiveTypedef instanceof TypeDefEffectiveStatementImpl);
160 typeStmt = ((TypeDefEffectiveStatementImpl) effectiveTypedef).asTypeEffectiveStatement();
163 if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
167 // Now instantiate the proper effective statement for that type
168 final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
169 if (baseType instanceof BinaryTypeDefinition) {
170 return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType);
171 } else if (baseType instanceof BitsTypeDefinition) {
172 return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
173 } else if (baseType instanceof BooleanTypeDefinition) {
174 return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType);
175 } else if (baseType instanceof DecimalTypeDefinition) {
176 return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
177 } else if (baseType instanceof EmptyTypeDefinition) {
178 return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType);
179 } else if (baseType instanceof EnumTypeDefinition) {
180 return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
181 } else if (baseType instanceof IdentityrefTypeDefinition) {
182 return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType);
183 } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
184 return new InstanceIdentifierTypeEffectiveStatementImpl(ctx, (InstanceIdentifierTypeDefinition) baseType);
185 } else if (baseType instanceof IntegerTypeDefinition) {
186 return new IntegerTypeEffectiveStatementImpl(ctx, (IntegerTypeDefinition) baseType);
187 } else if (baseType instanceof LeafrefTypeDefinition) {
188 return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
189 } else if (baseType instanceof StringTypeDefinition) {
190 return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
191 } else if (baseType instanceof UnionTypeDefinition) {
192 return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
193 } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
194 return new UnsignedIntegerTypeEffectiveStatementImpl(ctx, (UnsignedIntegerTypeDefinition) baseType);
196 throw new IllegalStateException("Unhandled base type " + baseType);
201 public void onFullDefinitionDeclared(
202 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt)
203 throws SourceException {
204 SUBSTATEMENT_VALIDATOR.validate(stmt);
206 // if it is yang built-in type, no prerequisite is needed, so simply return
207 if (TypeUtils.isYangBuiltInTypeString(stmt.getStatementArgument())) {
211 final QName typeQName = Utils.qNameFromArgument(stmt, stmt.getStatementArgument());
212 final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
213 final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
214 typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
215 typeAction.mutatesEffectiveCtx(stmt.getParentContext());
218 * If the type does not exist, throw new InferenceException.
219 * Otherwise perform no operation.
221 typeAction.apply(new InferenceAction() {
224 public void apply() {
229 public void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) {
230 if (failed.contains(typePrereq)) {
231 throw new InferenceException(String.format("Type [%s] was not found.", typeQName), stmt
232 .getStatementSourceReference());
241 public String getName() {