Make LazySerializedDOMNotification a DOMEvent
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / LazySerializedDOMNotification.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.dom.adapter;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.time.Instant;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
15 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
16 import org.opendaylight.mdsal.dom.api.DOMEvent;
17 import org.opendaylight.mdsal.dom.api.DOMNotification;
18 import org.opendaylight.yangtools.yang.binding.Notification;
19 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21
22 /**
23  * Lazy serialized implementation of DOM Notification.
24  *
25  * <p>
26  * This implementation performs serialization of data, only if receiver of notification actually accessed data from
27  * notification.
28  */
29 public final class LazySerializedDOMNotification implements DOMNotification, DOMEvent {
30     private final @NonNull BindingNormalizedNodeSerializer codec;
31     private final @NonNull Notification data;
32     private final @NonNull SchemaPath type;
33     private final @NonNull Instant eventInstant;
34
35     private volatile ContainerNode domBody;
36
37     LazySerializedDOMNotification(final BindingNormalizedNodeSerializer codec, final Notification data,
38             final SchemaPath type, final Instant eventInstant) {
39         this.codec = requireNonNull(codec);
40         this.data = requireNonNull(data);
41         this.type = requireNonNull(type);
42         this.eventInstant = requireNonNull(eventInstant);
43     }
44
45     static @NonNull DOMNotification create(final BindingNormalizedNodeSerializer codec, final Notification data,
46             final Instant eventInstant) {
47         // TODO: for nested (YANG 1.1) notifications we will need the SchemaPath where the notification is being invoked
48         //       and use that instead of ROOT. How Binding users will refer to it is TBD (but probably
49         //       InstanceIdentifier, which means we will need to do some lifting to find the SchemaPath)
50         final SchemaPath type = SchemaPath.ROOT.createChild(BindingReflections.findQName(data.implementedInterface()));
51         return new LazySerializedDOMNotification(codec, data, type, eventInstant);
52     }
53
54     @Override
55     public SchemaPath getType() {
56         return type;
57     }
58
59     @Override
60     public ContainerNode getBody() {
61         ContainerNode local = domBody;
62         if (local == null) {
63             domBody = local = codec.toNormalizedNodeNotification(data);
64         }
65         return local;
66     }
67
68     @Override
69     public Instant getEventInstant() {
70         return eventInstant;
71     }
72
73     public @NonNull Notification getBindingData() {
74         return data;
75     }
76 }