8294508bfe12f343d915775d1be6facbfe0f99ff
[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 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;
39
40 final class DataContainerCodecPrototype<T extends WithStatus> implements NodeContextSupplier {
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 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;
60
61     // Accessed via INSTANCE
62     @SuppressWarnings("unused")
63     private volatile DataContainerCodecContext<?, T> instance;
64
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);
69     }
70
71     private DataContainerCodecPrototype(final Item<?> bindingArg, final PathArgument arg, final T nodeSchema,
72             final CodecContextFactory factory) {
73         this.bindingArg = bindingArg;
74         this.yangArg = arg;
75         this.schema = nodeSchema;
76         this.factory = factory;
77
78         if (arg instanceof AugmentationIdentifier) {
79             this.namespace = Iterables.getFirst(((AugmentationIdentifier) arg).getPossibleChildNames(), null)
80                     .getModule();
81         } else {
82             this.namespace = arg.getNodeType().getModule();
83         }
84
85         this.childAddressabilitySummary = computeChildAddressabilitySummary(nodeSchema);
86     }
87
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;
98                     } else {
99                         haveAddressable = true;
100                     }
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)) {
106                         case ADDRESSABLE:
107                             haveAddressable = true;
108                             break;
109                         case MIXED:
110                             haveAddressable = true;
111                             haveUnaddressable = true;
112                             break;
113                         case UNADDRESSABLE:
114                             haveUnaddressable = true;
115                             break;
116                         default:
117                             throw new IllegalStateException("Unhandled accessibility summary for " + child);
118                     }
119                 } else {
120                     LOG.warn("Unhandled child node {}", child);
121                 }
122             }
123
124             if (!haveAddressable) {
125                 // Empty or all are unaddressable
126                 return ChildAddressabilitySummary.UNADDRESSABLE;
127             }
128
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)) {
135                     case ADDRESSABLE:
136                         haveAddressable = true;
137                         break;
138                     case UNADDRESSABLE:
139                         haveUnaddressable = true;
140                         break;
141                     case MIXED:
142                         // A child is mixed, which means we are mixed, too
143                         return ChildAddressabilitySummary.MIXED;
144                     default:
145                         throw new IllegalStateException("Unhandled accessibility summary for " + child);
146                 }
147             }
148
149             if (!haveAddressable) {
150                 // Empty or all are unaddressable
151                 return ChildAddressabilitySummary.UNADDRESSABLE;
152             }
153
154             return haveUnaddressable ? ChildAddressabilitySummary.MIXED : ChildAddressabilitySummary.ADDRESSABLE;
155         }
156
157         // No child nodes possible: return unaddressable
158         return ChildAddressabilitySummary.UNADDRESSABLE;
159     }
160
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);
165     }
166
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);
170     }
171
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);
175     }
176
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);
180     }
181
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);
186     }
187
188     T getSchema() {
189         return schema;
190     }
191
192     ChildAddressabilitySummary getChildAddressabilitySummary() {
193         return childAddressabilitySummary;
194     }
195
196     QNameModule getNamespace() {
197         return namespace;
198     }
199
200     CodecContextFactory getFactory() {
201         return factory;
202     }
203
204     Class<?> getBindingClass() {
205         return bindingArg.getType();
206     }
207
208     Item<?> getBindingArg() {
209         return bindingArg;
210     }
211
212     PathArgument getYangArg() {
213         return yangArg;
214     }
215
216     @Override
217     public DataContainerCodecContext<?, T> get() {
218         final DataContainerCodecContext<?, T> existing = (DataContainerCodecContext<?, T>) INSTANCE.getAcquire(this);
219         return existing != null ? existing : loadInstance();
220     }
221
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;
226     }
227
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);
244         }
245         throw new IllegalArgumentException("Unsupported type " + getBindingClass() + " " + schema);
246     }
247
248     boolean isChoice() {
249         return schema instanceof ChoiceSchemaNode;
250     }
251 }