Bug 6134: Introducing of DataTreeConfiguration concept
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / DerivedType.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;
9
10 import com.google.common.base.Preconditions;
11 import java.util.List;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
14 import org.opendaylight.yangtools.yang.model.api.Status;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
25 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
29
30 /**
31  *
32  * Implementations of derived type.
33  *
34  * This is set of utility classes which implements derived YANG type,
35  * preserving original implemented interface instead of {@link ExtendedType}
36  * which does not preserve final type of data.
37  *
38  * @deprecated Use {@link org.opendaylight.yangtools.yang.model.util.type.DerivedTypes} or
39  *             {@link org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes} instead
40  */
41 @Deprecated
42 public abstract class DerivedType<T extends TypeDefinition<T>> implements TypeDefinition<T> {
43
44     private final ExtendedType definition;
45     private final Class<T> publicType;
46
47     DerivedType(final Class<T> publicType, final ExtendedType delegate) {
48         this.definition = Preconditions.checkNotNull(delegate);
49         this.publicType = Preconditions.checkNotNull(publicType);
50     }
51
52     public static TypeDefinition<?> from(final TypeDefinition<?> type) {
53         if (type instanceof ExtendedType) {
54             return from((ExtendedType) type);
55         }
56         return type;
57     }
58
59     public static TypeDefinition<?> from(final ExtendedType type) {
60         TypeDefinition<? extends TypeDefinition<?>> baseType = type;
61         while (baseType.getBaseType() != null) {
62             baseType = baseType.getBaseType();
63         }
64         if (baseType instanceof BinaryTypeDefinition) {
65             return new DerivedBinaryType(type);
66         }
67         if (baseType instanceof BooleanTypeDefinition) {
68             return new DerivedBooleanType(type);
69         }
70         if (baseType instanceof DecimalTypeDefinition) {
71             return new DerivedDecimalType(type);
72         }
73         if (baseType instanceof IdentityrefTypeDefinition) {
74             return new DerivedIdentityrefType(type);
75         }
76         if (baseType instanceof InstanceIdentifierTypeDefinition) {
77             return new DerivedInstanceIdentifierType(type);
78         }
79         if (baseType instanceof IntegerTypeDefinition) {
80             return new DerivedIntegerType(type);
81         }
82         if (baseType instanceof LeafrefTypeDefinition) {
83             return new DerivedLeafrefType(type);
84         }
85         if (baseType instanceof UnsignedIntegerTypeDefinition) {
86             return new DerivedUnsignedIntegerType(type);
87         }
88         if (baseType instanceof StringTypeDefinition) {
89             return new DerivedStringType(type);
90         }
91         if (baseType instanceof UnionTypeDefinition) {
92             return new DerivedUnionType(type);
93         }
94         if (baseType instanceof EnumTypeDefinition) {
95             return new DerivedEnumType(type);
96         }
97         if (baseType instanceof BitsTypeDefinition) {
98             return new DerivedBitsType(type);
99         }
100         throw new IllegalArgumentException("Not supported base type of " + baseType.getClass());
101     }
102
103     @Override
104     public final QName getQName() {
105         return definition.getQName();
106     }
107
108     @Override
109     public final SchemaPath getPath() {
110         return definition.getPath();
111     }
112
113     @Override
114     public final List<UnknownSchemaNode> getUnknownSchemaNodes() {
115         return definition.getUnknownSchemaNodes();
116     }
117
118     @Override
119     public final String getDescription() {
120         return definition.getDescription();
121     }
122
123     @Override
124     public final String getReference() {
125         return definition.getReference();
126     }
127
128     @Override
129     public final String getUnits() {
130         return definition.getUnits();
131     }
132
133     @Override
134     public final Object getDefaultValue() {
135         return definition.getDefaultValue();
136     }
137
138     @Override
139     public final Status getStatus() {
140         return definition.getStatus();
141     }
142
143     @Override
144     public final T getBaseType() {
145         final TypeDefinition<?> base = definition.getBaseType();
146         if (publicType.isInstance(base)) {
147             return publicType.cast(base);
148         } else if (base instanceof ExtendedType) {
149             return createDerived((ExtendedType) base);
150         }
151         throw new IllegalStateException("Unsupported base type.");
152     }
153
154     protected ExtendedType delegate() {
155         return definition;
156     }
157
158     /**
159      *
160      * Creates derived type from supplied ExtendedType, which will implement
161      * proper {@link TypeDefinition} interface.
162      *
163      * @param base Base definition, which does not implement concrete API
164      * @return wrapper which implements proper subinterface of {@link TypeDefinition}.
165      */
166     abstract T createDerived(ExtendedType base);
167
168
169 }