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