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.mdsal.binding.dom.codec.impl;
10 import com.google.common.collect.ImmutableCollection;
11 import com.google.common.collect.ImmutableSet;
12 import java.io.IOException;
13 import java.util.List;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
19 import org.opendaylight.yangtools.yang.binding.BindingStreamEventWriter;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.DataObjectSerializer;
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;
30 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
32 abstract class DataContainerCodecContext<D extends DataObject, T extends WithStatus> extends NodeCodecContext
33 implements BindingDataObjectCodecTreeNode<D> {
35 private final DataContainerCodecPrototype<T> prototype;
36 private volatile DataObjectSerializer eventStreamSerializer;
38 protected DataContainerCodecContext(final DataContainerCodecPrototype<T> prototype) {
39 this.prototype = prototype;
43 public final T getSchema() {
44 return prototype.getSchema();
48 public final ChildAddressabilitySummary getChildAddressabilitySummary() {
49 return prototype.getChildAddressabilitySummary();
52 protected final QNameModule namespace() {
53 return prototype.getNamespace();
56 protected final CodecContextFactory factory() {
57 return prototype.getFactory();
61 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
62 return prototype.getYangArg();
66 * Returns nested node context using supplied YANG Instance Identifier.
68 * @param arg Yang Instance Identifier Argument
69 * @return Context of child
70 * @throws IllegalArgumentException If supplied argument does not represent valid child.
73 public abstract NodeCodecContext yangPathArgumentChild(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.
84 public DataContainerCodecContext<?, ?> bindingPathArgumentChild(final PathArgument arg,
85 final List<YangInstanceIdentifier.PathArgument> builder) {
86 final DataContainerCodecContext<?,?> child = streamChild(arg.getType());
87 if (builder != null) {
88 child.addYangPathArgument(arg,builder);
94 * Returns deserialized Binding Path Argument from YANG instance identifier.
96 protected PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
100 protected final PathArgument bindingArg() {
101 return prototype.getBindingArg();
104 @SuppressWarnings("unchecked")
106 public final Class<D> getBindingClass() {
107 return Class.class.cast(prototype.getBindingClass());
111 public abstract <C extends DataObject> DataContainerCodecContext<C, ?> streamChild(Class<C> childClass);
114 * Returns child context as if it was walked by {@link BindingStreamEventWriter}. This means that to enter case, one
115 * must issue getChild(ChoiceClass).getChild(CaseClass).
117 * @param childClass child class
118 * @return Context of child or Optional.empty is supplied class is not applicable in context.
121 public abstract <C extends DataObject> Optional<DataContainerCodecContext<C,?>> possibleStreamChild(
122 Class<C> childClass);
125 public String toString() {
126 return getClass().getSimpleName() + " [" + prototype.getBindingClass() + "]";
130 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
131 final ImmutableCollection<Class<? extends DataObject>> cacheSpecifier) {
132 if (cacheSpecifier.isEmpty()) {
133 return new NonCachingCodec<>(this);
135 return new CachingNormalizedNodeCodec<>(this, ImmutableSet.copyOf(cacheSpecifier));
138 BindingStreamEventWriter createWriter(final NormalizedNodeStreamWriter domWriter) {
139 return BindingToNormalizedStreamWriter.create(this, domWriter);
142 protected final <V> @NonNull V childNonNull(final @Nullable V nullable,
143 final YangInstanceIdentifier.PathArgument child, final String message, final Object... args) {
144 if (nullable != null) {
147 MissingSchemaException.checkModulePresent(factory().getRuntimeContext().getSchemaContext(), child);
148 throw IncorrectNestingException.create(message, args);
151 protected final <V> @NonNull V childNonNull(final @Nullable V nullable, final QName child, final String message,
152 final Object... args) {
153 if (nullable != null) {
156 MissingSchemaException.checkModulePresent(factory().getRuntimeContext().getSchemaContext(), child);
157 throw IncorrectNestingException.create(message, args);
160 protected final <V> @NonNull V childNonNull(final @Nullable V nullable, final Class<?> childClass,
161 final String message, final Object... args) {
162 if (nullable != null) {
165 MissingSchemaForClassException.check(factory().getRuntimeContext(), childClass);
166 MissingClassInLoadingStrategyException.check(factory().getRuntimeContext().getStrategy(), childClass);
167 throw IncorrectNestingException.create(message, args);
170 DataObjectSerializer eventStreamSerializer() {
171 if (eventStreamSerializer == null) {
172 eventStreamSerializer = factory().getEventStreamSerializer(getBindingClass());
174 return eventStreamSerializer;
178 public NormalizedNode<?, ?> serialize(final D data) {
179 final NormalizedNodeResult result = new NormalizedNodeResult();
180 // We create DOM stream writer which produces normalized nodes
181 final NormalizedNodeStreamWriter domWriter = ImmutableNormalizedNodeStreamWriter.from(result);
182 writeAsNormalizedNode(data, domWriter);
183 return result.getResult();
187 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
189 eventStreamSerializer().serialize(data, createWriter(writer));
190 } catch (final IOException e) {
191 throw new IllegalStateException("Failed to serialize Binding DTO",e);