2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
10 import com.google.common.collect.Iterables;
11 import java.lang.invoke.MethodHandles;
12 import java.lang.invoke.VarHandle;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode.ChildAddressabilitySummary;
15 import org.opendaylight.mdsal.binding.dom.codec.impl.NodeCodecContext.CodecContextFactory;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.DataRoot;
18 import org.opendaylight.yangtools.yang.binding.Identifiable;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
24 import org.opendaylight.yangtools.yang.model.api.AnydataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.AnyxmlSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
33 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.TypedDataSchemaNode;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 final class DataContainerCodecPrototype<T extends WithStatus> implements NodeContextSupplier {
41 private static final Logger LOG = LoggerFactory.getLogger(DataContainerCodecPrototype.class);
43 private static final VarHandle INSTANCE;
47 INSTANCE = MethodHandles.lookup().findVarHandle(DataContainerCodecPrototype.class,
48 "instance", DataContainerCodecContext.class);
49 } catch (NoSuchFieldException | IllegalAccessException e) {
50 throw new ExceptionInInitializerError(e);
54 private final T schema;
55 private final QNameModule namespace;
56 private final CodecContextFactory factory;
57 private final Item<?> bindingArg;
58 private final PathArgument yangArg;
59 private final ChildAddressabilitySummary childAddressabilitySummary;
61 // Accessed via INSTANCE
62 @SuppressWarnings("unused")
63 private volatile DataContainerCodecContext<?, T> instance;
65 @SuppressWarnings("unchecked")
66 private DataContainerCodecPrototype(final Class<?> cls, final PathArgument arg, final T nodeSchema,
67 final CodecContextFactory factory) {
68 this(Item.of((Class<? extends DataObject>) cls), arg, nodeSchema, factory);
71 private DataContainerCodecPrototype(final Item<?> bindingArg, final PathArgument arg, final T nodeSchema,
72 final CodecContextFactory factory) {
73 this.bindingArg = bindingArg;
75 this.schema = nodeSchema;
76 this.factory = factory;
78 if (arg instanceof AugmentationIdentifier) {
79 this.namespace = Iterables.getFirst(((AugmentationIdentifier) arg).getPossibleChildNames(), null)
82 this.namespace = arg.getNodeType().getModule();
85 this.childAddressabilitySummary = computeChildAddressabilitySummary(nodeSchema);
88 private static ChildAddressabilitySummary computeChildAddressabilitySummary(final WithStatus nodeSchema) {
89 if (nodeSchema instanceof DataNodeContainer) {
90 boolean haveAddressable = false;
91 boolean haveUnaddressable = false;
92 for (DataSchemaNode child : ((DataNodeContainer) nodeSchema).getChildNodes()) {
93 if (child instanceof ContainerSchemaNode || child instanceof AugmentationSchemaNode) {
94 haveAddressable = true;
95 } else if (child instanceof ListSchemaNode) {
96 if (((ListSchemaNode) child).getKeyDefinition().isEmpty()) {
97 haveUnaddressable = true;
99 haveAddressable = true;
101 } else if (child instanceof AnydataSchemaNode || child instanceof AnyxmlSchemaNode
102 || child instanceof TypedDataSchemaNode) {
103 haveUnaddressable = true;
104 } else if (child instanceof ChoiceSchemaNode) {
105 switch (computeChildAddressabilitySummary(child)) {
107 haveAddressable = true;
110 haveAddressable = true;
111 haveUnaddressable = true;
114 haveUnaddressable = true;
117 throw new IllegalStateException("Unhandled accessibility summary for " + child);
120 LOG.warn("Unhandled child node {}", child);
124 if (!haveAddressable) {
125 // Empty or all are unaddressable
126 return ChildAddressabilitySummary.UNADDRESSABLE;
129 return haveUnaddressable ? ChildAddressabilitySummary.MIXED : ChildAddressabilitySummary.ADDRESSABLE;
130 } else if (nodeSchema instanceof ChoiceSchemaNode) {
131 boolean haveAddressable = false;
132 boolean haveUnaddressable = false;
133 for (CaseSchemaNode child : ((ChoiceSchemaNode) nodeSchema).getCases()) {
134 switch (computeChildAddressabilitySummary(child)) {
136 haveAddressable = true;
139 haveUnaddressable = true;
142 // A child is mixed, which means we are mixed, too
143 return ChildAddressabilitySummary.MIXED;
145 throw new IllegalStateException("Unhandled accessibility summary for " + child);
149 if (!haveAddressable) {
150 // Empty or all are unaddressable
151 return ChildAddressabilitySummary.UNADDRESSABLE;
154 return haveUnaddressable ? ChildAddressabilitySummary.MIXED : ChildAddressabilitySummary.ADDRESSABLE;
157 // No child nodes possible: return unaddressable
158 return ChildAddressabilitySummary.UNADDRESSABLE;
161 static DataContainerCodecPrototype<SchemaContext> rootPrototype(final CodecContextFactory factory) {
162 final SchemaContext schema = factory.getRuntimeContext().getEffectiveModelContext();
163 final NodeIdentifier arg = NodeIdentifier.create(schema.getQName());
164 return new DataContainerCodecPrototype<>(DataRoot.class, arg, schema, factory);
167 static <T extends DataSchemaNode> DataContainerCodecPrototype<T> from(final Class<?> cls, final T schema,
168 final CodecContextFactory factory) {
169 return new DataContainerCodecPrototype<>(cls, NodeIdentifier.create(schema.getQName()), schema, factory);
172 static <T extends DataSchemaNode> DataContainerCodecPrototype<T> from(final Item<?> bindingArg, final T schema,
173 final CodecContextFactory factory) {
174 return new DataContainerCodecPrototype<>(bindingArg, NodeIdentifier.create(schema.getQName()), schema, factory);
177 static DataContainerCodecPrototype<AugmentationSchemaNode> from(final Class<?> augClass,
178 final AugmentationIdentifier arg, final AugmentationSchemaNode schema, final CodecContextFactory factory) {
179 return new DataContainerCodecPrototype<>(augClass, arg, schema, factory);
182 static DataContainerCodecPrototype<NotificationDefinition> from(final Class<?> augClass,
183 final NotificationDefinition schema, final CodecContextFactory factory) {
184 final PathArgument arg = NodeIdentifier.create(schema.getQName());
185 return new DataContainerCodecPrototype<>(augClass,arg, schema, factory);
192 ChildAddressabilitySummary getChildAddressabilitySummary() {
193 return childAddressabilitySummary;
196 QNameModule getNamespace() {
200 CodecContextFactory getFactory() {
204 Class<?> getBindingClass() {
205 return bindingArg.getType();
208 Item<?> getBindingArg() {
212 PathArgument getYangArg() {
217 public DataContainerCodecContext<?, T> get() {
218 final DataContainerCodecContext<?, T> existing = (DataContainerCodecContext<?, T>) INSTANCE.getAcquire(this);
219 return existing != null ? existing : loadInstance();
222 private @NonNull DataContainerCodecContext<?, T> loadInstance() {
223 final DataContainerCodecContext<?, T> tmp = createInstance();
224 final Object witness = INSTANCE.compareAndExchangeRelease(this, null, tmp);
225 return witness == null ? tmp : (DataContainerCodecContext<?, T>) witness;
228 @SuppressWarnings({ "rawtypes", "unchecked" })
229 // This method must allow concurrent loading, i.e. nothing in it may have effects outside of the loaded object
230 private @NonNull DataContainerCodecContext<?, T> createInstance() {
231 // FIXME: make protected abstract
232 if (schema instanceof ContainerSchemaNode) {
233 return new ContainerNodeCodecContext(this);
234 } else if (schema instanceof ListSchemaNode) {
235 return Identifiable.class.isAssignableFrom(getBindingClass())
236 ? KeyedListNodeCodecContext.create((DataContainerCodecPrototype<ListSchemaNode>) this)
237 : new ListNodeCodecContext(this);
238 } else if (schema instanceof ChoiceSchemaNode) {
239 return new ChoiceNodeCodecContext(this);
240 } else if (schema instanceof AugmentationSchemaNode) {
241 return new AugmentationNodeContext(this);
242 } else if (schema instanceof CaseSchemaNode) {
243 return new CaseNodeCodecContext(this);
245 throw new IllegalArgumentException("Unsupported type " + getBindingClass() + " " + schema);
249 return schema instanceof ChoiceSchemaNode;