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.VisibleForTesting;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
19 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
20 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
21 import org.opendaylight.mdsal.binding.model.api.Type;
22 import org.opendaylight.yangtools.yang.binding.Action;
23 import org.opendaylight.yangtools.yang.binding.Augmentable;
24 import org.opendaylight.yangtools.yang.binding.Augmentation;
25 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
26 import org.opendaylight.yangtools.yang.binding.ChildOf;
27 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
28 import org.opendaylight.yangtools.yang.binding.DataContainer;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.DataRoot;
31 import org.opendaylight.yangtools.yang.binding.Identifiable;
32 import org.opendaylight.yangtools.yang.binding.Identifier;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.InstanceNotification;
35 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.KeyedListAction;
37 import org.opendaylight.yangtools.yang.binding.KeyedListNotification;
38 import org.opendaylight.yangtools.yang.binding.Notification;
39 import org.opendaylight.yangtools.yang.binding.NotificationListener;
40 import org.opendaylight.yangtools.yang.binding.OpaqueObject;
41 import org.opendaylight.yangtools.yang.binding.RpcInput;
42 import org.opendaylight.yangtools.yang.binding.RpcOutput;
43 import org.opendaylight.yangtools.yang.binding.RpcService;
44 import org.opendaylight.yangtools.yang.binding.ScalarTypeObject;
45 import org.opendaylight.yangtools.yang.binding.TypeObject;
46 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
47 import org.opendaylight.yangtools.yang.common.QName;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
51 public final class BindingTypes {
53 public static final ConcreteType BASE_IDENTITY = typeForClass(BaseIdentity.class);
54 public static final ConcreteType DATA_CONTAINER = typeForClass(DataContainer.class);
55 public static final ConcreteType DATA_OBJECT = typeForClass(DataObject.class);
56 public static final ConcreteType TYPE_OBJECT = typeForClass(TypeObject.class);
57 public static final ConcreteType DATA_ROOT = typeForClass(DataRoot.class);
58 public static final ConcreteType NOTIFICATION_LISTENER = typeForClass(NotificationListener.class);
59 public static final ConcreteType QNAME = typeForClass(QName.class);
60 public static final ConcreteType RPC_INPUT = typeForClass(RpcInput.class);
61 public static final ConcreteType RPC_OUTPUT = typeForClass(RpcOutput.class);
62 public static final ConcreteType RPC_SERVICE = typeForClass(RpcService.class);
63 public static final ConcreteType SCALAR_TYPE_OBJECT = typeForClass(ScalarTypeObject.class);
64 public static final ConcreteType INSTANCE_IDENTIFIER = typeForClass(InstanceIdentifier.class);
65 public static final ConcreteType KEYED_INSTANCE_IDENTIFIER = typeForClass(KeyedInstanceIdentifier.class);
67 // This is an annotation, we are current just referencing the type
68 public static final JavaTypeName ROUTING_CONTEXT = JavaTypeName.create(RoutingContext.class);
71 static final ConcreteType AUGMENTABLE = typeForClass(Augmentable.class);
73 static final ConcreteType AUGMENTATION = typeForClass(Augmentation.class);
75 static final ConcreteType IDENTIFIABLE = typeForClass(Identifiable.class);
77 static final ConcreteType IDENTIFIER = typeForClass(Identifier.class);
79 private static final ConcreteType ACTION = typeForClass(Action.class);
80 private static final ConcreteType CHILD_OF = typeForClass(ChildOf.class);
81 private static final ConcreteType CHOICE_IN = typeForClass(ChoiceIn.class);
82 private static final ConcreteType INSTANCE_NOTIFICATION = typeForClass(InstanceNotification.class);
83 private static final ConcreteType KEYED_LIST_ACTION = typeForClass(KeyedListAction.class);
84 private static final ConcreteType KEYED_LIST_NOTIFICATION = typeForClass(KeyedListNotification.class);
85 private static final ConcreteType NOTIFICATION = typeForClass(Notification.class);
86 private static final ConcreteType OPAQUE_OBJECT = typeForClass(OpaqueObject.class);
87 private static final ConcreteType RPC_RESULT = typeForClass(RpcResult.class);
89 private BindingTypes() {
94 * Type specializing {@link Action} for a particular type.
96 * @param parent Type of parent defining the action
97 * @param input Type input type
98 * @param output Type output type
99 * @return A parameterized type corresponding to {@code Action<Parent, Input, Output>}
100 * @throws NullPointerException if any argument is is null
102 public static ParameterizedType action(final Type parent, final Type input, final Type output) {
103 return parameterizedTypeFor(ACTION, instanceIdentifier(parent), input, output);
107 * Type specializing {@link KeyedListAction} for a particular type.
109 * @param parent Type of parent defining the action
110 * @param keyType Type of parent's key
111 * @param input Type input type
112 * @param output Type output type
113 * @return A parameterized type corresponding to {@code KeyedListAction<ParentKey, Parent, Input, Output>}
114 * @throws NullPointerException if any argument is is null
116 public static ParameterizedType keyedListAction(final Type parent, final Type keyType, final Type input,
118 return parameterizedTypeFor(KEYED_LIST_ACTION, keyType, parent, input, output);
122 * Type specializing {@link Notification} for a particular type.
124 * @param concreteType The concrete type of this notification
125 * @return A parameterized type corresponding to {@code Notification<ConcreteType>}
126 * @throws NullPointerException if any argument is is null
128 public static ParameterizedType notification(final Type concreteType) {
129 return parameterizedTypeFor(NOTIFICATION, concreteType);
133 * Type specializing {@link InstanceNotification} for a particular type.
135 * @param concreteType The concrete type of this notification
136 * @param parent Type of parent defining the notification
137 * @return A parameterized type corresponding to {@code InstanceNotification<ConcreteType, Parent>}
138 * @throws NullPointerException if {@code parent} is is null
140 public static ParameterizedType instanceNotification(final Type concreteType, final Type parent) {
141 return parameterizedTypeFor(INSTANCE_NOTIFICATION, concreteType, parent);
145 * Type specializing {@link InstanceNotification} for a particular type.
147 * @param concreteType The concrete type of this notification
148 * @param parent Type of parent defining the notification
149 * @param keyType Type of parent's key
150 * @return A parameterized type corresponding to {@code KeyedInstanceNotification<ConcreteType, ParentKey, Parent>}
151 * @throws NullPointerException if any argument is is null
153 public static ParameterizedType keyedListNotification(final Type concreteType, final Type parent,
154 final Type keyType) {
155 return parameterizedTypeFor(KEYED_LIST_NOTIFICATION, concreteType, parent, keyType);
159 * Specialize {@link Augmentable} for a particular type.
161 * @param type Type for which to specialize
162 * @return A parameterized type corresponding to {@code Augmentable<Type>}
163 * @throws NullPointerException if {@code type} is null
165 public static @NonNull ParameterizedType augmentable(final Type type) {
166 return parameterizedTypeFor(AUGMENTABLE, type);
170 * Specialize {@link Augmentation} for a particular type.
172 * @param type Type for which to specialize
173 * @return A parameterized type corresponding to {@code Augmentation<Type>}
174 * @throws NullPointerException if {@code type} is null
176 public static @NonNull ParameterizedType augmentation(final Type type) {
177 return parameterizedTypeFor(AUGMENTATION, type);
181 * Specialize {@link ChildOf} for a particular type.
183 * @param type Type for which to specialize
184 * @return A parameterized type corresponding to {@code ChildOf<Type>}
185 * @throws NullPointerException if {@code type} is null
187 public static ParameterizedType childOf(final Type type) {
188 return parameterizedTypeFor(CHILD_OF, type);
192 * Type specializing {@link ChoiceIn} for a particular type.
194 * @param type Type for which to specialize
195 * @return A parameterized type corresponding to {@code ChoiceIn<Type>}
196 * @throws NullPointerException if {@code type} is null
198 public static ParameterizedType choiceIn(final Type type) {
199 return parameterizedTypeFor(CHOICE_IN, type);
203 * Type specializing {@link Identifier} for a particular type.
205 * @param type Type for which to specialize
206 * @return A parameterized type corresponding to {@code Identifier<Type>}
207 * @throws NullPointerException if {@code type} is null
209 public static ParameterizedType identifier(final Type type) {
210 return parameterizedTypeFor(IDENTIFIER, type);
214 * Type specializing {@link Identifiable} for a particular type.
216 * @param type Type for which to specialize
217 * @return A parameterized type corresponding to {@code Identifiable<Type>}
218 * @throws NullPointerException if {@code type} is null
220 public static ParameterizedType identifiable(final Type type) {
221 return parameterizedTypeFor(IDENTIFIABLE, type);
225 * Type specializing {@link InstanceIdentifier} for a particular type.
227 * @param type Type for which to specialize
228 * @return A parameterized type corresponding to {@code InstanceIdentifier<Type>}
229 * @throws NullPointerException if {@code type} is null
231 public static ParameterizedType instanceIdentifier(final Type type) {
232 return parameterizedTypeFor(INSTANCE_IDENTIFIER, type);
236 * Type specializing {@link KeyedInstanceIdentifier} for a particular type.
238 * @param type Type for which to specialize
239 * @param keyType Type of key
240 * @return A parameterized type corresponding to {@code KeyedInstanceIdentifier<Type, KeyType>}
241 * @throws NullPointerException if any argument is is null
243 public static ParameterizedType keyedInstanceIdentifier(final Type type, final Type keyType) {
244 return parameterizedTypeFor(KEYED_INSTANCE_IDENTIFIER, type, keyType);
248 * Type specializing {@link OpaqueObject} for a particular type.
250 * @param type Type for which to specialize
251 * @return A parameterized type corresponding to {@code OpaqueObject<Type>}
252 * @throws NullPointerException if {@code type} is null
254 public static ParameterizedType opaqueObject(final Type type) {
255 return parameterizedTypeFor(OPAQUE_OBJECT, type);
259 * Type specializing {@link RpcResult} for a particular type.
261 * @param type Type for which to specialize
262 * @return A parameterized type corresponding to {@code RpcResult<Type>}
263 * @throws NullPointerException if {@code type} is null
265 public static ParameterizedType rpcResult(final Type type) {
266 return parameterizedTypeFor(RPC_RESULT, type);
270 * Type specializing {@link ScalarTypeObject} for a particular type.
272 * @param type Type for which to specialize
273 * @return A parameterized type corresponding to {@code ScalarTypeObject<Type>}
274 * @throws NullPointerException if {@code type} is null
276 public static ParameterizedType scalarTypeObject(final Type type) {
277 return parameterizedTypeFor(SCALAR_TYPE_OBJECT, type);
281 * Check if specified type is generated for a {@code type bits}.
283 * @param type Type to examine
284 * @return {@code true} if the type is generated for a {@code type bits}
286 public static boolean isBitsType(final Type type) {
287 return type instanceof GeneratedTransferObject && isBitsType((GeneratedTransferObject) type);
291 * Check if specified type is generated for a {@code type bits}.
293 * @param gto Type to examine
294 * @return {@code true} if the type is generated for a {@code type bits}
296 public static boolean isBitsType(final GeneratedTransferObject gto) {
297 return gto.isTypedef() && gto.getBaseType() instanceof BitsTypeDefinition;
301 * Check if specified type is generated for an identity.
303 * @param type Type to examine
304 * @return {@code true} if the type is generated for an identity
306 public static boolean isIdentityType(final Type type) {
307 if (type instanceof GeneratedType) {
308 for (var constant : ((GeneratedType) type).getConstantDefinitions()) {
309 if (VALUE_STATIC_FIELD_NAME.equals(constant.getName())
310 && BaseIdentity.class.equals(constant.getValue())) {