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 com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.io.IOException;
13 import org.opendaylight.mdsal.binding.javav2.spec.base.Instantiable;
14 import org.opendaylight.mdsal.binding.javav2.spec.base.Item;
15 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
16 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingStreamEventWriter;
17 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializer;
18 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializerImplementation;
19 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializerRegistry;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Dispatch serializer, which emit DOM data from Binding v2 via stream writer.
27 public final class ChoiceDispatchSerializer implements TreeNodeSerializerImplementation {
29 private static final Logger LOG = LoggerFactory.getLogger(ChoiceDispatchSerializer.class);
31 @SuppressWarnings("rawtypes")
32 private final Class choiceClass;
34 @SuppressWarnings("rawtypes")
35 private ChoiceDispatchSerializer(final Class choiceClass) {
36 this.choiceClass = Preconditions.checkNotNull(choiceClass);
40 * Prepare instance of serializer from choice class.
44 * @return instance of serializer
46 public static ChoiceDispatchSerializer from(final Class<? extends Instantiable<?>> choiceClass) {
47 return new ChoiceDispatchSerializer(choiceClass);
50 @SuppressWarnings("unchecked")
52 public void serialize(final TreeNodeSerializerRegistry reg, final TreeNode obj,
53 final BindingStreamEventWriter stream) throws IOException {
54 @SuppressWarnings("rawtypes")
55 final Class cazeClass = ((Instantiable<?>) obj).implementedInterface();
56 stream.startChoiceNode(new Item<>(choiceClass), BindingStreamEventWriter.UNKNOWN_SIZE);
57 final TreeNodeSerializer caseSerializer = reg.getSerializer(cazeClass);
58 if (caseSerializer != null) {
59 caseSerializer.serialize(obj, stream);
61 LOG.warn("No serializer for case {} is available in registry {}", cazeClass, reg);