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.Rpc;
44 import org.opendaylight.yangtools.yang.binding.RpcInput;
45 import org.opendaylight.yangtools.yang.binding.RpcOutput;
46 import org.opendaylight.yangtools.yang.binding.RpcService;
47 import org.opendaylight.yangtools.yang.binding.ScalarTypeObject;
48 import org.opendaylight.yangtools.yang.binding.TypeObject;
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.model.api.type.BitsTypeDefinition;
55 public final class BindingTypes {
57 public static final ConcreteType BASE_IDENTITY = typeForClass(BaseIdentity.class);
58 public static final ConcreteType DATA_CONTAINER = typeForClass(DataContainer.class);
59 public static final ConcreteType DATA_OBJECT = typeForClass(DataObject.class);
60 public static final ConcreteType TYPE_OBJECT = typeForClass(TypeObject.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 public static final ConcreteType RPC_SERVICE = typeForClass(RpcService.class);
68 public static final ConcreteType SCALAR_TYPE_OBJECT = typeForClass(ScalarTypeObject.class);
69 public static final ConcreteType INSTANCE_IDENTIFIER = typeForClass(InstanceIdentifier.class);
70 public static final ConcreteType KEYED_INSTANCE_IDENTIFIER = typeForClass(KeyedInstanceIdentifier.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 IDENTIFIABLE = typeForClass(Identifiable.class);
82 static final ConcreteType IDENTIFIER = typeForClass(Identifier.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);
96 private BindingTypes() {
101 * Type specializing {@link Action} for a particular type.
103 * @param parent Type of parent defining the action
104 * @param input Type input type
105 * @param output Type output type
106 * @return A parameterized type corresponding to {@code Action<Parent, Input, Output>}
107 * @throws NullPointerException if any argument is is null
109 public static ParameterizedType action(final Type parent, final Type input, final Type output) {
110 return parameterizedTypeFor(ACTION, instanceIdentifier(parent), input, output);
114 * Type specializing {@link KeyedListAction} for a particular type.
116 * @param parent Type of parent defining the action
117 * @param keyType Type of parent's key
118 * @param input Type input type
119 * @param output Type output type
120 * @return A parameterized type corresponding to {@code KeyedListAction<ParentKey, Parent, Input, Output>}
121 * @throws NullPointerException if any argument is is null
123 public static ParameterizedType keyedListAction(final Type parent, final Type keyType, final Type input,
125 return parameterizedTypeFor(KEYED_LIST_ACTION, keyType, parent, input, output);
129 * Type specializing {@link Notification} for a particular type.
131 * @param concreteType The concrete type of this notification
132 * @return A parameterized type corresponding to {@code Notification<ConcreteType>}
133 * @throws NullPointerException if any argument is is null
135 public static ParameterizedType notification(final Type concreteType) {
136 return parameterizedTypeFor(NOTIFICATION, concreteType);
140 * Type specializing {@link InstanceNotification} for a particular type.
142 * @param concreteType The concrete type of this notification
143 * @param parent Type of parent defining the notification
144 * @return A parameterized type corresponding to {@code InstanceNotification<ConcreteType, Parent>}
145 * @throws NullPointerException if {@code parent} is is null
147 public static ParameterizedType instanceNotification(final Type concreteType, final Type parent) {
148 return parameterizedTypeFor(INSTANCE_NOTIFICATION, concreteType, parent);
152 * Type specializing {@link InstanceNotification} for a particular type.
154 * @param concreteType The concrete type of this notification
155 * @param parent Type of parent defining the notification
156 * @param keyType Type of parent's key
157 * @return A parameterized type corresponding to {@code KeyedInstanceNotification<ConcreteType, ParentKey, Parent>}
158 * @throws NullPointerException if any argument is is null
160 public static ParameterizedType keyedListNotification(final Type concreteType, final Type parent,
161 final Type keyType) {
162 return parameterizedTypeFor(KEYED_LIST_NOTIFICATION, concreteType, parent, keyType);
166 * Specialize {@link Augmentable} for a particular type.
168 * @param type Type for which to specialize
169 * @return A parameterized type corresponding to {@code Augmentable<Type>}
170 * @throws NullPointerException if {@code type} is null
172 public static @NonNull ParameterizedType augmentable(final Type type) {
173 return parameterizedTypeFor(AUGMENTABLE, type);
177 * Specialize {@link Augmentation} for a particular type.
179 * @param type Type for which to specialize
180 * @return A parameterized type corresponding to {@code Augmentation<Type>}
181 * @throws NullPointerException if {@code type} is null
183 public static @NonNull ParameterizedType augmentation(final Type type) {
184 return parameterizedTypeFor(AUGMENTATION, type);
188 * Specialize {@link ChildOf} for a particular type.
190 * @param type Type for which to specialize
191 * @return A parameterized type corresponding to {@code ChildOf<Type>}
192 * @throws NullPointerException if {@code type} is null
194 public static ParameterizedType childOf(final Type type) {
195 return parameterizedTypeFor(CHILD_OF, type);
199 * Type specializing {@link ChoiceIn} for a particular type.
201 * @param type Type for which to specialize
202 * @return A parameterized type corresponding to {@code ChoiceIn<Type>}
203 * @throws NullPointerException if {@code type} is null
205 public static ParameterizedType choiceIn(final Type type) {
206 return parameterizedTypeFor(CHOICE_IN, type);
210 * Type specializing {@link Identifier} for a particular type.
212 * @param type Type for which to specialize
213 * @return A parameterized type corresponding to {@code Identifier<Type>}
214 * @throws NullPointerException if {@code type} is null
216 public static ParameterizedType identifier(final Type type) {
217 return parameterizedTypeFor(IDENTIFIER, type);
221 * Type specializing {@link Identifiable} for a particular type.
223 * @param type Type for which to specialize
224 * @return A parameterized type corresponding to {@code Identifiable<Type>}
225 * @throws NullPointerException if {@code type} is null
227 public static ParameterizedType identifiable(final Type type) {
228 return parameterizedTypeFor(IDENTIFIABLE, type);
232 * Type specializing {@link InstanceIdentifier} for a particular type.
234 * @param type Type for which to specialize
235 * @return A parameterized type corresponding to {@code InstanceIdentifier<Type>}
236 * @throws NullPointerException if {@code type} is null
238 public static ParameterizedType instanceIdentifier(final Type type) {
239 return parameterizedTypeFor(INSTANCE_IDENTIFIER, type);
243 * Type specializing {@link KeyedInstanceIdentifier} for a particular type.
245 * @param type Type for which to specialize
246 * @param keyType Type of key
247 * @return A parameterized type corresponding to {@code KeyedInstanceIdentifier<Type, KeyType>}
248 * @throws NullPointerException if any argument is is null
250 public static ParameterizedType keyedInstanceIdentifier(final Type type, final Type keyType) {
251 return parameterizedTypeFor(KEYED_INSTANCE_IDENTIFIER, type, keyType);
255 * Type specializing {@link OpaqueObject} for a particular type.
257 * @param type Type for which to specialize
258 * @return A parameterized type corresponding to {@code OpaqueObject<Type>}
259 * @throws NullPointerException if {@code type} is null
261 public static ParameterizedType opaqueObject(final Type type) {
262 return parameterizedTypeFor(OPAQUE_OBJECT, type);
266 * Type specializing {@link Rpc} for a particular type.
268 * @param input Type input type
269 * @param output Type output type
270 * @return A parameterized type corresponding to {@code Rpc<Input, Output>}
271 * @throws NullPointerException if any argument is {@code null}
273 public static @NonNull ParameterizedType rpc(final Type input, final Type output) {
274 return parameterizedTypeFor(RPC, input, output);
278 * Type specializing {@link RpcResult} for a particular type.
280 * @param type Type for which to specialize
281 * @return A parameterized type corresponding to {@code RpcResult<Type>}
282 * @throws NullPointerException if {@code type} is null
284 public static ParameterizedType rpcResult(final Type type) {
285 return parameterizedTypeFor(RPC_RESULT, type);
289 * Type specializing {@link ScalarTypeObject} for a particular type.
291 * @param type Type for which to specialize
292 * @return A parameterized type corresponding to {@code ScalarTypeObject<Type>}
293 * @throws NullPointerException if {@code type} is null
295 public static ParameterizedType scalarTypeObject(final Type type) {
296 return parameterizedTypeFor(SCALAR_TYPE_OBJECT, type);
300 * Type specializing {@link YangFeature} for a particular type.
302 * @param concreteType The concrete type of this notification
303 * @param parent Type of parent defining the notification
304 * @return A parameterized type corresponding to {@code YangFeature<Type, DataRootType>}
305 * @throws NullPointerException if any argument is is null
307 public static ParameterizedType yangFeature(final Type concreteType, final Type parent) {
308 return parameterizedTypeFor(YANG_FEATURE, concreteType, parent);
312 * Check if specified type is generated for a {@code type bits}.
314 * @param type Type to examine
315 * @return {@code true} if the type is generated for a {@code type bits}
317 public static boolean isBitsType(final Type type) {
318 return type instanceof GeneratedTransferObject gto && isBitsType(gto);
322 * Check if specified type is generated for a {@code type bits}.
324 * @param gto Type to examine
325 * @return {@code true} if the type is generated for a {@code type bits}
327 public static boolean isBitsType(final GeneratedTransferObject gto) {
328 return gto.isTypedef() && gto.getBaseType() instanceof BitsTypeDefinition;
332 * Check if specified type is generated for an identity.
334 * @param type Type to examine
335 * @return {@code true} if the type is generated for an identity
337 public static boolean isIdentityType(final Type type) {
338 if (type instanceof GeneratedType generated) {
339 for (var constant : generated.getConstantDefinitions()) {
340 if (VALUE_STATIC_FIELD_NAME.equals(constant.getName())
341 && BaseIdentity.class.equals(constant.getValue())) {
350 * Return the {@link Augmentable} type a parameterized {@link Augmentation} type references.
352 * @param type Parameterized type
353 * @return Augmentable target, or null if {@code type} does not match the result of {@link #augmentation(Type)}
354 * @throws NullPointerException if {@code type} is null
357 public static @Nullable Type extractAugmentable(final ParameterizedType type) {
358 if (AUGMENTATION.equals(type.getRawType())) {
359 final var args = type.getActualTypeArguments();
360 if (args.length == 1) {
361 final var arg = args[0];
371 * Return the {@link Identifiable} type a parameterized {@link Identifier} type references.
373 * @param type Parameterized type
374 * @return Identifiable target, or null if {@code type} does not match the result of {@link #identifier(Type)}
375 * @throws NullPointerException if {@code type} is null
378 public static @Nullable Type extractIdentifiable(final ParameterizedType type) {
379 if (IDENTIFIER.equals(type.getRawType())) {
380 final var args = type.getActualTypeArguments();
381 if (args.length == 1) {
382 final var arg = args[0];
392 public static @Nullable Type extractYangFeatureDataRoot(final GeneratedTransferObject gto) {
393 if (!gto.isAbstract() && gto.getSuperType() == null) {
394 final var impls = gto.getImplements();
395 if (impls.size() == 1 && impls.get(0) instanceof ParameterizedType param
396 && YANG_FEATURE.equals(param.getRawType())) {
397 final var args = param.getActualTypeArguments();
398 if (args.length == 2) {