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()}.
44 * Here are the possible scenarios:
53 * The leaf type's schema path does not match the schema path of the leaf. We do NOT want to strip it, as
54 * we need to generate an inner class to hold the restrictions.
64 * The leaf type's schema path will match the schema path of the leaf. We do NOT want to strip it, as we need
65 * to generate an inner class to hold the restrictions.
73 * The leaf type's schema path will match the schema path of the leaf. We DO want to strip it, as we will deal
74 * with the default value ourselves.
81 * The leaf type's schema path will not match the schema path of the leaf. We do NOT want to strip it.
83 * The situation is different for types which do not have a default instantiation in YANG: leafref, enumeration,
84 * identityref, decimal64, bits and union. If these types are defined within this leaf's statement, a base type
85 * will be instantiated. If the leaf defines a default statement, this base type will be visible via getBaseType().
94 * 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
95 * needs to be generated.
105 * The leaf type's schema path will match the schema path of the leaf, and we DO want to strip it.
107 * @param leaf Leaf for which we are acquiring the type
108 * @return Potentially base type of the leaf type.
110 @Nonnull public static TypeDefinition<?> compatLeafType(@Nonnull final LeafSchemaNode leaf) {
111 final TypeDefinition<?> leafType = leaf.getType();
112 Preconditions.checkNotNull(leafType);
114 if (!leaf.getPath().equals(leafType.getPath())) {
115 // Old parser semantics, or no new default/units defined for this leaf
119 // We are dealing with a type generated for the leaf itself
120 final TypeDefinition<?> baseType = leafType.getBaseType();
121 Preconditions.checkArgument(baseType != null, "Leaf %s has type for leaf, but no base type", leaf);
123 if (leaf.getPath().equals(baseType.getPath().getParent())) {
124 // Internal instantiation of a base YANG type (decimal64 and similar)
128 // At this point we have dealt with the easy cases. Now we need to perform per-type checking if there are no
129 // new constraints introduced by this type. If there were not, we will return the base type.
130 if (leafType instanceof BinaryTypeDefinition) {
131 return baseTypeIfNotConstrained((BinaryTypeDefinition) leafType);
132 } else if (leafType instanceof DecimalTypeDefinition) {
133 return baseTypeIfNotConstrained((DecimalTypeDefinition) leafType);
134 } else if (leafType instanceof InstanceIdentifierTypeDefinition) {
135 return baseTypeIfNotConstrained((InstanceIdentifierTypeDefinition) leafType);
136 } else if (leafType instanceof IntegerTypeDefinition) {
137 return baseTypeIfNotConstrained((IntegerTypeDefinition) leafType);
138 } else if (leafType instanceof StringTypeDefinition) {
139 return baseTypeIfNotConstrained((StringTypeDefinition) leafType);
140 } else if (leafType instanceof UnsignedIntegerTypeDefinition) {
141 return baseTypeIfNotConstrained((UnsignedIntegerTypeDefinition) leafType);
143 // Other types cannot be constrained, return the base type
148 private static TypeDefinition<?> baseTypeIfNotConstrained(final BinaryTypeDefinition type) {
149 final BinaryTypeDefinition base = type.getBaseType();
150 return baseTypeIfNotConstrained(type, type.getLengthConstraints(), base, base.getLengthConstraints());
153 private static TypeDefinition<?> baseTypeIfNotConstrained(final DecimalTypeDefinition type) {
154 final DecimalTypeDefinition base = type.getBaseType();
155 return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
158 private static TypeDefinition<?> baseTypeIfNotConstrained(final InstanceIdentifierTypeDefinition type) {
159 final InstanceIdentifierTypeDefinition base = type.getBaseType();
160 return type.requireInstance() == base.requireInstance() ? base : type;
163 private static TypeDefinition<?> baseTypeIfNotConstrained(final IntegerTypeDefinition type) {
164 final IntegerTypeDefinition base = type.getBaseType();
165 return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
168 private static TypeDefinition<?> baseTypeIfNotConstrained(final StringTypeDefinition type) {
169 final StringTypeDefinition base = type.getBaseType();
170 final List<PatternConstraint> patterns = type.getPatternConstraints();
171 final List<LengthConstraint> lengths = type.getLengthConstraints();
173 if ((patterns.isEmpty() || patterns.equals(base.getPatternConstraints())) &&
174 (lengths.isEmpty() || lengths.equals(base.getLengthConstraints()))) {
181 private static TypeDefinition<?> baseTypeIfNotConstrained(final UnsignedIntegerTypeDefinition type) {
182 final UnsignedIntegerTypeDefinition base = type.getBaseType();
183 return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
186 private static TypeDefinition<?> baseTypeIfNotConstrained(final TypeDefinition<?> type,
187 final List<?> typeConstraints, final TypeDefinition<?> base, final List<?> baseConstraints) {
188 if (typeConstraints.isEmpty() || typeConstraints.equals(baseConstraints)) {