BUG-7052: Move qnameFromArgument to StmtContextUtils
[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.InferenceContext;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
47 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
48 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
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
89             AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
90
91         private static final Map<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS = ImmutableMap
92                 .<String, StatementSupport<?, ?, ?>> builder()
93                 .put(TypeUtils.DECIMAL64, new Decimal64SpecificationImpl.Definition())
94                 .put(TypeUtils.UNION, new UnionSpecificationImpl.Definition())
95                 .put(TypeUtils.ENUMERATION, new EnumSpecificationImpl.Definition())
96                 .put(TypeUtils.LEAF_REF, new LeafrefSpecificationImpl.Definition())
97                 .put(TypeUtils.BITS, new BitsSpecificationImpl.Definition())
98                 .put(TypeUtils.IDENTITY_REF, new IdentityRefSpecificationImpl.Definition())
99                 .put(TypeUtils.INSTANCE_IDENTIFIER, new InstanceIdentifierSpecificationImpl.Definition()).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                         ctx.getFromNamespace(TypeNamespace.class, qname);
165                 SourceException.throwIfNull(typedef, 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, (InstanceIdentifierTypeDefinition) baseType);
194             } else if (baseType instanceof IntegerTypeDefinition) {
195                 return new IntegerTypeEffectiveStatementImpl(ctx, (IntegerTypeDefinition) baseType);
196             } else if (baseType instanceof LeafrefTypeDefinition) {
197                 return new LeafrefTypeEffectiveStatementImpl(ctx, (LeafrefTypeDefinition) baseType);
198             } else if (baseType instanceof StringTypeDefinition) {
199                 return new StringTypeEffectiveStatementImpl(ctx, (StringTypeDefinition) baseType);
200             } else if (baseType instanceof UnionTypeDefinition) {
201                 return new UnionTypeEffectiveStatementImpl(ctx, (UnionTypeDefinition) baseType);
202             } else if (baseType instanceof UnsignedIntegerTypeDefinition) {
203                 return new UnsignedIntegerTypeEffectiveStatementImpl(ctx, (UnsignedIntegerTypeDefinition) baseType);
204             } else {
205                 throw new IllegalStateException("Unhandled base type " + baseType);
206             }
207         }
208
209         @Override
210         public void onFullDefinitionDeclared(
211                 final Mutable<String, TypeStatement, EffectiveStatement<String, TypeStatement>> stmt) {
212             super.onFullDefinitionDeclared(stmt);
213
214             // if it is yang built-in type, no prerequisite is needed, so simply return
215             if (TypeUtils.isYangBuiltInTypeString(stmt.getStatementArgument())) {
216                 return;
217             }
218
219             final QName typeQName = StmtContextUtils.qnameFromArgument(stmt, stmt.getStatementArgument());
220             final ModelActionBuilder typeAction = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
221             final Prerequisite<StmtContext<?, ?, ?>> typePrereq = typeAction.requiresCtx(stmt, TypeNamespace.class,
222                     typeQName, ModelProcessingPhase.EFFECTIVE_MODEL);
223             typeAction.mutatesEffectiveCtx(stmt.getParentContext());
224
225             /*
226              * If the type does not exist, throw new InferenceException.
227              * Otherwise perform no operation.
228              */
229             typeAction.apply(new InferenceAction() {
230                 @Override
231                 public void apply(final InferenceContext ctx) {
232                     // Intentional NOOP
233                 }
234
235                 @Override
236                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
237                     InferenceException.throwIf(failed.contains(typePrereq), stmt.getStatementSourceReference(),
238                         "Type [%s] was not found.", typeQName);
239                 }
240             });
241         }
242
243         @Override
244         protected SubstatementValidator getSubstatementValidator() {
245             return SUBSTATEMENT_VALIDATOR;
246         }
247
248         @Override
249         public boolean hasArgumentSpecificSupports() {
250             return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty();
251         }
252
253         @Override
254         public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
255             return ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
256         }
257
258         @Override
259         public String internArgument(final String rawArgument) {
260             final String found = TypeUtils.findBuiltinString(rawArgument);
261             return found != null ? found : rawArgument;
262         }
263     }
264
265     @Nonnull
266     @Override
267     public String getName() {
268         return argument();
269     }
270 }