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 Class<G> commonInterface;
31 private Set<Class<? extends Augmentation<?>>> classes;
34 * @param commonInterface common interface
36 public GroupingLooseResolver(Class<G> commonInterface) {
37 this.commonInterface = commonInterface;
38 classes = new HashSet<>();
42 * Get augmentation classes
43 * @return list of augmentation classes
45 public Set<Class<? extends Augmentation<?>>> getClasses() {
50 * @param cls equivalent augmentation class
51 * @return this for chaining
53 public GroupingLooseResolver<G> add(Class<? extends Augmentation<?>> cls) {
54 Preconditions.checkArgument(commonInterface.isAssignableFrom(cls),
55 "oh man! I got " + cls);
61 * @param data expected to match <tt><T extends Augmentable<T>></tt>
62 * @return shared grouping
64 @SuppressWarnings("unchecked")
65 public <T extends Augmentable<T>> Optional<G> getExtension(DataObject data) {
66 T guessData = (T) data;
68 for (Class<? extends Augmentation<?>> cls : classes) {
69 Augmentation<T> potential = guessData
70 .getAugmentation((Class<Augmentation<T>>) cls);
71 if (potential != null) {
72 return Optional.of((G) potential);
76 return Optional.empty();