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 static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableCollection;
13 import com.google.common.collect.ImmutableSet;
14 import java.io.IOException;
15 import java.util.List;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
21 import org.opendaylight.yangtools.yang.binding.BindingObject;
22 import org.opendaylight.yangtools.yang.binding.BindingStreamEventWriter;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.DataObjectSerializer;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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 org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
35 abstract class DataContainerCodecContext<D extends DataObject, T extends WithStatus> extends NodeCodecContext
36 implements BindingDataObjectCodecTreeNode<D> {
37 private final @NonNull DataContainerCodecPrototype<T> prototype;
39 private volatile DataObjectSerializer eventStreamSerializer;
41 protected DataContainerCodecContext(final DataContainerCodecPrototype<T> prototype) {
42 this.prototype = requireNonNull(prototype);
46 public final T getSchema() {
47 return prototype.getSchema();
51 public final ChildAddressabilitySummary getChildAddressabilitySummary() {
52 return prototype.getChildAddressabilitySummary();
55 protected final QNameModule namespace() {
56 return prototype.getNamespace();
59 protected final CodecContextFactory factory() {
60 return prototype.getFactory();
64 protected YangInstanceIdentifier.PathArgument getDomPathArgument() {
65 return prototype.getYangArg();
69 * Returns nested node context using supplied YANG Instance Identifier.
71 * @param arg Yang Instance Identifier Argument
72 * @return Context of child
73 * @throws IllegalArgumentException If supplied argument does not represent valid child.
76 public abstract NodeCodecContext yangPathArgumentChild(YangInstanceIdentifier.PathArgument arg);
79 * Returns nested node context using supplied Binding Instance Identifier
80 * and adds YANG instance identifiers to supplied list.
82 * @param arg Binding Instance Identifier Argument
83 * @return Context of child or null if supplied {@code arg} does not represent valid child.
84 * @throws IllegalArgumentException If supplied argument does not represent valid child.
87 public DataContainerCodecContext<?, ?> bindingPathArgumentChild(final PathArgument arg,
88 final List<YangInstanceIdentifier.PathArgument> builder) {
89 final DataContainerCodecContext<?,?> child = streamChild(arg.getType());
90 if (builder != null) {
91 child.addYangPathArgument(arg,builder);
97 * Returns deserialized Binding Path Argument from YANG instance identifier.
99 protected PathArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
103 protected final PathArgument bindingArg() {
104 return prototype.getBindingArg();
107 @SuppressWarnings("unchecked")
109 public final Class<D> getBindingClass() {
110 return Class.class.cast(prototype.getBindingClass());
114 public abstract <C extends DataObject> DataContainerCodecContext<C, ?> streamChild(Class<C> childClass);
117 * Returns child context as if it was walked by {@link BindingStreamEventWriter}. This means that to enter case, one
118 * must issue getChild(ChoiceClass).getChild(CaseClass).
120 * @param childClass child class
121 * @return Context of child or Optional.empty is supplied class is not applicable in context.
124 public abstract <C extends DataObject> Optional<DataContainerCodecContext<C,?>> possibleStreamChild(
125 Class<C> childClass);
128 public String toString() {
129 return getClass().getSimpleName() + " [" + prototype.getBindingClass() + "]";
133 public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
134 final ImmutableCollection<Class<? extends BindingObject>> cacheSpecifier) {
135 if (cacheSpecifier.isEmpty()) {
136 return new NonCachingCodec<>(this);
138 return new CachingNormalizedNodeCodec<>(this, ImmutableSet.copyOf(cacheSpecifier));
141 BindingStreamEventWriter createWriter(final NormalizedNodeStreamWriter domWriter) {
142 return BindingToNormalizedStreamWriter.create(this, domWriter);
145 protected final <V> @NonNull V childNonNull(final @Nullable V nullable,
146 final YangInstanceIdentifier.PathArgument child, final String message, final Object... args) {
147 if (nullable != null) {
150 MissingSchemaException.checkModulePresent(factory().getRuntimeContext().getSchemaContext(), child);
151 throw IncorrectNestingException.create(message, args);
154 protected final <V> @NonNull V childNonNull(final @Nullable V nullable, final QName child, final String message,
155 final Object... args) {
156 if (nullable != null) {
159 MissingSchemaException.checkModulePresent(factory().getRuntimeContext().getSchemaContext(), child);
160 throw IncorrectNestingException.create(message, args);
163 protected final <V> @NonNull V childNonNull(final @Nullable V nullable, final Class<?> childClass,
164 final String message, final Object... args) {
165 if (nullable != null) {
168 MissingSchemaForClassException.check(factory().getRuntimeContext(), childClass);
169 MissingClassInLoadingStrategyException.check(factory().getRuntimeContext().getStrategy(), childClass);
170 throw IncorrectNestingException.create(message, args);
173 DataObjectSerializer eventStreamSerializer() {
174 if (eventStreamSerializer == null) {
175 eventStreamSerializer = factory().getEventStreamSerializer(getBindingClass());
177 return eventStreamSerializer;
181 public NormalizedNode<?, ?> serialize(final D data) {
182 final NormalizedNodeResult result = new NormalizedNodeResult();
183 // We create DOM stream writer which produces normalized nodes
184 final NormalizedNodeStreamWriter domWriter = ImmutableNormalizedNodeStreamWriter.from(result);
185 writeAsNormalizedNode(data, domWriter);
186 return result.getResult();
190 public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
192 eventStreamSerializer().serialize(data, createWriter(writer));
193 } catch (final IOException e) {
194 throw new IllegalStateException("Failed to serialize Binding DTO",e);