Move DeviateKind parsing
[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 com.google.common.collect.ImmutableMap;
12 import java.util.Collection;
13 import java.util.Map;
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.Prerequisite;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
48 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
49 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.TypeDefEffectiveStatementImpl;
50 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryTypeEffectiveStatementImpl;
51 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BitsTypeEffectiveStatementImpl;
52 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BooleanTypeEffectiveStatementImpl;
53 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BuiltinEffectiveStatements;
54 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.DecimalTypeEffectiveStatementImpl;
55 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EmptyTypeEffectiveStatementImpl;
56 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EnumTypeEffectiveStatementImpl;
57 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IdentityrefTypeEffectiveStatementImpl;
58 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.InstanceIdentifierTypeEffectiveStatementImpl;
59 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.IntegerTypeEffectiveStatementImpl;
60 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LeafrefTypeEffectiveStatementImpl;
61 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.StringTypeEffectiveStatementImpl;
62 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnionTypeEffectiveStatementImpl;
63 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UnsignedIntegerTypeEffectiveStatementImpl;
64
65 public class TypeStatementImpl extends AbstractDeclaredStatement<String>
66         implements TypeStatement {
67     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
68             .TYPE)
69             .addOptional(YangStmtMapping.BASE)
70             .addAny(YangStmtMapping.BIT)
71             .addAny(YangStmtMapping.ENUM)
72             .addOptional(YangStmtMapping.FRACTION_DIGITS)
73             .addOptional(YangStmtMapping.LENGTH)
74             .addOptional(YangStmtMapping.PATH)
75             .addAny(YangStmtMapping.PATTERN)
76             .addOptional(YangStmtMapping.RANGE)
77             .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
78             .addAny(YangStmtMapping.TYPE)
79             .build();
80
81     protected TypeStatementImpl(final StmtContext<String, TypeStatement, ?> context) {
82         super(context);
83     }
84
85     public static class Definition
86             extends
87             AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
88
89         private static final Map<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS = ImmutableMap
90                 .<String, StatementSupport<?, ?, ?>> builder()
91                 .put(TypeUtils.DECIMAL64, new Decimal64SpecificationImpl.Definition())
92                 .put(TypeUtils.UNION, new UnionSpecificationImpl.Definition())
93                 .put(TypeUtils.ENUMERATION, new EnumSpecificationImpl.Definition())
94                 .put(TypeUtils.LEAF_REF, new LeafrefSpecificationImpl.Definition())
95                 .put(TypeUtils.BITS, new BitsSpecificationImpl.Definition())
96                 .put(TypeUtils.IDENTITY_REF, new IdentityRefSpecificationImpl.Definition())
97                 .put(TypeUtils.INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationImpl.Definition()).build();
98
99         public Definition() {
100             super(YangStmtMapping.TYPE);
101         }
102
103         @Override
104         public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
105             return value;
106         }
107
108         @Override
109         public TypeStatement createDeclared(final StmtContext<String, TypeStatement, ?> ctx) {
110             return BuiltinTypeStatement.maybeReplace(new TypeStatementImpl(ctx));
111         }
112
113         @Override
114         public TypeEffectiveStatement<TypeStatement> createEffective(
115                 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
116
117             // First look up the proper base type
118             final TypeEffectiveStatement<TypeStatement> typeStmt;
119             switch (ctx.getStatementArgument()) {
120                 case TypeUtils.BINARY:
121                     typeStmt = BuiltinEffectiveStatements.BINARY;
122                     break;
123                 case TypeUtils.BOOLEAN:
124                     typeStmt = BuiltinEffectiveStatements.BOOLEAN;
125                     break;
126                 case TypeUtils.EMPTY:
127                     typeStmt = BuiltinEffectiveStatements.EMPTY;
128                     break;
129                 case TypeUtils.INSTANCE_IDENTIFIER:
130                     typeStmt = BuiltinEffectiveStatements.INSTANCE_IDENTIFIER;
131                     break;
132             case TypeUtils.INT8:
133                 typeStmt = BuiltinEffectiveStatements.INT8;
134                 break;
135             case TypeUtils.INT16:
136                 typeStmt = BuiltinEffectiveStatements.INT16;
137                 break;
138             case TypeUtils.INT32:
139                 typeStmt = BuiltinEffectiveStatements.INT32;
140                 break;
141             case TypeUtils.INT64:
142                 typeStmt = BuiltinEffectiveStatements.INT64;
143                 break;
144             case TypeUtils.STRING:
145                 typeStmt = BuiltinEffectiveStatements.STRING;
146                 break;
147             case TypeUtils.UINT8:
148                 typeStmt = BuiltinEffectiveStatements.UINT8;
149                 break;
150             case TypeUtils.UINT16:
151                 typeStmt = BuiltinEffectiveStatements.UINT16;
152                 break;
153             case TypeUtils.UINT32:
154                 typeStmt = BuiltinEffectiveStatements.UINT32;
155                 break;
156             case TypeUtils.UINT64:
157                 typeStmt = BuiltinEffectiveStatements.UINT64;
158                 break;
159             default:
160                 final QName qname = Utils.qNameFromArgument(ctx, ctx.getStatementArgument());
161                 final StmtContext<?, TypedefStatement, TypedefEffectiveStatement> typedef =
162                         ctx.getFromNamespace(TypeNamespace.class, qname);
163                 SourceException.throwIfNull(typedef, ctx.getStatementSourceReference(), "Type '%s' not found", qname);
164
165                 final TypedefEffectiveStatement effectiveTypedef = typedef.buildEffective();
166                 Verify.verify(effectiveTypedef instanceof TypeDefEffectiveStatementImpl);
167                 typeStmt = ((TypeDefEffectiveStatementImpl) effectiveTypedef).asTypeEffectiveStatement();
168             }
169
170             if (ctx.declaredSubstatements().isEmpty() && ctx.effectiveSubstatements().isEmpty()) {
171                 return typeStmt;
172             }
173
174             // Now instantiate the proper effective statement for that type
175             final TypeDefinition<?> baseType = typeStmt.getTypeDefinition();
176             if (baseType instanceof BinaryTypeDefinition) {
177                 return new BinaryTypeEffectiveStatementImpl(ctx, (BinaryTypeDefinition) baseType);
178             } else if (baseType instanceof BitsTypeDefinition) {
179                 return new BitsTypeEffectiveStatementImpl(ctx, (BitsTypeDefinition) baseType);
180             } else if (baseType instanceof BooleanTypeDefinition) {
181                 return new BooleanTypeEffectiveStatementImpl(ctx, (BooleanTypeDefinition) baseType);
182             } else if (baseType instanceof DecimalTypeDefinition) {
183                 return new DecimalTypeEffectiveStatementImpl(ctx, (DecimalTypeDefinition) baseType);
184             } else if (baseType instanceof EmptyTypeDefinition) {
185                 return new EmptyTypeEffectiveStatementImpl(ctx, (EmptyTypeDefinition) baseType);
186             } else if (baseType instanceof EnumTypeDefinition) {
187                 return new EnumTypeEffectiveStatementImpl(ctx, (EnumTypeDefinition) baseType);
188             } else if (baseType instanceof IdentityrefTypeDefinition) {
189                 return new IdentityrefTypeEffectiveStatementImpl(ctx, (IdentityrefTypeDefinition) baseType);
190             } else if (baseType instanceof InstanceIdentifierTypeDefinition) {
191                 return new InstanceIdentifierTypeEffectiveStatementImpl(ctx, (InstanceIdentifierTypeDefinition) baseType);
192             } else if (baseType instanceof IntegerTypeDefinition) {
193                 return new IntegerTypeEffectiveStatementImpl(ctx, (IntegerTypeDefinition) baseType);
194             } else if (baseType instanceof LeafrefTypeDefinition) {
195                 return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
196             } else if (baseType instanceof StringTypeDefinition) {
197                 return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
198             } else if (baseType instanceof UnionTypeDefinition) {
199                 return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
200             } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
201                 return new UnsignedIntegerTypeEffectiveStatementImpl(ctx, (UnsignedIntegerTypeDefinition) baseType);
202             } else {
203                 throw new IllegalStateException("Unhandled base type " + baseType);
204             }
205         }
206
207         @Override
208         public void onFullDefinitionDeclared(
209                 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
210             super.onFullDefinitionDeclared(stmt);
211
212             // if it is yang built-in type, no prerequisite is needed, so simply return
213             if (TypeUtils.isYangBuiltInTypeString(stmt.getStatementArgument())) {
214                 return;
215             }
216
217             final QName typeQName = Utils.qNameFromArgument(stmt, stmt.getStatementArgument());
218             final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
219             final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
220                     typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
221             typeAction.mutatesEffectiveCtx(stmt.getParentContext());
222
223             /*
224              * If the type does not exist, throw new InferenceException.
225              * Otherwise perform no operation.
226              */
227             typeAction.apply(new InferenceAction() {
228                 @Override
229                 public void apply() {
230                     // Intentional NOOP
231                 }
232
233                 @Override
234                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
235                     InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
236                         "Type [%s] was not found.", typeQName);
237                 }
238             });
239         }
240
241         @Override
242         protected SubstatementValidator getSubstatementValidator() {
243             return SUBSTATEMENT_VALIDATOR;
244         }
245
246         @Override
247         public boolean hasArgumentSpecificSupports() {
248             return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
249         }
250
251         @Override
252         public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
253             return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
254         }
255
256         @Override
257         public String internArgument(final String rawArgument) {
258             final String found = TypeUtils.findBuiltinString(rawArgument);
259             return found != null ? found : rawArgument;
260         }
261     }
262
263     @Nonnull
264     @Override
265     public String getName() {
266         return argument();
267     }
268 }