BUG-8043: introduce RangeRestrictedTypeDefinition
[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 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
24 /**
25  * Compatibility utilities for dealing with differences between the old parser's ExtendedType-driven type
26  * representation versus the representation this package models.
27  *
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
30  *             removed.
31  */
32 @Deprecated
33 public final class CompatUtils {
34     private CompatUtils() {
35         throw new UnsupportedOperationException();
36     }
37
38     /**
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()}.
43      *
44      * <p>
45      * Here are the possible scenarios:
46      *
47      * <pre>
48      * leaf foo {
49      *     type uint8 {
50      *         range 1..2;
51      *     }
52      * }
53      * </pre>
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.
56      *
57      * <pre>
58      * leaf foo {
59      *     type uint8 {
60      *         range 1..2;
61      *     }
62      *     default 1;
63      * }
64      * </pre>
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.
67      *
68      * <pre>
69      * leaf foo {
70      *     type uint8;
71      *     default 1;
72      * }
73      * </pre>
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.
76      *
77      * <pre>
78      * leaf foo {
79      *     type uint8;
80      * }
81      * </pre>
82      * The leaf type's schema path will not match the schema path of the leaf. We do NOT want to strip it.
83      *
84      * <p>
85      * The situation is different for types which do not have a default instantiation in YANG: leafref, enumeration,
86      * identityref, decimal64, bits and union. If these types are defined within this leaf's statement, a base type
87      * will be instantiated. If the leaf defines a default statement, this base type will be visible via getBaseType().
88      *
89      * <pre>
90      * leaf foo {
91      *     type decimal64 {
92      *         fraction-digits 2;
93      *     }
94      * }
95      * </pre>
96      * 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
97      * needs to be generated.
98      *
99      * <pre>
100      * leaf foo {
101      *     type decimal64 {
102      *         fraction-digits 2;
103      *     }
104      *     default 1;
105      * }
106      * </pre>
107      * The leaf type's schema path will match the schema path of the leaf, and we DO want to strip it.
108      *
109      * @param leaf Leaf for which we are acquiring the type
110      * @return Potentially base type of the leaf type.
111      */
112     @Nonnull public static TypeDefinition<?> compatLeafType(@Nonnull final LeafSchemaNode leaf) {
113         final TypeDefinition<?> leafType = leaf.getType();
114         Preconditions.checkNotNull(leafType);
115
116         if (!leaf.getPath().equals(leafType.getPath())) {
117             // Old parser semantics, or no new default/units defined for this leaf
118             return leafType;
119         }
120
121         // We are dealing with a type generated for the leaf itself
122         final TypeDefinition<?> baseType = leafType.getBaseType();
123         Preconditions.checkArgument(baseType != null, "Leaf %s has type for leaf, but no base type", leaf);
124
125         if (leaf.getPath().equals(baseType.getPath().getParent())) {
126             // Internal instantiation of a base YANG type (decimal64 and similar)
127             return baseType;
128         }
129
130         // At this point we have dealt with the easy cases. Now we need to perform per-type checking if there are no
131         // new constraints introduced by this type. If there were not, we will return the base type.
132         if (leafType instanceof BinaryTypeDefinition) {
133             return baseTypeIfNotConstrained((BinaryTypeDefinition) leafType);
134         } else if (leafType instanceof DecimalTypeDefinition) {
135             return baseTypeIfNotConstrained((DecimalTypeDefinition) leafType);
136         } else if (leafType instanceof InstanceIdentifierTypeDefinition) {
137             return baseTypeIfNotConstrained((InstanceIdentifierTypeDefinition) leafType);
138         } else if (leafType instanceof IntegerTypeDefinition) {
139             return baseTypeIfNotConstrained((IntegerTypeDefinition) leafType);
140         } else if (leafType instanceof StringTypeDefinition) {
141             return baseTypeIfNotConstrained((StringTypeDefinition) leafType);
142         } else if (leafType instanceof UnsignedIntegerTypeDefinition) {
143             return baseTypeIfNotConstrained((UnsignedIntegerTypeDefinition) leafType);
144         } else {
145             // Other types cannot be constrained, return the base type
146             return baseType;
147         }
148     }
149
150     private static TypeDefinition<?> baseTypeIfNotConstrained(final BinaryTypeDefinition type) {
151         final BinaryTypeDefinition base = type.getBaseType();
152         return baseTypeIfNotConstrained(type, type.getLengthConstraints(), base, base.getLengthConstraints());
153     }
154
155     private static TypeDefinition<?> baseTypeIfNotConstrained(final DecimalTypeDefinition type) {
156         final DecimalTypeDefinition base = type.getBaseType();
157         return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
158     }
159
160     private static TypeDefinition<?> baseTypeIfNotConstrained(final InstanceIdentifierTypeDefinition type) {
161         final InstanceIdentifierTypeDefinition base = type.getBaseType();
162         return type.requireInstance() == base.requireInstance() ? base : type;
163     }
164
165     private static TypeDefinition<?> baseTypeIfNotConstrained(final IntegerTypeDefinition type) {
166         final IntegerTypeDefinition base = type.getBaseType();
167         return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
168     }
169
170     private static TypeDefinition<?> baseTypeIfNotConstrained(final StringTypeDefinition type) {
171         final StringTypeDefinition base = type.getBaseType();
172         final List<PatternConstraint> patterns = type.getPatternConstraints();
173         final List<LengthConstraint> lengths = type.getLengthConstraints();
174
175         if ((patterns.isEmpty() || patterns.equals(base.getPatternConstraints()))
176                 && (lengths.isEmpty() || lengths.equals(base.getLengthConstraints()))) {
177             return base;
178         }
179
180         return type;
181     }
182
183     private static TypeDefinition<?> baseTypeIfNotConstrained(final UnsignedIntegerTypeDefinition type) {
184         final UnsignedIntegerTypeDefinition base = type.getBaseType();
185         return baseTypeIfNotConstrained(type, type.getRangeConstraints(), base, base.getRangeConstraints());
186     }
187
188     private static TypeDefinition<?> baseTypeIfNotConstrained(final TypeDefinition<?> type,
189             final List<?> typeConstraints, final TypeDefinition<?> base, final List<?> baseConstraints) {
190         if (typeConstraints.isEmpty() || typeConstraints.equals(baseConstraints)) {
191             return base;
192         }
193         return type;
194     }
195 }