Move RemoteDeviceId
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / NotificationHandler.java
1 /*
2  * Copyright (c) 2014 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.netconf.sal.connect.netconf;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
13
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.function.Predicate;
17 import org.opendaylight.mdsal.dom.api.DOMNotification;
18 import org.opendaylight.netconf.api.NetconfMessage;
19 import org.opendaylight.netconf.api.xml.XmlUtil;
20 import org.opendaylight.netconf.sal.connect.api.NotificationTransformer;
21 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
22 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Handles incoming notifications. Either caches them(until onRemoteSchemaUp is called) or passes to sal Facade.
28  */
29 final class NotificationHandler {
30     private static final Logger LOG = LoggerFactory.getLogger(NotificationHandler.class);
31
32     private final RemoteDeviceHandler salFacade;
33     // FIXME: better implementation?
34     private final List<NetconfMessage> queue = new LinkedList<>();
35     private final RemoteDeviceId id;
36
37     private boolean passNotifications = false;
38     private NotificationFilter filter;
39     private NotificationTransformer messageTransformer;
40
41     NotificationHandler(final RemoteDeviceHandler salFacade, final RemoteDeviceId id) {
42         this.salFacade = requireNonNull(salFacade);
43         this.id = requireNonNull(id);
44     }
45
46     synchronized void handleNotification(final NetconfMessage notification) {
47         if (passNotifications) {
48             passNotification(transformNotification(notification));
49         } else {
50             queueNotification(notification);
51         }
52     }
53
54     /**
55      * Forward all cached notifications and pass all notifications from this point directly to sal facade.
56      * @param transformer Message transformer
57      */
58     synchronized void onRemoteSchemaUp(final NotificationTransformer transformer) {
59         messageTransformer = requireNonNull(transformer);
60
61         passNotifications = true;
62
63         for (final NetconfMessage cachedNotification : queue) {
64             passNotification(transformNotification(cachedNotification));
65         }
66
67         queue.clear();
68     }
69
70     private DOMNotification transformNotification(final NetconfMessage cachedNotification) {
71         return checkNotNull(messageTransformer.toNotification(cachedNotification),
72             "%s: Unable to parse received notification: %s", id, cachedNotification);
73     }
74
75     private void queueNotification(final NetconfMessage notification) {
76         checkState(!passNotifications);
77
78         LOG.debug("{}: Caching notification {}, remote schema not yet fully built", id, notification);
79         if (LOG.isTraceEnabled()) {
80             LOG.trace("{}: Caching notification {}", id, XmlUtil.toString(notification.getDocument()));
81         }
82
83         queue.add(notification);
84     }
85
86     private synchronized void passNotification(final DOMNotification parsedNotification) {
87         LOG.debug("{}: Forwarding notification {}", id, parsedNotification);
88
89         if (filter == null || filter.test(parsedNotification)) {
90             salFacade.onNotification(parsedNotification);
91         }
92     }
93
94     synchronized void addNotificationFilter(final NotificationFilter newFilter) {
95         filter = newFilter;
96     }
97
98     synchronized void onRemoteSchemaDown() {
99         queue.clear();
100         passNotifications = false;
101         messageTransformer = null;
102     }
103
104     @FunctionalInterface
105     interface NotificationFilter extends Predicate<DOMNotification> {
106
107     }
108 }