ffab566e68e03c8dda5cbddcb418accbc47a7476
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / 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.controller.md.sal.binding.impl;
9
10 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
11 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
12 import org.opendaylight.yangtools.yang.binding.Notification;
13 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
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  * This implementation performs serialization of data, only if receiver
21  * of notification actually accessed data from notification.
22  *
23  */
24 public final class LazySerializedDOMNotification implements DOMNotification {
25
26     private final BindingNormalizedNodeSerializer codec;
27     private final Notification data;
28     private final SchemaPath type;
29
30     private ContainerNode domBody;
31
32     private LazySerializedDOMNotification(final BindingNormalizedNodeSerializer codec, final Notification data, final SchemaPath type) {
33         this.codec = codec;
34         this.data = data;
35         this.type = type;
36     }
37
38     static DOMNotification create(final BindingNormalizedNodeSerializer codec, final Notification data) {
39         final SchemaPath type = SchemaPath.create(true, BindingReflections.findQName(data.getImplementedInterface()));
40         return new LazySerializedDOMNotification(codec, data, type);
41     }
42
43     @Override
44     public SchemaPath getType() {
45         return type;
46     }
47
48     @Override
49     public ContainerNode getBody() {
50         if (domBody == null) {
51             domBody = codec.toNormalizedNodeNotification(data);
52         }
53         return domBody;
54     }
55
56     public Notification getBindingData() {
57         return data;
58     }
59 }