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.mdsal.binding.runtime.api;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.Throwables;
15 import com.google.common.cache.CacheBuilder;
16 import com.google.common.cache.CacheLoader;
17 import com.google.common.cache.LoadingCache;
18 import java.util.concurrent.ExecutionException;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
21 import org.opendaylight.yangtools.yang.binding.Action;
22 import org.opendaylight.yangtools.yang.binding.Augmentation;
23 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
24 import org.opendaylight.yangtools.yang.binding.Notification;
25 import org.opendaylight.yangtools.yang.binding.Rpc;
26 import org.opendaylight.yangtools.yang.binding.RpcInput;
27 import org.opendaylight.yangtools.yang.binding.RpcOutput;
28 import org.opendaylight.yangtools.yang.binding.YangData;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.YangDataName;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
34 * Runtime Context for Java YANG Binding classes. It provides information derived from the backing effective model,
35 * which is not captured in generated classes (and hence cannot be obtained from {@code BindingReflections}.
38 public abstract class AbstractBindingRuntimeContext implements BindingRuntimeContext {
39 private final LoadingCache<@NonNull QName, @NonNull Class<? extends BaseIdentity>> identityClasses =
40 CacheBuilder.newBuilder().weakValues().build(new CacheLoader<>() {
42 public Class<? extends BaseIdentity> load(final QName key) {
43 final var type = getTypes().findIdentity(key).orElseThrow(
44 () -> new IllegalArgumentException("Supplied QName " + key + " is not a valid identity"));
46 return loadClass(type.getIdentifier()).asSubclass(BaseIdentity.class);
47 } catch (ClassNotFoundException e) {
48 throw new IllegalArgumentException("Required class " + type + " was not found.", e);
49 } catch (ClassCastException e) {
50 throw new IllegalArgumentException(key + " resolves to a non-identity class", e);
56 public final <T extends Augmentation<?>> AugmentRuntimeType getAugmentationDefinition(final Class<T> augClass) {
57 return getTypes().findSchema(JavaTypeName.create(augClass))
58 .filter(AugmentRuntimeType.class::isInstance)
59 .map(AugmentRuntimeType.class::cast)
64 public final CompositeRuntimeType getSchemaDefinition(final Class<?> cls) {
65 checkArgument(!Augmentation.class.isAssignableFrom(cls), "Supplied class must not be an augmentation (%s is)",
67 checkArgument(!Action.class.isAssignableFrom(cls), "Supplied class must not be an action (%s is)", cls);
68 checkArgument(!Notification.class.isAssignableFrom(cls), "Supplied class must not be a notification (%s is)",
70 return (CompositeRuntimeType) getTypes().findSchema(JavaTypeName.create(cls)).orElse(null);
74 public final ActionRuntimeType getActionDefinition(final Class<? extends Action<?, ?, ?>> cls) {
75 return (ActionRuntimeType) getTypes().findSchema(JavaTypeName.create(cls)).orElse(null);
79 public final RpcRuntimeType getRpcDefinition(final Class<? extends Rpc<?, ?>> cls) {
80 return (RpcRuntimeType) getTypes().findSchema(JavaTypeName.create(cls)).orElse(null);
84 public final RuntimeType getTypeWithSchema(final Class<?> type) {
85 return getTypes().findSchema(JavaTypeName.create(type))
86 .orElseThrow(() -> new IllegalArgumentException("Failed to find schema for " + type));
90 public final Class<?> getClassForSchema(final Absolute schema) {
91 final var child = getTypes().schemaTreeChild(schema);
92 checkArgument(child != null, "Failed to find binding type for %s", schema);
93 return loadClass(child);
97 public final Class<? extends BaseIdentity> getIdentityClass(final QName input) {
99 return identityClasses.get(requireNonNull(input));
100 } catch (ExecutionException e) {
101 Throwables.throwIfUnchecked(e.getCause());
102 throw new IllegalStateException("Unexpected error looking up " + input, e);
107 public final Class<? extends RpcInput> getRpcInput(final QName rpcName) {
108 return loadClass(getTypes().findRpcInput(rpcName)
109 .orElseThrow(() -> new IllegalArgumentException("Failed to find RpcInput for " + rpcName)))
110 .asSubclass(RpcInput.class);
114 public final Class<? extends RpcOutput> getRpcOutput(final QName rpcName) {
115 return loadClass(getTypes().findRpcOutput(rpcName)
116 .orElseThrow(() -> new IllegalArgumentException("Failed to find RpcOutput for " + rpcName)))
117 .asSubclass(RpcOutput.class);
121 @SuppressWarnings("unchecked")
122 public final Class<? extends YangData<?>> getYangDataClass(final YangDataName templateName) {
123 return (Class) loadClass(getTypes().findYangData(templateName)
124 .orElseThrow(() -> new IllegalArgumentException("Failed to find YangData for " + templateName)))
125 .asSubclass(YangData.class);
128 private Class<?> loadClass(final RuntimeType type) {
130 return loadClass(type.javaType());
131 } catch (final ClassNotFoundException e) {
132 throw new IllegalStateException(e);