Use mdsal.dom.codec instead of yangtools.data.codec
[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         super();
34         this.codec = codec;
35         this.data = data;
36         this.type = type;
37     }
38
39     static DOMNotification create(final BindingNormalizedNodeSerializer codec, final Notification data) {
40         final SchemaPath type = SchemaPath.create(true, BindingReflections.findQName(data.getImplementedInterface()));
41         return new LazySerializedDOMNotification(codec, data, type);
42     }
43
44     @Override
45     public SchemaPath getType() {
46         return type;
47     }
48
49     @Override
50     public ContainerNode getBody() {
51         if (domBody == null) {
52             domBody = codec.toNormalizedNodeNotification(data);
53         }
54         return domBody;
55     }
56
57     public Notification getBindingData() {
58         return data;
59     }
60 }