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