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