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;
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;
63 public class TypeStatementImpl extends AbstractDeclaredStatement<String>
64 implements TypeStatement {
65 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
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)
79 protected TypeStatementImpl(final StmtContext<String, TypeStatement, ?> context) {
83 public static class Definition
85 AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
88 super(Rfc6020Mapping.TYPE);
92 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value)
93 throws SourceException {
98 public TypeStatement createDeclared(
99 final StmtContext<String, TypeStatement, ?> ctx) {
100 return new TypeStatementImpl(ctx);
104 public TypeEffectiveStatement<TypeStatement> createEffective(
105 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
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;
113 case TypeUtils.BOOLEAN:
114 typeStmt = BuiltinEffectiveStatements.BOOLEAN;
116 case TypeUtils.EMPTY:
117 typeStmt = BuiltinEffectiveStatements.EMPTY;
119 case TypeUtils.INSTANCE_IDENTIFIER:
120 typeStmt = BuiltinEffectiveStatements.INSTANCE_IDENTIFIER;
123 typeStmt = BuiltinEffectiveStatements.INT8;
125 case TypeUtils.INT16:
126 typeStmt = BuiltinEffectiveStatements.INT16;
128 case TypeUtils.INT32:
129 typeStmt = BuiltinEffectiveStatements.INT32;
131 case TypeUtils.INT64:
132 typeStmt = BuiltinEffectiveStatements.INT64;
134 case TypeUtils.STRING:
135 typeStmt = BuiltinEffectiveStatements.STRING;
137 case TypeUtils.UINT8:
138 typeStmt = BuiltinEffectiveStatements.UINT8;
140 case TypeUtils.UINT16:
141 typeStmt = BuiltinEffectiveStatements.UINT16;
143 case TypeUtils.UINT32:
144 typeStmt = BuiltinEffectiveStatements.UINT32;
146 case TypeUtils.UINT64:
147 typeStmt = BuiltinEffectiveStatements.UINT64;
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);
155 final TypedefEffectiveStatement effectiveTypedef = typedef.buildEffective();
156 Verify.verify(effectiveTypedef instanceof TypeDefEffectiveStatementImpl);
157 typeStmt = ((TypeDefEffectiveStatementImpl) effectiveTypedef).asTypeEffectiveStatement();
160 if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
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);
193 throw new IllegalStateException("Unhandled base type " + baseType);
198 public void onFullDefinitionDeclared(
199 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt){
200 SUBSTATEMENT_VALIDATOR.validate(stmt);
202 // if it is yang built-in type, no prerequisite is needed, so simply return
203 if (TypeUtils.isYangBuiltInTypeString(stmt.getStatementArgument())) {
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());
214 * If the type does not exist, throw new InferenceException.
215 * Otherwise perform no operation.
217 typeAction.apply(new InferenceAction() {
219 public void apply() {
224 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
225 InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
226 "Type [%s] was not found.", typeQName);
234 public String getName() {