Cleanup yang-parser-impl
[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 java.util.Collection;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
18 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
19 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ExtendedTypeEffectiveStatementImpl;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BinaryEffectiveStatementImpl;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.BooleanEffectiveStatementImpl;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.EmptyEffectiveStatementImpl;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Int16EffectiveStatementImpl;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Int32EffectiveStatementImpl;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Int64EffectiveStatementImpl;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.Int8EffectiveStatementImpl;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.StringEffectiveStatementImpl;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UInt16EffectiveStatementImpl;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UInt32EffectiveStatementImpl;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UInt64EffectiveStatementImpl;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.UInt8EffectiveStatementImpl;
33
34 public class TypeStatementImpl extends AbstractDeclaredStatement<String>
35         implements TypeStatement {
36
37     protected TypeStatementImpl(final StmtContext<String, TypeStatement, ?> context) {
38         super(context);
39     }
40
41     public static class Definition
42             extends
43             AbstractStatementSupport<String, TypeStatement, EffectiveStatement<String, TypeStatement>> {
44
45         public Definition() {
46             super(Rfc6020Mapping.TYPE);
47         }
48
49         @Override
50         public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value)
51                 throws SourceException {
52             return value;
53         }
54
55         @Override
56         public TypeStatement createDeclared(
57                 final StmtContext<String, TypeStatement, ?> ctx) {
58             return new TypeStatementImpl(ctx);
59         }
60
61         @Override
62         public EffectiveStatement<String, TypeStatement> createEffective(
63                 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx) {
64
65             // :FIXME improve the test of isExtended - e.g. unknown statements,
66             // etc..
67             Collection<StatementContextBase<?, ?, ?>> declaredSubstatements = ctx
68                     .declaredSubstatements();
69             boolean isExtended = !declaredSubstatements.isEmpty();
70             if (isExtended) {
71                 return new ExtendedTypeEffectiveStatementImpl(ctx, true);
72             }
73
74             switch (ctx.getStatementArgument()) {
75             case TypeUtils.INT8:
76                 return new Int8EffectiveStatementImpl(ctx);
77             case TypeUtils.INT16:
78                 return new Int16EffectiveStatementImpl(ctx);
79             case TypeUtils.INT32:
80                 return new Int32EffectiveStatementImpl(ctx);
81             case TypeUtils.INT64:
82                 return new Int64EffectiveStatementImpl(ctx);
83             case TypeUtils.UINT8:
84                 return new UInt8EffectiveStatementImpl(ctx);
85             case TypeUtils.UINT16:
86                 return new UInt16EffectiveStatementImpl(ctx);
87             case TypeUtils.UINT32:
88                 return new UInt32EffectiveStatementImpl(ctx);
89             case TypeUtils.UINT64:
90                 return new UInt64EffectiveStatementImpl(ctx);
91             case TypeUtils.STRING:
92                 return new StringEffectiveStatementImpl(ctx);
93             case TypeUtils.BOOLEAN:
94                 return new BooleanEffectiveStatementImpl(ctx);
95             case TypeUtils.EMPTY:
96                 return new EmptyEffectiveStatementImpl(ctx);
97             case TypeUtils.BINARY:
98                 return new BinaryEffectiveStatementImpl(ctx);
99             default:
100                 // :FIXME try to resolve original typedef context here and
101                 // return buildEffective of original typedef context
102                 return new ExtendedTypeEffectiveStatementImpl(ctx, false);
103             }
104         }
105     }
106
107     @Nonnull
108     @Override
109     public String getName() {
110         return argument();
111     }
112 }