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