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