2 * Copyright (c) 2014 Cisco Systems, Inc. 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
8 package org.opendaylight.yangtools.binding.data.codec.impl;
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;
30 final class DataContainerCodecPrototype<T> implements NodeContextSupplier {
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;
40 @SuppressWarnings({"rawtypes", "unchecked"})
41 private DataContainerCodecPrototype(final Class<?> cls, final YangInstanceIdentifier.PathArgument arg, final T nodeSchema,
42 final CodecContextFactory factory) {
44 this.bindingClass = cls;
46 this.schema = nodeSchema;
47 this.factory = factory;
48 this.bindingArg = new InstanceIdentifier.Item(bindingClass);
50 if (arg instanceof AugmentationIdentifier) {
51 this.namespace = Iterables.getFirst(((AugmentationIdentifier) arg).getPossibleChildNames(), null).getModule();
53 this.namespace = arg.getNodeType().getModule();
57 @SuppressWarnings({ "unchecked", "rawtypes" })
58 static <T extends DataSchemaNode> DataContainerCodecPrototype<T> from(final Class<?> cls, final T schema,
59 final CodecContextFactory factory) {
60 return new DataContainerCodecPrototype(cls, NodeIdentifier.create(schema.getQName()), schema, factory);
63 static DataContainerCodecPrototype<SchemaContext> rootPrototype(final CodecContextFactory factory) {
64 final SchemaContext schema = factory.getRuntimeContext().getSchemaContext();
65 final NodeIdentifier arg = NodeIdentifier.create(schema.getQName());
66 return new DataContainerCodecPrototype<SchemaContext>(DataRoot.class, arg, schema, factory);
70 @SuppressWarnings({ "rawtypes", "unchecked" })
71 static DataContainerCodecPrototype<?> from(final Class<?> augClass, final AugmentationIdentifier arg,
72 final AugmentationSchema schema, final CodecContextFactory factory) {
73 return new DataContainerCodecPrototype(augClass, arg, schema, factory);
76 static DataContainerCodecPrototype<NotificationDefinition> from(final Class<?> augClass, final NotificationDefinition schema, final CodecContextFactory factory) {
77 final PathArgument arg = NodeIdentifier.create(schema.getQName());
78 return new DataContainerCodecPrototype<NotificationDefinition>(augClass,arg, schema, factory);
81 protected T getSchema() {
85 protected QNameModule getNamespace() {
89 protected CodecContextFactory getFactory() {
93 protected Class<?> getBindingClass() {
97 protected InstanceIdentifier.Item<?> getBindingArg() {
101 protected YangInstanceIdentifier.PathArgument getYangArg() {
106 public DataContainerCodecContext<?,T> get() {
107 DataContainerCodecContext<?,T> tmp = instance;
109 synchronized (this) {
112 tmp = createInstance();
122 @SuppressWarnings({ "rawtypes", "unchecked" })
123 private DataContainerCodecContext<?,T> createInstance() {
124 // FIXME: make protected abstract
125 if (schema instanceof ContainerSchemaNode) {
126 return new ContainerNodeCodecContext(this);
127 } else if (schema instanceof ListSchemaNode) {
128 if (Identifiable.class.isAssignableFrom(getBindingClass())) {
129 return new KeyedListNodeCodecContext(this);
131 return new ListNodeCodecContext(this);
133 } else if (schema instanceof ChoiceSchemaNode) {
134 return new ChoiceNodeCodecContext(this);
135 } else if (schema instanceof AugmentationSchema) {
136 return new AugmentationNodeContext(this);
137 } else if (schema instanceof ChoiceCaseNode) {
138 return new CaseNodeCodecContext(this);
140 throw new IllegalArgumentException("Unsupported type " + bindingClass + " " + schema);
144 return schema instanceof ChoiceSchemaNode;