2 * Copyright (c) 2013 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.yangtools.yang.binding.util;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
13 import java.lang.reflect.Field;
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Type;
17 import java.util.LinkedList;
18 import java.util.List;
19 import java.util.ServiceLoader;
20 import java.util.concurrent.Callable;
21 import java.util.concurrent.Future;
22 import java.util.concurrent.TimeUnit;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
26 import org.opendaylight.yangtools.yang.binding.Augmentable;
27 import org.opendaylight.yangtools.yang.binding.Augmentation;
28 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
29 import org.opendaylight.yangtools.yang.binding.BindingMapping;
30 import org.opendaylight.yangtools.yang.binding.ChildOf;
31 import org.opendaylight.yangtools.yang.binding.DataContainer;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.Notification;
34 import org.opendaylight.yangtools.yang.binding.RpcService;
35 import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
36 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.common.base.Optional;
42 import com.google.common.cache.CacheBuilder;
43 import com.google.common.cache.CacheLoader;
44 import com.google.common.cache.LoadingCache;
45 import com.google.common.collect.ImmutableSet;
46 import com.google.common.collect.ImmutableSet.Builder;
48 public class BindingReflections {
50 private static final long EXPIRATION_TIME = 60;
51 private static final String ROOT_PACKAGE_PATTERN_STRING = "(org.opendaylight.yang.gen.v1.[a-z0-9_\\.]*\\.rev[0-9][0-9][0-1][0-9][0-3][0-9])";
52 private static final Pattern ROOT_PACKAGE_PATTERN = Pattern.compile(ROOT_PACKAGE_PATTERN_STRING);
53 private static final Logger LOG = LoggerFactory.getLogger(BindingReflections.class);
55 private static final LoadingCache<Class<?>, Optional<QName>> classToQName = CacheBuilder.newBuilder() //
57 .expireAfterAccess(EXPIRATION_TIME, TimeUnit.SECONDS) //
58 .build(new ClassToQNameLoader());
61 private BindingReflections() {
62 throw new UnsupportedOperationException("Utility class.");
67 * Find augmentation target class from concrete Augmentation class
69 * This method uses first generic argument of
70 * implemented {@link Augmentation} interface.
73 * {@link Augmentation} subclass for which we want to determine
74 * augmentation target.
75 * @return Augmentation target - class which augmentation provides
76 * additional extensions.
78 public static Class<? extends Augmentable<?>> findAugmentationTarget(
79 final Class<? extends Augmentation<?>> augmentation) {
80 return ClassLoaderUtils.findFirstGenericArgument(augmentation, Augmentation.class);
84 * Find data hierarchy parent from concrete Data class
86 * This method uses first generic argument of
87 * implemented {@link ChildOf} interface.
90 * {@link Augmentation} subclass for which we want to determine
91 * augmentation target.
92 * @return Augmentation target - class which augmentation provides
93 * additional extensions.
95 public static Class<?> findHierarchicalParent(final Class<? extends ChildOf<?>> childClass) {
96 return ClassLoaderUtils.findFirstGenericArgument(childClass, ChildOf.class);
100 * Find data hierarchy parent from concrete Data class
102 * This method is shorthand which gets DataObject class by invoking
103 * {@link DataObject#getImplementedInterface()} and uses {@link #findHierarchicalParent(Class)}.
106 * {@link Augmentation} subclass for which we want to determine
107 * augmentation target.
108 * @return Augmentation target - class which augmentation provides
109 * additional extensions.
111 public static Class<?> findHierarchicalParent(final DataObject child) {
112 if (child instanceof ChildOf) {
113 return ClassLoaderUtils.findFirstGenericArgument(child.getImplementedInterface(), ChildOf.class);
119 * Returns a QName associated to supplied type
122 * @return QName associated to supplied dataType. If dataType is
123 * Augmentation method does not return canonical QName, but QName
124 * with correct namespace revision, but virtual local name, since
125 * augmentations do not have name.
127 * May return null if QName is not present.
129 public static final QName findQName(final Class<?> dataType) {
130 return classToQName.getUnchecked(dataType).orNull();
134 * Checks if method is RPC invocation
138 * @param possibleMethod Method to check
139 * @return true if method is RPC invocation, false otherwise.
141 public static boolean isRpcMethod(final Method possibleMethod) {
142 return possibleMethod != null && RpcService.class.isAssignableFrom(possibleMethod.getDeclaringClass())
143 && Future.class.isAssignableFrom(possibleMethod.getReturnType())
144 && possibleMethod.getParameterTypes().length <= 1;
149 * Extracts Output class for RPC method
151 * @param targetMethod method to scan
152 * @return Optional.absent() if result type could not be get,
153 * or return type is Void.
155 @SuppressWarnings("rawtypes")
156 public static Optional<Class<?>> resolveRpcOutputClass(final Method targetMethod) {
157 checkState(isRpcMethod(targetMethod), "Supplied method is not Rpc invocation method");
158 Type futureType = targetMethod.getGenericReturnType();
159 Type rpcResultType = ClassLoaderUtils.getFirstGenericParameter(futureType);
160 Type rpcResultArgument = ClassLoaderUtils.getFirstGenericParameter(rpcResultType);
161 if (rpcResultArgument instanceof Class && !Void.class.equals(rpcResultArgument)) {
162 return Optional.<Class<?>> of((Class) rpcResultArgument);
164 return Optional.absent();
169 * Extracts input class for RPC method
171 * @param targetMethod method to scan
172 * @return Optional.absent() if rpc has no input, Rpc input type otherwise.
174 @SuppressWarnings("unchecked")
175 public static Optional<Class<? extends DataContainer>> resolveRpcInputClass(final Method targetMethod) {
176 @SuppressWarnings("rawtypes")
177 Class[] types = targetMethod.getParameterTypes();
178 if (types.length == 0) {
179 return Optional.absent();
181 if (types.length == 1) {
182 return Optional.<Class<? extends DataContainer>> of(types[0]);
184 throw new IllegalArgumentException("Method has 2 or more arguments.");
187 public static QName getQName(final Class<? extends BaseIdentity> context) {
188 return findQName(context);
193 * Checks if class is child of augmentation.
199 public static boolean isAugmentationChild(final Class<?> clazz) {
200 // FIXME: Current resolver could be still confused when
201 // child node was added by grouping
202 checkArgument(clazz != null);
204 @SuppressWarnings({ "rawtypes", "unchecked" })
205 Class<?> parent = findHierarchicalParent((Class) clazz);
206 if (parent == null) {
207 LOG.debug("Did not find a parent for class {}", clazz);
211 String clazzModelPackage = getModelRootPackageName(clazz.getPackage());
212 String parentModelPackage = getModelRootPackageName(parent.getPackage());
214 return !clazzModelPackage.equals(parentModelPackage);
218 * Returns root package name for suplied package.
220 * @param pkg Package for which find model root package.
221 * @return Package of model root.
223 public static String getModelRootPackageName(final Package pkg) {
224 return getModelRootPackageName(pkg.getName());
228 * Returns root package name for suplied package name.
230 * @param pkg Package for which find model root package.
231 * @return Package of model root.
233 public static String getModelRootPackageName(final String name) {
234 checkArgument(name != null, "Package name should not be null.");
235 checkArgument(name.startsWith(BindingMapping.PACKAGE_PREFIX), "Package name not starting with %s, is: %s",
236 BindingMapping.PACKAGE_PREFIX, name);
237 Matcher match = ROOT_PACKAGE_PATTERN.matcher(name);
238 checkArgument(match.find(), "Package name '%s' does not match required pattern '%s'", name,
239 ROOT_PACKAGE_PATTERN_STRING);
240 return match.group(0);
245 * Returns instance of {@link YangModuleInfo} of declaring model for specific class.
248 * @return Instance of {@link YangModuleInfo} associated with model, from which this class was derived.
251 public static YangModuleInfo getModuleInfo(final Class<?> cls) throws Exception {
252 checkArgument(cls != null);
253 String packageName = getModelRootPackageName(cls.getPackage());
254 final String potentialClassName = getModuleInfoClassName(packageName);
255 return ClassLoaderUtils.withClassLoader(cls.getClassLoader(), new Callable<YangModuleInfo>() {
257 public YangModuleInfo call() throws ClassNotFoundException, IllegalAccessException,
258 IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
259 Class<?> moduleInfoClass = Thread.currentThread().getContextClassLoader().loadClass(potentialClassName);
260 return (YangModuleInfo) moduleInfoClass.getMethod("getInstance").invoke(null);
265 public static String getModuleInfoClassName(final String packageName) {
266 return packageName + "." + BindingMapping.MODULE_INFO_CLASS_NAME;
271 * Check if supplied class is derived from YANG model.
273 * @param cls Class to check
274 * @return true if class is derived from YANG model.
276 public static boolean isBindingClass(final Class<?> cls) {
277 if (DataContainer.class.isAssignableFrom(cls) || Augmentation.class.isAssignableFrom(cls)) {
280 return (cls.getName().startsWith(BindingMapping.PACKAGE_PREFIX));
285 * Checks if supplied method is callback for notifications.
288 * @return true if method is notification callback.
290 public static boolean isNotificationCallback(final Method method) {
291 checkArgument(method != null);
292 if (method.getName().startsWith("on") && method.getParameterTypes().length == 1) {
293 Class<?> potentialNotification = method.getParameterTypes()[0];
294 if (isNotification(potentialNotification)
295 && method.getName().equals("on" + potentialNotification.getSimpleName())) {
304 * Checks is supplied class is Notification.
306 * @param potentialNotification
309 public static boolean isNotification(final Class<?> potentialNotification) {
310 checkArgument(potentialNotification != null,"potentialNotification must not be null.");
311 return Notification.class.isAssignableFrom(potentialNotification);
316 * Loads {@link YangModuleInfo} infos available on current classloader.
318 * This method is shorthand for {@link #loadModuleInfos(ClassLoader)} with
319 * {@link Thread#getContextClassLoader()} for current thread.
321 * @return Set of {@link YangModuleInfo} available for current classloader.
323 public static ImmutableSet<YangModuleInfo> loadModuleInfos() {
324 return loadModuleInfos(Thread.currentThread().getContextClassLoader());
329 * Loads {@link YangModuleInfo} infos available on supplied classloader.
331 * {@link YangModuleInfo} are discovered using {@link ServiceLoader}
332 * for {@link YangModelBindingProvider}. {@link YangModelBindingProvider}
333 * are simple classes which holds only pointers to actual instance
334 * {@link YangModuleInfo}.
336 * When {@link YangModuleInfo} is available, all dependencies are recursivelly collected
337 * into returning set by collecting results of {@link YangModuleInfo#getImportedModules()}.
340 * @param loader Classloader for which {@link YangModuleInfo} should be retrieved.
341 * @return Set of {@link YangModuleInfo} available for supplied classloader.
343 public static ImmutableSet<YangModuleInfo> loadModuleInfos(final ClassLoader loader) {
344 Builder<YangModuleInfo> moduleInfoSet = ImmutableSet.<YangModuleInfo> builder();
345 ServiceLoader<YangModelBindingProvider> serviceLoader = ServiceLoader.load(YangModelBindingProvider.class,
347 for (YangModelBindingProvider bindingProvider : serviceLoader) {
348 YangModuleInfo moduleInfo = bindingProvider.getModuleInfo();
349 checkState(moduleInfo != null, "Module Info for %s is not available.", bindingProvider.getClass());
350 collectYangModuleInfo(bindingProvider.getModuleInfo(), moduleInfoSet);
352 return moduleInfoSet.build();
355 private static void collectYangModuleInfo(final YangModuleInfo moduleInfo,
356 final Builder<YangModuleInfo> moduleInfoSet) {
357 moduleInfoSet.add(moduleInfo);
358 for (YangModuleInfo dependency : moduleInfo.getImportedModules()) {
359 collectYangModuleInfo(dependency, moduleInfoSet);
365 * Checks if supplied class represents RPC Input / RPC Output.
367 * @param targetType Class to be checked
368 * @return true if class represents RPC Input or RPC Output class.
370 public static boolean isRpcType(final Class<? extends DataObject> targetType) {
371 return DataContainer.class.isAssignableFrom(targetType) //
372 && !ChildOf.class.isAssignableFrom(targetType) //
373 && !Notification.class.isAssignableFrom(targetType) //
374 && (targetType.getName().endsWith("Input") || targetType.getName().endsWith("Output"));
379 * Scans supplied class and returns an iterable of all data children classes.
381 * @param type YANG Modeled Entity derived from DataContainer
382 * @return Iterable of all data children, which have YANG modeled entity
384 @SuppressWarnings("unchecked")
385 public static Iterable<Class<? extends DataObject>> getChildrenClasses(final Class<? extends DataContainer> type) {
386 checkArgument(type != null, "Target type must not be null");
387 checkArgument(DataContainer.class.isAssignableFrom(type), "Supplied type must be derived from DataContainer");
388 List<Class<? extends DataObject>> ret = new LinkedList<>();
389 for (Method method : type.getMethods()) {
390 Optional<Class<? extends DataContainer>> entity = getYangModeledReturnType(method);
391 if (entity.isPresent()) {
392 ret.add((Class<? extends DataObject>) entity.get());
398 @SuppressWarnings("unchecked")
399 private static Optional<Class<? extends DataContainer>> getYangModeledReturnType(final Method method) {
400 if (method.getName().equals("getClass") || !method.getName().startsWith("get")
401 || method.getParameterTypes().length > 0) {
402 return Optional.absent();
405 @SuppressWarnings("rawtypes")
406 Class returnType = method.getReturnType();
407 if (DataContainer.class.isAssignableFrom(returnType)) {
408 return Optional.<Class<? extends DataContainer>> of(returnType);
409 } else if (List.class.isAssignableFrom(returnType)) {
411 return ClassLoaderUtils.withClassLoader(method.getDeclaringClass().getClassLoader(),
412 new Callable<Optional<Class<? extends DataContainer>>>() {
413 @SuppressWarnings("rawtypes")
415 public Optional<Class<? extends DataContainer>> call() {
416 Type listResult = ClassLoaderUtils.getFirstGenericParameter(method
417 .getGenericReturnType());
418 if (listResult instanceof Class
419 && DataContainer.class.isAssignableFrom((Class) listResult)) {
420 return Optional.<Class<? extends DataContainer>> of((Class) listResult);
422 return Optional.absent();
426 } catch (Exception e) {
429 * It is safe to log this this exception on debug, since
430 * this method should not fail. Only failures are possible if
431 * the runtime / backing.
434 LOG.debug("Unable to find YANG modeled return type for {}", method, e);
437 return Optional.absent();
440 private static class ClassToQNameLoader extends CacheLoader<Class<?>, Optional<QName>> {
443 public Optional<QName> load(final Class<?> key) throws Exception {
445 Field field = key.getField(BindingMapping.QNAME_STATIC_FIELD_NAME);
446 Object obj = field.get(null);
447 if (obj instanceof QName) {
448 return Optional.of((QName) obj);
450 } catch (NoSuchFieldException e) {
451 if (Augmentation.class.isAssignableFrom(key)) {
452 YangModuleInfo moduleInfo = getModuleInfo(key);
453 return Optional.of(QName.create(moduleInfo.getNamespace(), moduleInfo.getRevision(),
454 moduleInfo.getName()));
456 } catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
459 * It is safe to log this this exception on debug, since
460 * this method should not fail. Only failures are possible if
461 * the runtime / backing.
464 LOG.debug("Unexpected exception during extracting QName for {}",key,e);
466 return Optional.absent();