2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.model.ri;
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;
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;
54 public final class BindingTypes {
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);
71 // This is an annotation, we are current just referencing the type
72 public static final JavaTypeName ROUTING_CONTEXT = JavaTypeName.create(RoutingContext.class);
75 static final ConcreteType AUGMENTABLE = typeForClass(Augmentable.class);
77 static final ConcreteType AUGMENTATION = typeForClass(Augmentation.class);
79 static final ConcreteType IDENTIFIABLE = typeForClass(Identifiable.class);
81 static final ConcreteType IDENTIFIER = typeForClass(Identifier.class);
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);
94 private BindingTypes() {
99 * Type specializing {@link Action} for a particular type.
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
107 public static ParameterizedType action(final Type parent, final Type input, final Type output) {
108 return parameterizedTypeFor(ACTION, instanceIdentifier(parent), input, output);
112 * Type specializing {@link KeyedListAction} for a particular type.
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
121 public static ParameterizedType keyedListAction(final Type parent, final Type keyType, final Type input,
123 return parameterizedTypeFor(KEYED_LIST_ACTION, keyType, parent, input, output);
127 * Type specializing {@link Notification} for a particular type.
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
133 public static ParameterizedType notification(final Type concreteType) {
134 return parameterizedTypeFor(NOTIFICATION, concreteType);
138 * Type specializing {@link InstanceNotification} for a particular type.
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
145 public static ParameterizedType instanceNotification(final Type concreteType, final Type parent) {
146 return parameterizedTypeFor(INSTANCE_NOTIFICATION, concreteType, parent);
150 * Type specializing {@link InstanceNotification} for a particular type.
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
158 public static ParameterizedType keyedListNotification(final Type concreteType, final Type parent,
159 final Type keyType) {
160 return parameterizedTypeFor(KEYED_LIST_NOTIFICATION, concreteType, parent, keyType);
164 * Specialize {@link Augmentable} for a particular type.
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
170 public static @NonNull ParameterizedType augmentable(final Type type) {
171 return parameterizedTypeFor(AUGMENTABLE, type);
175 * Specialize {@link Augmentation} for a particular type.
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
181 public static @NonNull ParameterizedType augmentation(final Type type) {
182 return parameterizedTypeFor(AUGMENTATION, type);
186 * Specialize {@link ChildOf} for a particular type.
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
192 public static ParameterizedType childOf(final Type type) {
193 return parameterizedTypeFor(CHILD_OF, type);
197 * Type specializing {@link ChoiceIn} for a particular type.
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
203 public static ParameterizedType choiceIn(final Type type) {
204 return parameterizedTypeFor(CHOICE_IN, type);
208 * Type specializing {@link Identifier} for a particular type.
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
214 public static ParameterizedType identifier(final Type type) {
215 return parameterizedTypeFor(IDENTIFIER, type);
219 * Type specializing {@link Identifiable} for a particular type.
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
225 public static ParameterizedType identifiable(final Type type) {
226 return parameterizedTypeFor(IDENTIFIABLE, type);
230 * Type specializing {@link InstanceIdentifier} for a particular type.
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
236 public static ParameterizedType instanceIdentifier(final Type type) {
237 return parameterizedTypeFor(INSTANCE_IDENTIFIER, type);
241 * Type specializing {@link KeyedInstanceIdentifier} for a particular type.
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
248 public static ParameterizedType keyedInstanceIdentifier(final Type type, final Type keyType) {
249 return parameterizedTypeFor(KEYED_INSTANCE_IDENTIFIER, type, keyType);
253 * Type specializing {@link OpaqueObject} for a particular type.
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
259 public static ParameterizedType opaqueObject(final Type type) {
260 return parameterizedTypeFor(OPAQUE_OBJECT, type);
264 * Type specializing {@link RpcResult} for a particular type.
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
270 public static ParameterizedType rpcResult(final Type type) {
271 return parameterizedTypeFor(RPC_RESULT, type);
275 * Type specializing {@link ScalarTypeObject} for a particular type.
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
281 public static ParameterizedType scalarTypeObject(final Type type) {
282 return parameterizedTypeFor(SCALAR_TYPE_OBJECT, type);
286 * Type specializing {@link YangFeature} for a particular type.
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
293 public static ParameterizedType yangFeature(final Type concreteType, final Type parent) {
294 return parameterizedTypeFor(YANG_FEATURE, concreteType, parent);
298 * Check if specified type is generated for a {@code type bits}.
300 * @param type Type to examine
301 * @return {@code true} if the type is generated for a {@code type bits}
303 public static boolean isBitsType(final Type type) {
304 return type instanceof GeneratedTransferObject gto && isBitsType(gto);
308 * Check if specified type is generated for a {@code type bits}.
310 * @param gto Type to examine
311 * @return {@code true} if the type is generated for a {@code type bits}
313 public static boolean isBitsType(final GeneratedTransferObject gto) {
314 return gto.isTypedef() && gto.getBaseType() instanceof BitsTypeDefinition;
318 * Check if specified type is generated for an identity.
320 * @param type Type to examine
321 * @return {@code true} if the type is generated for an identity
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())) {
336 * Return the {@link Augmentable} type a parameterized {@link Augmentation} type references.
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
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];
357 * Return the {@link Identifiable} type a parameterized {@link Identifier} type references.
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
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];
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) {