Fixup Augmentable and Identifiable methods changing
[openflowplugin.git] / extension / openflowplugin-extension-api / src / main / java / org / opendaylight / openflowplugin / extension / api / GroupingLooseResolver.java
index 037715d34ea914f2bb3220f33d26c12da764b4c0..bb1fc0da6cde5fe3cef1a6e7086ab4cf25a183c6 100644 (file)
@@ -14,6 +14,8 @@ import java.util.Set;
 import org.opendaylight.yangtools.yang.binding.Augmentable;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
 import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Provides augmentation resolving upon given {@link Augmentable}.
@@ -26,11 +28,14 @@ import org.opendaylight.yangtools.yang.binding.DataObject;
  * @param <G> grouping
  */
 public class GroupingLooseResolver<G> {
+    private static final Logger LOG = LoggerFactory.getLogger(GroupingLooseResolver.class);
 
-    private Class<G> commonInterface;
-    private Set<Class<? extends Augmentation<?>>> classes;
+    private final Class<G> commonInterface;
+    private final Set<Class<? extends Augmentation<?>>> classes;
 
     /**
+     * Constructor.
+     *
      * @param commonInterface common interface
      */
     public GroupingLooseResolver(Class<G> commonInterface) {
@@ -39,6 +44,17 @@ public class GroupingLooseResolver<G> {
     }
 
     /**
+     * Get augmentation classes.
+     *
+     * @return list of augmentation classes
+     */
+    public Set<Class<? extends Augmentation<?>>> getClasses() {
+        return classes;
+    }
+
+    /**
+     * Adds an augmentation class.
+     *
      * @param cls equivalent augmentation class
      * @return this for chaining
      */
@@ -50,16 +66,28 @@ public class GroupingLooseResolver<G> {
     }
 
     /**
+     * Gets the extension for the give data.
+     *
      * @param data expected to match <tt>&lt;T extends Augmentable&lt;T&gt;&gt;</tt>
      * @return shared grouping
      */
     @SuppressWarnings("unchecked")
     public <T extends Augmentable<T>> Optional<G> getExtension(DataObject data) {
-        T guessData = (T) data;
+        // The type of 'data' should really be T for compile-time checking. Several call sites do not pass an
+        // Augmentable DataObject type which would result in a ClassCastException at runtime. This is clearly
+        // broken - those call sites need to be analyzed to determine the correct behavior in order for this method
+        // signature to be changed but for now catch ClassCastException.
+        T guessData;
+        try {
+            guessData = (T) data;
+        } catch (ClassCastException e) {
+            LOG.warn("Cannot cast to Augmentable", e);
+            return Optional.empty();
+        }
 
         for (Class<? extends Augmentation<?>> cls : classes) {
             Augmentation<T> potential = guessData
-                    .getAugmentation((Class<Augmentation<T>>) cls);
+                    .augmentation((Class<Augmentation<T>>) cls);
             if (potential != null) {
                 return Optional.of((G) potential);
             }