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.yangtools.yang.binding.contract.Naming.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.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.InstanceNotification;
36 import org.opendaylight.yangtools.yang.binding.Key;
37 import org.opendaylight.yangtools.yang.binding.KeyAware;
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.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.ScalarTypeObject;
47 import org.opendaylight.yangtools.yang.binding.UnionTypeObject;
48 import org.opendaylight.yangtools.yang.binding.YangData;
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.common.YangDataName;
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 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 SCALAR_TYPE_OBJECT = typeForClass(ScalarTypeObject.class);
66 public static final ConcreteType BITS_TYPE_OBJECT = typeForClass(BitsTypeObject.class);
67 public static final ConcreteType UNION_TYPE_OBJECT = typeForClass(UnionTypeObject.class);
68 public static final ConcreteType INSTANCE_IDENTIFIER = typeForClass(InstanceIdentifier.class);
69 public static final ConcreteType KEYED_INSTANCE_IDENTIFIER = typeForClass(KeyedInstanceIdentifier.class);
70 public static final ConcreteType YANG_DATA_NAME = typeForClass(YangDataName.class);
72 // This is an annotation, we are current just referencing the type
73 public static final JavaTypeName ROUTING_CONTEXT = JavaTypeName.create(RoutingContext.class);
76 static final ConcreteType AUGMENTABLE = typeForClass(Augmentable.class);
78 static final ConcreteType AUGMENTATION = typeForClass(Augmentation.class);
80 static final ConcreteType KEY_AWARE = typeForClass(KeyAware.class);
82 static final ConcreteType KEY = typeForClass(Key.class);
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 private static final ConcreteType YANG_DATA = typeForClass(YangData.class);
97 private BindingTypes() {
102 * Type specializing {@link Action} for a particular type.
104 * @param parent Type of parent defining the action
105 * @param input Type input type
106 * @param output Type output type
107 * @return A parameterized type corresponding to {@code Action<Parent, Input, Output>}
108 * @throws NullPointerException if any argument is {@code null}
110 public static ParameterizedType action(final Type parent, final Type input, final Type output) {
111 return parameterizedTypeFor(ACTION, instanceIdentifier(parent), input, output);
115 * Type specializing {@link KeyedListAction} for a particular type.
117 * @param parent Type of parent defining the action
118 * @param keyType Type of parent's key
119 * @param input Type input type
120 * @param output Type output type
121 * @return A parameterized type corresponding to {@code KeyedListAction<ParentKey, Parent, Input, Output>}
122 * @throws NullPointerException if any argument is {@code null}
124 public static ParameterizedType keyedListAction(final Type parent, final Type keyType, final Type input,
126 return parameterizedTypeFor(KEYED_LIST_ACTION, keyType, parent, input, output);
130 * Type specializing {@link Notification} for a particular type.
132 * @param concreteType The concrete type of this notification
133 * @return A parameterized type corresponding to {@code Notification<ConcreteType>}
134 * @throws NullPointerException if any argument is {@code null}
136 public static ParameterizedType notification(final Type concreteType) {
137 return parameterizedTypeFor(NOTIFICATION, concreteType);
141 * Type specializing {@link InstanceNotification} for a particular type.
143 * @param concreteType The concrete type of this notification
144 * @param parent Type of parent defining the notification
145 * @return A parameterized type corresponding to {@code InstanceNotification<ConcreteType, Parent>}
146 * @throws NullPointerException if {@code parent} is {@code null}
148 public static ParameterizedType instanceNotification(final Type concreteType, final Type parent) {
149 return parameterizedTypeFor(INSTANCE_NOTIFICATION, concreteType, parent);
153 * Type specializing {@link InstanceNotification} for a particular type.
155 * @param concreteType The concrete type of this notification
156 * @param parent Type of parent defining the notification
157 * @param keyType Type of parent's key
158 * @return A parameterized type corresponding to {@code KeyedInstanceNotification<ConcreteType, ParentKey, Parent>}
159 * @throws NullPointerException if any argument is {@code null}
161 public static ParameterizedType keyedListNotification(final Type concreteType, final Type parent,
162 final Type keyType) {
163 return parameterizedTypeFor(KEYED_LIST_NOTIFICATION, concreteType, parent, keyType);
167 * Specialize {@link Augmentable} for a particular type.
169 * @param type Type for which to specialize
170 * @return A parameterized type corresponding to {@code Augmentable<Type>}
171 * @throws NullPointerException if {@code type} is {@code null}
173 public static @NonNull ParameterizedType augmentable(final Type type) {
174 return parameterizedTypeFor(AUGMENTABLE, type);
178 * Specialize {@link Augmentation} for a particular type.
180 * @param type Type for which to specialize
181 * @return A parameterized type corresponding to {@code Augmentation<Type>}
182 * @throws NullPointerException if {@code type} is {@code null}
184 public static @NonNull ParameterizedType augmentation(final Type type) {
185 return parameterizedTypeFor(AUGMENTATION, type);
189 * Specialize {@link ChildOf} for a particular type.
191 * @param type Type for which to specialize
192 * @return A parameterized type corresponding to {@code ChildOf<Type>}
193 * @throws NullPointerException if {@code type} is {@code null}
195 public static ParameterizedType childOf(final Type type) {
196 return parameterizedTypeFor(CHILD_OF, type);
200 * Type specializing {@link ChoiceIn} for a particular type.
202 * @param type Type for which to specialize
203 * @return A parameterized type corresponding to {@code ChoiceIn<Type>}
204 * @throws NullPointerException if {@code type} is {@code null}
206 public static ParameterizedType choiceIn(final Type type) {
207 return parameterizedTypeFor(CHOICE_IN, type);
211 * Type specializing {@link Key} for a particular type.
213 * @param type Type for which to specialize
214 * @return A parameterized type corresponding to {@code Key<Type>}
215 * @throws NullPointerException if {@code type} is {@code null}
217 public static ParameterizedType key(final Type type) {
218 return parameterizedTypeFor(KEY, type);
222 * Type specializing {@link KeyAware} for a particular type.
224 * @param type Type for which to specialize
225 * @return A parameterized type corresponding to {@code KeyAware<Type>}
226 * @throws NullPointerException if {@code type} is {@code null}
228 public static ParameterizedType keyAware(final Type type) {
229 return parameterizedTypeFor(KEY_AWARE, type);
233 * Type specializing {@link InstanceIdentifier} for a particular type.
235 * @param type Type for which to specialize
236 * @return A parameterized type corresponding to {@code InstanceIdentifier<Type>}
237 * @throws NullPointerException if {@code type} is {@code null}
239 public static ParameterizedType instanceIdentifier(final Type type) {
240 return parameterizedTypeFor(INSTANCE_IDENTIFIER, type);
244 * Type specializing {@link KeyedInstanceIdentifier} for a particular type.
246 * @param type Type for which to specialize
247 * @param keyType Type of key
248 * @return A parameterized type corresponding to {@code KeyedInstanceIdentifier<Type, KeyType>}
249 * @throws NullPointerException if any argument is is {@code null}
251 public static ParameterizedType keyedInstanceIdentifier(final Type type, final Type keyType) {
252 return parameterizedTypeFor(KEYED_INSTANCE_IDENTIFIER, type, keyType);
256 * Type specializing {@link OpaqueObject} for a particular type.
258 * @param type Type for which to specialize
259 * @return A parameterized type corresponding to {@code OpaqueObject<Type>}
260 * @throws NullPointerException if {@code type} is {@code null}
262 public static ParameterizedType opaqueObject(final Type type) {
263 return parameterizedTypeFor(OPAQUE_OBJECT, type);
267 * Type specializing {@link Rpc} for a particular type.
269 * @param input Type input type
270 * @param output Type output type
271 * @return A parameterized type corresponding to {@code Rpc<Input, Output>}
272 * @throws NullPointerException if any argument is {@code null}
274 public static @NonNull ParameterizedType rpc(final Type input, final Type output) {
275 return parameterizedTypeFor(RPC, input, output);
279 * Type specializing {@link RpcResult} for a particular type.
281 * @param type Type for which to specialize
282 * @return A parameterized type corresponding to {@code RpcResult<Type>}
283 * @throws NullPointerException if {@code type} is {@code null}
285 public static ParameterizedType rpcResult(final Type type) {
286 return parameterizedTypeFor(RPC_RESULT, type);
290 * Type specializing {@link ScalarTypeObject} for a particular type.
292 * @param type Type for which to specialize
293 * @return A parameterized type corresponding to {@code ScalarTypeObject<Type>}
294 * @throws NullPointerException if {@code type} is {@code null}
296 public static ParameterizedType scalarTypeObject(final Type type) {
297 return parameterizedTypeFor(SCALAR_TYPE_OBJECT, type);
301 * Type specializing {@link YangData} for a particular type.
303 * @param concreteType The concrete type of this notification
304 * @return A parameterized type corresponding to {@code YangData<Type>}
305 * @throws NullPointerException if any argument is is {@code null}
307 public static ParameterizedType yangData(final Type concreteType) {
308 return parameterizedTypeFor(YANG_DATA, concreteType);
312 * Type specializing {@link YangFeature} for a particular type.
314 * @param concreteType The concrete type of this feature
315 * @param parent Type of parent defining the feature
316 * @return A parameterized type corresponding to {@code YangFeature<Type, DataRootType>}
317 * @throws NullPointerException if any argument is is {@code null}
319 public static ParameterizedType yangFeature(final Type concreteType, final Type parent) {
320 return parameterizedTypeFor(YANG_FEATURE, concreteType, parent);
324 * Check if specified type is generated for a {@code type bits}.
326 * @param type Type to examine
327 * @return {@code true} if the type is generated for a {@code type bits}
329 public static boolean isBitsType(final Type type) {
330 return type instanceof GeneratedTransferObject gto && isBitsType(gto);
334 * Check if specified type is generated for a {@code type bits}.
336 * @param gto Type to examine
337 * @return {@code true} if the type is generated for a {@code type bits}
339 public static boolean isBitsType(final GeneratedTransferObject gto) {
340 return gto.isTypedef() && gto.getBaseType() instanceof BitsTypeDefinition;
344 * Check if specified type is generated for an identity.
346 * @param type Type to examine
347 * @return {@code true} if the type is generated for an identity
349 public static boolean isIdentityType(final Type type) {
350 if (type instanceof GeneratedType generated) {
351 for (var constant : generated.getConstantDefinitions()) {
352 if (VALUE_STATIC_FIELD_NAME.equals(constant.getName())
353 && BaseIdentity.class.equals(constant.getValue())) {
362 * Return the {@link Augmentable} type a parameterized {@link Augmentation} type references.
364 * @param type Parameterized type
365 * @return Augmentable target, or null if {@code type} does not match the result of {@link #augmentation(Type)}
366 * @throws NullPointerException if {@code type} is {@code null}
369 public static @Nullable Type extractAugmentable(final ParameterizedType type) {
370 if (AUGMENTATION.equals(type.getRawType())) {
371 final var args = type.getActualTypeArguments();
372 if (args.length == 1) {
373 final var arg = args[0];
383 * Return the {@link KeyAware} type a parameterized {@link Key} type references.
385 * @param type Parameterized type
386 * @return Identifiable target, or null if {@code type} does not match the result of {@link #key(Type)}
387 * @throws NullPointerException if {@code type} is {@code null}
390 public static @Nullable Type extractKeyType(final ParameterizedType type) {
391 if (KEY.equals(type.getRawType())) {
392 final var args = type.getActualTypeArguments();
393 if (args.length == 1) {
394 final var arg = args[0];
404 public static @Nullable Type extractYangFeatureDataRoot(final GeneratedTransferObject gto) {
405 if (!gto.isAbstract() && gto.getSuperType() == null) {
406 final var impls = gto.getImplements();
407 if (impls.size() == 1 && impls.get(0) instanceof ParameterizedType param
408 && YANG_FEATURE.equals(param.getRawType())) {
409 final var args = param.getActualTypeArguments();
410 if (args.length == 2) {