Map identities to proper objects
[mdsal.git] / binding / mdsal-binding-model-ri / src / main / java / org / opendaylight / mdsal / binding / model / ri / BindingTypes.java
index c4beb7b174144d2a046e2a00e67efafee327a20b..26dd49ef12b0bf093c0da489dcb698e51ba45693 100644 (file)
@@ -9,10 +9,13 @@ package org.opendaylight.mdsal.binding.model.ri;
 
 import static org.opendaylight.mdsal.binding.model.ri.Types.parameterizedTypeFor;
 import static org.opendaylight.mdsal.binding.model.ri.Types.typeForClass;
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.VALUE_STATIC_FIELD_NAME;
 
 import com.google.common.annotations.VisibleForTesting;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
+import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
+import org.opendaylight.mdsal.binding.model.api.GeneratedType;
 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
 import org.opendaylight.mdsal.binding.model.api.Type;
@@ -43,6 +46,7 @@ import org.opendaylight.yangtools.yang.binding.TypeObject;
 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
 
 public final class BindingTypes {
 
@@ -272,4 +276,42 @@ public final class BindingTypes {
     public static ParameterizedType scalarTypeObject(final Type type) {
         return parameterizedTypeFor(SCALAR_TYPE_OBJECT, type);
     }
+
+    /**
+     * Check if specified type is generated for a {@code type bits}.
+     *
+     * @param type Type to examine
+     * @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);
+    }
+
+    /**
+     * Check if specified type is generated for a {@code type bits}.
+     *
+     * @param gto Type to examine
+     * @return {@code true} if the type is generated for a {@code type bits}
+     */
+    public static boolean isBitsType(final GeneratedTransferObject gto) {
+        return gto.isTypedef() && gto.getBaseType() instanceof BitsTypeDefinition;
+    }
+
+    /**
+     * Check if specified type is generated for an identity.
+     *
+     * @param type Type to examine
+     * @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 (VALUE_STATIC_FIELD_NAME.equals(constant.getName())
+                    && BaseIdentity.class.equals(constant.getValue())) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
 }