9b497fce4b34e00e4bceb72453e56e4e7c1b7504
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / MapCodecContext.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 java.lang.reflect.Method;
13 import java.util.List;
14 import java.util.Map;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
21 import org.opendaylight.yangtools.yang.binding.Key;
22 import org.opendaylight.yangtools.yang.binding.KeyAware;
23 import org.opendaylight.yangtools.yang.binding.contract.Naming;
24 import org.opendaylight.yangtools.yang.common.Ordering;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
28
29 abstract sealed class MapCodecContext<I extends Key<D>, D extends DataObject & KeyAware<I>>
30         extends ListCodecContext<D> {
31     private static final class Ordered<I extends Key<D>, D extends DataObject & KeyAware<I>>
32             extends MapCodecContext<I, D> {
33         Ordered(final MapCodecPrototype prototype, final Method keyMethod, final IdentifiableItemCodec codec) {
34             super(prototype, keyMethod, codec);
35         }
36     }
37
38     static final class Unordered<I extends Key<D>, D extends DataObject & KeyAware<I>>
39             extends MapCodecContext<I, D> {
40         private Unordered(final MapCodecPrototype prototype, final Method keyMethod,
41                 final IdentifiableItemCodec codec) {
42             super(prototype, keyMethod, codec);
43         }
44
45         @Override
46         Map<I, D> fromMap(final MapNode map, final int size) {
47             return LazyBindingMap.of(this, map, size);
48         }
49     }
50
51     private final IdentifiableItemCodec codec;
52
53     private MapCodecContext(final MapCodecPrototype prototype, final Method keyMethod,
54             final IdentifiableItemCodec codec) {
55         super(prototype, keyMethod);
56         this.codec = requireNonNull(codec);
57     }
58
59     static @NonNull MapCodecContext<?, ?>  of(final Class<? extends DataObject> cls, final ListRuntimeType type,
60             final CodecContextFactory factory) {
61         return of(new MapCodecPrototype(Item.of(cls), type, factory));
62     }
63
64     static @NonNull MapCodecContext<?, ?> of(final MapCodecPrototype prototype) {
65         final var bindingClass = prototype.javaClass();
66         final Method keyMethod;
67         try {
68             keyMethod = bindingClass.getMethod(Naming.KEY_AWARE_KEY_NAME);
69         } catch (NoSuchMethodException e) {
70             throw new IllegalStateException("Required method not available", e);
71         }
72
73         final var type = prototype.runtimeType();
74         final var codec = prototype.contextFactory().getPathArgumentCodec(bindingClass, type);
75
76         return type.statement().ordering() == Ordering.SYSTEM ? new Unordered<>(prototype, keyMethod, codec)
77             : new Ordered<>(prototype, keyMethod, codec);
78     }
79
80     @Override
81     void addYangPathArgument(final List<YangInstanceIdentifier.PathArgument> builder,
82             final InstanceIdentifier.PathArgument arg) {
83         /*
84          * DOM Instance Identifier for list is always represent by two entries one for map and one for children. This
85          * is also true for wildcarded instance identifiers
86          */
87         final var yangArg = getDomPathArgument();
88         builder.add(yangArg);
89
90         if (arg instanceof IdentifiableItem<?, ?> identifiable) {
91             builder.add(codec.bindingToDom(identifiable));
92         } else {
93             // Adding wildcarded
94             builder.add(yangArg);
95         }
96     }
97
98     @Override
99     protected final InstanceIdentifier.PathArgument getBindingPathArgument(
100             final YangInstanceIdentifier.PathArgument domArg) {
101         return domArg instanceof NodeIdentifierWithPredicates nip ? codec.domToBinding(nip)
102             : super.getBindingPathArgument(domArg);
103     }
104
105     @SuppressWarnings({ "rawtypes", "unchecked" })
106     final NodeIdentifierWithPredicates serialize(final Key<?> key) {
107         return codec.bindingToDom(IdentifiableItem.of((Class)getBindingClass(), (Key)key));
108     }
109
110     final @NonNull Key<?> deserialize(final @NonNull NodeIdentifierWithPredicates arg) {
111         return codec.deserializeIdentifier(arg);
112     }
113
114     @Override
115     public final YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
116         return arg instanceof IdentifiableItem<?, ?> identifiable ? codec.bindingToDom(identifiable)
117             : super.serializePathArgument(arg);
118     }
119
120     @Override
121     public final InstanceIdentifier.PathArgument deserializePathArgument(
122         final YangInstanceIdentifier.PathArgument arg) {
123         return arg instanceof NodeIdentifierWithPredicates nip ? codec.domToBinding(nip)
124             : super.deserializePathArgument(arg);
125     }
126 }