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();
46 public final ChildAddressabilitySummary getChildAddressabilitySummary() {
47 return prototype.getChildAddressabilitySummary();
50 protected final QNameModule namespace() {
51 return prototype.getNamespace();
54 protected final CodecContextFactory factory() {
55 return prototype.getFactory();
59 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
60 return prototype.getYangArg();
64 * Returns nested node context using supplied YANG Instance Identifier
66 * @param arg Yang Instance Identifier Argument
67 * @return Context of child
68 * @throws IllegalArgumentException If supplied argument does not represent valid child.
71 public abstract NodeCodecContext<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument arg);
74 * Returns nested node context using supplied Binding Instance Identifier
75 * and adds YANG instance identifiers to supplied list.
77 * @param arg Binding Instance Identifier Argument
78 * @return Context of child or null if supplied {@code arg} does not represent valid child.
79 * @throws IllegalArgumentException If supplied argument does not represent valid child.
82 public @Nullable DataContainerCodecContext<?,?> bindingPathArgumentChild(final InstanceIdentifier.PathArgument arg,
83 final List<YangInstanceIdentifier.PathArgument> builder) {
84 final DataContainerCodecContext<?,?> child = streamChild(arg.getType());
86 if (builder != null) {
87 child.addYangPathArgument(arg,builder);
91 throw new IllegalArgumentException("SUpplied argument is not valid child");
95 * Returns deserialized Binding Path Argument from YANG instance identifier.
100 protected PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
104 protected final PathArgument bindingArg() {
105 return prototype.getBindingArg();
108 @SuppressWarnings("unchecked")
110 public final Class<D> getBindingClass() {
111 return Class.class.cast(prototype.getBindingClass());
116 * Returns child context as if it was walked by
117 * {@link BindingStreamEventWriter}. This means that to enter case, one
118 * must issue getChild(ChoiceClass).getChild(CaseClass).
121 * @return Context of child node or null, if supplied class is not subtree child
122 * @throws IllegalArgumentException If supplied child class is not valid in specified context.
125 public abstract @Nullable <DV extends DataObject> DataContainerCodecContext<DV,?> streamChild(final Class<DV> childClass) throws IllegalArgumentException;
129 * Returns child context as if it was walked by
130 * {@link BindingStreamEventWriter}. This means that to enter case, one
131 * must issue getChild(ChoiceClass).getChild(CaseClass).
134 * @return Context of child or Optional absent is supplied class is not applicable in context.
137 public abstract <DV extends DataObject> Optional<DataContainerCodecContext<DV,?>> possibleStreamChild(final Class<DV> childClass);
140 public String toString() {
141 return getClass().getSimpleName() + " [" + prototype.getBindingClass() + "]";
145 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
146 final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
147 if(cacheSpecifier.isEmpty()) {
148 return new NonCachingCodec<>(this);
150 return new CachingNormalizedNodeCodec<D>(this,ImmutableSet.copyOf(cacheSpecifier));
153 BindingStreamEventWriter createWriter(final NormalizedNodeStreamWriter domWriter) {
154 return BindingToNormalizedStreamWriter.create(this, domWriter);
158 protected final <V> V childNonNull(@Nullable final V nullable, final YangInstanceIdentifier.PathArgument child,
159 final String message, final Object... args) {
160 if (nullable != null) {
163 MissingSchemaException.checkModulePresent(factory().getRuntimeContext().getSchemaContext(), child);
164 throw IncorrectNestingException.create(message, args);
168 protected final <V> V childNonNull(@Nullable final V nullable, final QName child, final String message,
169 final Object... args) {
170 if (nullable != null) {
173 MissingSchemaException.checkModulePresent(factory().getRuntimeContext().getSchemaContext(), child);
174 throw IncorrectNestingException.create(message, args);
178 protected final <V> V childNonNull(@Nullable final V nullable, final Class<?> childClass, final String message,
179 final Object... args) {
180 if (nullable != null) {
183 MissingSchemaForClassException.check(factory().getRuntimeContext(), childClass);
184 MissingClassInLoadingStrategyException.check(factory().getRuntimeContext().getStrategy(), childClass);
185 throw IncorrectNestingException.create(message, args);
188 DataObjectSerializer eventStreamSerializer() {
189 if(eventStreamSerializer == null) {
190 eventStreamSerializer = factory().getEventStreamSerializer(getBindingClass());
192 return eventStreamSerializer;
196 public NormalizedNode<?, ?> serialize(final D data) {
197 final NormalizedNodeResult result = new NormalizedNodeResult();
198 // We create DOM stream writer which produces normalized nodes
199 final NormalizedNodeStreamWriter domWriter = ImmutableNormalizedNodeStreamWriter.from(result);
200 writeAsNormalizedNode(data, domWriter);
201 return result.getResult();
205 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
207 eventStreamSerializer().serialize(data, createWriter(writer));
208 } catch (final IOException e) {
209 throw new IllegalStateException("Failed to serialize Binding DTO",e);