d0e2f04db38233ca2e7b478cf789fffd46db72ab
[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<?>, CommonDataObjectCodecPrototype<?>> byBindingArgClass;
47     private final ImmutableMap<Class<?>, CommonDataObjectCodecPrototype<?>> 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     CommonDataObjectCodecPrototype<?> 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         if (context instanceof ChoiceCodecContext<?> choice) {
79             choice.addYangPathArgument(arg, builder);
80
81             final var caseType = arg.getCaseType();
82             final var type = arg.getType();
83             final DataContainerCodecContext<?, ?, ?> caze;
84             if (caseType.isPresent()) {
85                 // Non-ambiguous addressing this should not pose any problems
86                 caze = choice.getStreamChild(caseType.orElseThrow());
87             } else {
88                 caze = choice.getCaseByChildClass(type);
89             }
90
91             caze.addYangPathArgument(arg, builder);
92             return caze.bindingPathArgumentChild(arg, builder);
93         }
94         context.addYangPathArgument(arg, builder);
95         return context;
96     }
97
98     @Nullable CommonDataObjectCodecPrototype<?> pathChildPrototype(final @NonNull Class<? extends DataObject> argType) {
99         return byBindingArgClass.get(argType);
100     }
101
102     @Override
103     CodecContextSupplier yangChildSupplier(final NodeIdentifier arg) {
104         return byYang.get(arg);
105     }
106
107     final ValueNodeCodecContext getLeafChild(final String name) {
108         final ValueNodeCodecContext value = leafChild.get(name);
109         if (value == null) {
110             throw new IncorrectNestingException("Leaf %s is not valid for %s", name, getBindingClass());
111         }
112         return value;
113     }
114
115     final @NonNull ImmutableSet<NodeIdentifier> byYangKeySet() {
116         return byYang.keySet();
117     }
118
119     abstract @NonNull Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAllAugmentationsFrom(
120         DataContainerNode data);
121 }