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.Optional;
13 import com.google.common.collect.ImmutableCollection;
14 import java.io.IOException;
15 import java.util.List;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.codecs.BindingNormalizedNodeCachingCodec;
19 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.MissingSchemaException;
20 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.serializer.BindingToNormalizedStreamWriter;
21 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeArgument;
22 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
23 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingStreamEventWriter;
24 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializer;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.QNameModule;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
33 import sun.reflect.generics.reflectiveObjects.NotImplementedException;
36 public abstract class DataContainerCodecContext<D extends TreeNode, T> extends NodeCodecContext<D> {
38 private final DataContainerCodecPrototype<T> prototype;
39 private volatile TreeNodeSerializer eventStreamSerializer;
41 protected DataContainerCodecContext(final DataContainerCodecPrototype<T> prototype) {
42 this.prototype = prototype;
47 public final T getSchema() {
48 return prototype.getSchema();
51 protected final QNameModule namespace() {
52 return prototype.getNamespace();
55 protected final CodecContextFactory factory() {
56 return prototype.getFactory();
60 public YangInstanceIdentifier.PathArgument getDomPathArgument() {
61 return prototype.getYangArg();
65 * Returns nested node context using supplied YANG Instance Identifier
67 * @param arg Yang Instance Identifier Argument
68 * @return Context of child
69 * @throws IllegalArgumentException If supplied argument does not represent valid child.
73 public abstract NodeCodecContext<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument arg);
76 * Returns nested node context using supplied Binding Instance Identifier
77 * and adds YANG instance identifiers to supplied list.
79 * @param arg Binding Instance Identifier Argument
80 * @return Context of child or null if supplied {@code arg} does not represent valid child.
81 * @throws IllegalArgumentException If supplied argument does not represent valid child.
83 @SuppressWarnings("unchecked")
86 public DataContainerCodecContext<?,?> bindingPathArgumentChild(@Nonnull final TreeArgument<?> arg,
87 final List<PathArgument> builder) {
88 final DataContainerCodecContext<?,?> child = streamChild((Class<? extends TreeNode>) arg.getType());
89 if (builder != null) {
90 child.addYangPathArgument(arg,builder);
96 * Returns de-serialized Binding Path Argument from YANG instance identifier.
98 * @param domArg input path argument
99 * @return returns binding path argument
101 @SuppressWarnings("rawtypes")
102 protected TreeArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
106 @SuppressWarnings("rawtypes")
107 protected final TreeArgument bindingArg() {
108 return prototype.getBindingArg();
112 @SuppressWarnings("unchecked")
114 public final Class<D> getBindingClass() {
115 return Class.class.cast(prototype.getBindingClass());
119 * Returns child context as if it was walked by
120 * {@link BindingStreamEventWriter}. This means that to enter case, one
121 * must issue getChild(ChoiceClass).getChild(CaseClass).
123 * @param childClass input child class
124 * @return Context of child node or null, if supplied class is not subtree child
125 * @throws IllegalArgumentException If supplied child class is not valid in specified context.
129 public abstract <DV extends TreeNode> DataContainerCodecContext<DV,?> streamChild(@Nonnull final Class<DV> childClass) throws IllegalArgumentException;
132 * Returns child context as if it was walked by
133 * {@link BindingStreamEventWriter}. This means that to enter case, one
134 * must issue getChild(ChoiceClass).getChild(CaseClass).
136 * @param childClass input child class
137 * @return Context of child or Optional absent is supplied class is not applicable in context.
140 public abstract <DV extends TreeNode> Optional<DataContainerCodecContext<DV, ?>> possibleStreamChild(@Nonnull
141 final Class<DV> childClass);
144 public String toString() {
145 return getClass().getSimpleName() + " [" + prototype.getBindingClass() + "]";
150 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
151 @Nonnull final ImmutableCollection<Class<? extends TreeNode>> cacheSpecifier) {
153 //TODO: implement in cache-related patches to come
154 throw new NotImplementedException();
157 BindingStreamEventWriter createWriter(final NormalizedNodeStreamWriter domWriter) {
158 return BindingToNormalizedStreamWriter.create(this, domWriter);
162 protected final <V> V childNonNull(@Nullable final V nullable, final YangInstanceIdentifier.PathArgument child,
163 final String message, final Object... args) {
164 if (nullable != null) {
167 MissingSchemaException.checkModulePresent(factory().getRuntimeContext().getSchemaContext(), child);
168 throw IncorrectNestingException.create(message, args);
172 protected final <V> V childNonNull(@Nullable final V nullable, final QName child, final String message,
173 final Object... args) {
174 if (nullable != null) {
177 MissingSchemaException.checkModulePresent(factory().getRuntimeContext().getSchemaContext(), child);
178 throw IncorrectNestingException.create(message, args);
182 protected final <V> V childNonNull(@Nullable final V nullable, final Class<?> childClass, final String message,
183 final Object... args) {
184 if (nullable != null) {
187 MissingSchemaForClassException.check(factory().getRuntimeContext(), childClass);
188 MissingClassInLoadingStrategyException.check(factory().getRuntimeContext().getStrategy(), childClass);
189 throw IncorrectNestingException.create(message, args);
192 public TreeNodeSerializer eventStreamSerializer() {
193 if(eventStreamSerializer == null) {
194 eventStreamSerializer = factory().getEventStreamSerializer(getBindingClass());
196 return eventStreamSerializer;
201 public NormalizedNode<?, ?> serialize(@Nonnull final D data) {
202 final NormalizedNodeResult result = new NormalizedNodeResult();
203 // We create DOM stream writer which produces normalized nodes
204 final NormalizedNodeStreamWriter domWriter = ImmutableNormalizedNodeStreamWriter.from(result);
205 writeAsNormalizedNode(data, domWriter);
206 return result.getResult();
210 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
212 eventStreamSerializer().serialize(data, createWriter(writer));
213 } catch (final IOException e) {
214 throw new IllegalStateException("Failed to serialize Binding DTO",e);