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.util;
10 import com.google.common.base.Preconditions;
11 import java.io.IOException;
12 import java.lang.reflect.InvocationHandler;
13 import java.lang.reflect.Proxy;
14 import java.util.Collections;
16 import java.util.Map.Entry;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingStreamEventWriter;
18 import org.opendaylight.mdsal.binding.dom.codec.impl.DataObjectSerializer;
19 import org.opendaylight.mdsal.binding.dom.codec.impl.DataObjectSerializerImplementation;
20 import org.opendaylight.mdsal.binding.dom.codec.impl.DataObjectSerializerRegistry;
21 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
22 import org.opendaylight.yangtools.yang.binding.Augmentable;
23 import org.opendaylight.yangtools.yang.binding.Augmentation;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Dispatch serializer, which emits {@link BindingStreamEventWriter#startAugmentationNode(Class)}
30 * events for supplied augmentation node.
32 @Deprecated(forRemoval = true)
33 public class AugmentableDispatchSerializer implements DataObjectSerializerImplementation {
35 private static final Logger LOG = LoggerFactory.getLogger(AugmentableDispatchSerializer.class);
38 public void serialize(final DataObjectSerializerRegistry reg, final DataObject obj,
39 final BindingStreamEventWriter stream) throws IOException {
40 if (obj instanceof Augmentable<?>) {
41 final Map<Class<? extends Augmentation<?>>, Augmentation<?>> augmentations;
42 if (reg instanceof AugmentationReader) {
43 augmentations = ((AugmentationReader) reg).getAugmentations(obj);
44 } else if (Proxy.isProxyClass(obj.getClass())) {
45 augmentations = getFromProxy(obj);
47 augmentations = BindingReflections.getAugmentations((Augmentable<?>) obj);
49 for (final Entry<Class<? extends Augmentation<?>>, Augmentation<?>> aug : augmentations.entrySet()) {
50 emitAugmentation(aug.getKey(), aug.getValue(), stream, reg);
55 private static Map<Class<? extends Augmentation<?>>, Augmentation<?>> getFromProxy(final DataObject obj) {
56 final InvocationHandler proxy = Proxy.getInvocationHandler(obj);
57 if (proxy instanceof AugmentationReader) {
58 return ((AugmentationReader) proxy).getAugmentations(obj);
60 return Collections.emptyMap();
63 @SuppressWarnings("rawtypes")
64 private static void emitAugmentation(final Class type, final Augmentation<?> value,
65 final BindingStreamEventWriter stream, final DataObjectSerializerRegistry registry) throws IOException {
67 * Binding Specification allowed to insert augmentation with null for
68 * value, which effectively could be used to remove augmentation
74 Preconditions.checkArgument(value instanceof DataObject);
75 @SuppressWarnings("unchecked")
76 final DataObjectSerializer serializer = registry.getSerializer(type);
77 if (serializer != null) {
78 serializer.serialize((DataObject) value, stream);
80 LOG.warn("DataObjectSerializer is not present for {} in registry {}", type, registry);