2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.extension.api;
10 import com.google.common.base.Preconditions;
11 import java.util.HashSet;
12 import java.util.Optional;
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;
21 * Provides augmentation resolving upon given {@link Augmentable}.
22 * Used {@link Augmentation}s do not share {@link Augmentable}.
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.
30 public class GroupingLooseResolver<G> {
31 private static final Logger LOG = LoggerFactory.getLogger(GroupingLooseResolver.class);
33 private final Class<G> commonInterface;
34 private final Set<Class<? extends Augmentation<?>>> classes;
39 * @param commonInterface common interface
41 public GroupingLooseResolver(Class<G> commonInterface) {
42 this.commonInterface = commonInterface;
43 classes = new HashSet<>();
47 * Get augmentation classes.
49 * @return list of augmentation classes
51 public Set<Class<? extends Augmentation<?>>> getClasses() {
56 * Adds an augmentation class.
58 * @param cls equivalent augmentation class
59 * @return this for chaining
61 public GroupingLooseResolver<G> add(Class<? extends Augmentation<?>> cls) {
62 Preconditions.checkArgument(commonInterface.isAssignableFrom(cls),
63 "oh man! I got " + cls);
69 * Gets the extension for the give data.
71 * @param data expected to match <tt><T extends Augmentable<T>></tt>
72 * @return shared grouping
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.
83 } catch (ClassCastException e) {
84 LOG.warn("Cannot cast to Augmentable", e);
85 return Optional.empty();
88 for (Class<? extends Augmentation<?>> cls : classes) {
89 Augmentation<T> potential = guessData
90 .getAugmentation((Class<Augmentation<T>>) cls);
91 if (potential != null) {
92 return Optional.of((G) potential);
96 return Optional.empty();