BUG-8043: correct LengthConstraint definition
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / type / CompatUtils.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.util.type;
9
10 import com.google.common.base.Preconditions;
11 import java.util.List;
12 import java.util.Optional;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
21 import org.opendaylight.yangtools.yang.model.api.type.LengthRestrictedTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
23 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
25
26 /**
27  * Compatibility utilities for dealing with differences between the old parser's ExtendedType-driven type
28  * representation versus the representation this package models.
29  *
30  * @deprecated This class is provided strictly for compatibility only. No new users should be introduced, as this class
31  *             is scheduled for removal when its two OpenDaylight users, Java Binding v1 and YANG JMX Bindings are
32  *             removed.
33  */
34 @Deprecated
35 public final class CompatUtils {
36     private CompatUtils() {
37         throw new UnsupportedOperationException();
38     }
39
40     /**
41      * This package's type hierarchy model generates a type which encapsulates the default value and units for leaves.
42      * Java Binding specification is implemented in a way, where it needs to revert this process if the internal
43      * declaration has not restricted the type further -- which is not something available via
44      * {@link TypeDefinition#getBaseType()}.
45      *
46      * <p>
47      * Here are the possible scenarios:
48      *
49      * <pre>
50      * leaf foo {
51      *     type uint8 {
52      *         range 1..2;
53      *     }
54      * }
55      * </pre>
56      * The leaf type's schema path does not match the schema path of the leaf. We do NOT want to strip it, as
57      * we need to generate an inner class to hold the restrictions.
58      *
59      * <pre>
60      * leaf foo {
61      *     type uint8 {
62      *         range 1..2;
63      *     }
64      *     default 1;
65      * }
66      * </pre>
67      * The leaf type's schema path will match the schema path of the leaf. We do NOT want to strip it, as we need
68      * to generate an inner class to hold the restrictions.
69      *
70      * <pre>
71      * leaf foo {
72      *     type uint8;
73      *     default 1;
74      * }
75      * </pre>
76      * The leaf type's schema path will match the schema path of the leaf. We DO want to strip it, as we will deal
77      * with the default value ourselves.
78      *
79      * <pre>
80      * leaf foo {
81      *     type uint8;
82      * }
83      * </pre>
84      * The leaf type's schema path will not match the schema path of the leaf. We do NOT want to strip it.
85      *
86      * <p>
87      * The situation is different for types which do not have a default instantiation in YANG: leafref, enumeration,
88      * identityref, decimal64, bits and union. If these types are defined within this leaf's statement, a base type
89      * will be instantiated. If the leaf defines a default statement, this base type will be visible via getBaseType().
90      *
91      * <pre>
92      * leaf foo {
93      *     type decimal64 {
94      *         fraction-digits 2;
95      *     }
96      * }
97      * </pre>
98      * 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
99      * needs to be generated.
100      *
101      * <pre>
102      * leaf foo {
103      *     type decimal64 {
104      *         fraction-digits 2;
105      *     }
106      *     default 1;
107      * }
108      * </pre>
109      * The leaf type's schema path will match the schema path of the leaf, and we DO want to strip it.
110      *
111      * @param leaf Leaf for which we are acquiring the type
112      * @return Potentially base type of the leaf type.
113      */
114     @Nonnull public static TypeDefinition<?> compatLeafType(@Nonnull final LeafSchemaNode leaf) {
115         final TypeDefinition<?> leafType = leaf.getType();
116         Preconditions.checkNotNull(leafType);
117
118         if (!leaf.getPath().equals(leafType.getPath())) {
119             // Old parser semantics, or no new default/units defined for this leaf
120             return leafType;
121         }
122
123         // We are dealing with a type generated for the leaf itself
124         final TypeDefinition<?> baseType = leafType.getBaseType();
125         Preconditions.checkArgument(baseType != null, "Leaf %s has type for leaf, but no base type", leaf);
126
127         if (leaf.getPath().equals(baseType.getPath().getParent())) {
128             // Internal instantiation of a base YANG type (decimal64 and similar)
129             return baseType;
130         }
131
132         // At this point we have dealt with the easy cases. Now we need to perform per-type checking if there are no
133         // new constraints introduced by this type. If there were not, we will return the base type.
134         if (leafType instanceof BinaryTypeDefinition) {
135             return baseTypeIfNotConstrained((BinaryTypeDefinition) leafType);
136         } else if (leafType instanceof DecimalTypeDefinition) {
137             return baseTypeIfNotConstrained((DecimalTypeDefinition) leafType);
138         } else if (leafType instanceof InstanceIdentifierTypeDefinition) {
139             return baseTypeIfNotConstrained((InstanceIdentifierTypeDefinition) leafType);
140         } else if (leafType instanceof IntegerTypeDefinition) {
141             return baseTypeIfNotConstrained((IntegerTypeDefinition) leafType);
142         } else if (leafType instanceof StringTypeDefinition) {
143             return baseTypeIfNotConstrained((StringTypeDefinition) leafType);
144         } else if (leafType instanceof UnsignedIntegerTypeDefinition) {
145             return baseTypeIfNotConstrained((UnsignedIntegerTypeDefinition) leafType);
146         } else {
147             // Other types cannot be constrained, return the base type
148             return baseType;
149         }
150     }
151
152     private static BinaryTypeDefinition baseTypeIfNotConstrained(final BinaryTypeDefinition type) {
153         return baseTypeIfNotConstrained(type, type.getBaseType());
154     }
155
156     private static TypeDefinition<?> baseTypeIfNotConstrained(final DecimalTypeDefinition type) {
157         final DecimalTypeDefinition base = type.getBaseType();
158         return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
159     }
160
161     private static TypeDefinition<?> baseTypeIfNotConstrained(final InstanceIdentifierTypeDefinition type) {
162         final InstanceIdentifierTypeDefinition base = type.getBaseType();
163         return type.requireInstance() == base.requireInstance() ? base : type;
164     }
165
166     private static TypeDefinition<?> baseTypeIfNotConstrained(final IntegerTypeDefinition type) {
167         final IntegerTypeDefinition base = type.getBaseType();
168         return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
169     }
170
171     private static TypeDefinition<?> baseTypeIfNotConstrained(final StringTypeDefinition type) {
172         final StringTypeDefinition base = type.getBaseType();
173         final List<PatternConstraint> patterns = type.getPatternConstraints();
174         final Optional<LengthConstraint> optLengths = type.getLengthConstraint();
175
176         if ((patterns.isEmpty() || patterns.equals(base.getPatternConstraints()))
177                 && (!optLengths.isPresent() || optLengths.equals(base.getLengthConstraint()))) {
178             return base;
179         }
180
181         return type;
182     }
183
184     private static TypeDefinition<?> baseTypeIfNotConstrained(final UnsignedIntegerTypeDefinition type) {
185         final UnsignedIntegerTypeDefinition base = type.getBaseType();
186         return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
187     }
188
189     private static TypeDefinition<?> baseTypeIfNotConstrained(final TypeDefinition<?> type,
190             final List<?> typeConstraints, final TypeDefinition<?> base, final List<?> baseConstraints) {
191         return typeConstraints.isEmpty() || typeConstraints.equals(baseConstraints) ? base : type;
192     }
193
194     private static <T extends LengthRestrictedTypeDefinition<T>> T baseTypeIfNotConstrained(final T type,
195             final T base) {
196         final Optional<LengthConstraint> optConstraint = type.getLengthConstraint();
197         if (!optConstraint.isPresent()) {
198             return base;
199         }
200         return optConstraint.equals(base.getLengthConstraint()) ? base : type;
201     }
202 }