Migrate OSGI compendium reference
[controller.git] / opendaylight / md-sal / sal-dom-compat / src / main / java / org / opendaylight / controller / sal / core / compat / LegacyDOMNotificationServiceAdapter.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.sal.core.compat;
9
10 import static java.util.Objects.requireNonNull;
11
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;
25
26 @Deprecated(forRemoval = true)
27 public class LegacyDOMNotificationServiceAdapter extends ForwardingObject implements DOMNotificationService {
28     private final org.opendaylight.mdsal.dom.api.DOMNotificationService delegate;
29
30     public LegacyDOMNotificationServiceAdapter(final org.opendaylight.mdsal.dom.api.DOMNotificationService delegate) {
31         this.delegate = requireNonNull(delegate);
32     }
33
34     @Override
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);
41                         return;
42                     }
43
44                     if (notification instanceof org.opendaylight.mdsal.dom.api.DOMEvent) {
45                         listener.onNotification(new DefaultDOMEvent(notification,
46                             (org.opendaylight.mdsal.dom.api.DOMEvent)notification));
47                         return;
48                     }
49
50                     listener.onNotification(new DefaultDOMNotification(notification));
51                 }, types);
52
53         return new AbstractListenerRegistration<T>(listener) {
54             @Override
55             protected void removeRegistration() {
56                 reg.close();
57             }
58         };
59     }
60
61     @Override
62     public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener,
63             final SchemaPath... types) {
64         return registerNotificationListener(listener, Arrays.asList(types));
65     }
66
67     @Override
68     protected org.opendaylight.mdsal.dom.api.DOMNotificationService delegate() {
69         return delegate;
70     }
71
72     private static class DefaultDOMNotification implements DOMNotification {
73         private final org.opendaylight.mdsal.dom.api.DOMNotification delegate;
74
75         DefaultDOMNotification(final org.opendaylight.mdsal.dom.api.DOMNotification delegate) {
76             this.delegate = requireNonNull(delegate);
77         }
78
79         @Override
80         public SchemaPath getType() {
81             return delegate.getType();
82         }
83
84         @Override
85         public ContainerNode getBody() {
86             return delegate.getBody();
87         }
88     }
89
90     private static class DefaultDOMEvent extends DefaultDOMNotification implements DOMEvent {
91         private final Date eventTime;
92
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;
98         }
99
100         @Override
101         public Date getEventTime() {
102             return eventTime;
103         }
104     }
105 }