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