e446c7bdf28a1b717c6680664dfcb4c56b94c5b2
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / DataContainerCodecPrototype.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 com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.VarHandle;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.dom.codec.api.CommonDataObjectCodecTreeNode.ChildAddressabilitySummary;
17 import org.opendaylight.mdsal.binding.dom.codec.impl.NodeCodecContext.CodecContextFactory;
18 import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType;
19 import org.opendaylight.mdsal.binding.runtime.api.NotificationRuntimeType;
20 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
21 import org.opendaylight.mdsal.binding.runtime.api.RuntimeTypeContainer;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 abstract sealed class DataContainerCodecPrototype<T extends RuntimeTypeContainer> implements NodeContextSupplier
40         permits AugmentationCodecPrototype, DataObjectCodecPrototype {
41     private static final Logger LOG = LoggerFactory.getLogger(DataContainerCodecPrototype.class);
42
43     private static final VarHandle INSTANCE;
44
45     static {
46         try {
47             INSTANCE = MethodHandles.lookup().findVarHandle(DataContainerCodecPrototype.class,
48                 "instance", DataContainerCodecContext.class);
49         } catch (NoSuchFieldException | IllegalAccessException e) {
50             throw new ExceptionInInitializerError(e);
51         }
52     }
53
54     private final @NonNull T type;
55     private final @NonNull QNameModule namespace;
56     private final @NonNull CodecContextFactory factory;
57     private final @NonNull Item<?> bindingArg;
58     private final @NonNull ChildAddressabilitySummary childAddressabilitySummary;
59
60     // multiple paths represent augmentation wrapper
61     // FIXME: this means it is either this or 'childArgs'
62
63     // Accessed via INSTANCE
64     @SuppressWarnings("unused")
65     private volatile DataContainerCodecContext<?, T> instance;
66
67     DataContainerCodecPrototype(final Item<?> bindingArg, final QNameModule namespace, final T type,
68             final CodecContextFactory factory) {
69         this.bindingArg = requireNonNull(bindingArg);
70         this.namespace = requireNonNull(namespace);
71         this.type = requireNonNull(type);
72         this.factory = requireNonNull(factory);
73
74         childAddressabilitySummary = type instanceof RuntimeType runtimeType
75             ? computeChildAddressabilitySummary(runtimeType.statement())
76                 // BindingRuntimeTypes, does not matter
77                 : ChildAddressabilitySummary.MIXED;
78     }
79
80     private static @NonNull ChildAddressabilitySummary computeChildAddressabilitySummary(final Object nodeSchema) {
81         // FIXME: rework this to work on EffectiveStatements
82         if (nodeSchema instanceof DataNodeContainer contaner) {
83             boolean haveAddressable = false;
84             boolean haveUnaddressable = false;
85             for (DataSchemaNode child : contaner.getChildNodes()) {
86                 if (child instanceof ContainerSchemaNode || child instanceof AugmentationSchemaNode) {
87                     haveAddressable = true;
88                 } else if (child instanceof ListSchemaNode list) {
89                     if (list.getKeyDefinition().isEmpty()) {
90                         haveUnaddressable = true;
91                     } else {
92                         haveAddressable = true;
93                     }
94                 } else if (child instanceof AnydataSchemaNode || child instanceof AnyxmlSchemaNode
95                         || child instanceof TypedDataSchemaNode) {
96                     haveUnaddressable = true;
97                 } else if (child instanceof ChoiceSchemaNode choice) {
98                     switch (computeChildAddressabilitySummary(choice)) {
99                         case ADDRESSABLE -> haveAddressable = true;
100                         case UNADDRESSABLE -> haveUnaddressable = true;
101                         case MIXED -> {
102                             haveAddressable = true;
103                             haveUnaddressable = true;
104                         }
105                         default -> throw new IllegalStateException("Unhandled accessibility summary for " + child);
106                     }
107                 } else {
108                     LOG.warn("Unhandled child node {}", child);
109                 }
110             }
111
112             if (!haveAddressable) {
113                 // Empty or all are unaddressable
114                 return ChildAddressabilitySummary.UNADDRESSABLE;
115             }
116
117             return haveUnaddressable ? ChildAddressabilitySummary.MIXED : ChildAddressabilitySummary.ADDRESSABLE;
118         } else if (nodeSchema instanceof ChoiceSchemaNode choice) {
119             return computeChildAddressabilitySummary(choice);
120         }
121
122         // No child nodes possible: return unaddressable
123         return ChildAddressabilitySummary.UNADDRESSABLE;
124     }
125
126     private static @NonNull ChildAddressabilitySummary computeChildAddressabilitySummary(
127             final ChoiceSchemaNode choice) {
128         boolean haveAddressable = false;
129         boolean haveUnaddressable = false;
130         for (CaseSchemaNode child : choice.getCases()) {
131             switch (computeChildAddressabilitySummary(child)) {
132                 case ADDRESSABLE:
133                     haveAddressable = true;
134                     break;
135                 case UNADDRESSABLE:
136                     haveUnaddressable = true;
137                     break;
138                 case MIXED:
139                     // A child is mixed, which means we are mixed, too
140                     return ChildAddressabilitySummary.MIXED;
141                 default:
142                     throw new IllegalStateException("Unhandled accessibility summary for " + child);
143             }
144         }
145
146         if (!haveAddressable) {
147             // Empty or all are unaddressable
148             return ChildAddressabilitySummary.UNADDRESSABLE;
149         }
150
151         return haveUnaddressable ? ChildAddressabilitySummary.MIXED : ChildAddressabilitySummary.ADDRESSABLE;
152     }
153
154     static <T extends CompositeRuntimeType> DataContainerCodecPrototype<T> from(final Class<?> cls, final T type,
155             final CodecContextFactory factory) {
156         return new DataObjectCodecPrototype<>(cls, createIdentifier(type), type, factory);
157     }
158
159     static <T extends CompositeRuntimeType> DataContainerCodecPrototype<T> from(final Item<?> bindingArg, final T type,
160             final CodecContextFactory factory) {
161         return new DataObjectCodecPrototype<>(bindingArg, createIdentifier(type), type, factory);
162     }
163
164     static DataContainerCodecPrototype<NotificationRuntimeType> from(final Class<?> augClass,
165             final NotificationRuntimeType schema, final CodecContextFactory factory) {
166         return new DataObjectCodecPrototype<>(augClass, NodeIdentifier.create(schema.statement().argument()), schema,
167             factory);
168     }
169
170     private static @NonNull NodeIdentifier createIdentifier(final CompositeRuntimeType type) {
171         final Object arg = type.statement().argument();
172         verify(arg instanceof QName, "Unexpected type %s argument %s", type, arg);
173         return NodeIdentifier.create((QName) arg);
174     }
175
176     final @NonNull T getType() {
177         return type;
178     }
179
180     final @NonNull ChildAddressabilitySummary getChildAddressabilitySummary() {
181         return childAddressabilitySummary;
182     }
183
184     final @NonNull QNameModule getNamespace() {
185         return namespace;
186     }
187
188     final @NonNull CodecContextFactory getFactory() {
189         return factory;
190     }
191
192     final @NonNull Class<?> getBindingClass() {
193         return bindingArg.getType();
194     }
195
196     final @NonNull Item<?> getBindingArg() {
197         return bindingArg;
198     }
199
200     abstract @NonNull NodeIdentifier getYangArg();
201
202     @Override
203     public final DataContainerCodecContext<?, T> get() {
204         final var existing = (DataContainerCodecContext<?, T>) INSTANCE.getAcquire(this);
205         return existing != null ? existing : loadInstance();
206     }
207
208     @SuppressWarnings("unchecked")
209     final <R extends CompositeRuntimeType> DataObjectCodecContext<?, R> getDataObject() {
210         final var context = get();
211         verify(context instanceof DataObjectCodecContext, "Unexpected instance %s", context);
212         return (DataObjectCodecContext<?, R>) context;
213     }
214
215     private @NonNull DataContainerCodecContext<?, T> loadInstance() {
216         final var tmp = createInstance();
217         final var witness = (DataContainerCodecContext<?, T>) INSTANCE.compareAndExchangeRelease(this, null, tmp);
218         return witness == null ? tmp : witness;
219     }
220
221     // This method must allow concurrent loading, i.e. nothing in it may have effects outside of the loaded object
222     abstract @NonNull DataContainerCodecContext<?, T> createInstance();
223 }