2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base;
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;
32 final class DataContainerCodecPrototype<T> implements NodeContextSupplier {
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;
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);
50 this.bindingArg = new Item(bindingClass);
52 if (arg instanceof AugmentationIdentifier) {
53 this.namespace = Iterables.getFirst(((AugmentationIdentifier) arg).getPossibleChildNames(), null).getModule();
55 this.namespace = arg.getNodeType().getModule();
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);
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);
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);
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);
82 protected T getSchema() {
86 protected QNameModule getNamespace() {
90 protected CodecContextFactory getFactory() {
94 protected Class<?> getBindingClass() {
98 protected Item<?> getBindingArg() {
102 protected YangInstanceIdentifier.PathArgument getYangArg() {
108 public DataContainerCodecContext<?,T> get() {
109 DataContainerCodecContext<?,T> tmp = instance;
111 synchronized (this) {
114 tmp = createInstance();
124 @SuppressWarnings({"rawtypes", "unchecked"})
125 protected DataContainerCodecContext<?, T> createInstance() {
126 //TODO - implement it
127 throw new NotImplementedException();
131 return schema instanceof ChoiceSchemaNode;