2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.ImmutableList;
12 import java.util.Collections;
13 import java.util.List;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16 import org.opendaylight.yangtools.yang.model.api.Status;
17 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.TypedefStatement;
22 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
23 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
24 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
25 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
26 import org.opendaylight.yangtools.yang.model.util.ExtendedType.Builder;
27 import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.TypeDefinitionEffectiveBuilder;
34 public class TypeDefEffectiveStatementImpl extends EffectiveStatementBase<QName, TypedefStatement> implements
35 TypeDefinition<TypeDefinition<?>>, TypeDefinitionEffectiveBuilder {
37 private final QName qName;
38 private final SchemaPath path;
40 private final TypeDefinition<?> baseType;
42 private String defaultValue;
45 private String description;
46 private String reference;
48 private Status status;
50 private final List<RangeConstraint> ranges;
51 private final List<LengthConstraint> lengths;
52 private final List<PatternConstraint> patterns;
53 private final Integer fractionDigits;
55 private ExtendedType extendedType = null;
57 public TypeDefEffectiveStatementImpl(StmtContext<QName, TypedefStatement, ?> ctx) {
60 qName = ctx.getStatementArgument();
61 path = Utils.getSchemaPath(ctx);
63 ExtendedTypeEffectiveStatementImpl type = null;
65 for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
66 if (effectiveStatement instanceof ExtendedTypeEffectiveStatementImpl) {
67 type = ((ExtendedTypeEffectiveStatementImpl) effectiveStatement);
69 if (effectiveStatement instanceof DefaultEffectiveStatementImpl) {
70 defaultValue = ((DefaultEffectiveStatementImpl) effectiveStatement).argument();
72 if (effectiveStatement instanceof UnitsEffectiveStatementImpl) {
73 units = ((UnitsEffectiveStatementImpl) effectiveStatement).argument();
75 if (effectiveStatement instanceof DescriptionEffectiveStatementImpl) {
76 description = ((DescriptionEffectiveStatementImpl) effectiveStatement).argument();
78 if (effectiveStatement instanceof ReferenceEffectiveStatementImpl) {
79 reference = ((ReferenceEffectiveStatementImpl) effectiveStatement).argument();
81 if (effectiveStatement instanceof StatusEffectiveStatementImpl) {
82 status = ((StatusEffectiveStatementImpl) effectiveStatement).argument();
88 ranges = ImmutableList.copyOf(type.getRangeConstraints());
89 lengths = ImmutableList.copyOf(type.getLengthConstraints());
90 patterns = ImmutableList.copyOf(type.getPatternConstraints());
91 fractionDigits = type.getFractionDigits();
94 ranges = Collections.emptyList();
95 lengths = Collections.emptyList();
96 patterns = Collections.emptyList();
97 fractionDigits = null;
100 baseType = parseBaseTypeFromCtx(ctx);
103 private TypeDefinition<?> parseBaseTypeFromCtx(final StmtContext<QName, TypedefStatement, ?> ctx) {
105 TypeDefinition<?> baseType;
107 QName baseTypeQName = Utils.qNameFromArgument(ctx,
108 StmtContextUtils.firstAttributeOf(ctx.declaredSubstatements(), TypeStatement.class));
110 if (TypeUtils.isYangBaseTypeString(baseTypeQName.getLocalName())) {
111 baseType = TypeUtils.getYangBaseTypeFromString(baseTypeQName.getLocalName());
113 StmtContext<?, TypedefStatement, EffectiveStatement<QName, TypedefStatement>> baseTypeCtx = ctx
114 .getParentContext().getFromNamespace(TypeNamespace.class, baseTypeQName);
115 baseType = (TypeDefEffectiveStatementImpl) baseTypeCtx.buildEffective();
118 if (baseType == null) {
119 baseType = firstSubstatementOfType(TypeDefinition.class);
126 public TypeDefinition<?> getBaseType() {
131 public String getUnits() {
136 public Object getDefaultValue() {
141 public QName getQName() {
146 public SchemaPath getPath() {
151 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
152 return Collections.emptyList();
156 public String getDescription() {
161 public String getReference() {
166 public Status getStatus() {
170 public List<RangeConstraint> getRangeConstraints() {
174 public List<LengthConstraint> getLengthConstraints() {
178 public List<PatternConstraint> getPatternConstraints() {
182 public Integer getFractionDigits() {
183 return fractionDigits;
187 public ExtendedType buildType() {
189 if (extendedType != null) {
193 Builder extendedTypeBuilder;
194 if (baseType instanceof TypeDefinitionEffectiveBuilder) {
195 TypeDefinitionEffectiveBuilder typeDefBaseType = (TypeDefinitionEffectiveBuilder) baseType;
196 extendedTypeBuilder = ExtendedType.builder(qName, typeDefBaseType.buildType(),
197 Optional.fromNullable(description), Optional.fromNullable(reference), path);
199 extendedTypeBuilder = ExtendedType.builder(qName, baseType, Optional.fromNullable(description),
200 Optional.fromNullable(reference), path);
203 extendedTypeBuilder.defaultValue(defaultValue);
204 extendedTypeBuilder.units(units);
206 extendedTypeBuilder.fractionDigits(fractionDigits);
207 extendedTypeBuilder.ranges(ranges);
208 extendedTypeBuilder.lengths(lengths);
209 extendedTypeBuilder.patterns(patterns);
211 extendedType = extendedTypeBuilder.build();