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.model.util.type;
10 import com.google.common.base.Preconditions;
11 import java.util.List;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
20 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
21 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
25 * Compatibility utilities for dealing with differences between the old parser's ExtendedType-driven type
26 * representation versus the representation this package models.
28 * @deprecated This class is provided strictly for compatibility only. No new users should be introduced, as this class
29 * is scheduled for removal when its two OpenDaylight users, Java Binding v1 and YANG JMX Bindings are
33 public final class CompatUtils {
34 private CompatUtils() {
35 throw new UnsupportedOperationException();
39 * This package's type hierarchy model generates a type which encapsulates the default value and units for leaves.
40 * Java Binding specification is implemented in a way, where it needs to revert this process if the internal
41 * declaration has not restricted the type further -- which is not something available via
42 * {@link TypeDefinition#getBaseType()}.
45 * Here are the possible scenarios:
54 * The leaf type's schema path does not match the schema path of the leaf. We do NOT want to strip it, as
55 * we need to generate an inner class to hold the restrictions.
65 * The leaf type's schema path will match the schema path of the leaf. We do NOT want to strip it, as we need
66 * to generate an inner class to hold the restrictions.
74 * The leaf type's schema path will match the schema path of the leaf. We DO want to strip it, as we will deal
75 * with the default value ourselves.
82 * The leaf type's schema path will not match the schema path of the leaf. We do NOT want to strip it.
85 * The situation is different for types which do not have a default instantiation in YANG: leafref, enumeration,
86 * identityref, decimal64, bits and union. If these types are defined within this leaf's statement, a base type
87 * will be instantiated. If the leaf defines a default statement, this base type will be visible via getBaseType().
96 * The leaf type's schema path will not match the schema path of the leaf, and we do not want to strip it, as it
97 * needs to be generated.
107 * The leaf type's schema path will match the schema path of the leaf, and we DO want to strip it.
109 * @param leaf Leaf for which we are acquiring the type
110 * @return Potentially base type of the leaf type.
112 @Nonnull public static TypeDefinition<?> compatLeafType(@Nonnull final LeafSchemaNode leaf) {
113 final TypeDefinition<?> leafType = leaf.getType();
114 Preconditions.checkNotNull(leafType);
116 if (!leaf.getPath().equals(leafType.getPath())) {
117 // Old parser semantics, or no new default/units defined for this leaf
121 // We are dealing with a type generated for the leaf itself
122 final TypeDefinition<?> baseType = leafType.getBaseType();
123 Preconditions.checkArgument(baseType != null, "Leaf %s has type for leaf, but no base type", leaf);
125 if (leaf.getPath().equals(baseType.getPath().getParent())) {
126 // Internal instantiation of a base YANG type (decimal64 and similar)
130 // At this point we have dealt with the easy cases. Now we need to perform per-type checking if there are no
131 // new constraints introduced by this type. If there were not, we will return the base type.
132 if (leafType instanceof BinaryTypeDefinition) {
133 return baseTypeIfNotConstrained((BinaryTypeDefinition) leafType);
134 } else if (leafType instanceof DecimalTypeDefinition) {
135 return baseTypeIfNotConstrained((DecimalTypeDefinition) leafType);
136 } else if (leafType instanceof InstanceIdentifierTypeDefinition) {
137 return baseTypeIfNotConstrained((InstanceIdentifierTypeDefinition) leafType);
138 } else if (leafType instanceof IntegerTypeDefinition) {
139 return baseTypeIfNotConstrained((IntegerTypeDefinition) leafType);
140 } else if (leafType instanceof StringTypeDefinition) {
141 return baseTypeIfNotConstrained((StringTypeDefinition) leafType);
142 } else if (leafType instanceof UnsignedIntegerTypeDefinition) {
143 return baseTypeIfNotConstrained((UnsignedIntegerTypeDefinition) leafType);
145 // Other types cannot be constrained, return the base type
150 private static TypeDefinition<?> baseTypeIfNotConstrained(final BinaryTypeDefinition type) {
151 final BinaryTypeDefinition base = type.getBaseType();
152 return baseTypeIfNotConstrained(type, type.getLengthConstraints(), base, base.getLengthConstraints());
155 private static TypeDefinition<?> baseTypeIfNotConstrained(final DecimalTypeDefinition type) {
156 final DecimalTypeDefinition base = type.getBaseType();
157 return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
160 private static TypeDefinition<?> baseTypeIfNotConstrained(final InstanceIdentifierTypeDefinition type) {
161 final InstanceIdentifierTypeDefinition base = type.getBaseType();
162 return type.requireInstance() == base.requireInstance() ? base : type;
165 private static TypeDefinition<?> baseTypeIfNotConstrained(final IntegerTypeDefinition type) {
166 final IntegerTypeDefinition base = type.getBaseType();
167 return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
170 private static TypeDefinition<?> baseTypeIfNotConstrained(final StringTypeDefinition type) {
171 final StringTypeDefinition base = type.getBaseType();
172 final List<PatternConstraint> patterns = type.getPatternConstraints();
173 final List<LengthConstraint> lengths = type.getLengthConstraints();
175 if ((patterns.isEmpty() || patterns.equals(base.getPatternConstraints()))
176 && (lengths.isEmpty() || lengths.equals(base.getLengthConstraints()))) {
183 private static TypeDefinition<?> baseTypeIfNotConstrained(final UnsignedIntegerTypeDefinition type) {
184 final UnsignedIntegerTypeDefinition base = type.getBaseType();
185 return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
188 private static TypeDefinition<?> baseTypeIfNotConstrained(final TypeDefinition<?> type,
189 final List<?> typeConstraints, final TypeDefinition<?> base, final List<?> baseConstraints) {
190 if (typeConstraints.isEmpty() || typeConstraints.equals(baseConstraints)) {