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