96e346eab35a02601397d838aec46faacf8fcd0f
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / DataObjectStreamer.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
9
10 import static com.google.common.base.Verify.verify;
11
12 import com.google.common.annotations.Beta;
13 import java.io.IOException;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import org.opendaylight.mdsal.binding.dom.codec.api.BindingStreamEventWriter;
19 import org.opendaylight.yangtools.yang.binding.Augmentable;
20 import org.opendaylight.yangtools.yang.binding.Augmentation;
21 import org.opendaylight.yangtools.yang.binding.DataContainer;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.Identifiable;
24 import org.opendaylight.yangtools.yang.binding.OpaqueObject;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Base superclass for all concrete streamers, that is objects which are able to turn a concrete DataObject into a
30  * stream of events.
31  *
32  * @param <T> DataObject type
33  */
34 @Beta
35 public abstract class DataObjectStreamer<T extends DataObject> implements DataObjectSerializerImplementation {
36     private static final Logger LOG = LoggerFactory.getLogger(DataObjectStreamer.class);
37
38     protected DataObjectStreamer() {
39
40     }
41
42     protected static final void streamAnydata(final BindingStreamEventWriter writer, final String localName,
43             final Object value) throws IOException {
44         if (value != null && writer instanceof AnydataBindingStreamWriter) {
45             verify(value instanceof OpaqueObject, "Unexpected data %s", value);
46             ((AnydataBindingStreamWriter) writer).anydataNode(localName, (OpaqueObject<?>) value);
47         }
48     }
49
50     protected static final void streamAnyxml(final BindingStreamEventWriter writer, final String localName,
51             final Object value) throws IOException {
52         if (value != null) {
53             writer.anyxmlNode(localName, value);
54         }
55     }
56
57     protected static final void streamAugmentations(final DataObjectSerializerRegistry registry,
58             final BindingStreamEventWriter writer, final Augmentable<?> obj) throws IOException {
59         for (final var aug : obj.augmentations().entrySet()) {
60             emitAugmentation(aug.getKey(), aug.getValue(), writer, registry);
61         }
62     }
63
64     protected static final <C extends DataContainer> void streamChoice(final Class<C> choiceClass,
65             final DataObjectSerializerRegistry registry, final BindingStreamEventWriter writer, final C value)
66                     throws IOException {
67         if (value != null) {
68             final Class<? extends DataContainer> caseClass = value.implementedInterface();
69             writer.startChoiceNode(choiceClass, BindingStreamEventWriter.UNKNOWN_SIZE);
70             final DataObjectSerializer caseStreamer = registry.getSerializer(caseClass.asSubclass(DataObject.class));
71             if (caseStreamer != null) {
72                 if (tryCache(writer, (DataObject) value)) {
73                     caseStreamer.serialize((DataObject) value, writer);
74                 }
75             } else {
76                 LOG.warn("No serializer for case {} is available in registry {}", caseClass, registry);
77             }
78
79             writer.endNode();
80         }
81     }
82
83     protected static final <C extends DataObject> void streamContainer(final DataObjectStreamer<C> childStreamer,
84             final DataObjectSerializerRegistry registry, final BindingStreamEventWriter writer, final C value)
85                     throws IOException {
86         if (value != null && tryCache(writer, value)) {
87             childStreamer.serialize(registry, value, writer);
88         }
89     }
90
91     protected static final void streamLeaf(final BindingStreamEventWriter writer, final String localName,
92             final Object value) throws IOException {
93         if (value != null) {
94             writer.leafNode(localName, value);
95         }
96     }
97
98     protected static final void streamLeafList(final BindingStreamEventWriter writer, final String localName,
99             final Set<?> value) throws IOException {
100         if (value != null) {
101             writer.startLeafSet(localName, value.size());
102             commonStreamLeafset(writer, value);
103         }
104     }
105
106     protected static final void streamOrderedLeafList(final BindingStreamEventWriter writer,
107             final String localName, final List<?> value) throws IOException {
108         if (value != null) {
109             writer.startOrderedLeafSet(localName, value.size());
110             commonStreamLeafset(writer, value);
111         }
112     }
113
114     protected static final <E extends DataObject> void streamList(final Class<E> childClass,
115             final DataObjectStreamer<E> childStreamer, final DataObjectSerializerRegistry registry,
116             final BindingStreamEventWriter writer, final List<? extends E> value) throws IOException {
117         final int size = nullSize(value);
118         if (size != 0) {
119             writer.startUnkeyedList(childClass, size);
120             commonStreamList(registry, writer, childStreamer, value);
121         }
122     }
123
124     protected static final <E extends DataObject & Identifiable<?>> void streamMap(final Class<E> childClass,
125             final DataObjectStreamer<E> childStreamer, final DataObjectSerializerRegistry registry,
126             final BindingStreamEventWriter writer, final Map<?, ? extends E> value) throws IOException {
127         final int size = nullSize(value);
128         if (size != 0) {
129             writer.startMapNode(childClass, size);
130             commonStreamList(registry, writer, childStreamer, value.values());
131         }
132     }
133
134     protected static final <E extends DataObject & Identifiable<?>> void streamOrderedMap(final Class<E> childClass,
135             final DataObjectStreamer<E> childStreamer, final DataObjectSerializerRegistry registry,
136             final BindingStreamEventWriter writer, final List<? extends E> value) throws IOException {
137         final int size = nullSize(value);
138         if (size != 0) {
139             writer.startOrderedMapNode(childClass, size);
140             commonStreamList(registry, writer, childStreamer, value);
141         }
142     }
143
144     private static <E extends DataObject> void commonStreamList(final DataObjectSerializerRegistry registry,
145             final BindingStreamEventWriter writer, final DataObjectStreamer<E> childStreamer,
146             final Collection<? extends E> value) throws IOException {
147
148         for (E entry : value) {
149             if (tryCache(writer, entry)) {
150                 childStreamer.serialize(registry, entry, writer);
151             }
152         }
153         writer.endNode();
154     }
155
156     private static void commonStreamLeafset(final BindingStreamEventWriter writer, final Collection<?> value)
157             throws IOException {
158         for (Object entry : value) {
159             writer.leafSetEntryNode(entry);
160         }
161         writer.endNode();
162     }
163
164     private static void emitAugmentation(final Class<? extends Augmentation<?>> type, final Augmentation<?> value,
165             final BindingStreamEventWriter writer, final DataObjectSerializerRegistry registry) throws IOException {
166         /*
167          * Binding Specification allowed to insert augmentation with null for value, which effectively could be used to
168          * remove augmentation from builder / DTO.
169          */
170         if (value != null) {
171             final DataObjectSerializer serializer = registry.getSerializer(type);
172             if (serializer != null) {
173                 serializer.serialize(value, writer);
174             } else {
175                 LOG.warn("DataObjectSerializer is not present for {} in registry {}", type, registry);
176             }
177         }
178     }
179
180     @SuppressWarnings("unchecked")
181     private static <T extends DataObject> boolean tryCache(final BindingStreamEventWriter writer, final T value) {
182         // Force serialization if writer is not a BindingSerializer, otherwise defer to it for a decision
183         return !(writer instanceof BindingSerializer) || ((BindingSerializer<?, T>) writer).serialize(value) == null;
184     }
185
186     private static int nullSize(final Collection<?> list) {
187         return list == null ? 0 : list.size();
188     }
189
190     private static int nullSize(final Map<?, ?> map) {
191         return map == null ? 0 : map.size();
192     }
193 }