Unify {Action,Notification}Spec lookup
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / CurrentAdapterSerializer.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.dom.adapter;
9
10 import static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.annotations.Beta;
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.base.VerifyException;
17 import com.google.common.cache.CacheBuilder;
18 import com.google.common.cache.CacheLoader;
19 import com.google.common.cache.LoadingCache;
20 import java.util.Collection;
21 import java.util.Map;
22 import java.util.Map.Entry;
23 import java.util.concurrent.ConcurrentHashMap;
24 import java.util.concurrent.ConcurrentMap;
25 import java.util.stream.Collectors;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.opendaylight.mdsal.binding.api.ActionSpec;
29 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
30 import org.opendaylight.mdsal.binding.api.InstanceNotificationSpec;
31 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
32 import org.opendaylight.mdsal.binding.dom.codec.spi.ForwardingBindingDOMCodecServices;
33 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
34 import org.opendaylight.mdsal.binding.runtime.api.ActionRuntimeType;
35 import org.opendaylight.mdsal.binding.runtime.api.InputRuntimeType;
36 import org.opendaylight.mdsal.binding.runtime.api.NotificationRuntimeType;
37 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
38 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
39 import org.opendaylight.yangtools.yang.binding.BindingContract;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.QNameModule;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
45 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
46 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
47 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
48 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
49 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
50 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 @Beta
55 @VisibleForTesting
56 public final class CurrentAdapterSerializer extends ForwardingBindingDOMCodecServices {
57     private static final Logger LOG = LoggerFactory.getLogger(CurrentAdapterSerializer.class);
58
59     private final LoadingCache<InstanceIdentifier<?>, YangInstanceIdentifier> cache = CacheBuilder.newBuilder()
60             .softValues().build(new CacheLoader<InstanceIdentifier<?>, YangInstanceIdentifier>() {
61                 @Override
62                 public YangInstanceIdentifier load(final InstanceIdentifier<?> key) {
63                     return toYangInstanceIdentifier(key);
64                 }
65             });
66
67     private final ConcurrentMap<JavaTypeName, ContextReferenceExtractor> extractors = new ConcurrentHashMap<>();
68     private final @NonNull BindingDOMCodecServices delegate;
69
70     public CurrentAdapterSerializer(final BindingDOMCodecServices delegate) {
71         this.delegate = requireNonNull(delegate);
72     }
73
74     @Override
75     protected BindingDOMCodecServices delegate() {
76         return delegate;
77     }
78
79     @NonNull YangInstanceIdentifier toCachedYangInstanceIdentifier(final @NonNull InstanceIdentifier<?> path) {
80         return cache.getUnchecked(path);
81     }
82
83     <T extends DataObject> @NonNull InstanceIdentifier<T> coerceInstanceIdentifier(final YangInstanceIdentifier dom) {
84         return verifyNotNull(fromYangInstanceIdentifier(dom));
85     }
86
87     DOMDataTreeIdentifier toDOMDataTreeIdentifier(final DataTreeIdentifier<?> path) {
88         return DOMDataTreeIdentifier.of(path.datastore(), toYangInstanceIdentifier(path.path()));
89     }
90
91     Collection<DOMDataTreeIdentifier> toDOMDataTreeIdentifiers(final Collection<DataTreeIdentifier<?>> subtrees) {
92         return subtrees.stream().map(this::toDOMDataTreeIdentifier).collect(Collectors.toSet());
93     }
94
95     @NonNull Absolute getActionPath(final @NonNull ActionSpec<?, ?> spec) {
96         return getSchemaNodeIdentifier(spec.path(), spec.type(), ActionRuntimeType.class,
97             ActionEffectiveStatement.class);
98     }
99
100     @NonNull Absolute getNotificationPath(final @NonNull InstanceNotificationSpec<?, ?> spec) {
101         return getSchemaNodeIdentifier(spec.path(), spec.type(), NotificationRuntimeType.class,
102             NotificationEffectiveStatement.class);
103     }
104
105     private <T extends RuntimeType> @NonNull Absolute getSchemaNodeIdentifier(final @NonNull InstanceIdentifier<?> path,
106             final @NonNull Class<? extends BindingContract<?>> type, final @NonNull Class<T> expectedRuntime,
107             final @NonNull Class<? extends SchemaTreeEffectiveStatement<?>> expectedStatement) {
108         final var typeName = JavaTypeName.create(type);
109         final var runtimeType = getRuntimeContext().getTypes().findSchema(typeName)
110             .orElseThrow(() -> new IllegalArgumentException(typeName + " is not known"));
111         final T casted;
112         try {
113             casted = expectedRuntime.cast(runtimeType);
114         } catch (ClassCastException e) {
115             throw new IllegalArgumentException(typeName + " resolved to unexpected " + runtimeType, e);
116         }
117         final var qname = expectedStatement.cast(casted.statement()).argument();
118
119         final var entry = resolvePath(path);
120         final var stack = entry.getKey();
121         final var stmt = stack.enterSchemaTree(qname.bindTo(entry.getValue()));
122         if (expectedStatement.isInstance(stmt)) {
123             return stack.toSchemaNodeIdentifier();
124         }
125         throw new VerifyException(path + " child " + typeName + " resolved to unexpected statement" + stmt);
126     }
127
128     @Nullable ContextReferenceExtractor findExtractor(final @NonNull InputRuntimeType inputType) {
129         final var inputName = inputType.getIdentifier();
130         final var cached = extractors.get(inputName);
131         if (cached != null) {
132             return cached;
133         }
134
135         // Load the class
136         final Class<?> inputClass;
137         try {
138             inputClass = getRuntimeContext().loadClass(inputName);
139         } catch (ClassNotFoundException e) {
140             throw new IllegalArgumentException("Failed to load class for " + inputType, e);
141         }
142
143         // Check if there is an extractor at all
144         final var created = ContextReferenceExtractor.of(inputClass);
145         if (created == null) {
146             return null;
147         }
148
149         // Reconcile with cache
150         final var raced = extractors.putIfAbsent(inputName, created);
151         return raced != null ? raced : created;
152     }
153
154     private @NonNull Entry<SchemaInferenceStack, QNameModule> resolvePath(final @NonNull InstanceIdentifier<?> path) {
155         final var stack = SchemaInferenceStack.of(getRuntimeContext().getEffectiveModelContext());
156         final var it = toYangInstanceIdentifier(path).getPathArguments().iterator();
157         verify(it.hasNext(), "Unexpected empty instance identifier for %s", path);
158
159         QNameModule lastNamespace;
160         do {
161             final var arg = it.next();
162             final var qname = arg.getNodeType();
163             final var stmt = stack.enterDataTree(qname);
164             lastNamespace = qname.getModule();
165             if (stmt instanceof ListEffectiveStatement) {
166                 // Lists have two steps
167                 verify(it.hasNext(), "Unexpected list termination at %s in %s", stmt, path);
168                 // Verify just to make sure we are doing the right thing
169                 final var skipped = it.next();
170                 verify(skipped instanceof NodeIdentifier, "Unexpected skipped list entry item %s in %s", skipped, path);
171                 verify(stmt.argument().equals(skipped.getNodeType()), "Mismatched list entry item %s in %s", skipped,
172                     path);
173             }
174         } while (it.hasNext());
175
176         return Map.entry(stack, lastNamespace);
177     }
178 }