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.TypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
26 import org.opendaylight.yangtools.yang.model.api.type.LengthRestrictedTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
28 import org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
30 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
36 * Compatibility utilities for dealing with differences between the old parser's ExtendedType-driven type
37 * representation versus the representation this package models.
39 * @deprecated This class is provided strictly for compatibility only. No new users should be introduced, as this class
40 * is scheduled for removal when its two OpenDaylight users, Java Binding v1 and YANG JMX Bindings are
44 public final class CompatUtils {
45 private CompatUtils() {
50 * This package's type hierarchy model generates a type which encapsulates the default value and units for leaves.
51 * Java Binding specification is implemented in a way, where it needs to revert this process if the internal
52 * declaration has not restricted the type further -- which is not something available via
53 * {@link TypeDefinition#getBaseType()}.
56 * Here are the possible scenarios:
65 * The leaf type's schema path does not match the schema path of the leaf. We do NOT want to strip it, as
66 * we need to generate an inner class to hold the restrictions.
76 * The leaf type's schema path will match the schema path of the leaf. We do NOT want to strip it, as we need
77 * to generate an inner class to hold the restrictions.
85 * The leaf type's schema path will match the schema path of the leaf. We DO want to strip it, as we will deal
86 * with the default value ourselves.
93 * The leaf type's schema path will not match the schema path of the leaf. We do NOT want to strip it.
96 * The situation is different for types which do not have a default instantiation in YANG: leafref, enumeration,
97 * identityref, decimal64, bits and union. If these types are defined within this leaf's statement, a base type
98 * will be instantiated. If the leaf defines a default statement, this base type will be visible via getBaseType().
107 * 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
108 * needs to be generated.
118 * The leaf type's schema path will match the schema path of the leaf, and we DO want to strip it.
120 * @param leaf Leaf for which we are acquiring the type
121 * @return Potentially base type of the leaf type.
123 public static @NonNull TypeDefinition<?> compatType(final @NonNull TypedDataSchemaNode leaf) {
124 final TypeDefinition<?> leafType = requireNonNull(leaf.getType());
126 if (!leaf.getPath().equals(leafType.getPath())) {
127 // Old parser semantics, or no new default/units defined for this leaf
131 // We are dealing with a type generated for the leaf itself
132 final TypeDefinition<?> baseType = leafType.getBaseType();
133 checkArgument(baseType != null, "Leaf %s has type for leaf, but no base type", leaf);
135 if (leaf.getPath().equals(baseType.getPath().getParent())) {
136 // Internal instantiation of a base YANG type (decimal64 and similar)
140 // At this point we have dealt with the easy cases. Now we need to perform per-type checking if there are no
141 // new constraints introduced by this type. If there were not, we will return the base type.
142 if (leafType instanceof BinaryTypeDefinition) {
143 return baseTypeIfNotConstrained((BinaryTypeDefinition) leafType);
144 } else if (leafType instanceof DecimalTypeDefinition) {
145 return baseTypeIfNotConstrained((DecimalTypeDefinition) leafType);
146 } else if (leafType instanceof InstanceIdentifierTypeDefinition) {
147 return baseTypeIfNotConstrained((InstanceIdentifierTypeDefinition) leafType);
148 } else if (leafType instanceof Int8TypeDefinition) {
149 return baseTypeIfNotConstrained((Int8TypeDefinition) leafType);
150 } else if (leafType instanceof Int16TypeDefinition) {
151 return baseTypeIfNotConstrained((Int16TypeDefinition) leafType);
152 } else if (leafType instanceof Int32TypeDefinition) {
153 return baseTypeIfNotConstrained((Int32TypeDefinition) leafType);
154 } else if (leafType instanceof Int64TypeDefinition) {
155 return baseTypeIfNotConstrained((Int64TypeDefinition) leafType);
156 } else if (leafType instanceof StringTypeDefinition) {
157 return baseTypeIfNotConstrained((StringTypeDefinition) leafType);
158 } else if (leafType instanceof Uint8TypeDefinition) {
159 return baseTypeIfNotConstrained((Uint8TypeDefinition) leafType);
160 } else if (leafType instanceof Uint16TypeDefinition) {
161 return baseTypeIfNotConstrained((Uint16TypeDefinition) leafType);
162 } else if (leafType instanceof Uint32TypeDefinition) {
163 return baseTypeIfNotConstrained((Uint32TypeDefinition) leafType);
164 } else if (leafType instanceof Uint64TypeDefinition) {
165 return baseTypeIfNotConstrained((Uint64TypeDefinition) leafType);
167 // Other types cannot be constrained, return the base type
172 private static BinaryTypeDefinition baseTypeIfNotConstrained(final @NonNull BinaryTypeDefinition type) {
173 return baseTypeIfNotConstrained(type, type.getBaseType());
176 private static TypeDefinition<?> baseTypeIfNotConstrained(final @NonNull DecimalTypeDefinition type) {
177 return baseTypeIfNotConstrained(type, type.getBaseType());
180 private static TypeDefinition<?> baseTypeIfNotConstrained(final @NonNull InstanceIdentifierTypeDefinition type) {
181 final InstanceIdentifierTypeDefinition base = type.getBaseType();
182 return type.requireInstance() == base.requireInstance() ? base : type;
185 private static TypeDefinition<?> baseTypeIfNotConstrained(final @NonNull StringTypeDefinition type) {
186 final StringTypeDefinition base = type.getBaseType();
187 final List<PatternConstraint> patterns = type.getPatternConstraints();
188 final Optional<LengthConstraint> optLengths = type.getLengthConstraint();
190 if ((patterns.isEmpty() || patterns.equals(base.getPatternConstraints()))
191 && (optLengths.isEmpty() || optLengths.equals(base.getLengthConstraint()))) {
198 private static <T extends RangeRestrictedTypeDefinition<T, ?>> T baseTypeIfNotConstrained(final @NonNull T type) {
199 return baseTypeIfNotConstrained(type, type.getBaseType());
202 private static <T extends RangeRestrictedTypeDefinition<T, ?>> T baseTypeIfNotConstrained(final @NonNull T type,
204 final Optional<?> optConstraint = type.getRangeConstraint();
205 if (optConstraint.isEmpty()) {
208 return optConstraint.equals(base.getRangeConstraint()) ? base : type;
211 private static <T extends LengthRestrictedTypeDefinition<T>> T baseTypeIfNotConstrained(final @NonNull T type,
213 final Optional<LengthConstraint> optConstraint = type.getLengthConstraint();
214 if (optConstraint.isEmpty()) {
217 return optConstraint.equals(base.getLengthConstraint()) ? base : type;