Binding2 runtime - Codecs - serialized
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / serialized / LazySerializedDOMNotification.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.javav2.dom.codec.serialized;
9
10 import com.google.common.annotations.Beta;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.serializer.BindingNormalizedNodeSerializer;
13 import org.opendaylight.mdsal.binding.javav2.runtime.reflection.BindingReflections;
14 import org.opendaylight.mdsal.binding.javav2.spec.base.Notification;
15 import org.opendaylight.mdsal.dom.api.DOMNotification;
16 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
17 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
18
19 /**
20  * Lazy serialized implementation of DOM Notification.
21  *
22  * <p>
23  * This implementation performs serialization of data, only if receiver of notification actually accessed data
24  * from notification.
25  *
26  */
27 @Beta
28 public final class LazySerializedDOMNotification implements DOMNotification {
29
30     private final BindingNormalizedNodeSerializer codec;
31     private final Notification<?> data;
32     private final SchemaPath type;
33
34     private ContainerNode domBody;
35
36     private LazySerializedDOMNotification(final BindingNormalizedNodeSerializer codec, final Notification<?> data,
37             final SchemaPath type) {
38         this.codec = codec;
39         this.data = data;
40         this.type = type;
41     }
42
43     /**
44      * Create serializer of Binding notification data to DOM notification with specific codec.
45      *
46      * @param codec
47      *            - specific codec
48      * @param data
49      *            - binding notification data
50      * @return DOM notification serializer
51      */
52     public static DOMNotification create(final BindingNormalizedNodeSerializer codec, final Notification<?> data) {
53         final SchemaPath type = SchemaPath.create(true, BindingReflections.findQName(data.getClass()));
54         return new LazySerializedDOMNotification(codec, data, type);
55     }
56
57     @Nonnull
58     @Override
59     public SchemaPath getType() {
60         return type;
61     }
62
63     @Nonnull
64     @Override
65     public ContainerNode getBody() {
66         if (domBody == null) {
67             domBody = codec.toNormalizedNodeNotification(data);
68         }
69         return domBody;
70     }
71
72     /**
73      * Get binding notification data.
74      *
75      * @return binding notification data
76      */
77     public Notification<?> getBindingData() {
78         return data;
79     }
80 }