Merge branch 'master' of ../controller
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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;
34
35 /**
36  * Compatibility utilities for dealing with differences between the old parser's ExtendedType-driven type
37  * representation versus the representation this package models.
38  *
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
41  *             removed.
42  */
43 @Deprecated
44 public final class CompatUtils {
45     private CompatUtils() {
46         throw new UnsupportedOperationException();
47     }
48
49     /**
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()}.
54      *
55      * <p>
56      * Here are the possible scenarios:
57      *
58      * <pre>
59      * leaf foo {
60      *     type uint8 {
61      *         range 1..2;
62      *     }
63      * }
64      * </pre>
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.
67      *
68      * <pre>
69      * leaf foo {
70      *     type uint8 {
71      *         range 1..2;
72      *     }
73      *     default 1;
74      * }
75      * </pre>
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.
78      *
79      * <pre>
80      * leaf foo {
81      *     type uint8;
82      *     default 1;
83      * }
84      * </pre>
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.
87      *
88      * <pre>
89      * leaf foo {
90      *     type uint8;
91      * }
92      * </pre>
93      * The leaf type's schema path will not match the schema path of the leaf. We do NOT want to strip it.
94      *
95      * <p>
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().
99      *
100      * <pre>
101      * leaf foo {
102      *     type decimal64 {
103      *         fraction-digits 2;
104      *     }
105      * }
106      * </pre>
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.
109      *
110      * <pre>
111      * leaf foo {
112      *     type decimal64 {
113      *         fraction-digits 2;
114      *     }
115      *     default 1;
116      * }
117      * </pre>
118      * The leaf type's schema path will match the schema path of the leaf, and we DO want to strip it.
119      *
120      * @param leaf Leaf for which we are acquiring the type
121      * @return Potentially base type of the leaf type.
122      */
123     public static @NonNull TypeDefinition<?> compatType(final @NonNull TypedDataSchemaNode leaf) {
124         final TypeDefinition<?> leafType = requireNonNull(leaf.getType());
125
126         if (!leaf.getPath().equals(leafType.getPath())) {
127             // Old parser semantics, or no new default/units defined for this leaf
128             return leafType;
129         }
130
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);
134
135         if (leaf.getPath().equals(baseType.getPath().getParent())) {
136             // Internal instantiation of a base YANG type (decimal64 and similar)
137             return baseType;
138         }
139
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);
166         } else {
167             // Other types cannot be constrained, return the base type
168             return baseType;
169         }
170     }
171
172     private static BinaryTypeDefinition baseTypeIfNotConstrained(final @NonNull BinaryTypeDefinition type) {
173         return baseTypeIfNotConstrained(type, type.getBaseType());
174     }
175
176     private static TypeDefinition<?> baseTypeIfNotConstrained(final @NonNull DecimalTypeDefinition type) {
177         return baseTypeIfNotConstrained(type, type.getBaseType());
178     }
179
180     private static TypeDefinition<?> baseTypeIfNotConstrained(final @NonNull InstanceIdentifierTypeDefinition type) {
181         final InstanceIdentifierTypeDefinition base = type.getBaseType();
182         return type.requireInstance() == base.requireInstance() ? base : type;
183     }
184
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();
189
190         if ((patterns.isEmpty() || patterns.equals(base.getPatternConstraints()))
191                 && (optLengths.isEmpty() || optLengths.equals(base.getLengthConstraint()))) {
192             return base;
193         }
194
195         return type;
196     }
197
198     private static <T extends RangeRestrictedTypeDefinition<T, ?>> T baseTypeIfNotConstrained(final @NonNull T type) {
199         return baseTypeIfNotConstrained(type, type.getBaseType());
200     }
201
202     private static <T extends RangeRestrictedTypeDefinition<T, ?>> T baseTypeIfNotConstrained(final @NonNull T type,
203             final T base) {
204         final Optional<?> optConstraint = type.getRangeConstraint();
205         if (optConstraint.isEmpty()) {
206             return base;
207         }
208         return optConstraint.equals(base.getRangeConstraint()) ? base : type;
209     }
210
211     private static <T extends LengthRestrictedTypeDefinition<T>> T baseTypeIfNotConstrained(final @NonNull T type,
212             final T base) {
213         final Optional<LengthConstraint> optConstraint = type.getLengthConstraint();
214         if (optConstraint.isEmpty()) {
215             return base;
216         }
217         return optConstraint.equals(base.getLengthConstraint()) ? base : type;
218     }
219 }