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;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
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<GROUPING> {
32 Class<GROUPING> commonInterface;
33 Set<Class<? extends Augmentation<?>>> classes;
36 * @param commonInterface
37 * @param common grouping Interface
39 public GroupingLooseResolver(Class<GROUPING> commonInterface) {
40 this.commonInterface = commonInterface;
41 classes = new HashSet<>();
45 * @param cls equivalent augmentation class
46 * @return this for chaining
48 public GroupingLooseResolver<GROUPING> add(Class<? extends Augmentation<?>> cls) {
49 Preconditions.checkArgument(commonInterface.isAssignableFrom(cls),
50 "oh man! I got " + cls);
56 * @param data expected to match <tt><T extends Augmentable<T>></tt>
57 * @return shared grouping
59 @SuppressWarnings("unchecked")
60 public <T extends Augmentable<T>> Optional<GROUPING> getExtension(DataObject data) {
61 T guessData = (T) data;
63 for (Class<? extends Augmentation<?>> cls : classes) {
64 Augmentation<T> potential = guessData
65 .getAugmentation((Class<Augmentation<T>>) cls);
66 if (potential != null) {
67 return Optional.of((GROUPING) potential);
71 return Optional.absent();