10a3248a1c29f2e2d6426bbfdb98fe6c15e34ef8
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / CodecItemFactory.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 static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.yang.binding.DataObject;
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
15 import org.opendaylight.yangtools.yang.model.api.AddedByUsesAware;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17
18 sealed class CodecItemFactory {
19     private static final class Case extends CodecItemFactory {
20         private final Class<?> bindingClass;
21
22         Case(final Class<?> bindingClass) {
23             this.bindingClass = requireNonNull(bindingClass);
24         }
25
26         @Override
27         @SuppressWarnings({ "rawtypes", "unchecked" })
28         Item<?> createItem(final Class<?> childClass, final EffectiveStatement<?, ?> childSchema) {
29             // FIXME: MDSAL-697: see overridden method for further guidance
30             return childSchema instanceof AddedByUsesAware aware && aware.isAddedByUses()
31                 ? Item.of((Class) bindingClass, (Class) childClass) : super.createItem(childClass, childSchema);
32         }
33     }
34
35     private static final @NonNull CodecItemFactory DEFAULT = new CodecItemFactory();
36
37     private CodecItemFactory() {
38         // Hidden on purpose
39     }
40
41     // FIXME: MDSAL-697: move this method into BindingRuntimeContext
42     //        This method is only called from loadChildPrototype() and exists only to be overridden by
43     //        CaseNodeCodecContext. Since we are providing childClass and our schema to BindingRuntimeContext and
44     //        receiving childSchema from it via findChildSchemaDefinition, we should be able to receive the equivalent
45     //        of Map.Entry<Item, DataSchemaNode>, along with the override we create here. One more input we may need to
46     //        provide is our bindingClass().
47     @SuppressWarnings("unchecked")
48     Item<?> createItem(final Class<?> childClass, final EffectiveStatement<?, ?> childSchema) {
49         return Item.of((Class<? extends DataObject>) childClass);
50     }
51
52     static @NonNull CodecItemFactory of() {
53         return DEFAULT;
54     }
55
56     static @NonNull CodecItemFactory of(final Class<?> bindingClass) {
57         return new Case(bindingClass);
58     }
59 }