2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.controller.sal.core.compat;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ForwardingObject;
13 import java.time.Instant;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Date;
17 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
18 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
19 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
21 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 public class LegacyDOMNotificationServiceAdapter extends ForwardingObject implements DOMNotificationService {
28 private final org.opendaylight.mdsal.dom.api.DOMNotificationService delegate;
30 public LegacyDOMNotificationServiceAdapter(final org.opendaylight.mdsal.dom.api.DOMNotificationService delegate) {
31 this.delegate = requireNonNull(delegate);
35 public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener,
36 final Collection<SchemaPath> types) {
37 final ListenerRegistration<org.opendaylight.mdsal.dom.api.DOMNotificationListener> reg =
38 delegate().registerNotificationListener(notification -> {
39 if (notification instanceof DOMNotification) {
40 listener.onNotification((DOMNotification)notification);
44 if (notification instanceof org.opendaylight.mdsal.dom.api.DOMEvent) {
45 listener.onNotification(new DefaultDOMEvent(notification,
46 (org.opendaylight.mdsal.dom.api.DOMEvent)notification));
50 listener.onNotification(new DefaultDOMNotification(notification));
53 return new AbstractListenerRegistration<T>(listener) {
55 protected void removeRegistration() {
62 public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener,
63 final SchemaPath... types) {
64 return registerNotificationListener(listener, Arrays.asList(types));
68 protected org.opendaylight.mdsal.dom.api.DOMNotificationService delegate() {
72 private static class DefaultDOMNotification implements DOMNotification {
73 private final org.opendaylight.mdsal.dom.api.DOMNotification delegate;
75 DefaultDOMNotification(final org.opendaylight.mdsal.dom.api.DOMNotification delegate) {
76 this.delegate = requireNonNull(delegate);
80 public SchemaPath getType() {
81 return delegate.getType();
85 public ContainerNode getBody() {
86 return delegate.getBody();
90 private static class DefaultDOMEvent extends DefaultDOMNotification implements DOMEvent {
91 private final Date eventTime;
93 DefaultDOMEvent(final org.opendaylight.mdsal.dom.api.DOMNotification fromNotification,
94 final org.opendaylight.mdsal.dom.api.DOMEvent fromEvent) {
95 super(fromNotification);
96 final Instant eventInstant = fromEvent.getEventInstant();
97 this.eventTime = eventInstant != null ? Date.from(eventInstant) : null;
101 public Date getEventTime() {