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.BitsTypeObject;
29 import org.opendaylight.yangtools.yang.binding.ChildOf;
30 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
31 import org.opendaylight.yangtools.yang.binding.DataContainer;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.DataRoot;
34 import org.opendaylight.yangtools.yang.binding.Identifiable;
35 import org.opendaylight.yangtools.yang.binding.Identifier;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.InstanceNotification;
38 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.KeyedListAction;
40 import org.opendaylight.yangtools.yang.binding.KeyedListNotification;
41 import org.opendaylight.yangtools.yang.binding.Notification;
42 import org.opendaylight.yangtools.yang.binding.NotificationListener;
43 import org.opendaylight.yangtools.yang.binding.OpaqueObject;
44 import org.opendaylight.yangtools.yang.binding.Rpc;
45 import org.opendaylight.yangtools.yang.binding.RpcInput;
46 import org.opendaylight.yangtools.yang.binding.RpcOutput;
47 import org.opendaylight.yangtools.yang.binding.RpcService;
48 import org.opendaylight.yangtools.yang.binding.ScalarTypeObject;
49 import org.opendaylight.yangtools.yang.binding.UnionTypeObject;
50 import org.opendaylight.yangtools.yang.binding.YangFeature;
51 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
52 import org.opendaylight.yangtools.yang.common.QName;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
54 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
56 public final class BindingTypes {
58 public static final ConcreteType BASE_IDENTITY = typeForClass(BaseIdentity.class);
59 public static final ConcreteType DATA_CONTAINER = typeForClass(DataContainer.class);
60 public static final ConcreteType DATA_OBJECT = typeForClass(DataObject.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 @Deprecated(since = "11.0.0", forRemoval = true)
68 public static final ConcreteType RPC_SERVICE = typeForClass(RpcService.class);
69 public static final ConcreteType SCALAR_TYPE_OBJECT = typeForClass(ScalarTypeObject.class);
70 public static final ConcreteType BITS_TYPE_OBJECT = typeForClass(BitsTypeObject.class);
71 public static final ConcreteType UNION_TYPE_OBJECT = typeForClass(UnionTypeObject.class);
72 public static final ConcreteType INSTANCE_IDENTIFIER = typeForClass(InstanceIdentifier.class);
73 public static final ConcreteType KEYED_INSTANCE_IDENTIFIER = typeForClass(KeyedInstanceIdentifier.class);
75 // This is an annotation, we are current just referencing the type
76 public static final JavaTypeName ROUTING_CONTEXT = JavaTypeName.create(RoutingContext.class);
79 static final ConcreteType AUGMENTABLE = typeForClass(Augmentable.class);
81 static final ConcreteType AUGMENTATION = typeForClass(Augmentation.class);
83 static final ConcreteType IDENTIFIABLE = typeForClass(Identifiable.class);
85 static final ConcreteType IDENTIFIER = typeForClass(Identifier.class);
87 private static final ConcreteType ACTION = typeForClass(Action.class);
88 private static final ConcreteType CHILD_OF = typeForClass(ChildOf.class);
89 private static final ConcreteType CHOICE_IN = typeForClass(ChoiceIn.class);
90 private static final ConcreteType INSTANCE_NOTIFICATION = typeForClass(InstanceNotification.class);
91 private static final ConcreteType KEYED_LIST_ACTION = typeForClass(KeyedListAction.class);
92 private static final ConcreteType KEYED_LIST_NOTIFICATION = typeForClass(KeyedListNotification.class);
93 private static final ConcreteType NOTIFICATION = typeForClass(Notification.class);
94 private static final ConcreteType OPAQUE_OBJECT = typeForClass(OpaqueObject.class);
95 private static final ConcreteType RPC = typeForClass(Rpc.class);
96 private static final ConcreteType RPC_RESULT = typeForClass(RpcResult.class);
97 private static final ConcreteType YANG_FEATURE = typeForClass(YangFeature.class);
99 private BindingTypes() {
104 * Type specializing {@link Action} for a particular type.
106 * @param parent Type of parent defining the action
107 * @param input Type input type
108 * @param output Type output type
109 * @return A parameterized type corresponding to {@code Action<Parent, Input, Output>}
110 * @throws NullPointerException if any argument is is null
112 public static ParameterizedType action(final Type parent, final Type input, final Type output) {
113 return parameterizedTypeFor(ACTION, instanceIdentifier(parent), input, output);
117 * Type specializing {@link KeyedListAction} for a particular type.
119 * @param parent Type of parent defining the action
120 * @param keyType Type of parent's key
121 * @param input Type input type
122 * @param output Type output type
123 * @return A parameterized type corresponding to {@code KeyedListAction<ParentKey, Parent, Input, Output>}
124 * @throws NullPointerException if any argument is is null
126 public static ParameterizedType keyedListAction(final Type parent, final Type keyType, final Type input,
128 return parameterizedTypeFor(KEYED_LIST_ACTION, keyType, parent, input, output);
132 * Type specializing {@link Notification} for a particular type.
134 * @param concreteType The concrete type of this notification
135 * @return A parameterized type corresponding to {@code Notification<ConcreteType>}
136 * @throws NullPointerException if any argument is is null
138 public static ParameterizedType notification(final Type concreteType) {
139 return parameterizedTypeFor(NOTIFICATION, concreteType);
143 * Type specializing {@link InstanceNotification} for a particular type.
145 * @param concreteType The concrete type of this notification
146 * @param parent Type of parent defining the notification
147 * @return A parameterized type corresponding to {@code InstanceNotification<ConcreteType, Parent>}
148 * @throws NullPointerException if {@code parent} is is null
150 public static ParameterizedType instanceNotification(final Type concreteType, final Type parent) {
151 return parameterizedTypeFor(INSTANCE_NOTIFICATION, concreteType, parent);
155 * Type specializing {@link InstanceNotification} for a particular type.
157 * @param concreteType The concrete type of this notification
158 * @param parent Type of parent defining the notification
159 * @param keyType Type of parent's key
160 * @return A parameterized type corresponding to {@code KeyedInstanceNotification<ConcreteType, ParentKey, Parent>}
161 * @throws NullPointerException if any argument is is null
163 public static ParameterizedType keyedListNotification(final Type concreteType, final Type parent,
164 final Type keyType) {
165 return parameterizedTypeFor(KEYED_LIST_NOTIFICATION, concreteType, parent, keyType);
169 * Specialize {@link Augmentable} for a particular type.
171 * @param type Type for which to specialize
172 * @return A parameterized type corresponding to {@code Augmentable<Type>}
173 * @throws NullPointerException if {@code type} is null
175 public static @NonNull ParameterizedType augmentable(final Type type) {
176 return parameterizedTypeFor(AUGMENTABLE, type);
180 * Specialize {@link Augmentation} for a particular type.
182 * @param type Type for which to specialize
183 * @return A parameterized type corresponding to {@code Augmentation<Type>}
184 * @throws NullPointerException if {@code type} is null
186 public static @NonNull ParameterizedType augmentation(final Type type) {
187 return parameterizedTypeFor(AUGMENTATION, type);
191 * Specialize {@link ChildOf} for a particular type.
193 * @param type Type for which to specialize
194 * @return A parameterized type corresponding to {@code ChildOf<Type>}
195 * @throws NullPointerException if {@code type} is null
197 public static ParameterizedType childOf(final Type type) {
198 return parameterizedTypeFor(CHILD_OF, type);
202 * Type specializing {@link ChoiceIn} for a particular type.
204 * @param type Type for which to specialize
205 * @return A parameterized type corresponding to {@code ChoiceIn<Type>}
206 * @throws NullPointerException if {@code type} is null
208 public static ParameterizedType choiceIn(final Type type) {
209 return parameterizedTypeFor(CHOICE_IN, type);
213 * Type specializing {@link Identifier} for a particular type.
215 * @param type Type for which to specialize
216 * @return A parameterized type corresponding to {@code Identifier<Type>}
217 * @throws NullPointerException if {@code type} is null
219 public static ParameterizedType identifier(final Type type) {
220 return parameterizedTypeFor(IDENTIFIER, type);
224 * Type specializing {@link Identifiable} for a particular type.
226 * @param type Type for which to specialize
227 * @return A parameterized type corresponding to {@code Identifiable<Type>}
228 * @throws NullPointerException if {@code type} is null
230 public static ParameterizedType identifiable(final Type type) {
231 return parameterizedTypeFor(IDENTIFIABLE, type);
235 * Type specializing {@link InstanceIdentifier} for a particular type.
237 * @param type Type for which to specialize
238 * @return A parameterized type corresponding to {@code InstanceIdentifier<Type>}
239 * @throws NullPointerException if {@code type} is null
241 public static ParameterizedType instanceIdentifier(final Type type) {
242 return parameterizedTypeFor(INSTANCE_IDENTIFIER, type);
246 * Type specializing {@link KeyedInstanceIdentifier} for a particular type.
248 * @param type Type for which to specialize
249 * @param keyType Type of key
250 * @return A parameterized type corresponding to {@code KeyedInstanceIdentifier<Type, KeyType>}
251 * @throws NullPointerException if any argument is is null
253 public static ParameterizedType keyedInstanceIdentifier(final Type type, final Type keyType) {
254 return parameterizedTypeFor(KEYED_INSTANCE_IDENTIFIER, type, keyType);
258 * Type specializing {@link OpaqueObject} for a particular type.
260 * @param type Type for which to specialize
261 * @return A parameterized type corresponding to {@code OpaqueObject<Type>}
262 * @throws NullPointerException if {@code type} is null
264 public static ParameterizedType opaqueObject(final Type type) {
265 return parameterizedTypeFor(OPAQUE_OBJECT, type);
269 * Type specializing {@link Rpc} for a particular type.
271 * @param input Type input type
272 * @param output Type output type
273 * @return A parameterized type corresponding to {@code Rpc<Input, Output>}
274 * @throws NullPointerException if any argument is {@code null}
276 public static @NonNull ParameterizedType rpc(final Type input, final Type output) {
277 return parameterizedTypeFor(RPC, input, output);
281 * Type specializing {@link RpcResult} for a particular type.
283 * @param type Type for which to specialize
284 * @return A parameterized type corresponding to {@code RpcResult<Type>}
285 * @throws NullPointerException if {@code type} is null
287 public static ParameterizedType rpcResult(final Type type) {
288 return parameterizedTypeFor(RPC_RESULT, type);
292 * Type specializing {@link ScalarTypeObject} for a particular type.
294 * @param type Type for which to specialize
295 * @return A parameterized type corresponding to {@code ScalarTypeObject<Type>}
296 * @throws NullPointerException if {@code type} is null
298 public static ParameterizedType scalarTypeObject(final Type type) {
299 return parameterizedTypeFor(SCALAR_TYPE_OBJECT, type);
303 * Type specializing {@link YangFeature} for a particular type.
305 * @param concreteType The concrete type of this notification
306 * @param parent Type of parent defining the notification
307 * @return A parameterized type corresponding to {@code YangFeature<Type, DataRootType>}
308 * @throws NullPointerException if any argument is is null
310 public static ParameterizedType yangFeature(final Type concreteType, final Type parent) {
311 return parameterizedTypeFor(YANG_FEATURE, concreteType, parent);
315 * Check if specified type is generated for a {@code type bits}.
317 * @param type Type to examine
318 * @return {@code true} if the type is generated for a {@code type bits}
320 public static boolean isBitsType(final Type type) {
321 return type instanceof GeneratedTransferObject gto && isBitsType(gto);
325 * Check if specified type is generated for a {@code type bits}.
327 * @param gto Type to examine
328 * @return {@code true} if the type is generated for a {@code type bits}
330 public static boolean isBitsType(final GeneratedTransferObject gto) {
331 return gto.isTypedef() && gto.getBaseType() instanceof BitsTypeDefinition;
335 * Check if specified type is generated for an identity.
337 * @param type Type to examine
338 * @return {@code true} if the type is generated for an identity
340 public static boolean isIdentityType(final Type type) {
341 if (type instanceof GeneratedType generated) {
342 for (var constant : generated.getConstantDefinitions()) {
343 if (VALUE_STATIC_FIELD_NAME.equals(constant.getName())
344 && BaseIdentity.class.equals(constant.getValue())) {
353 * Return the {@link Augmentable} type a parameterized {@link Augmentation} type references.
355 * @param type Parameterized type
356 * @return Augmentable target, or null if {@code type} does not match the result of {@link #augmentation(Type)}
357 * @throws NullPointerException if {@code type} is null
360 public static @Nullable Type extractAugmentable(final ParameterizedType type) {
361 if (AUGMENTATION.equals(type.getRawType())) {
362 final var args = type.getActualTypeArguments();
363 if (args.length == 1) {
364 final var arg = args[0];
374 * Return the {@link Identifiable} type a parameterized {@link Identifier} type references.
376 * @param type Parameterized type
377 * @return Identifiable target, or null if {@code type} does not match the result of {@link #identifier(Type)}
378 * @throws NullPointerException if {@code type} is null
381 public static @Nullable Type extractIdentifiable(final ParameterizedType type) {
382 if (IDENTIFIER.equals(type.getRawType())) {
383 final var args = type.getActualTypeArguments();
384 if (args.length == 1) {
385 final var arg = args[0];
395 public static @Nullable Type extractYangFeatureDataRoot(final GeneratedTransferObject gto) {
396 if (!gto.isAbstract() && gto.getSuperType() == null) {
397 final var impls = gto.getImplements();
398 if (impls.size() == 1 && impls.get(0) instanceof ParameterizedType param
399 && YANG_FEATURE.equals(param.getRawType())) {
400 final var args = param.getActualTypeArguments();
401 if (args.length == 2) {