Add UnionTypeObject
[yangtools.git] / binding / mdsal-binding-model-ri / src / main / java / org / opendaylight / mdsal / binding / model / ri / BindingTypes.java
index 511a96adb9635da16858d26edf2410e15e3f77d7..bf29aecee3c131410be1bf3e4dc064627bc43b51 100644 (file)
@@ -25,6 +25,7 @@ import org.opendaylight.yangtools.yang.binding.Action;
 import org.opendaylight.yangtools.yang.binding.Augmentable;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
+import org.opendaylight.yangtools.yang.binding.BitsTypeObject;
 import org.opendaylight.yangtools.yang.binding.ChildOf;
 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
@@ -40,11 +41,13 @@ import org.opendaylight.yangtools.yang.binding.KeyedListNotification;
 import org.opendaylight.yangtools.yang.binding.Notification;
 import org.opendaylight.yangtools.yang.binding.NotificationListener;
 import org.opendaylight.yangtools.yang.binding.OpaqueObject;
+import org.opendaylight.yangtools.yang.binding.Rpc;
 import org.opendaylight.yangtools.yang.binding.RpcInput;
 import org.opendaylight.yangtools.yang.binding.RpcOutput;
 import org.opendaylight.yangtools.yang.binding.RpcService;
 import org.opendaylight.yangtools.yang.binding.ScalarTypeObject;
-import org.opendaylight.yangtools.yang.binding.TypeObject;
+import org.opendaylight.yangtools.yang.binding.UnionTypeObject;
+import org.opendaylight.yangtools.yang.binding.YangFeature;
 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.RpcResult;
@@ -55,7 +58,6 @@ public final class BindingTypes {
     public static final ConcreteType BASE_IDENTITY = typeForClass(BaseIdentity.class);
     public static final ConcreteType DATA_CONTAINER = typeForClass(DataContainer.class);
     public static final ConcreteType DATA_OBJECT = typeForClass(DataObject.class);
-    public static final ConcreteType TYPE_OBJECT = typeForClass(TypeObject.class);
     public static final ConcreteType DATA_ROOT = typeForClass(DataRoot.class);
     @Deprecated(since = "10.0.0", forRemoval = true)
     public static final ConcreteType NOTIFICATION_LISTENER = typeForClass(NotificationListener.class);
@@ -64,6 +66,8 @@ public final class BindingTypes {
     public static final ConcreteType RPC_OUTPUT = typeForClass(RpcOutput.class);
     public static final ConcreteType RPC_SERVICE = typeForClass(RpcService.class);
     public static final ConcreteType SCALAR_TYPE_OBJECT = typeForClass(ScalarTypeObject.class);
+    public static final ConcreteType BITS_TYPE_OBJECT = typeForClass(BitsTypeObject.class);
+    public static final ConcreteType UNION_TYPE_OBJECT = typeForClass(UnionTypeObject.class);
     public static final ConcreteType INSTANCE_IDENTIFIER = typeForClass(InstanceIdentifier.class);
     public static final ConcreteType KEYED_INSTANCE_IDENTIFIER = typeForClass(KeyedInstanceIdentifier.class);
 
@@ -87,7 +91,9 @@ public final class BindingTypes {
     private static final ConcreteType KEYED_LIST_NOTIFICATION = typeForClass(KeyedListNotification.class);
     private static final ConcreteType NOTIFICATION = typeForClass(Notification.class);
     private static final ConcreteType OPAQUE_OBJECT = typeForClass(OpaqueObject.class);
+    private static final ConcreteType RPC = typeForClass(Rpc.class);
     private static final ConcreteType RPC_RESULT = typeForClass(RpcResult.class);
+    private static final ConcreteType YANG_FEATURE = typeForClass(YangFeature.class);
 
     private BindingTypes() {
 
@@ -258,6 +264,18 @@ public final class BindingTypes {
         return parameterizedTypeFor(OPAQUE_OBJECT, type);
     }
 
+    /**
+     * Type specializing {@link Rpc} for a particular type.
+     *
+     * @param input Type input type
+     * @param output Type output type
+     * @return A parameterized type corresponding to {@code Rpc<Input, Output>}
+     * @throws NullPointerException if any argument is {@code null}
+     */
+    public static @NonNull ParameterizedType rpc(final Type input, final Type output) {
+        return parameterizedTypeFor(RPC, input, output);
+    }
+
     /**
      * Type specializing {@link RpcResult} for a particular type.
      *
@@ -280,6 +298,18 @@ public final class BindingTypes {
         return parameterizedTypeFor(SCALAR_TYPE_OBJECT, type);
     }
 
+    /**
+     * Type specializing {@link YangFeature} for a particular type.
+     *
+     * @param concreteType The concrete type of this notification
+     * @param parent Type of parent defining the notification
+     * @return A parameterized type corresponding to {@code YangFeature<Type, DataRootType>}
+     * @throws NullPointerException if any argument is is null
+     */
+    public static ParameterizedType yangFeature(final Type concreteType, final Type parent) {
+        return parameterizedTypeFor(YANG_FEATURE, concreteType, parent);
+    }
+
     /**
      * Check if specified type is generated for a {@code type bits}.
      *
@@ -287,7 +317,7 @@ public final class BindingTypes {
      * @return {@code true} if the type is generated for a {@code type bits}
      */
     public static boolean isBitsType(final Type type) {
-        return type instanceof GeneratedTransferObject && isBitsType((GeneratedTransferObject) type);
+        return type instanceof GeneratedTransferObject gto && isBitsType(gto);
     }
 
     /**
@@ -307,8 +337,8 @@ public final class BindingTypes {
      * @return {@code true} if the type is generated for an identity
      */
     public static boolean isIdentityType(final Type type) {
-        if (type instanceof GeneratedType) {
-            for (var constant : ((GeneratedType) type).getConstantDefinitions()) {
+        if (type instanceof GeneratedType generated) {
+            for (var constant : generated.getConstantDefinitions()) {
                 if (VALUE_STATIC_FIELD_NAME.equals(constant.getName())
                     && BaseIdentity.class.equals(constant.getValue())) {
                     return true;
@@ -338,4 +368,40 @@ public final class BindingTypes {
         }
         return null;
     }
+
+    /**
+     * Return the {@link Identifiable} type a parameterized {@link Identifier} type references.
+     *
+     * @param type Parameterized type
+     * @return Identifiable target, or null if {@code type} does not match the result of {@link #identifier(Type)}
+     * @throws NullPointerException if {@code type} is null
+     */
+    @Beta
+    public static @Nullable Type extractIdentifiable(final ParameterizedType type) {
+        if (IDENTIFIER.equals(type.getRawType())) {
+            final var args = type.getActualTypeArguments();
+            if (args.length == 1) {
+                final var arg = args[0];
+                if (arg != null) {
+                    return arg;
+                }
+            }
+        }
+        return null;
+    }
+
+    @Beta
+    public static @Nullable Type extractYangFeatureDataRoot(final GeneratedTransferObject gto) {
+        if (!gto.isAbstract() && gto.getSuperType() == null) {
+            final var impls = gto.getImplements();
+            if (impls.size() == 1 && impls.get(0) instanceof ParameterizedType param
+                && YANG_FEATURE.equals(param.getRawType())) {
+                final var args = param.getActualTypeArguments();
+                if (args.length == 2) {
+                    return args[1];
+                }
+            }
+        }
+        return null;
+    }
 }