Binding2 runtime - Codecs impl #2
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / context / base / DataContainerCodecPrototype.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Iterables;
14 import javax.annotation.Nonnull;
15 import javax.annotation.concurrent.GuardedBy;
16 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base.NodeCodecContext.CodecContextFactory;
17 import org.opendaylight.mdsal.binding.javav2.spec.base.Item;
18 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeRoot;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.AugmentationSchema;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import sun.reflect.generics.reflectiveObjects.NotImplementedException;
30
31 @Beta
32 final class DataContainerCodecPrototype<T> implements NodeContextSupplier {
33
34     private final T schema;
35     private final QNameModule namespace;
36     private final CodecContextFactory factory;
37     private final Class<?> bindingClass;
38     private final Item<?> bindingArg;
39     private final YangInstanceIdentifier.PathArgument yangArg;
40     private volatile DataContainerCodecContext<?,T> instance = null;
41
42     @SuppressWarnings({"rawtypes", "unchecked"})
43     private DataContainerCodecPrototype(final Class<?> cls, final YangInstanceIdentifier.PathArgument arg, final T nodeSchema,
44             final CodecContextFactory factory) {
45         this.bindingClass = Preconditions.checkNotNull(cls);
46         this.yangArg = Preconditions.checkNotNull(arg);
47         this.schema = Preconditions.checkNotNull(nodeSchema);
48         this.factory = Preconditions.checkNotNull(factory);
49
50         this.bindingArg = new Item(bindingClass);
51
52         if (arg instanceof AugmentationIdentifier) {
53             this.namespace = Iterables.getFirst(((AugmentationIdentifier) arg).getPossibleChildNames(), null).getModule();
54         } else {
55             this.namespace = arg.getNodeType().getModule();
56         }
57     }
58
59     @SuppressWarnings({ "unchecked", "rawtypes" })
60     static <T extends DataSchemaNode> DataContainerCodecPrototype<T> from(final Class<?> cls, final T schema,
61             final CodecContextFactory factory) {
62         return new DataContainerCodecPrototype(cls, NodeIdentifier.create(schema.getQName()), schema, factory);
63     }
64
65     static DataContainerCodecPrototype<SchemaContext> rootPrototype(final CodecContextFactory factory) {
66         final SchemaContext schema = factory.getRuntimeContext().getSchemaContext();
67         final NodeIdentifier arg = NodeIdentifier.create(schema.getQName());
68         return new DataContainerCodecPrototype<>(TreeRoot.class, arg, schema, factory);
69     }
70
71     @SuppressWarnings({ "rawtypes", "unchecked" })
72     static DataContainerCodecPrototype<?> from(final Class<?> augClass, final AugmentationIdentifier arg,
73                                                final AugmentationSchema schema, final CodecContextFactory factory) {
74         return new DataContainerCodecPrototype(augClass, arg, schema, factory);
75     }
76
77     static DataContainerCodecPrototype<NotificationDefinition> from(final Class<?> augClass, final NotificationDefinition schema, final CodecContextFactory factory) {
78         final PathArgument arg = NodeIdentifier.create(schema.getQName());
79         return new DataContainerCodecPrototype<>(augClass, arg, schema, factory);
80     }
81
82     protected T getSchema() {
83         return schema;
84     }
85
86     protected QNameModule getNamespace() {
87         return namespace;
88     }
89
90     protected CodecContextFactory getFactory() {
91         return factory;
92     }
93
94     protected Class<?> getBindingClass() {
95         return bindingClass;
96     }
97
98     protected Item<?> getBindingArg() {
99         return bindingArg;
100     }
101
102     protected YangInstanceIdentifier.PathArgument getYangArg() {
103         return yangArg;
104     }
105
106     @Nonnull
107     @Override
108     public DataContainerCodecContext<?,T> get() {
109         DataContainerCodecContext<?,T> tmp = instance;
110         if (tmp == null) {
111             synchronized (this) {
112                 tmp = instance;
113                 if (tmp == null) {
114                     tmp = createInstance();
115                     instance = tmp;
116                 }
117             }
118         }
119
120         return tmp;
121     }
122
123     @GuardedBy("this")
124     @SuppressWarnings({"rawtypes", "unchecked"})
125     protected DataContainerCodecContext<?, T> createInstance() {
126         //TODO - implement it
127         throw new NotImplementedException();
128     }
129
130     boolean isChoice() {
131         return schema instanceof ChoiceSchemaNode;
132     }
133 }