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