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.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.YangData;
51 import org.opendaylight.yangtools.yang.binding.YangFeature;
52 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
53 import org.opendaylight.yangtools.yang.common.QName;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
55 import org.opendaylight.yangtools.yang.common.YangDataName;
56 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
58 public final class BindingTypes {
60 public static final ConcreteType BASE_IDENTITY = typeForClass(BaseIdentity.class);
61 public static final ConcreteType DATA_CONTAINER = typeForClass(DataContainer.class);
62 public static final ConcreteType DATA_OBJECT = typeForClass(DataObject.class);
63 public static final ConcreteType DATA_ROOT = typeForClass(DataRoot.class);
64 @Deprecated(since = "10.0.0", forRemoval = true)
65 public static final ConcreteType NOTIFICATION_LISTENER = typeForClass(NotificationListener.class);
66 public static final ConcreteType QNAME = typeForClass(QName.class);
67 public static final ConcreteType RPC_INPUT = typeForClass(RpcInput.class);
68 public static final ConcreteType RPC_OUTPUT = typeForClass(RpcOutput.class);
69 @Deprecated(since = "11.0.0", forRemoval = true)
70 public static final ConcreteType RPC_SERVICE = typeForClass(RpcService.class);
71 public static final ConcreteType SCALAR_TYPE_OBJECT = typeForClass(ScalarTypeObject.class);
72 public static final ConcreteType BITS_TYPE_OBJECT = typeForClass(BitsTypeObject.class);
73 public static final ConcreteType UNION_TYPE_OBJECT = typeForClass(UnionTypeObject.class);
74 public static final ConcreteType INSTANCE_IDENTIFIER = typeForClass(InstanceIdentifier.class);
75 public static final ConcreteType KEYED_INSTANCE_IDENTIFIER = typeForClass(KeyedInstanceIdentifier.class);
76 public static final ConcreteType YANG_DATA_NAME = typeForClass(YangDataName.class);
78 // This is an annotation, we are current just referencing the type
79 public static final JavaTypeName ROUTING_CONTEXT = JavaTypeName.create(RoutingContext.class);
82 static final ConcreteType AUGMENTABLE = typeForClass(Augmentable.class);
84 static final ConcreteType AUGMENTATION = typeForClass(Augmentation.class);
86 static final ConcreteType IDENTIFIABLE = typeForClass(Identifiable.class);
88 static final ConcreteType IDENTIFIER = typeForClass(Identifier.class);
90 private static final ConcreteType ACTION = typeForClass(Action.class);
91 private static final ConcreteType CHILD_OF = typeForClass(ChildOf.class);
92 private static final ConcreteType CHOICE_IN = typeForClass(ChoiceIn.class);
93 private static final ConcreteType INSTANCE_NOTIFICATION = typeForClass(InstanceNotification.class);
94 private static final ConcreteType KEYED_LIST_ACTION = typeForClass(KeyedListAction.class);
95 private static final ConcreteType KEYED_LIST_NOTIFICATION = typeForClass(KeyedListNotification.class);
96 private static final ConcreteType NOTIFICATION = typeForClass(Notification.class);
97 private static final ConcreteType OPAQUE_OBJECT = typeForClass(OpaqueObject.class);
98 private static final ConcreteType RPC = typeForClass(Rpc.class);
99 private static final ConcreteType RPC_RESULT = typeForClass(RpcResult.class);
100 private static final ConcreteType YANG_FEATURE = typeForClass(YangFeature.class);
101 private static final ConcreteType YANG_DATA = typeForClass(YangData.class);
103 private BindingTypes() {
108 * Type specializing {@link Action} for a particular type.
110 * @param parent Type of parent defining the action
111 * @param input Type input type
112 * @param output Type output type
113 * @return A parameterized type corresponding to {@code Action<Parent, Input, Output>}
114 * @throws NullPointerException if any argument is {@code null}
116 public static ParameterizedType action(final Type parent, final Type input, final Type output) {
117 return parameterizedTypeFor(ACTION, instanceIdentifier(parent), input, output);
121 * Type specializing {@link KeyedListAction} for a particular type.
123 * @param parent Type of parent defining the action
124 * @param keyType Type of parent's key
125 * @param input Type input type
126 * @param output Type output type
127 * @return A parameterized type corresponding to {@code KeyedListAction<ParentKey, Parent, Input, Output>}
128 * @throws NullPointerException if any argument is {@code null}
130 public static ParameterizedType keyedListAction(final Type parent, final Type keyType, final Type input,
132 return parameterizedTypeFor(KEYED_LIST_ACTION, keyType, parent, input, output);
136 * Type specializing {@link Notification} for a particular type.
138 * @param concreteType The concrete type of this notification
139 * @return A parameterized type corresponding to {@code Notification<ConcreteType>}
140 * @throws NullPointerException if any argument is {@code null}
142 public static ParameterizedType notification(final Type concreteType) {
143 return parameterizedTypeFor(NOTIFICATION, concreteType);
147 * Type specializing {@link InstanceNotification} for a particular type.
149 * @param concreteType The concrete type of this notification
150 * @param parent Type of parent defining the notification
151 * @return A parameterized type corresponding to {@code InstanceNotification<ConcreteType, Parent>}
152 * @throws NullPointerException if {@code parent} is {@code null}
154 public static ParameterizedType instanceNotification(final Type concreteType, final Type parent) {
155 return parameterizedTypeFor(INSTANCE_NOTIFICATION, concreteType, parent);
159 * Type specializing {@link InstanceNotification} for a particular type.
161 * @param concreteType The concrete type of this notification
162 * @param parent Type of parent defining the notification
163 * @param keyType Type of parent's key
164 * @return A parameterized type corresponding to {@code KeyedInstanceNotification<ConcreteType, ParentKey, Parent>}
165 * @throws NullPointerException if any argument is {@code null}
167 public static ParameterizedType keyedListNotification(final Type concreteType, final Type parent,
168 final Type keyType) {
169 return parameterizedTypeFor(KEYED_LIST_NOTIFICATION, concreteType, parent, keyType);
173 * Specialize {@link Augmentable} for a particular type.
175 * @param type Type for which to specialize
176 * @return A parameterized type corresponding to {@code Augmentable<Type>}
177 * @throws NullPointerException if {@code type} is {@code null}
179 public static @NonNull ParameterizedType augmentable(final Type type) {
180 return parameterizedTypeFor(AUGMENTABLE, type);
184 * Specialize {@link Augmentation} for a particular type.
186 * @param type Type for which to specialize
187 * @return A parameterized type corresponding to {@code Augmentation<Type>}
188 * @throws NullPointerException if {@code type} is {@code null}
190 public static @NonNull ParameterizedType augmentation(final Type type) {
191 return parameterizedTypeFor(AUGMENTATION, type);
195 * Specialize {@link ChildOf} for a particular type.
197 * @param type Type for which to specialize
198 * @return A parameterized type corresponding to {@code ChildOf<Type>}
199 * @throws NullPointerException if {@code type} is {@code null}
201 public static ParameterizedType childOf(final Type type) {
202 return parameterizedTypeFor(CHILD_OF, type);
206 * Type specializing {@link ChoiceIn} for a particular type.
208 * @param type Type for which to specialize
209 * @return A parameterized type corresponding to {@code ChoiceIn<Type>}
210 * @throws NullPointerException if {@code type} is {@code null}
212 public static ParameterizedType choiceIn(final Type type) {
213 return parameterizedTypeFor(CHOICE_IN, type);
217 * Type specializing {@link Identifier} for a particular type.
219 * @param type Type for which to specialize
220 * @return A parameterized type corresponding to {@code Identifier<Type>}
221 * @throws NullPointerException if {@code type} is {@code null}
223 public static ParameterizedType identifier(final Type type) {
224 return parameterizedTypeFor(IDENTIFIER, type);
228 * Type specializing {@link Identifiable} for a particular type.
230 * @param type Type for which to specialize
231 * @return A parameterized type corresponding to {@code Identifiable<Type>}
232 * @throws NullPointerException if {@code type} is {@code null}
234 public static ParameterizedType identifiable(final Type type) {
235 return parameterizedTypeFor(IDENTIFIABLE, type);
239 * Type specializing {@link InstanceIdentifier} for a particular type.
241 * @param type Type for which to specialize
242 * @return A parameterized type corresponding to {@code InstanceIdentifier<Type>}
243 * @throws NullPointerException if {@code type} is {@code null}
245 public static ParameterizedType instanceIdentifier(final Type type) {
246 return parameterizedTypeFor(INSTANCE_IDENTIFIER, type);
250 * Type specializing {@link KeyedInstanceIdentifier} for a particular type.
252 * @param type Type for which to specialize
253 * @param keyType Type of key
254 * @return A parameterized type corresponding to {@code KeyedInstanceIdentifier<Type, KeyType>}
255 * @throws NullPointerException if any argument is is {@code null}
257 public static ParameterizedType keyedInstanceIdentifier(final Type type, final Type keyType) {
258 return parameterizedTypeFor(KEYED_INSTANCE_IDENTIFIER, type, keyType);
262 * Type specializing {@link OpaqueObject} for a particular type.
264 * @param type Type for which to specialize
265 * @return A parameterized type corresponding to {@code OpaqueObject<Type>}
266 * @throws NullPointerException if {@code type} is {@code null}
268 public static ParameterizedType opaqueObject(final Type type) {
269 return parameterizedTypeFor(OPAQUE_OBJECT, type);
273 * Type specializing {@link Rpc} for a particular type.
275 * @param input Type input type
276 * @param output Type output type
277 * @return A parameterized type corresponding to {@code Rpc<Input, Output>}
278 * @throws NullPointerException if any argument is {@code null}
280 public static @NonNull ParameterizedType rpc(final Type input, final Type output) {
281 return parameterizedTypeFor(RPC, input, output);
285 * Type specializing {@link RpcResult} for a particular type.
287 * @param type Type for which to specialize
288 * @return A parameterized type corresponding to {@code RpcResult<Type>}
289 * @throws NullPointerException if {@code type} is {@code null}
291 public static ParameterizedType rpcResult(final Type type) {
292 return parameterizedTypeFor(RPC_RESULT, type);
296 * Type specializing {@link ScalarTypeObject} for a particular type.
298 * @param type Type for which to specialize
299 * @return A parameterized type corresponding to {@code ScalarTypeObject<Type>}
300 * @throws NullPointerException if {@code type} is {@code null}
302 public static ParameterizedType scalarTypeObject(final Type type) {
303 return parameterizedTypeFor(SCALAR_TYPE_OBJECT, type);
307 * Type specializing {@link YangData} for a particular type.
309 * @param concreteType The concrete type of this notification
310 * @return A parameterized type corresponding to {@code YangData<Type>}
311 * @throws NullPointerException if any argument is is {@code null}
313 public static ParameterizedType yangData(final Type concreteType) {
314 return parameterizedTypeFor(YANG_DATA, concreteType);
318 * Type specializing {@link YangFeature} for a particular type.
320 * @param concreteType The concrete type of this feature
321 * @param parent Type of parent defining the feature
322 * @return A parameterized type corresponding to {@code YangFeature<Type, DataRootType>}
323 * @throws NullPointerException if any argument is is {@code null}
325 public static ParameterizedType yangFeature(final Type concreteType, final Type parent) {
326 return parameterizedTypeFor(YANG_FEATURE, concreteType, parent);
330 * Check if specified type is generated for a {@code type bits}.
332 * @param type Type to examine
333 * @return {@code true} if the type is generated for a {@code type bits}
335 public static boolean isBitsType(final Type type) {
336 return type instanceof GeneratedTransferObject gto && isBitsType(gto);
340 * Check if specified type is generated for a {@code type bits}.
342 * @param gto Type to examine
343 * @return {@code true} if the type is generated for a {@code type bits}
345 public static boolean isBitsType(final GeneratedTransferObject gto) {
346 return gto.isTypedef() && gto.getBaseType() instanceof BitsTypeDefinition;
350 * Check if specified type is generated for an identity.
352 * @param type Type to examine
353 * @return {@code true} if the type is generated for an identity
355 public static boolean isIdentityType(final Type type) {
356 if (type instanceof GeneratedType generated) {
357 for (var constant : generated.getConstantDefinitions()) {
358 if (VALUE_STATIC_FIELD_NAME.equals(constant.getName())
359 && BaseIdentity.class.equals(constant.getValue())) {
368 * Return the {@link Augmentable} type a parameterized {@link Augmentation} type references.
370 * @param type Parameterized type
371 * @return Augmentable target, or null if {@code type} does not match the result of {@link #augmentation(Type)}
372 * @throws NullPointerException if {@code type} is {@code null}
375 public static @Nullable Type extractAugmentable(final ParameterizedType type) {
376 if (AUGMENTATION.equals(type.getRawType())) {
377 final var args = type.getActualTypeArguments();
378 if (args.length == 1) {
379 final var arg = args[0];
389 * Return the {@link Identifiable} type a parameterized {@link Identifier} type references.
391 * @param type Parameterized type
392 * @return Identifiable target, or null if {@code type} does not match the result of {@link #identifier(Type)}
393 * @throws NullPointerException if {@code type} is {@code null}
396 public static @Nullable Type extractIdentifiable(final ParameterizedType type) {
397 if (IDENTIFIER.equals(type.getRawType())) {
398 final var args = type.getActualTypeArguments();
399 if (args.length == 1) {
400 final var arg = args[0];
410 public static @Nullable Type extractYangFeatureDataRoot(final GeneratedTransferObject gto) {
411 if (!gto.isAbstract() && gto.getSuperType() == null) {
412 final var impls = gto.getImplements();
413 if (impls.size() == 1 && impls.get(0) instanceof ParameterizedType param
414 && YANG_FEATURE.equals(param.getRawType())) {
415 final var args = param.getActualTypeArguments();
416 if (args.length == 2) {