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;
23 import org.opendaylight.yangtools.yang.model.util.ExtendedType;
26 * Compatibility utilities for dealing with differences between the {@link ExtendedType}-driven type representation
27 * versus the representation this package models.
29 * @deprecated This class is provided strictly for compatibility only. No new users should be introduced, as this class
30 * is scheduled for removal when its two OpenDaylight users, Java Binding v1 and YANG JMX Bindings are
34 public final class CompatUtils {
35 private CompatUtils() {
36 throw new UnsupportedOperationException();
40 * This package's type hierarchy model generates a type which encapsulates the default value and units for leaves.
41 * Java Binding specification is implemented in a way, where it needs to revert this process if the internal
42 * declaration has not restricted the type further -- which is not something available via
43 * {@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.
84 * The situation is different for types which do not have a default instantiation in YANG: leafref, enumeration,
85 * identityref, decimal64, bits and union. If these types are defined within this leaf's statement, a base type
86 * will be instantiated. If the leaf defines a default statement, this base type will be visible via getBaseType().
95 * 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
96 * needs to be generated.
106 * The leaf type's schema path will match the schema path of the leaf, and we DO want to strip it.
108 * @param leaf Leaf for which we are acquiring the type
109 * @return Potentially base type of the leaf type.
111 @Nonnull public static TypeDefinition<?> compatLeafType(@Nonnull final LeafSchemaNode leaf) {
112 final TypeDefinition<?> leafType = leaf.getType();
113 Preconditions.checkNotNull(leafType);
115 if (leafType instanceof ExtendedType) {
116 // Old parser referring to a typedef
120 if (!leaf.getPath().equals(leafType.getPath())) {
121 // Old parser semantics, or no new default/units defined for this leaf
125 // We are dealing with a type generated for the leaf itself
126 final TypeDefinition<?> baseType = leafType.getBaseType();
127 Preconditions.checkArgument(baseType != null, "Leaf %s has type for leaf, but no base type", leaf);
129 if (leaf.getPath().equals(baseType.getPath().getParent())) {
130 // Internal instantiation of a base YANG type (decimal64 and similar)
134 // At this point we have dealt with the easy cases. Now we need to perform per-type checking if there are no
135 // new constraints introduced by this type. If there were not, we will return the base type.
136 if (leafType instanceof BinaryTypeDefinition) {
137 return baseTypeIfNotConstrained((BinaryTypeDefinition) leafType);
138 } else if (leafType instanceof DecimalTypeDefinition) {
139 return baseTypeIfNotConstrained((DecimalTypeDefinition) leafType);
140 } else if (leafType instanceof InstanceIdentifierTypeDefinition) {
141 return baseTypeIfNotConstrained((InstanceIdentifierTypeDefinition) leafType);
142 } else if (leafType instanceof IntegerTypeDefinition) {
143 return baseTypeIfNotConstrained((IntegerTypeDefinition) leafType);
144 } else if (leafType instanceof StringTypeDefinition) {
145 return baseTypeIfNotConstrained((StringTypeDefinition) leafType);
146 } else if (leafType instanceof UnsignedIntegerTypeDefinition) {
147 return baseTypeIfNotConstrained((UnsignedIntegerTypeDefinition) leafType);
149 // Other types cannot be constrained, return the base type
154 private static TypeDefinition<?> baseTypeIfNotConstrained(final BinaryTypeDefinition type) {
155 final BinaryTypeDefinition base = type.getBaseType();
156 return baseTypeIfNotConstrained(type, type.getLengthConstraints(), base, base.getLengthConstraints());
159 private static TypeDefinition<?> baseTypeIfNotConstrained(final DecimalTypeDefinition type) {
160 final DecimalTypeDefinition base = type.getBaseType();
161 return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
164 private static TypeDefinition<?> baseTypeIfNotConstrained(final InstanceIdentifierTypeDefinition type) {
165 final InstanceIdentifierTypeDefinition base = type.getBaseType();
166 return type.requireInstance() == base.requireInstance() ? base : type;
169 private static TypeDefinition<?> baseTypeIfNotConstrained(final IntegerTypeDefinition type) {
170 final IntegerTypeDefinition base = type.getBaseType();
171 return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
174 private static TypeDefinition<?> baseTypeIfNotConstrained(final StringTypeDefinition type) {
175 final StringTypeDefinition base = type.getBaseType();
176 final List<PatternConstraint> patterns = type.getPatternConstraints();
177 final List<LengthConstraint> lengths = type.getLengthConstraints();
179 if ((patterns.isEmpty() || patterns.equals(base.getPatternConstraints())) &&
180 (lengths.isEmpty() || lengths.equals(base.getLengthConstraints()))) {
187 private static TypeDefinition<?> baseTypeIfNotConstrained(final UnsignedIntegerTypeDefinition type) {
188 final UnsignedIntegerTypeDefinition base = type.getBaseType();
189 return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
192 private static TypeDefinition<?> baseTypeIfNotConstrained(final TypeDefinition<?> type,
193 final List<?> typeConstraints, final TypeDefinition<?> base, final List<?> baseConstraints) {
194 if (typeConstraints.isEmpty() || typeConstraints.equals(baseConstraints)) {