Merge "Bug 2924: DataCodecTree expose (de)serialization methods for path arguments."
[yangtools.git] / code-generator / binding-data-codec / src / main / java / org / opendaylight / yangtools / binding / data / 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.yangtools.binding.data.codec.impl;
9
10 import com.google.common.collect.Iterables;
11 import javax.annotation.concurrent.GuardedBy;
12 import org.opendaylight.yangtools.binding.data.codec.impl.NodeCodecContext.CodecContextFactory;
13 import org.opendaylight.yangtools.yang.binding.DataRoot;
14 import org.opendaylight.yangtools.yang.binding.Identifiable;
15 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29
30 final class DataContainerCodecPrototype<T> implements NodeContextSupplier {
31
32     private final T schema;
33     private final QNameModule namespace;
34     private final CodecContextFactory factory;
35     private final Class<?> bindingClass;
36     private final InstanceIdentifier.Item<?> bindingArg;
37     private final YangInstanceIdentifier.PathArgument yangArg;
38     private volatile DataContainerCodecContext<?,T> instance = null;
39
40     @SuppressWarnings({"rawtypes", "unchecked"})
41     private DataContainerCodecPrototype(final Class<?> cls, final YangInstanceIdentifier.PathArgument arg, final T nodeSchema,
42             final CodecContextFactory factory) {
43         super();
44         this.bindingClass = cls;
45         this.yangArg = arg;
46         this.schema = nodeSchema;
47         this.factory = factory;
48         this.bindingArg = new InstanceIdentifier.Item(bindingClass);
49
50         if (arg instanceof AugmentationIdentifier) {
51             this.namespace = Iterables.getFirst(((AugmentationIdentifier) arg).getPossibleChildNames(), null).getModule();
52         } else {
53             this.namespace = arg.getNodeType().getModule();
54         }
55     }
56
57     @SuppressWarnings({"rawtypes", "unchecked"})
58     private DataContainerCodecPrototype(final Class<?> cls, final YangInstanceIdentifier.PathArgument arg, final T nodeSchema,
59             final CodecContextFactory factory,final DataContainerCodecContext<?,T> instance) {
60         super();
61         this.yangArg = arg;
62         this.schema = nodeSchema;
63         this.factory = factory;
64         this.namespace = arg.getNodeType().getModule();
65         this.bindingClass = cls;
66         this.bindingArg = new InstanceIdentifier.Item(bindingClass);
67         this.instance = instance;
68     }
69
70     @SuppressWarnings({ "unchecked", "rawtypes" })
71     static <T extends DataSchemaNode> DataContainerCodecPrototype<T> from(final Class<?> cls, final T schema,
72             final CodecContextFactory factory) {
73         final NodeIdentifier arg = new NodeIdentifier(schema.getQName());
74         return new DataContainerCodecPrototype(cls, arg, schema, factory);
75     }
76
77     static DataContainerCodecPrototype<SchemaContext> rootPrototype(final CodecContextFactory factory) {
78         final SchemaContext schema = factory.getRuntimeContext().getSchemaContext();
79         final NodeIdentifier arg = new NodeIdentifier(schema.getQName());
80         return new DataContainerCodecPrototype<SchemaContext>(DataRoot.class, arg, schema, factory);
81     }
82
83
84     @SuppressWarnings({ "rawtypes", "unchecked" })
85     static DataContainerCodecPrototype<?> from(final Class<?> augClass, final AugmentationIdentifier arg,
86             final AugmentationSchema schema, final CodecContextFactory factory) {
87         return new DataContainerCodecPrototype(augClass, arg, schema, factory);
88     }
89
90     static DataContainerCodecPrototype<NotificationDefinition> from(final Class<?> augClass, final NotificationDefinition schema, final CodecContextFactory factory) {
91         final PathArgument arg = new NodeIdentifier(schema.getQName());
92         return new DataContainerCodecPrototype<NotificationDefinition>(augClass,arg, schema, factory);
93     }
94
95     protected final T getSchema() {
96         return schema;
97     }
98
99     protected final QNameModule getNamespace() {
100         return namespace;
101     }
102
103     protected final CodecContextFactory getFactory() {
104         return factory;
105     }
106
107     protected final Class<?> getBindingClass() {
108         return bindingClass;
109     }
110
111     protected final InstanceIdentifier.Item<?> getBindingArg() {
112         return bindingArg;
113     }
114
115     protected final YangInstanceIdentifier.PathArgument getYangArg() {
116         return yangArg;
117     }
118
119     @Override
120     public DataContainerCodecContext<?,T> get() {
121         DataContainerCodecContext<?,T> tmp = instance;
122         if (tmp == null) {
123             synchronized (this) {
124                 tmp = instance;
125                 if (tmp == null) {
126                     tmp = createInstance();
127                     instance = tmp;
128                 }
129             }
130         }
131
132         return tmp;
133     }
134
135     @GuardedBy("this")
136     @SuppressWarnings({ "rawtypes", "unchecked" })
137     private DataContainerCodecContext<?,T> createInstance() {
138         // FIXME: make protected abstract
139         if (schema instanceof ContainerSchemaNode) {
140             return new ContainerNodeCodecContext(this);
141         } else if (schema instanceof ListSchemaNode) {
142             if (Identifiable.class.isAssignableFrom(getBindingClass())) {
143                 return new KeyedListNodeCodecContext(this);
144             } else {
145                 return new ListNodeCodecContext(this);
146             }
147         } else if (schema instanceof ChoiceSchemaNode) {
148             return new ChoiceNodeCodecContext(this);
149         } else if (schema instanceof AugmentationSchema) {
150             return new AugmentationNodeContext(this);
151         } else if (schema instanceof ChoiceCaseNode) {
152             return new CaseNodeCodecContext(this);
153         }
154         throw new IllegalArgumentException("Unsupported type " + bindingClass + " " + schema);
155     }
156
157     boolean isChoice() {
158         return schema instanceof ChoiceSchemaNode;
159     }
160 }