Add support for registering single-RPC services
[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.common.QName;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
30
31 /**
32  * Runtime Context for Java YANG Binding classes. It provides information derived from the backing effective model,
33  * which is not captured in generated classes (and hence cannot be obtained from {@code BindingReflections}.
34  */
35 @Beta
36 public abstract class AbstractBindingRuntimeContext implements BindingRuntimeContext {
37     private final LoadingCache<@NonNull QName, @NonNull Class<? extends BaseIdentity>> identityClasses =
38         CacheBuilder.newBuilder().weakValues().build(new CacheLoader<>() {
39             @Override
40             public Class<? extends BaseIdentity> load(final QName key) {
41                 final var type = getTypes().findIdentity(key).orElseThrow(
42                     () -> new IllegalArgumentException("Supplied QName " + key + " is not a valid identity"));
43                 try {
44                     return loadClass(type.getIdentifier()).asSubclass(BaseIdentity.class);
45                 } catch (ClassNotFoundException e) {
46                     throw new IllegalArgumentException("Required class " + type + " was not found.", e);
47                 } catch (ClassCastException e) {
48                     throw new IllegalArgumentException(key + " resolves to a non-identity class", e);
49                 }
50             }
51         });
52
53     @Override
54     public final <T extends Augmentation<?>> AugmentRuntimeType getAugmentationDefinition(final Class<T> augClass) {
55         return getTypes().findSchema(JavaTypeName.create(augClass))
56             .filter(AugmentRuntimeType.class::isInstance)
57             .map(AugmentRuntimeType.class::cast)
58             .orElse(null);
59     }
60
61     @Override
62     public final CompositeRuntimeType getSchemaDefinition(final Class<?> cls) {
63         checkArgument(!Augmentation.class.isAssignableFrom(cls), "Supplied class must not be an augmentation (%s is)",
64             cls);
65         checkArgument(!Action.class.isAssignableFrom(cls), "Supplied class must not be an action (%s is)", cls);
66         checkArgument(!Notification.class.isAssignableFrom(cls), "Supplied class must not be a notification (%s is)",
67             cls);
68         return (CompositeRuntimeType) getTypes().findSchema(JavaTypeName.create(cls)).orElse(null);
69     }
70
71     @Override
72     public final ActionRuntimeType getActionDefinition(final Class<? extends Action<?, ?, ?>> cls) {
73         return (ActionRuntimeType) getTypes().findSchema(JavaTypeName.create(cls)).orElse(null);
74     }
75
76     @Override
77     public final RpcRuntimeType getRpcDefinition(final Class<? extends Rpc<?, ?>> cls) {
78         return (RpcRuntimeType) getTypes().findSchema(JavaTypeName.create(cls)).orElse(null);
79     }
80
81     @Override
82     public final RuntimeType getTypeWithSchema(final Class<?> type) {
83         return getTypes().findSchema(JavaTypeName.create(type))
84             .orElseThrow(() -> new IllegalArgumentException("Failed to find schema for " + type));
85     }
86
87     @Override
88     public final Class<?> getClassForSchema(final Absolute schema) {
89         final var child = getTypes().schemaTreeChild(schema);
90         checkArgument(child != null, "Failed to find binding type for %s", schema);
91         return loadClass(child);
92     }
93
94     @Override
95     public final Class<? extends BaseIdentity> getIdentityClass(final QName input) {
96         try {
97             return identityClasses.get(requireNonNull(input));
98         } catch (ExecutionException e) {
99             Throwables.throwIfUnchecked(e.getCause());
100             throw new IllegalStateException("Unexpected error looking up " + input, e);
101         }
102     }
103
104     @Override
105     public final Class<? extends RpcInput> getRpcInput(final QName rpcName) {
106         return loadClass(getTypes().findRpcInput(rpcName)
107             .orElseThrow(() -> new IllegalArgumentException("Failed to find RpcInput for " + rpcName)))
108             .asSubclass(RpcInput.class);
109     }
110
111     @Override
112     public final Class<? extends RpcOutput> getRpcOutput(final QName rpcName) {
113         return loadClass(getTypes().findRpcOutput(rpcName)
114             .orElseThrow(() -> new IllegalArgumentException("Failed to find RpcOutput for " + rpcName)))
115             .asSubclass(RpcOutput.class);
116     }
117
118     private Class<?> loadClass(final RuntimeType type) {
119         try {
120             return loadClass(type.javaType());
121         } catch (final ClassNotFoundException e) {
122             throw new IllegalStateException(e);
123         }
124     }
125 }