Binding v2 DOM Codec - augmentation 80/58680/2
authorJakub Toth <jakub.toth@pantheon.tech>
Tue, 6 Jun 2017 12:29:06 +0000 (14:29 +0200)
committerMartin Ciglan <martin.ciglan@pantheon.tech>
Sun, 11 Jun 2017 19:34:10 +0000 (19:34 +0000)
  * api for augmentation
  * impl of agumentation serializer
  * fix jdoc in spec
    * use of TreeNode instead of DataObject

Change-Id: I7c6ec42ddb8bc7f3957e3a82b55fb982fab6de4d
Signed-off-by: Jakub Toth <jakub.toth@pantheon.tech>
(cherry picked from commit 6c7141733d72557464d267762a755fa75056b2e5)

binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/api/AugmentationReader.java [new file with mode: 0644]
binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/serializer/AugmentableDispatchSerializer.java [new file with mode: 0644]
binding2/mdsal-binding2-spec/src/main/java/org/opendaylight/mdsal/binding/javav2/spec/runtime/TreeNodeSerializerImplementation.java

diff --git a/binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/api/AugmentationReader.java b/binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/api/AugmentationReader.java
new file mode 100644 (file)
index 0000000..a5243a3
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.javav2.dom.codec.api;
+
+import com.google.common.annotations.Beta;
+import java.lang.reflect.InvocationHandler;
+import java.util.Map;
+import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
+import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
+
+/**
+ * Interface which should be implemented by proxy {@link InvocationHandler} to
+ * obtain augmentations from proxy implementations of {@link Augmentable}
+ * object.
+ *
+ * If implemented proxy does not implement this interface, its augmentations are
+ * not properly serialized / deserialized.
+ */
+@Beta
+public interface AugmentationReader {
+
+    /**
+     * Get augmentations.
+     *
+     * @param obj
+     *            - object implemented this interface
+     * @return augmentations
+     */
+    Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAugmentations(Object obj);
+}
\ No newline at end of file
diff --git a/binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/serializer/AugmentableDispatchSerializer.java b/binding2/mdsal-binding2-dom-codec/src/main/java/org/opendaylight/mdsal/binding/javav2/dom/codec/impl/serializer/AugmentableDispatchSerializer.java
new file mode 100644 (file)
index 0000000..9fc787a
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.serializer;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Preconditions;
+import java.io.IOException;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Map.Entry;
+import org.opendaylight.mdsal.binding.javav2.dom.codec.api.AugmentationReader;
+import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
+import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingStreamEventWriter;
+import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializer;
+import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializerImplementation;
+import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializerRegistry;
+import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
+import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
+import org.opendaylight.mdsal.binding.javav2.spec.util.BindingReflections;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Dispatch serializer, which emits
+ * {@link BindingStreamEventWriter#startAugmentationNode(Class)} events for
+ * supplied augmentation node.
+ */
+@Beta
+public class AugmentableDispatchSerializer implements TreeNodeSerializerImplementation {
+
+    private static final Logger LOG = LoggerFactory.getLogger(AugmentableDispatchSerializer.class);
+
+    @Override
+    public void serialize(final TreeNodeSerializerRegistry reg, final TreeNode obj,
+            final BindingStreamEventWriter stream) throws IOException {
+        if (obj instanceof Augmentable<?>) {
+            final Map<Class<? extends Augmentation<?>>, Augmentation<?>> augmentations;
+            if (reg instanceof AugmentationReader) {
+                augmentations = ((AugmentationReader) reg).getAugmentations(obj);
+            } else if (Proxy.isProxyClass(obj.getClass())) {
+                augmentations = getFromProxy(obj);
+            } else {
+                augmentations = BindingReflections.getAugmentations((Augmentable<?>) obj);
+            }
+            for (final Entry<Class<? extends Augmentation<?>>, Augmentation<?>> aug : augmentations.entrySet()) {
+                emitAugmentation(aug.getKey(), aug.getValue(), stream, reg);
+            }
+        }
+    }
+
+    private Map<Class<? extends Augmentation<?>>, Augmentation<?>> getFromProxy(final TreeNode obj) {
+        final InvocationHandler proxy = Proxy.getInvocationHandler(obj);
+        if (proxy instanceof AugmentationReader) {
+            return ((AugmentationReader) proxy).getAugmentations(obj);
+        }
+        return Collections.emptyMap();
+    }
+
+    @SuppressWarnings("rawtypes")
+    private void emitAugmentation(final Class type, final Augmentation<?> value, final BindingStreamEventWriter stream,
+            final TreeNodeSerializerRegistry registry) throws IOException {
+        Preconditions.checkArgument(value instanceof TreeNode);
+        @SuppressWarnings("unchecked")
+        final TreeNodeSerializer serializer = registry.getSerializer(type);
+        if (serializer != null) {
+            serializer.serialize((TreeNode) value, stream);
+        } else {
+            LOG.warn("TreeNodeSerializer is not present for {} in registry {}", type, registry);
+        }
+    }
+}
\ No newline at end of file
index b38810af11461bc36ccd33eeff07809085427113..a13d7b4ca1ce269f0006c16d48019756722acf77 100644 (file)
@@ -19,11 +19,10 @@ import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
 public interface TreeNodeSerializerImplementation {
 
     /**
-     * Writes stream events for supplied data object to provided stream.
+     * Writes stream events for supplied tree node to provided stream.
      *
-     * DataObjectSerializerRegistry may be used to lookup serializers
-     * for other generated classes  in order to support writing
-     * their events.
+     * TreeNodeSerializerRegistry may be used to lookup serializers for other generated classes in order to
+     * support writing their events.
      */
     void serialize(TreeNodeSerializerRegistry reg, TreeNode obj, BindingStreamEventWriter stream) throws IOException;
 }