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;
19 * Provides augmentation resolving upon given {@link Augmentable}.
20 * Used {@link Augmentation}s do not share {@link Augmentable}.
22 * <b>Usage:</b> in case there are multiple {@link Augmentable} classes which might contain
23 * corresponding {@link Augmentation}s (1:1..n binding). And those {@link Augmentation}s
24 * are sharing the same grouping so that they could be processed in the same way.
28 public class GroupingLooseResolver<G> {
30 private final Class<G> commonInterface;
31 private final Set<Class<? extends Augmentation<?>>> classes;
36 * @param commonInterface common interface
38 public GroupingLooseResolver(Class<G> commonInterface) {
39 this.commonInterface = commonInterface;
40 classes = new HashSet<>();
44 * Get augmentation classes.
46 * @return list of augmentation classes
48 public Set<Class<? extends Augmentation<?>>> getClasses() {
53 * Adds an augmentation class.
55 * @param cls equivalent augmentation class
56 * @return this for chaining
58 public GroupingLooseResolver<G> add(Class<? extends Augmentation<?>> cls) {
59 Preconditions.checkArgument(commonInterface.isAssignableFrom(cls),
60 "oh man! I got " + cls);
66 * Gets the extension for the give data.
68 * @param data expected to match <tt><T extends Augmentable<T>></tt>
69 * @return shared grouping
71 @SuppressWarnings("unchecked")
72 public <T extends Augmentable<T>> Optional<G> getExtension(DataObject data) {
73 T guessData = (T) data;
75 for (Class<? extends Augmentation<?>> cls : classes) {
76 Augmentation<T> potential = guessData
77 .getAugmentation((Class<Augmentation<T>>) cls);
78 if (potential != null) {
79 return Optional.of((G) potential);
83 return Optional.empty();