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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import java.util.List;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
27 import org.opendaylight.yangtools.yang.model.api.type.LengthRestrictedTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
29 import org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
37 * Compatibility utilities for dealing with differences between the old parser's ExtendedType-driven type
38 * representation versus the representation this package models.
40 * @deprecated This class is provided strictly for compatibility only. No new users should be introduced, as this class
41 * is scheduled for removal when its two OpenDaylight users, Java Binding v1 and YANG JMX Bindings are
45 public final class CompatUtils {
46 private CompatUtils() {
47 throw new UnsupportedOperationException();
51 * This package's type hierarchy model generates a type which encapsulates the default value and units for leaves.
52 * Java Binding specification is implemented in a way, where it needs to revert this process if the internal
53 * declaration has not restricted the type further -- which is not something available via
54 * {@link TypeDefinition#getBaseType()}.
57 * Here are the possible scenarios:
66 * The leaf type's schema path does not match the schema path of the leaf. We do NOT want to strip it, as
67 * we need to generate an inner class to hold the restrictions.
77 * The leaf type's schema path will match the schema path of the leaf. We do NOT want to strip it, as we need
78 * to generate an inner class to hold the restrictions.
86 * The leaf type's schema path will match the schema path of the leaf. We DO want to strip it, as we will deal
87 * with the default value ourselves.
94 * The leaf type's schema path will not match the schema path of the leaf. We do NOT want to strip it.
97 * The situation is different for types which do not have a default instantiation in YANG: leafref, enumeration,
98 * identityref, decimal64, bits and union. If these types are defined within this leaf's statement, a base type
99 * will be instantiated. If the leaf defines a default statement, this base type will be visible via getBaseType().
108 * 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
109 * needs to be generated.
119 * The leaf type's schema path will match the schema path of the leaf, and we DO want to strip it.
121 * @param leaf Leaf for which we are acquiring the type
122 * @return Potentially base type of the leaf type.
124 public static @NonNull TypeDefinition<?> compatType(final @NonNull TypedDataSchemaNode leaf) {
125 final TypeDefinition<?> leafType = requireNonNull(leaf.getType());
127 if (!leaf.getPath().equals(leafType.getPath())) {
128 // Old parser semantics, or no new default/units defined for this leaf
132 // We are dealing with a type generated for the leaf itself
133 final TypeDefinition<?> baseType = leafType.getBaseType();
134 checkArgument(baseType != null, "Leaf %s has type for leaf, but no base type", leaf);
136 if (leaf.getPath().equals(baseType.getPath().getParent())) {
137 // Internal instantiation of a base YANG type (decimal64 and similar)
141 // At this point we have dealt with the easy cases. Now we need to perform per-type checking if there are no
142 // new constraints introduced by this type. If there were not, we will return the base type.
143 if (leafType instanceof BinaryTypeDefinition) {
144 return baseTypeIfNotConstrained((BinaryTypeDefinition) leafType);
145 } else if (leafType instanceof DecimalTypeDefinition) {
146 return baseTypeIfNotConstrained((DecimalTypeDefinition) leafType);
147 } else if (leafType instanceof InstanceIdentifierTypeDefinition) {
148 return baseTypeIfNotConstrained((InstanceIdentifierTypeDefinition) leafType);
149 } else if (leafType instanceof Int8TypeDefinition) {
150 return baseTypeIfNotConstrained((Int8TypeDefinition) leafType);
151 } else if (leafType instanceof Int16TypeDefinition) {
152 return baseTypeIfNotConstrained((Int16TypeDefinition) leafType);
153 } else if (leafType instanceof Int32TypeDefinition) {
154 return baseTypeIfNotConstrained((Int32TypeDefinition) leafType);
155 } else if (leafType instanceof Int64TypeDefinition) {
156 return baseTypeIfNotConstrained((Int64TypeDefinition) leafType);
157 } else if (leafType instanceof StringTypeDefinition) {
158 return baseTypeIfNotConstrained((StringTypeDefinition) leafType);
159 } else if (leafType instanceof Uint8TypeDefinition) {
160 return baseTypeIfNotConstrained((Uint8TypeDefinition) leafType);
161 } else if (leafType instanceof Uint16TypeDefinition) {
162 return baseTypeIfNotConstrained((Uint16TypeDefinition) leafType);
163 } else if (leafType instanceof Uint32TypeDefinition) {
164 return baseTypeIfNotConstrained((Uint32TypeDefinition) leafType);
165 } else if (leafType instanceof Uint64TypeDefinition) {
166 return baseTypeIfNotConstrained((Uint64TypeDefinition) leafType);
168 // Other types cannot be constrained, return the base type
173 // FIXME: 3.0.0: remove this method
174 public static @NonNull TypeDefinition<?> compatLeafType(final @NonNull LeafSchemaNode leaf) {
175 return compatType(leaf);
178 private static BinaryTypeDefinition baseTypeIfNotConstrained(final @NonNull BinaryTypeDefinition type) {
179 return baseTypeIfNotConstrained(type, type.getBaseType());
182 private static TypeDefinition<?> baseTypeIfNotConstrained(final @NonNull DecimalTypeDefinition type) {
183 return baseTypeIfNotConstrained(type, type.getBaseType());
186 private static TypeDefinition<?> baseTypeIfNotConstrained(final @NonNull InstanceIdentifierTypeDefinition type) {
187 final InstanceIdentifierTypeDefinition base = type.getBaseType();
188 return type.requireInstance() == base.requireInstance() ? base : type;
191 private static TypeDefinition<?> baseTypeIfNotConstrained(final @NonNull StringTypeDefinition type) {
192 final StringTypeDefinition base = type.getBaseType();
193 final List<PatternConstraint> patterns = type.getPatternConstraints();
194 final Optional<LengthConstraint> optLengths = type.getLengthConstraint();
196 if ((patterns.isEmpty() || patterns.equals(base.getPatternConstraints()))
197 && (!optLengths.isPresent() || optLengths.equals(base.getLengthConstraint()))) {
204 private static <T extends RangeRestrictedTypeDefinition<T, ?>> T baseTypeIfNotConstrained(final @NonNull T type) {
205 return baseTypeIfNotConstrained(type, type.getBaseType());
208 private static <T extends RangeRestrictedTypeDefinition<T, ?>> T baseTypeIfNotConstrained(final @NonNull T type,
210 final Optional<?> optConstraint = type.getRangeConstraint();
211 if (!optConstraint.isPresent()) {
214 return optConstraint.equals(base.getRangeConstraint()) ? base : type;
217 private static <T extends LengthRestrictedTypeDefinition<T>> T baseTypeIfNotConstrained(final @NonNull T type,
219 final Optional<LengthConstraint> optConstraint = type.getLengthConstraint();
220 if (!optConstraint.isPresent()) {
223 return optConstraint.equals(base.getLengthConstraint()) ? base : type;