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.yangtools.binding.data.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.yangtools.yang.binding.Augmentable;
18 import org.opendaylight.yangtools.yang.binding.Augmentation;
19 import org.opendaylight.yangtools.yang.binding.BindingStreamEventWriter;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.DataObjectSerializer;
22 import org.opendaylight.yangtools.yang.binding.DataObjectSerializerImplementation;
23 import org.opendaylight.yangtools.yang.binding.DataObjectSerializerRegistry;
24 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
30 * Dispatch serializer, which emits {@link BindingStreamEventWriter#startAugmentationNode(Class)}
31 * events for supplied augmentation node.
34 public class AugmentableDispatchSerializer implements DataObjectSerializerImplementation {
36 private static final Logger LOG = LoggerFactory.getLogger(AugmentableDispatchSerializer.class);
39 public void serialize(final DataObjectSerializerRegistry reg, final DataObject obj,
40 final BindingStreamEventWriter stream) throws IOException {
41 if (obj instanceof Augmentable<?>) {
42 final Map<Class<? extends Augmentation<?>>, Augmentation<?>> augmentations;
43 if (reg instanceof AugmentationReader) {
44 augmentations = ((AugmentationReader) reg).getAugmentations(obj);
45 } else if (Proxy.isProxyClass(obj.getClass())) {
46 augmentations = getFromProxy(obj);
48 augmentations = BindingReflections.getAugmentations((Augmentable<?>) obj);
50 for (final Entry<Class<? extends Augmentation<?>>, Augmentation<?>> aug : augmentations.entrySet()) {
51 emitAugmentation(aug.getKey(), aug.getValue(), stream, reg);
56 private Map<Class<? extends Augmentation<?>>, Augmentation<?>> getFromProxy(final DataObject obj) {
57 final InvocationHandler proxy = Proxy.getInvocationHandler(obj);
58 if (proxy instanceof AugmentationReader) {
59 return ((AugmentationReader) proxy).getAugmentations(obj);
61 return Collections.emptyMap();
64 @SuppressWarnings("rawtypes")
65 private void emitAugmentation(final Class type, final Augmentation<?> value, final BindingStreamEventWriter stream,
66 final DataObjectSerializerRegistry registry) throws IOException {
68 * Binding Specification allowed to insert augmentation with null for
69 * value, which effectively could be used to remove augmentation
75 Preconditions.checkArgument(value instanceof DataObject);
76 @SuppressWarnings("unchecked")
77 final DataObjectSerializer serializer = registry.getSerializer(type);
78 if (serializer != null) {
79 serializer.serialize((DataObject) value, stream);
81 LOG.warn("DataObjectSerializer is not present for {} in registry {}", type, registry);