2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.dom.codec.impl;
10 import com.google.common.base.Throwables;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableSet;
13 import java.lang.invoke.MethodHandle;
14 import java.util.List;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.mdsal.binding.dom.codec.api.IncorrectNestingException;
19 import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType;
20 import org.opendaylight.yangtools.yang.binding.Augmentation;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
27 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
30 * Abstract base for {@link DataObjectCodecContext} and {@link AugmentationCodecContext}. They share most of their
31 * mechanics, but notably:
33 * <li>DataObjectCodecContext has an exact DistinctNodeContainer and YangInstanceIdentifier mapping and can be the
34 * target of augmentations (i.e. can implement Augmentable contract)</li>
35 * <li>AugmentationNodeContext has neither of those traits and really is just a filter of its parent
36 * DistinctNodeContainer</li>
40 * Unfortunately {@code Augmentation} is a also a {@link DataObject}, so things get a bit messy.
43 * While this class is public, it not part of API surface and is an implementation detail. The only reason for it being
44 * public is that it needs to be accessible by code generated at runtime.
46 public abstract sealed class AbstractDataObjectCodecContext<D extends DataObject, T extends CompositeRuntimeType>
47 extends CommonDataObjectCodecContext<D, T>
48 permits AugmentationCodecContext, DataObjectCodecContext {
49 private final ImmutableMap<Class<?>, CommonDataObjectCodecPrototype<?>> byBindingArgClass;
50 private final ImmutableMap<Class<?>, CommonDataObjectCodecPrototype<?>> byStreamClass;
51 private final ImmutableMap<NodeIdentifier, CodecContextSupplier> byYang;
52 private final ImmutableMap<String, ValueNodeCodecContext> leafChild;
53 private final MethodHandle proxyConstructor;
55 AbstractDataObjectCodecContext(final CommonDataObjectCodecPrototype<T> prototype,
56 final CodecDataObjectAnalysis<T> analysis) {
58 byBindingArgClass = analysis.byBindingArgClass;
59 byStreamClass = analysis.byStreamClass;
60 byYang = analysis.byYang;
61 leafChild = analysis.leafNodes;
62 proxyConstructor = analysis.proxyConstructor;
66 public final WithStatus getSchema() {
67 // FIXME: Bad cast, we should be returning an EffectiveStatement perhaps?
68 return (WithStatus) type().statement();
71 @SuppressWarnings("unchecked")
73 public final <C extends DataObject> CommonDataObjectCodecContext<C, ?> streamChild(final Class<C> childClass) {
74 final var childProto = streamChildPrototype(childClass);
75 return childProto == null ? null : (CommonDataObjectCodecContext<C, ?>) childProto.get();
78 @Nullable CommonDataObjectCodecPrototype<?> streamChildPrototype(final @NonNull Class<?> childClass) {
79 return byStreamClass.get(childClass);
83 public final CommonDataObjectCodecContext<?, ?> bindingPathArgumentChild(final InstanceIdentifier.PathArgument arg,
84 final List<PathArgument> builder) {
85 final var argType = arg.getType();
86 final var context = childNonNull(pathChildPrototype(argType), argType,
87 "Class %s is not valid child of %s", argType, getBindingClass())
89 if (context instanceof ChoiceCodecContext<?> choice) {
90 choice.addYangPathArgument(arg, builder);
92 final var caseType = arg.getCaseType();
93 final var type = arg.getType();
94 final DataContainerCodecContext<?, ?> caze;
95 if (caseType.isPresent()) {
96 // Non-ambiguous addressing this should not pose any problems
97 caze = choice.getStreamChild(caseType.orElseThrow());
99 caze = choice.getCaseByChildClass(type);
102 caze.addYangPathArgument(arg, builder);
103 return caze.bindingPathArgumentChild(arg, builder);
105 context.addYangPathArgument(arg, builder);
109 @Nullable CommonDataObjectCodecPrototype<?> pathChildPrototype(final @NonNull Class<? extends DataObject> argType) {
110 return byBindingArgClass.get(argType);
114 public final CodecContext yangPathArgumentChild(final PathArgument arg) {
115 CodecContextSupplier supplier;
116 if (arg instanceof NodeIdentifier nodeId) {
117 supplier = yangChildSupplier(nodeId);
118 } else if (arg instanceof NodeIdentifierWithPredicates nip) {
119 supplier = yangChildSupplier(new NodeIdentifier(nip.getNodeType()));
123 return childNonNull(supplier, arg, "Argument %s is not valid child of %s", arg, getSchema()).get();
126 @Nullable CodecContextSupplier yangChildSupplier(final @NonNull NodeIdentifier arg) {
127 return byYang.get(arg);
130 @SuppressWarnings("checkstyle:illegalCatch")
131 final @NonNull D createBindingProxy(final DataContainerNode node) {
133 return (D) proxyConstructor.invokeExact(this, node);
134 } catch (final Throwable e) {
135 Throwables.throwIfUnchecked(e);
136 throw new IllegalStateException(e);
140 final ValueNodeCodecContext getLeafChild(final String name) {
141 final ValueNodeCodecContext value = leafChild.get(name);
143 throw new IncorrectNestingException("Leaf %s is not valid for %s", name, getBindingClass());
148 final @NonNull ImmutableSet<NodeIdentifier> byYangKeySet() {
149 return byYang.keySet();
152 abstract @NonNull Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAllAugmentationsFrom(
153 DataContainerNode data);