Bump versions to 13.0.4-SNAPSHOT
[mdsal.git] / binding / mdsal-binding-runtime-api / src / main / java / org / opendaylight / mdsal / binding / runtime / api / AbstractBindingRuntimeContext.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.runtime.api;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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;
32
33 /**
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}.
36  */
37 @Beta
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<>() {
41             @Override
42             public Class<? extends BaseIdentity> load(final QName key) {
43                 final var type = getTypes().identityChild(key);
44                 if (type == null) {
45                     throw new IllegalArgumentException("Supplied QName " + key + " is not a valid identity");
46                 }
47                 try {
48                     return loadClass(type.getIdentifier()).asSubclass(BaseIdentity.class);
49                 } catch (ClassNotFoundException e) {
50                     throw new IllegalArgumentException("Required class " + type + " was not found.", e);
51                 } catch (ClassCastException e) {
52                     throw new IllegalArgumentException(key + " resolves to a non-identity class", e);
53                 }
54             }
55         });
56
57     @Override
58     public final <T extends Augmentation<?>> AugmentRuntimeType getAugmentationDefinition(final Class<T> augClass) {
59         return getTypes().findSchema(JavaTypeName.create(augClass))
60             .filter(AugmentRuntimeType.class::isInstance)
61             .map(AugmentRuntimeType.class::cast)
62             .orElse(null);
63     }
64
65     @Override
66     public final CompositeRuntimeType getSchemaDefinition(final Class<?> cls) {
67         checkArgument(!Augmentation.class.isAssignableFrom(cls), "Supplied class must not be an augmentation (%s is)",
68             cls);
69         checkArgument(!Action.class.isAssignableFrom(cls), "Supplied class must not be an action (%s is)", cls);
70         checkArgument(!Notification.class.isAssignableFrom(cls), "Supplied class must not be a notification (%s is)",
71             cls);
72         return (CompositeRuntimeType) getTypes().findSchema(JavaTypeName.create(cls)).orElse(null);
73     }
74
75     @Override
76     public final ActionRuntimeType getActionDefinition(final Class<? extends Action<?, ?, ?>> cls) {
77         return (ActionRuntimeType) getTypes().findSchema(JavaTypeName.create(cls)).orElse(null);
78     }
79
80     @Override
81     public final RpcRuntimeType getRpcDefinition(final Class<? extends Rpc<?, ?>> cls) {
82         return (RpcRuntimeType) getTypes().findSchema(JavaTypeName.create(cls)).orElse(null);
83     }
84
85     @Override
86     public final RuntimeType getTypeWithSchema(final Class<?> type) {
87         return getTypes().findSchema(JavaTypeName.create(type))
88             .orElseThrow(() -> new IllegalArgumentException("Failed to find schema for " + type));
89     }
90
91     @Override
92     public final Class<?> getClassForSchema(final Absolute schema) {
93         final var child = getTypes().schemaTreeChild(schema);
94         checkArgument(child != null, "Failed to find binding type for %s", schema);
95         return loadClass(child);
96     }
97
98     @Override
99     public final Class<? extends BaseIdentity> getIdentityClass(final QName input) {
100         try {
101             return identityClasses.get(requireNonNull(input));
102         } catch (ExecutionException e) {
103             Throwables.throwIfUnchecked(e.getCause());
104             throw new IllegalStateException("Unexpected error looking up " + input, e);
105         }
106     }
107
108     @Override
109     public final Class<? extends RpcInput> getRpcInput(final QName rpcName) {
110         return loadClass(getRpc(rpcName).input()).asSubclass(RpcInput.class);
111     }
112
113     @Override
114     public final Class<? extends RpcOutput> getRpcOutput(final QName rpcName) {
115         return loadClass(getRpc(rpcName).output()).asSubclass(RpcOutput.class);
116     }
117
118     private @NonNull RpcRuntimeType getRpc(final QName rpcName) {
119         if (getTypes().schemaTreeChild(rpcName) instanceof RpcRuntimeType rpc) {
120             return rpc;
121         }
122         throw new IllegalArgumentException("Failed to find RPC for " + rpcName);
123     }
124
125     @Override
126     @SuppressWarnings("unchecked")
127     public final Class<? extends YangData<?>> getYangDataClass(final YangDataName templateName) {
128         return (Class) loadClass(getTypes().findYangData(templateName)
129             .orElseThrow(() -> new IllegalArgumentException("Failed to find YangData for " + templateName)))
130             .asSubclass(YangData.class);
131     }
132
133     private Class<?> loadClass(final RuntimeType type) {
134         try {
135             return loadClass(type.javaType());
136         } catch (final ClassNotFoundException e) {
137             throw new IllegalStateException(e);
138         }
139     }
140 }