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