Binding2 runtime - Codecs impl - context - part2
[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 public 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     public 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     public 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     public static DataContainerCodecPrototype<NotificationDefinition> from(final Class<?> augClass,
78             final NotificationDefinition schema, final CodecContextFactory factory) {
79         final PathArgument arg = NodeIdentifier.create(schema.getQName());
80         return new DataContainerCodecPrototype<>(augClass, arg, schema, factory);
81     }
82
83     public T getSchema() {
84         return schema;
85     }
86
87     protected QNameModule getNamespace() {
88         return namespace;
89     }
90
91     protected CodecContextFactory getFactory() {
92         return factory;
93     }
94
95     public Class<?> getBindingClass() {
96         return bindingClass;
97     }
98
99     protected Item<?> getBindingArg() {
100         return bindingArg;
101     }
102
103     protected YangInstanceIdentifier.PathArgument getYangArg() {
104         return yangArg;
105     }
106
107     @Nonnull
108     @Override
109     public DataContainerCodecContext<?,T> get() {
110         DataContainerCodecContext<?,T> tmp = instance;
111         if (tmp == null) {
112             synchronized (this) {
113                 tmp = instance;
114                 if (tmp == null) {
115                     tmp = createInstance();
116                     instance = tmp;
117                 }
118             }
119         }
120
121         return tmp;
122     }
123
124     @GuardedBy("this")
125     @SuppressWarnings({"rawtypes", "unchecked"})
126     protected DataContainerCodecContext<?, T> createInstance() {
127         //TODO - implement it
128         throw new NotImplementedException();
129     }
130
131     boolean isChoice() {
132         return schema instanceof ChoiceSchemaNode;
133     }
134 }