Move BindingReflections to mdsal-binding-spec-util
[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 org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
11 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
12 import org.opendaylight.mdsal.dom.api.DOMNotification;
13 import org.opendaylight.yangtools.yang.binding.Notification;
14 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
15 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
16
17 /**
18  * Lazy serialized implementation of DOM Notification.
19  *
20  * <p>
21  * This implementation performs serialization of data, only if receiver
22  * of notification actually accessed data from notification.
23  *
24  */
25 public final class LazySerializedDOMNotification implements DOMNotification {
26
27     private final BindingNormalizedNodeSerializer codec;
28     private final Notification data;
29     private final SchemaPath type;
30
31     private ContainerNode domBody;
32
33     private LazySerializedDOMNotification(final BindingNormalizedNodeSerializer codec,
34             final Notification data, final SchemaPath type) {
35         this.codec = codec;
36         this.data = data;
37         this.type = type;
38     }
39
40     static DOMNotification create(final BindingNormalizedNodeSerializer codec, final Notification data) {
41         final SchemaPath type = SchemaPath.create(true, BindingReflections.findQName(data.getImplementedInterface()));
42         return new LazySerializedDOMNotification(codec, data, type);
43     }
44
45     @Override
46     public SchemaPath getType() {
47         return type;
48     }
49
50     @Override
51     public ContainerNode getBody() {
52         if (domBody == null) {
53             domBody = codec.toNormalizedNodeNotification(data);
54         }
55         return domBody;
56     }
57
58     public Notification getBindingData() {
59         return data;
60     }
61 }