2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.dom.adapter;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import java.time.Instant;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
18 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
19 import org.opendaylight.mdsal.dom.api.DOMEvent;
20 import org.opendaylight.mdsal.dom.api.DOMNotification;
21 import org.opendaylight.yangtools.yang.binding.Notification;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
26 * Lazy serialized implementation of DOM Notification.
29 * This implementation performs serialization of data, only if receiver of notification actually accessed data from
32 final class LazySerializedDOMNotification implements DOMNotification, DOMEvent {
33 private static final LoadingCache<Class<?>, Absolute> PATHS = CacheBuilder.newBuilder().weakKeys()
34 .build(new CacheLoader<Class<?>, Absolute>() {
36 public Absolute load(final Class<?> key) {
37 // TODO: for nested (YANG 1.1) notifications we will need the SchemaNodeIdentifier where the
38 // notification is being invoked and use that instead of ROOT. How Binding users will refer to
39 // it is TBD (but probably InstanceIdentifier, which means we will need to do some lifting to
40 // find the SchemaNodeIdentifier), as we will need to deal with the same multiplicies we need
41 // for data due to grouping-based reuse.
42 return Absolute.of(BindingReflections.findQName(key)).intern();
46 private final @NonNull BindingNormalizedNodeSerializer codec;
47 private final @NonNull Notification data;
48 private final @NonNull Absolute type;
49 private final @NonNull Instant eventInstant;
51 private volatile ContainerNode domBody;
53 LazySerializedDOMNotification(final BindingNormalizedNodeSerializer codec, final Notification data,
54 final Absolute type, final Instant eventInstant) {
55 this.codec = requireNonNull(codec);
56 this.data = requireNonNull(data);
57 this.type = requireNonNull(type);
58 this.eventInstant = requireNonNull(eventInstant);
61 static @NonNull DOMNotification create(final BindingNormalizedNodeSerializer codec, final Notification data,
62 final Instant eventInstant) {
63 final Absolute type = PATHS.getUnchecked(data.implementedInterface());
64 return new LazySerializedDOMNotification(codec, data, type, eventInstant);
68 public Absolute getType() {
73 public ContainerNode getBody() {
74 ContainerNode local = domBody;
76 domBody = local = codec.toNormalizedNodeNotification(data);
82 public Instant getEventInstant() {
86 @NonNull Notification getBindingData() {