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.base.Optional;
11 import com.google.common.collect.ImmutableCollection;
12 import com.google.common.collect.ImmutableSet;
13 import java.io.IOException;
14 import java.util.List;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
18 import org.opendaylight.yangtools.yang.binding.BindingStreamEventWriter;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.DataObjectSerializer;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
31 abstract class DataContainerCodecContext<D extends DataObject,T> extends NodeCodecContext<D> {
33 private final DataContainerCodecPrototype<T> prototype;
34 private volatile DataObjectSerializer eventStreamSerializer;
36 protected DataContainerCodecContext(final DataContainerCodecPrototype<T> prototype) {
37 this.prototype = prototype;
41 public final T getSchema() {
42 return prototype.getSchema();
45 protected final QNameModule namespace() {
46 return prototype.getNamespace();
49 protected final CodecContextFactory factory() {
50 return prototype.getFactory();
54 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
55 return prototype.getYangArg();
59 * Returns nested node context using supplied YANG Instance Identifier
61 * @param arg Yang Instance Identifier Argument
62 * @return Context of child
63 * @throws IllegalArgumentException If supplied argument does not represent valid child.
66 public abstract NodeCodecContext<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument arg);
69 * Returns nested node context using supplied Binding Instance Identifier
70 * and adds YANG instance identifiers to supplied list.
72 * @param arg Binding Instance Identifier Argument
73 * @return Context of child or null if supplied {@code arg} does not represent valid child.
74 * @throws IllegalArgumentException If supplied argument does not represent valid child.
77 public @Nullable DataContainerCodecContext<?,?> bindingPathArgumentChild(final InstanceIdentifier.PathArgument arg,
78 final List<YangInstanceIdentifier.PathArgument> builder) {
79 final DataContainerCodecContext<?,?> child = streamChild(arg.getType());
81 if (builder != null) {
82 child.addYangPathArgument(arg,builder);
86 throw new IllegalArgumentException("SUpplied argument is not valid child");
90 * Returns deserialized Binding Path Argument from YANG instance identifier.
95 protected PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
99 protected final PathArgument bindingArg() {
100 return prototype.getBindingArg();
103 @SuppressWarnings("unchecked")
105 public final Class<D> getBindingClass() {
106 return Class.class.cast(prototype.getBindingClass());
111 * Returns child context as if it was walked by
112 * {@link BindingStreamEventWriter}. This means that to enter case, one
113 * must issue getChild(ChoiceClass).getChild(CaseClass).
116 * @return Context of child node or null, if supplied class is not subtree child
117 * @throws IllegalArgumentException If supplied child class is not valid in specified context.
120 public abstract @Nullable <DV extends DataObject> DataContainerCodecContext<DV,?> streamChild(final Class<DV> childClass) throws IllegalArgumentException;
124 * Returns child context as if it was walked by
125 * {@link BindingStreamEventWriter}. This means that to enter case, one
126 * must issue getChild(ChoiceClass).getChild(CaseClass).
129 * @return Context of child or Optional absent is supplied class is not applicable in context.
132 public abstract <DV extends DataObject> Optional<DataContainerCodecContext<DV,?>> possibleStreamChild(final Class<DV> childClass);
135 public String toString() {
136 return getClass().getSimpleName() + " [" + prototype.getBindingClass() + "]";
140 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
141 final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
142 if(cacheSpecifier.isEmpty()) {
143 return new NonCachingCodec<>(this);
145 return new CachingNormalizedNodeCodec<D>(this,ImmutableSet.copyOf(cacheSpecifier));
148 BindingStreamEventWriter createWriter(final NormalizedNodeStreamWriter domWriter) {
149 return BindingToNormalizedStreamWriter.create(this, domWriter);
153 protected final <V> V childNonNull(@Nullable final V nullable, final YangInstanceIdentifier.PathArgument child,
154 final String message, final Object... args) {
155 if (nullable != null) {
158 MissingSchemaException.checkModulePresent(factory().getRuntimeContext().getSchemaContext(), child);
159 throw IncorrectNestingException.create(message, args);
163 protected final <V> V childNonNull(@Nullable final V nullable, final QName child, final String message,
164 final Object... args) {
165 if (nullable != null) {
168 MissingSchemaException.checkModulePresent(factory().getRuntimeContext().getSchemaContext(), child);
169 throw IncorrectNestingException.create(message, args);
173 protected final <V> V childNonNull(@Nullable final V nullable, final Class<?> childClass, final String message,
174 final Object... args) {
175 if (nullable != null) {
178 MissingSchemaForClassException.check(factory().getRuntimeContext(), childClass);
179 MissingClassInLoadingStrategyException.check(factory().getRuntimeContext().getStrategy(), childClass);
180 throw IncorrectNestingException.create(message, args);
183 DataObjectSerializer eventStreamSerializer() {
184 if(eventStreamSerializer == null) {
185 eventStreamSerializer = factory().getEventStreamSerializer(getBindingClass());
187 return eventStreamSerializer;
191 public NormalizedNode<?, ?> serialize(final D data) {
192 final NormalizedNodeResult result = new NormalizedNodeResult();
193 // We create DOM stream writer which produces normalized nodes
194 final NormalizedNodeStreamWriter domWriter = ImmutableNormalizedNodeStreamWriter.from(result);
195 writeAsNormalizedNode(data, domWriter);
196 return result.getResult();
200 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
202 eventStreamSerializer().serialize(data, createWriter(writer));
203 } catch (final IOException e) {
204 throw new IllegalStateException("Failed to serialize Binding DTO",e);