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
8 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.serializer;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import java.io.IOException;
14 import org.opendaylight.mdsal.binding.javav2.spec.base.Instantiable;
15 import org.opendaylight.mdsal.binding.javav2.spec.base.Item;
16 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
17 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingStreamEventWriter;
18 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializer;
19 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializerImplementation;
20 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializerRegistry;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Dispatch serializer, which emit DOM data from Binding v2 via stream writer.
28 public final class ChoiceDispatchSerializer implements TreeNodeSerializerImplementation {
30 private static final Logger LOG = LoggerFactory.getLogger(ChoiceDispatchSerializer.class);
32 @SuppressWarnings("rawtypes")
33 private final Class choiceClass;
35 @SuppressWarnings("rawtypes")
36 private ChoiceDispatchSerializer(final Class choiceClass) {
37 this.choiceClass = requireNonNull(choiceClass);
41 * Prepare instance of serializer from choice class.
45 * @return instance of serializer
47 public static ChoiceDispatchSerializer from(final Class<? extends Instantiable<?>> choiceClass) {
48 return new ChoiceDispatchSerializer(choiceClass);
51 @SuppressWarnings("unchecked")
53 public void serialize(final TreeNodeSerializerRegistry reg, final TreeNode obj,
54 final BindingStreamEventWriter stream) throws IOException {
55 @SuppressWarnings("rawtypes")
56 final Class cazeClass = ((Instantiable<?>) obj).implementedInterface();
57 stream.startChoiceNode(new Item<>(choiceClass), BindingStreamEventWriter.UNKNOWN_SIZE);
58 final TreeNodeSerializer caseSerializer = reg.getSerializer(cazeClass);
59 if (caseSerializer != null) {
60 caseSerializer.serialize(obj, stream);
62 LOG.warn("No serializer for case {} is available in registry {}", cazeClass, reg);