65394235dd76559feeb04737c84ac930ce0d81c9
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / AbstractDataObjectCodecContext.java
1 /*
2  * Copyright (c) 2023 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.codec.impl;
9
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.List;
13 import java.util.Map;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.mdsal.binding.dom.codec.api.IncorrectNestingException;
17 import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType;
18 import org.opendaylight.yangtools.yang.binding.Augmentation;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
24 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
25
26 /**
27  * Abstract base for {@link DataObjectCodecContext} and {@link AugmentationCodecContext}. They share most of their
28  * mechanics, but notably:
29  * <ol>
30  *   <li>DataObjectCodecContext has an exact DistinctNodeContainer and YangInstanceIdentifier mapping and can be the
31  *       target of augmentations (i.e. can implement Augmentable contract)</li>
32  *   <li>AugmentationNodeContext has neither of those traits and really is just a filter of its parent
33  *       DistinctNodeContainer</li>
34  * </ol>
35  *
36  * <p>
37  * Unfortunately {@code Augmentation} is a also a {@link DataObject}, so things get a bit messy.
38  *
39  * <p>
40  * While this class is public, it not part of API surface and is an implementation detail. The only reason for it being
41  * public is that it needs to be accessible by code generated at runtime.
42  */
43 public abstract sealed class AbstractDataObjectCodecContext<D extends DataObject, T extends CompositeRuntimeType>
44         extends CommonDataObjectCodecContext<D, T>
45         permits AugmentationCodecContext, DataObjectCodecContext {
46     private final ImmutableMap<Class<?>, DataContainerPrototype<?, ?>> byBindingArgClass;
47     private final ImmutableMap<Class<?>, DataContainerPrototype<?, ?>> byStreamClass;
48     private final ImmutableMap<NodeIdentifier, CodecContextSupplier> byYang;
49     private final ImmutableMap<String, ValueNodeCodecContext> leafChild;
50
51     AbstractDataObjectCodecContext(final CommonDataObjectCodecPrototype<T> prototype,
52             final DataContainerAnalysis<T> analysis) {
53         super(prototype);
54         byBindingArgClass = analysis.byBindingArgClass;
55         byStreamClass = analysis.byStreamClass;
56         byYang = analysis.byYang;
57         leafChild = analysis.leafNodes;
58     }
59
60     @Override
61     public final WithStatus getSchema() {
62         // FIXME: Bad cast, we should be returning an EffectiveStatement perhaps?
63         return (WithStatus) prototype().runtimeType().statement();
64     }
65
66     @Override
67     DataContainerPrototype<?, ?> streamChildPrototype(final Class<?> childClass) {
68         return byStreamClass.get(childClass);
69     }
70
71     @Override
72     public final CommonDataObjectCodecContext<?, ?> bindingPathArgumentChild(final InstanceIdentifier.PathArgument arg,
73             final List<PathArgument> builder) {
74         final var argType = arg.getType();
75         final var context = childNonNull(pathChildPrototype(argType), argType,
76             "Class %s is not valid child of %s", argType, getBindingClass())
77             .getCodecContext();
78         context.addYangPathArgument(arg, builder);
79         if (context instanceof CommonDataObjectCodecContext<?, ?> dataObject) {
80             return dataObject;
81         } else if (context instanceof ChoiceCodecContext<?> choice) {
82             return choice.bindingPathArgumentChild(arg, builder);
83         } else {
84             throw new IllegalStateException("Unhandled context " + context);
85         }
86     }
87
88     @Nullable DataContainerPrototype<?, ?> pathChildPrototype(final @NonNull Class<? extends DataObject> argType) {
89         return byBindingArgClass.get(argType);
90     }
91
92     @Override
93     CodecContextSupplier yangChildSupplier(final NodeIdentifier arg) {
94         return byYang.get(arg);
95     }
96
97     final ValueNodeCodecContext getLeafChild(final String name) {
98         final ValueNodeCodecContext value = leafChild.get(name);
99         if (value == null) {
100             throw new IncorrectNestingException("Leaf %s is not valid for %s", name, getBindingClass());
101         }
102         return value;
103     }
104
105     final @NonNull ImmutableSet<NodeIdentifier> byYangKeySet() {
106         return byYang.keySet();
107     }
108
109     abstract @NonNull Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAllAugmentationsFrom(
110         DataContainerNode data);
111 }