Reuse cardinality constants
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / TypeStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
9
10 import com.google.common.base.Verify;
11 import java.util.Collection;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
21 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
34 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
35 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
45 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
46 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.TypeDefEffectiveStatementImpl;
47 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryTypeEffectiveStatementImpl;
48 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BitsTypeEffectiveStatementImpl;
49 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BooleanTypeEffectiveStatementImpl;
50 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BuiltinEffectiveStatements;
51 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.DecimalTypeEffectiveStatementImpl;
52 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EmptyTypeEffectiveStatementImpl;
53 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EnumTypeEffectiveStatementImpl;
54 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityrefTypeEffectiveStatementImpl;
55 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.InstanceIdentifierTypeEffectiveStatementImpl;
56 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IntegerTypeEffectiveStatementImpl;
57 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LeafrefTypeEffectiveStatementImpl;
58 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.StringTypeEffectiveStatementImpl;
59 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnionTypeEffectiveStatementImpl;
60 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnsignedIntegerTypeEffectiveStatementImpl;
61
62 public class TypeStatementImpl extends AbstractDeclaredStatement<String>
63         implements TypeStatement {
64     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
65             .TYPE)
66             .addOptional(Rfc6020Mapping.BASE)
67             .addAny(Rfc6020Mapping.BIT)
68             .addAny(Rfc6020Mapping.ENUM)
69             .addOptional(Rfc6020Mapping.FRACTION_DIGITS)
70             .addOptional(Rfc6020Mapping.LENGTH)
71             .addOptional(Rfc6020Mapping.PATH)
72             .addAny(Rfc6020Mapping.PATTERN)
73             .addOptional(Rfc6020Mapping.RANGE)
74             .addOptional(Rfc6020Mapping.REQUIRE_INSTANCE)
75             .addAny(Rfc6020Mapping.TYPE)
76             .build();
77
78     protected TypeStatementImpl(final StmtContext<String, TypeStatement, ?> context) {
79         super(context);
80     }
81
82     public static class Definition
83             extends
84             AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
85
86         public Definition() {
87             super(Rfc6020Mapping.TYPE);
88         }
89
90         @Override
91         public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
92             return value;
93         }
94
95         @Override
96         public TypeStatement createDeclared(
97                 final StmtContext<String, TypeStatement, ?> ctx) {
98             return new TypeStatementImpl(ctx);
99         }
100
101         @Override
102         public TypeEffectiveStatement<TypeStatement> createEffective(
103                 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
104
105             // First look up the proper base type
106             final TypeEffectiveStatement<TypeStatement> typeStmt;
107             switch (ctx.getStatementArgument()) {
108                 case TypeUtils.BINARY:
109                     typeStmt = BuiltinEffectiveStatements.BINARY;
110                     break;
111                 case TypeUtils.BOOLEAN:
112                     typeStmt = BuiltinEffectiveStatements.BOOLEAN;
113                     break;
114                 case TypeUtils.EMPTY:
115                     typeStmt = BuiltinEffectiveStatements.EMPTY;
116                     break;
117                 case TypeUtils.INSTANCE_IDENTIFIER:
118                     typeStmt = BuiltinEffectiveStatements.INSTANCE_IDENTIFIER;
119                     break;
120             case TypeUtils.INT8:
121                 typeStmt = BuiltinEffectiveStatements.INT8;
122                 break;
123             case TypeUtils.INT16:
124                 typeStmt = BuiltinEffectiveStatements.INT16;
125                 break;
126             case TypeUtils.INT32:
127                 typeStmt = BuiltinEffectiveStatements.INT32;
128                 break;
129             case TypeUtils.INT64:
130                 typeStmt = BuiltinEffectiveStatements.INT64;
131                 break;
132             case TypeUtils.STRING:
133                 typeStmt = BuiltinEffectiveStatements.STRING;
134                 break;
135             case TypeUtils.UINT8:
136                 typeStmt = BuiltinEffectiveStatements.UINT8;
137                 break;
138             case TypeUtils.UINT16:
139                 typeStmt = BuiltinEffectiveStatements.UINT16;
140                 break;
141             case TypeUtils.UINT32:
142                 typeStmt = BuiltinEffectiveStatements.UINT32;
143                 break;
144             case TypeUtils.UINT64:
145                 typeStmt = BuiltinEffectiveStatements.UINT64;
146                 break;
147             default:
148                 final QName qname = Utils.qNameFromArgument(ctx, ctx.getStatementArgument());
149                 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
150                         ctx.getFromNamespace(TypeNamespace.class, qname);
151                 SourceException.throwIfNull(typedef, ctx.getStatementSourceReference(), "Type '%s' not found", qname);
152
153                 final TypedefEffectiveStatement effectiveTypedef = typedef.buildEffective();
154                 Verify.verify(effectiveTypedef instanceof TypeDefEffectiveStatementImpl);
155                 typeStmt = ((TypeDefEffectiveStatementImpl) effectiveTypedef).asTypeEffectiveStatement();
156             }
157
158             if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
159                 return typeStmt;
160             }
161
162             // Now instantiate the proper effective statement for that type
163             final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
164             if (baseType instanceof BinaryTypeDefinition) {
165                 return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType);
166             } else if (baseType instanceof BitsTypeDefinition) {
167                 return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
168             } else if (baseType instanceof BooleanTypeDefinition) {
169                 return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType);
170             } else if (baseType instanceof DecimalTypeDefinition) {
171                 return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
172             } else if (baseType instanceof EmptyTypeDefinition) {
173                 return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType);
174             } else if (baseType instanceof EnumTypeDefinition) {
175                 return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
176             } else if (baseType instanceof IdentityrefTypeDefinition) {
177                 return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType);
178             } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
179                 return new InstanceIdentifierTypeEffectiveStatementImpl(ctx, (InstanceIdentifierTypeDefinition) baseType);
180             } else if (baseType instanceof IntegerTypeDefinition) {
181                 return new IntegerTypeEffectiveStatementImpl(ctx, (IntegerTypeDefinition) baseType);
182             } else if (baseType instanceof LeafrefTypeDefinition) {
183                 return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
184             } else if (baseType instanceof StringTypeDefinition) {
185                 return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
186             } else if (baseType instanceof UnionTypeDefinition) {
187                 return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
188             } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
189                 return new UnsignedIntegerTypeEffectiveStatementImpl(ctx, (UnsignedIntegerTypeDefinition) baseType);
190             } else {
191                 throw new IllegalStateException("Unhandled base type " + baseType);
192             }
193         }
194
195         @Override
196         public void onFullDefinitionDeclared(
197                 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt){
198             SUBSTATEMENT_VALIDATOR.validate(stmt);
199
200             // if it is yang built-in type, no prerequisite is needed, so simply return
201             if (TypeUtils.isYangBuiltInTypeString(stmt.getStatementArgument())) {
202                 return;
203             }
204
205             final QName typeQName = Utils.qNameFromArgument(stmt, stmt.getStatementArgument());
206             final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
207             final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
208                     typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
209             typeAction.mutatesEffectiveCtx(stmt.getParentContext());
210
211             /*
212              * If the type does not exist, throw new InferenceException.
213              * Otherwise perform no operation.
214              */
215             typeAction.apply(new InferenceAction() {
216                 @Override
217                 public void apply() {
218                     // Intentional NOOP
219                 }
220
221                 @Override
222                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
223                     InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
224                         "Type [%s] was not found.", typeQName);
225                 }
226             });
227         }
228     }
229
230     @Nonnull
231     @Override
232     public String getName() {
233         return argument();
234     }
235 }