Fixup Augmentable and Identifiable methods changing
[openflowplugin.git] / extension / openflowplugin-extension-api / src / main / java / org / opendaylight / openflowplugin / extension / api / GroupingLooseResolver.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.extension.api;
9
10 import com.google.common.base.Preconditions;
11 import java.util.HashSet;
12 import java.util.Optional;
13 import java.util.Set;
14 import org.opendaylight.yangtools.yang.binding.Augmentable;
15 import org.opendaylight.yangtools.yang.binding.Augmentation;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Provides augmentation resolving upon given {@link Augmentable}.
22  * Used {@link Augmentation}s do not share {@link Augmentable}.
23  * <br>
24  * <b>Usage:</b> in case there are multiple {@link Augmentable} classes which might contain
25  * corresponding {@link Augmentation}s (1:1..n binding). And those {@link Augmentation}s
26  * are sharing the same grouping so that they could be processed in the same way.
27  *
28  * @param <G> grouping
29  */
30 public class GroupingLooseResolver<G> {
31     private static final Logger LOG = LoggerFactory.getLogger(GroupingLooseResolver.class);
32
33     private final Class<G> commonInterface;
34     private final Set<Class<? extends Augmentation<?>>> classes;
35
36     /**
37      * Constructor.
38      *
39      * @param commonInterface common interface
40      */
41     public GroupingLooseResolver(Class<G> commonInterface) {
42         this.commonInterface = commonInterface;
43         classes = new HashSet<>();
44     }
45
46     /**
47      * Get augmentation classes.
48      *
49      * @return list of augmentation classes
50      */
51     public Set<Class<? extends Augmentation<?>>> getClasses() {
52         return classes;
53     }
54
55     /**
56      * Adds an augmentation class.
57      *
58      * @param cls equivalent augmentation class
59      * @return this for chaining
60      */
61     public GroupingLooseResolver<G> add(Class<? extends Augmentation<?>> cls) {
62         Preconditions.checkArgument(commonInterface.isAssignableFrom(cls),
63                 "oh man! I got " + cls);
64         classes.add(cls);
65         return this;
66     }
67
68     /**
69      * Gets the extension for the give data.
70      *
71      * @param data expected to match <tt>&lt;T extends Augmentable&lt;T&gt;&gt;</tt>
72      * @return shared grouping
73      */
74     @SuppressWarnings("unchecked")
75     public <T extends Augmentable<T>> Optional<G> getExtension(DataObject data) {
76         // The type of 'data' should really be T for compile-time checking. Several call sites do not pass an
77         // Augmentable DataObject type which would result in a ClassCastException at runtime. This is clearly
78         // broken - those call sites need to be analyzed to determine the correct behavior in order for this method
79         // signature to be changed but for now catch ClassCastException.
80         T guessData;
81         try {
82             guessData = (T) data;
83         } catch (ClassCastException e) {
84             LOG.warn("Cannot cast to Augmentable", e);
85             return Optional.empty();
86         }
87
88         for (Class<? extends Augmentation<?>> cls : classes) {
89             Augmentation<T> potential = guessData
90                     .augmentation((Class<Augmentation<T>>) cls);
91             if (potential != null) {
92                 return Optional.of((G) potential);
93             }
94         }
95
96         return Optional.empty();
97     }
98 }