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 private DataContainerCodecPrototype(final Class<?> cls, final YangInstanceIdentifier.PathArgument arg, final T nodeSchema,
41 final CodecContextFactory factory) {
43 this.bindingClass = cls;
45 this.schema = nodeSchema;
46 this.factory = factory;
47 this.bindingArg = new InstanceIdentifier.Item(bindingClass);
49 if (arg instanceof AugmentationIdentifier) {
50 this.namespace = Iterables.getFirst(((AugmentationIdentifier) arg).getPossibleChildNames(), null).getModule();
52 this.namespace = arg.getNodeType().getModule();
56 private DataContainerCodecPrototype(final Class<?> cls, final YangInstanceIdentifier.PathArgument arg, final T nodeSchema,
57 final CodecContextFactory factory,final DataContainerCodecContext<T> instance) {
60 this.schema = nodeSchema;
61 this.factory = factory;
62 this.namespace = arg.getNodeType().getModule();
63 this.bindingClass = cls;
64 this.bindingArg = new InstanceIdentifier.Item(bindingClass);
65 this.instance = instance;
68 @SuppressWarnings({ "unchecked", "rawtypes" })
69 static <T extends DataSchemaNode> DataContainerCodecPrototype<T> from(final Class<?> cls, final T schema,
70 final CodecContextFactory factory) {
71 final NodeIdentifier arg = new NodeIdentifier(schema.getQName());
72 return new DataContainerCodecPrototype(cls, arg, schema, factory);
75 static DataContainerCodecPrototype<SchemaContext> rootPrototype(final CodecContextFactory factory) {
76 final SchemaContext schema = factory.getRuntimeContext().getSchemaContext();
77 final NodeIdentifier arg = new NodeIdentifier(schema.getQName());
78 return new DataContainerCodecPrototype<SchemaContext>(DataRoot.class, arg, schema, factory);
82 @SuppressWarnings({ "rawtypes", "unchecked" })
83 static DataContainerCodecPrototype<?> from(final Class<?> augClass, final AugmentationIdentifier arg,
84 final AugmentationSchema schema, final CodecContextFactory factory) {
85 return new DataContainerCodecPrototype(augClass, arg, schema, factory);
88 static DataContainerCodecPrototype<NotificationDefinition> from(final Class<?> augClass, final NotificationDefinition schema, final CodecContextFactory factory) {
89 final PathArgument arg = new NodeIdentifier(schema.getQName());
90 return new DataContainerCodecPrototype<NotificationDefinition>(augClass,arg, schema, factory);
93 protected final T getSchema() {
97 protected final QNameModule getNamespace() {
101 protected final CodecContextFactory getFactory() {
105 protected final Class<?> getBindingClass() {
109 protected final InstanceIdentifier.Item<?> getBindingArg() {
113 protected final YangInstanceIdentifier.PathArgument getYangArg() {
118 public DataContainerCodecContext<T> get() {
119 DataContainerCodecContext<T> tmp = instance;
121 synchronized (this) {
124 tmp = createInstance();
134 @SuppressWarnings({ "rawtypes", "unchecked" })
135 private DataContainerCodecContext createInstance() {
136 // FIXME: make protected abstract
137 if (schema instanceof ContainerSchemaNode) {
138 return new ContainerNodeCodecContext((DataContainerCodecPrototype) this);
139 } else if (schema instanceof ListSchemaNode) {
140 if (Identifiable.class.isAssignableFrom(getBindingClass())) {
141 return new KeyedListNodeCodecContext((DataContainerCodecPrototype) this);
143 return new ListNodeCodecContext((DataContainerCodecPrototype) this);
145 } else if (schema instanceof ChoiceSchemaNode) {
146 return new ChoiceNodeCodecContext((DataContainerCodecPrototype) this);
147 } else if (schema instanceof AugmentationSchema) {
148 return new AugmentationNodeContext((DataContainerCodecPrototype) this);
149 } else if (schema instanceof ChoiceCaseNode) {
150 return new CaseNodeCodecContext((DataContainerCodecPrototype) this);
152 throw new IllegalArgumentException("Unsupported type " + bindingClass + " " + schema);
156 return schema instanceof ChoiceSchemaNode;