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