Deprecate BindingRuntimeContext.getEnumMapping()
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / util / BindingRuntimeContext.java
index f40ff9cd69f0566b90cccefa6378e4d39689dbc7..a5eb055255edddb3a498e263f8c7f136f3d12250 100644 (file)
@@ -18,6 +18,7 @@ import com.google.common.cache.LoadingCache;
 import com.google.common.collect.BiMap;
 import com.google.common.collect.HashBiMap;
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import java.util.AbstractMap.SimpleEntry;
 import java.util.ArrayList;
@@ -30,7 +31,7 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.Set;
-import javax.annotation.Nullable;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.generator.api.BindingRuntimeTypes;
 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
 import org.opendaylight.mdsal.binding.generator.impl.BindingGeneratorImpl;
@@ -46,6 +47,7 @@ import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.binding.Action;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
+import org.opendaylight.yangtools.yang.binding.Enumeration;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
@@ -92,7 +94,7 @@ public final class BindingRuntimeContext implements Immutable {
         new CacheLoader<QName, Class<?>>() {
             @Override
             public Class<?> load(final QName key) {
-                final java.util.Optional<Type> identityType = runtimeTypes.findIdentity(key);
+                final Optional<Type> identityType = runtimeTypes.findIdentity(key);
                 checkArgument(identityType.isPresent(), "Supplied QName %s is not a valid identity", key);
                 try {
                     return strategy.loadClass(identityType.get());
@@ -223,7 +225,7 @@ public final class BindingRuntimeContext implements Immutable {
             }
         }
 
-        final AugmentationIdentifier identifier = new AugmentationIdentifier(childNames);
+        final AugmentationIdentifier identifier = AugmentationIdentifier.create(childNames);
         final AugmentationSchemaNode proxy = new EffectiveAugmentationSchema(origSchema, realChilds);
         return new SimpleEntry<>(identifier, proxy);
     }
@@ -252,10 +254,6 @@ public final class BindingRuntimeContext implements Immutable {
         return found;
     }
 
-    private static Type referencedType(final Class<?> type) {
-        return new ReferencedTypeImpl(JavaTypeName.create(type));
-    }
-
     /**
      * Returns schema ({@link DataSchemaNode}, {@link AugmentationSchemaNode} or {@link TypeDefinition})
      * from which supplied class was generated. Returned schema may be augmented with
@@ -289,7 +287,7 @@ public final class BindingRuntimeContext implements Immutable {
 
         for (final ChoiceSchemaNode choice :  Iterables.filter(schema.getChildNodes(), ChoiceSchemaNode.class)) {
             final ChoiceSchemaNode originalChoice = getOriginalSchema(choice);
-            final java.util.Optional<Type> optType = runtimeTypes.findType(originalChoice);
+            final Optional<Type> optType = runtimeTypes.findType(originalChoice);
             checkState(optType.isPresent(), "Failed to find generated type for choice %s", originalChoice);
             final Type choiceType = optType.get();
 
@@ -313,7 +311,9 @@ public final class BindingRuntimeContext implements Immutable {
      *
      * @param enumClass enum generated class
      * @return mapped enum constants from yang with their corresponding values in generated binding classes
+     * @deprecated This method is not guaranteed to be accurate. Use {@link Enumeration#getName()} instead.
      */
+    @Deprecated(forRemoval = true)
     public BiMap<String, String> getEnumMapping(final Class<?> enumClass) {
         final Entry<GeneratedType, WithStatus> typeWithSchema = getTypeWithSchema(enumClass);
         return getEnumMapping(typeWithSchema);
@@ -324,11 +324,29 @@ public final class BindingRuntimeContext implements Immutable {
      *
      * @param enumClassName enum generated class name
      * @return mapped enum constants from yang with their corresponding values in generated binding classes
+     * @deprecated This method is not guaranteed to be accurate. Use {@link Enumeration#getName()} instead.
      */
+    @Deprecated(forRemoval = true)
     public BiMap<String, String> getEnumMapping(final String enumClassName) {
         return getEnumMapping(findTypeWithSchema(enumClassName));
     }
 
+    private static BiMap<String, String> getEnumMapping(final Entry<GeneratedType, WithStatus> typeWithSchema) {
+        final TypeDefinition<?> typeDef = (TypeDefinition<?>) typeWithSchema.getValue();
+
+        Preconditions.checkArgument(typeDef instanceof EnumTypeDefinition);
+        final EnumTypeDefinition enumType = (EnumTypeDefinition) typeDef;
+
+        final HashBiMap<String, String> mappedEnums = HashBiMap.create();
+
+        for (final EnumTypeDefinition.EnumPair enumPair : enumType.getValues()) {
+            mappedEnums.put(enumPair.getName(), BindingMapping.getClassName(enumPair.getName()));
+        }
+
+        // TODO cache these maps for future use
+        return mappedEnums;
+    }
+
     private Entry<GeneratedType, WithStatus> findTypeWithSchema(final String className) {
         // All we have is a straight FQCN, which we need to split into a hierarchical JavaTypeName. This involves
         // some amount of guesswork -- we do that by peeling components at the dot and trying out, e.g. given
@@ -353,13 +371,13 @@ public final class BindingRuntimeContext implements Immutable {
             }
 
             final Type type = new ReferencedTypeImpl(name);
-            final java.util.Optional<WithStatus> optSchema = runtimeTypes.findSchema(type);
+            final Optional<WithStatus> optSchema = runtimeTypes.findSchema(type);
             if (!optSchema.isPresent()) {
                 continue;
             }
 
             final WithStatus schema = optSchema.get();
-            final java.util.Optional<Type> optDefinedType =  runtimeTypes.findType(schema);
+            final Optional<Type> optDefinedType =  runtimeTypes.findType(schema);
             if (!optDefinedType.isPresent()) {
                 continue;
             }
@@ -375,22 +393,6 @@ public final class BindingRuntimeContext implements Immutable {
         throw new IllegalArgumentException("Failed to find type for " + className);
     }
 
-    private static BiMap<String, String> getEnumMapping(final Entry<GeneratedType, WithStatus> typeWithSchema) {
-        final TypeDefinition<?> typeDef = (TypeDefinition<?>) typeWithSchema.getValue();
-
-        Preconditions.checkArgument(typeDef instanceof EnumTypeDefinition);
-        final EnumTypeDefinition enumType = (EnumTypeDefinition) typeDef;
-
-        final HashBiMap<String, String> mappedEnums = HashBiMap.create();
-
-        for (final EnumTypeDefinition.EnumPair enumPair : enumType.getValues()) {
-            mappedEnums.put(enumPair.getName(), BindingMapping.getClassName(enumPair.getName()));
-        }
-
-        // TODO cache these maps for future use
-        return mappedEnums;
-    }
-
     public Set<Class<?>> getCases(final Class<?> choice) {
         final Collection<Type> cazes = runtimeTypes.findCases(referencedType(choice));
         final Set<Class<?>> ret = new HashSet<>(cazes.size());
@@ -407,7 +409,7 @@ public final class BindingRuntimeContext implements Immutable {
 
     public Class<?> getClassForSchema(final SchemaNode childSchema) {
         final SchemaNode origSchema = getOriginalSchema(childSchema);
-        final java.util.Optional<Type> clazzType = runtimeTypes.findType(origSchema);
+        final Optional<Type> clazzType = runtimeTypes.findType(origSchema);
         checkArgument(clazzType.isPresent(), "Failed to find binding type for %s (original %s)",
             childSchema, origSchema);
 
@@ -430,7 +432,7 @@ public final class BindingRuntimeContext implements Immutable {
                 }
 
                 if (!augment.getChildNodes().isEmpty()) {
-                    final java.util.Optional<Type> augType = runtimeTypes.findType(augOrig);
+                    final Optional<Type> augType = runtimeTypes.findType(augOrig);
                     if (augType.isPresent()) {
                         identifierToType.put(getAugmentationIdentifier(augment), augType.get());
                     }
@@ -442,11 +444,13 @@ public final class BindingRuntimeContext implements Immutable {
     }
 
     private static AugmentationIdentifier getAugmentationIdentifier(final AugmentationSchemaNode augment) {
-        final Set<QName> childNames = new HashSet<>();
-        for (final DataSchemaNode child : augment.getChildNodes()) {
-            childNames.add(child.getQName());
-        }
-        return new AugmentationIdentifier(childNames);
+        // FIXME: use DataSchemaContextNode.augmentationIdentifierFrom() once it does caching
+        return AugmentationIdentifier.create(augment.getChildNodes().stream().map(DataSchemaNode::getQName)
+            .collect(ImmutableSet.toImmutableSet()));
+    }
+
+    private static Type referencedType(final Class<?> type) {
+        return new ReferencedTypeImpl(JavaTypeName.create(type));
     }
 
     private static Type referencedType(final Type type) {