bc0eb020c9dc68ea6c89dcdfefa46fbeecaff8f2
[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.DataObjectStep;
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
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         DataObjectStep<?> 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                 ? InstanceIdentifier.createStep((Class) bindingClass, (Class) childClass)
32                     : super.createItem(childClass, childSchema);
33         }
34     }
35
36     private static final @NonNull CodecItemFactory DEFAULT = new CodecItemFactory();
37
38     private CodecItemFactory() {
39         // Hidden on purpose
40     }
41
42     // FIXME: MDSAL-697: move this method into BindingRuntimeContext
43     //        This method is only called from loadChildPrototype() and exists only to be overridden by
44     //        CaseNodeCodecContext. Since we are providing childClass and our schema to BindingRuntimeContext and
45     //        receiving childSchema from it via findChildSchemaDefinition, we should be able to receive the equivalent
46     //        of Map.Entry<Item, DataSchemaNode>, along with the override we create here. One more input we may need to
47     //        provide is our bindingClass().
48     DataObjectStep<?> createItem(final Class<?> childClass, final EffectiveStatement<?, ?> childSchema) {
49         return InstanceIdentifier.createStep((Class) 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 }