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 java.util.HashSet;
13 import org.opendaylight.yangtools.yang.binding.Augmentable;
14 import org.opendaylight.yangtools.yang.binding.Augmentation;
16 import com.google.common.base.Optional;
17 import com.google.common.base.Preconditions;
20 * Provides augmentation resolving upon given {@link Augmentable}.
21 * Used {@link #classes} share the same {@link Augmentable}.
23 * <b>Usage:</b> in case there is {@link Augmentable} which might contain
24 * multiple {@link Augmentation}s depending on origin. And those {@link Augmentation}s
25 * are sharing the same grouping so that they could be processed in the same way.
30 public class GroupingResolver<GROUPING, T extends Augmentable<T>> {
32 Class<GROUPING> commonInterface;
33 Set<Class<? extends Augmentation<T>>> classes;
36 * @param commonInterface
38 public GroupingResolver(Class<GROUPING> commonInterface) {
39 this.commonInterface = commonInterface;
40 classes = new HashSet<>();
44 * @param cls equivalent augmentation class
45 * @return this for chaining
47 public <X extends Augmentation<T>> GroupingResolver<GROUPING, T> add(Class<X> cls) {
48 Preconditions.checkArgument(commonInterface.isAssignableFrom(cls));
54 * @param clses set of equivalent augmentation classes
56 public void setAugmentations(Set<Class<? extends Augmentation<T>>> clses) {
57 for (Class<? extends Augmentation<T>> cls : clses) {
58 Preconditions.checkArgument(commonInterface.isAssignableFrom(cls));
65 * @return shared grouping
67 @SuppressWarnings("unchecked")
68 public Optional<GROUPING> getExtension(T data) {
69 for (Class<? extends Augmentation<T>> cls : classes) {
70 Augmentation<T> potential = data.getAugmentation(cls);
71 if (potential != null) {
72 return Optional.of((GROUPING) potential);
75 return Optional.absent();