Bump versions to 14.0.0-SNAPSHOT
[mdsal.git] / binding / mdsal-binding-model-ri / src / main / java / org / opendaylight / mdsal / binding / model / ri / BindingTypes.java
1 /*
2  * Copyright (c) 2013 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.mdsal.binding.model.ri;
9
10 import static org.opendaylight.mdsal.binding.model.ri.Types.parameterizedTypeFor;
11 import static org.opendaylight.mdsal.binding.model.ri.Types.typeForClass;
12 import static org.opendaylight.yangtools.yang.binding.contract.Naming.VALUE_STATIC_FIELD_NAME;
13
14 import com.google.common.annotations.Beta;
15 import com.google.common.annotations.VisibleForTesting;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
21 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
22 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.binding.Action;
25 import org.opendaylight.yangtools.yang.binding.Augmentable;
26 import org.opendaylight.yangtools.yang.binding.Augmentation;
27 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
28 import org.opendaylight.yangtools.yang.binding.BitsTypeObject;
29 import org.opendaylight.yangtools.yang.binding.ChildOf;
30 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
31 import org.opendaylight.yangtools.yang.binding.DataContainer;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.DataRoot;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.InstanceNotification;
36 import org.opendaylight.yangtools.yang.binding.Key;
37 import org.opendaylight.yangtools.yang.binding.KeyAware;
38 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.KeyedListAction;
40 import org.opendaylight.yangtools.yang.binding.KeyedListNotification;
41 import org.opendaylight.yangtools.yang.binding.Notification;
42 import org.opendaylight.yangtools.yang.binding.OpaqueObject;
43 import org.opendaylight.yangtools.yang.binding.Rpc;
44 import org.opendaylight.yangtools.yang.binding.RpcInput;
45 import org.opendaylight.yangtools.yang.binding.RpcOutput;
46 import org.opendaylight.yangtools.yang.binding.ScalarTypeObject;
47 import org.opendaylight.yangtools.yang.binding.UnionTypeObject;
48 import org.opendaylight.yangtools.yang.binding.YangData;
49 import org.opendaylight.yangtools.yang.binding.YangFeature;
50 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
51 import org.opendaylight.yangtools.yang.common.QName;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.opendaylight.yangtools.yang.common.YangDataName;
54 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
55
56 public final class BindingTypes {
57
58     public static final ConcreteType BASE_IDENTITY = typeForClass(BaseIdentity.class);
59     public static final ConcreteType DATA_CONTAINER = typeForClass(DataContainer.class);
60     public static final ConcreteType DATA_OBJECT = typeForClass(DataObject.class);
61     public static final ConcreteType DATA_ROOT = typeForClass(DataRoot.class);
62     public static final ConcreteType QNAME = typeForClass(QName.class);
63     public static final ConcreteType RPC_INPUT = typeForClass(RpcInput.class);
64     public static final ConcreteType RPC_OUTPUT = typeForClass(RpcOutput.class);
65     public static final ConcreteType SCALAR_TYPE_OBJECT = typeForClass(ScalarTypeObject.class);
66     public static final ConcreteType BITS_TYPE_OBJECT = typeForClass(BitsTypeObject.class);
67     public static final ConcreteType UNION_TYPE_OBJECT = typeForClass(UnionTypeObject.class);
68     public static final ConcreteType INSTANCE_IDENTIFIER = typeForClass(InstanceIdentifier.class);
69     public static final ConcreteType KEYED_INSTANCE_IDENTIFIER = typeForClass(KeyedInstanceIdentifier.class);
70     public static final ConcreteType YANG_DATA_NAME = typeForClass(YangDataName.class);
71
72     // This is an annotation, we are current just referencing the type
73     public static final JavaTypeName ROUTING_CONTEXT = JavaTypeName.create(RoutingContext.class);
74
75     @VisibleForTesting
76     static final ConcreteType AUGMENTABLE = typeForClass(Augmentable.class);
77     @VisibleForTesting
78     static final ConcreteType AUGMENTATION = typeForClass(Augmentation.class);
79     @VisibleForTesting
80     static final ConcreteType KEY_AWARE = typeForClass(KeyAware.class);
81     @VisibleForTesting
82     static final ConcreteType KEY = typeForClass(Key.class);
83
84     private static final ConcreteType ACTION = typeForClass(Action.class);
85     private static final ConcreteType CHILD_OF = typeForClass(ChildOf.class);
86     private static final ConcreteType CHOICE_IN = typeForClass(ChoiceIn.class);
87     private static final ConcreteType INSTANCE_NOTIFICATION = typeForClass(InstanceNotification.class);
88     private static final ConcreteType KEYED_LIST_ACTION = typeForClass(KeyedListAction.class);
89     private static final ConcreteType KEYED_LIST_NOTIFICATION = typeForClass(KeyedListNotification.class);
90     private static final ConcreteType NOTIFICATION = typeForClass(Notification.class);
91     private static final ConcreteType OPAQUE_OBJECT = typeForClass(OpaqueObject.class);
92     private static final ConcreteType RPC = typeForClass(Rpc.class);
93     private static final ConcreteType RPC_RESULT = typeForClass(RpcResult.class);
94     private static final ConcreteType YANG_FEATURE = typeForClass(YangFeature.class);
95     private static final ConcreteType YANG_DATA = typeForClass(YangData.class);
96
97     private BindingTypes() {
98
99     }
100
101     /**
102      * Type specializing {@link Action} for a particular type.
103      *
104      * @param parent Type of parent defining the action
105      * @param input Type input type
106      * @param output Type output type
107      * @return A parameterized type corresponding to {@code Action<Parent, Input, Output>}
108      * @throws NullPointerException if any argument is {@code null}
109      */
110     public static ParameterizedType action(final Type parent, final Type input, final Type output) {
111         return parameterizedTypeFor(ACTION, instanceIdentifier(parent), input, output);
112     }
113
114     /**
115      * Type specializing {@link KeyedListAction} for a particular type.
116      *
117      * @param parent Type of parent defining the action
118      * @param keyType Type of parent's key
119      * @param input Type input type
120      * @param output Type output type
121      * @return A parameterized type corresponding to {@code KeyedListAction<ParentKey, Parent, Input, Output>}
122      * @throws NullPointerException if any argument is {@code null}
123      */
124     public static ParameterizedType keyedListAction(final Type parent, final Type keyType, final Type input,
125             final Type output) {
126         return parameterizedTypeFor(KEYED_LIST_ACTION, keyType, parent, input, output);
127     }
128
129     /**
130      * Type specializing {@link Notification} for a particular type.
131      *
132      * @param concreteType The concrete type of this notification
133      * @return A parameterized type corresponding to {@code Notification<ConcreteType>}
134      * @throws NullPointerException if any argument is {@code null}
135      */
136     public static ParameterizedType notification(final Type concreteType) {
137         return parameterizedTypeFor(NOTIFICATION, concreteType);
138     }
139
140     /**
141      * Type specializing {@link InstanceNotification} for a particular type.
142      *
143      * @param concreteType The concrete type of this notification
144      * @param parent Type of parent defining the notification
145      * @return A parameterized type corresponding to {@code InstanceNotification<ConcreteType, Parent>}
146      * @throws NullPointerException if {@code parent} is {@code null}
147      */
148     public static ParameterizedType instanceNotification(final Type concreteType, final Type parent) {
149         return parameterizedTypeFor(INSTANCE_NOTIFICATION, concreteType, parent);
150     }
151
152     /**
153      * Type specializing {@link InstanceNotification} for a particular type.
154      *
155      * @param concreteType The concrete type of this notification
156      * @param parent Type of parent defining the notification
157      * @param keyType Type of parent's key
158      * @return A parameterized type corresponding to {@code KeyedInstanceNotification<ConcreteType, ParentKey, Parent>}
159      * @throws NullPointerException if any argument is {@code null}
160      */
161     public static ParameterizedType keyedListNotification(final Type concreteType, final Type parent,
162             final Type keyType) {
163         return parameterizedTypeFor(KEYED_LIST_NOTIFICATION, concreteType, parent, keyType);
164     }
165
166     /**
167      * Specialize {@link Augmentable} for a particular type.
168      *
169      * @param type Type for which to specialize
170      * @return A parameterized type corresponding to {@code Augmentable<Type>}
171      * @throws NullPointerException if {@code type} is {@code null}
172      */
173     public static @NonNull ParameterizedType augmentable(final Type type) {
174         return parameterizedTypeFor(AUGMENTABLE, type);
175     }
176
177     /**
178      * Specialize {@link Augmentation} for a particular type.
179      *
180      * @param type Type for which to specialize
181      * @return A parameterized type corresponding to {@code Augmentation<Type>}
182      * @throws NullPointerException if {@code type} is {@code null}
183      */
184     public static @NonNull ParameterizedType augmentation(final Type type) {
185         return parameterizedTypeFor(AUGMENTATION, type);
186     }
187
188     /**
189      * Specialize {@link ChildOf} for a particular type.
190      *
191      * @param type Type for which to specialize
192      * @return A parameterized type corresponding to {@code ChildOf<Type>}
193      * @throws NullPointerException if {@code type} is {@code null}
194      */
195     public static ParameterizedType childOf(final Type type) {
196         return parameterizedTypeFor(CHILD_OF, type);
197     }
198
199     /**
200      * Type specializing {@link ChoiceIn} for a particular type.
201      *
202      * @param type Type for which to specialize
203      * @return A parameterized type corresponding to {@code ChoiceIn<Type>}
204      * @throws NullPointerException if {@code type} is {@code null}
205      */
206     public static ParameterizedType choiceIn(final Type type) {
207         return parameterizedTypeFor(CHOICE_IN, type);
208     }
209
210     /**
211      * Type specializing {@link Key} for a particular type.
212      *
213      * @param type Type for which to specialize
214      * @return A parameterized type corresponding to {@code Key<Type>}
215      * @throws NullPointerException if {@code type} is {@code null}
216      */
217     public static ParameterizedType key(final Type type) {
218         return parameterizedTypeFor(KEY, type);
219     }
220
221     /**
222      * Type specializing {@link KeyAware} for a particular type.
223      *
224      * @param type Type for which to specialize
225      * @return A parameterized type corresponding to {@code KeyAware<Type>}
226      * @throws NullPointerException if {@code type} is {@code null}
227      */
228     public static ParameterizedType keyAware(final Type type) {
229         return parameterizedTypeFor(KEY_AWARE, type);
230     }
231
232     /**
233      * Type specializing {@link InstanceIdentifier} for a particular type.
234      *
235      * @param type Type for which to specialize
236      * @return A parameterized type corresponding to {@code InstanceIdentifier<Type>}
237      * @throws NullPointerException if {@code type} is {@code null}
238      */
239     public static ParameterizedType instanceIdentifier(final Type type) {
240         return parameterizedTypeFor(INSTANCE_IDENTIFIER, type);
241     }
242
243     /**
244      * Type specializing {@link KeyedInstanceIdentifier} for a particular type.
245      *
246      * @param type Type for which to specialize
247      * @param keyType Type of key
248      * @return A parameterized type corresponding to {@code KeyedInstanceIdentifier<Type, KeyType>}
249      * @throws NullPointerException if any argument is is {@code null}
250      */
251     public static ParameterizedType keyedInstanceIdentifier(final Type type, final Type keyType) {
252         return parameterizedTypeFor(KEYED_INSTANCE_IDENTIFIER, type, keyType);
253     }
254
255     /**
256      * Type specializing {@link OpaqueObject} for a particular type.
257      *
258      * @param type Type for which to specialize
259      * @return A parameterized type corresponding to {@code OpaqueObject<Type>}
260      * @throws NullPointerException if {@code type} is {@code null}
261      */
262     public static ParameterizedType opaqueObject(final Type type) {
263         return parameterizedTypeFor(OPAQUE_OBJECT, type);
264     }
265
266     /**
267      * Type specializing {@link Rpc} for a particular type.
268      *
269      * @param input Type input type
270      * @param output Type output type
271      * @return A parameterized type corresponding to {@code Rpc<Input, Output>}
272      * @throws NullPointerException if any argument is {@code null}
273      */
274     public static @NonNull ParameterizedType rpc(final Type input, final Type output) {
275         return parameterizedTypeFor(RPC, input, output);
276     }
277
278     /**
279      * Type specializing {@link RpcResult} for a particular type.
280      *
281      * @param type Type for which to specialize
282      * @return A parameterized type corresponding to {@code RpcResult<Type>}
283      * @throws NullPointerException if {@code type} is {@code null}
284      */
285     public static ParameterizedType rpcResult(final Type type) {
286         return parameterizedTypeFor(RPC_RESULT, type);
287     }
288
289     /**
290      * Type specializing {@link ScalarTypeObject} for a particular type.
291      *
292      * @param type Type for which to specialize
293      * @return A parameterized type corresponding to {@code ScalarTypeObject<Type>}
294      * @throws NullPointerException if {@code type} is {@code null}
295      */
296     public static ParameterizedType scalarTypeObject(final Type type) {
297         return parameterizedTypeFor(SCALAR_TYPE_OBJECT, type);
298     }
299
300     /**
301      * Type specializing {@link YangData} for a particular type.
302      *
303      * @param concreteType The concrete type of this notification
304      * @return A parameterized type corresponding to {@code YangData<Type>}
305      * @throws NullPointerException if any argument is is {@code null}
306      */
307     public static ParameterizedType yangData(final Type concreteType) {
308         return parameterizedTypeFor(YANG_DATA, concreteType);
309     }
310
311     /**
312      * Type specializing {@link YangFeature} for a particular type.
313      *
314      * @param concreteType The concrete type of this feature
315      * @param parent Type of parent defining the feature
316      * @return A parameterized type corresponding to {@code YangFeature<Type, DataRootType>}
317      * @throws NullPointerException if any argument is is {@code null}
318      */
319     public static ParameterizedType yangFeature(final Type concreteType, final Type parent) {
320         return parameterizedTypeFor(YANG_FEATURE, concreteType, parent);
321     }
322
323     /**
324      * Check if specified type is generated for a {@code type bits}.
325      *
326      * @param type Type to examine
327      * @return {@code true} if the type is generated for a {@code type bits}
328      */
329     public static boolean isBitsType(final Type type) {
330         return type instanceof GeneratedTransferObject gto && isBitsType(gto);
331     }
332
333     /**
334      * Check if specified type is generated for a {@code type bits}.
335      *
336      * @param gto Type to examine
337      * @return {@code true} if the type is generated for a {@code type bits}
338      */
339     public static boolean isBitsType(final GeneratedTransferObject gto) {
340         return gto.isTypedef() && gto.getBaseType() instanceof BitsTypeDefinition;
341     }
342
343     /**
344      * Check if specified type is generated for an identity.
345      *
346      * @param type Type to examine
347      * @return {@code true} if the type is generated for an identity
348      */
349     public static boolean isIdentityType(final Type type) {
350         if (type instanceof GeneratedType generated) {
351             for (var constant : generated.getConstantDefinitions()) {
352                 if (VALUE_STATIC_FIELD_NAME.equals(constant.getName())
353                     && BaseIdentity.class.equals(constant.getValue())) {
354                     return true;
355                 }
356             }
357         }
358         return false;
359     }
360
361     /**
362      * Return the {@link Augmentable} type a parameterized {@link Augmentation} type references.
363      *
364      * @param type Parameterized type
365      * @return Augmentable target, or null if {@code type} does not match the result of {@link #augmentation(Type)}
366      * @throws NullPointerException if {@code type} is {@code null}
367      */
368     @Beta
369     public static @Nullable Type extractAugmentable(final ParameterizedType type) {
370         if (AUGMENTATION.equals(type.getRawType())) {
371             final var args = type.getActualTypeArguments();
372             if (args.length == 1) {
373                 final var arg = args[0];
374                 if (arg != null) {
375                     return arg;
376                 }
377             }
378         }
379         return null;
380     }
381
382     /**
383      * Return the {@link KeyAware} type a parameterized {@link Key} type references.
384      *
385      * @param type Parameterized type
386      * @return Identifiable target, or null if {@code type} does not match the result of {@link #key(Type)}
387      * @throws NullPointerException if {@code type} is {@code null}
388      */
389     @Beta
390     public static @Nullable Type extractKeyType(final ParameterizedType type) {
391         if (KEY.equals(type.getRawType())) {
392             final var args = type.getActualTypeArguments();
393             if (args.length == 1) {
394                 final var arg = args[0];
395                 if (arg != null) {
396                     return arg;
397                 }
398             }
399         }
400         return null;
401     }
402
403     @Beta
404     public static @Nullable Type extractYangFeatureDataRoot(final GeneratedTransferObject gto) {
405         if (!gto.isAbstract() && gto.getSuperType() == null) {
406             final var impls = gto.getImplements();
407             if (impls.size() == 1 && impls.get(0) instanceof ParameterizedType param
408                 && YANG_FEATURE.equals(param.getRawType())) {
409                 final var args = param.getActualTypeArguments();
410                 if (args.length == 2) {
411                     return args[1];
412                 }
413             }
414         }
415         return null;
416     }
417 }