Merge "Startup archetype: remove 'Impl' from config subsystem Module name."
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.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.netconf.api.NetconfMessage;
15 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
16 import org.opendaylight.controller.sal.connect.api.MessageTransformer;
17 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
18 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
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 logger = LoggerFactory.getLogger(NotificationHandler.class);
29
30     private final RemoteDeviceHandler<?> salFacade;
31     private final List<NetconfMessage> queue = new LinkedList<>();
32     private final MessageTransformer<NetconfMessage> messageTransformer;
33     private final RemoteDeviceId id;
34     private boolean passNotifications = false;
35     private NotificationFilter filter;
36
37     NotificationHandler(final RemoteDeviceHandler<?> salFacade, final MessageTransformer<NetconfMessage> messageTransformer, final RemoteDeviceId id) {
38         this.salFacade = Preconditions.checkNotNull(salFacade);
39         this.messageTransformer = Preconditions.checkNotNull(messageTransformer);
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      */
54     synchronized void onRemoteSchemaUp() {
55         passNotifications = true;
56
57         for (final NetconfMessage cachedNotification : queue) {
58             passNotification(transformNotification(cachedNotification));
59         }
60
61         queue.clear();
62     }
63
64     private CompositeNode transformNotification(final NetconfMessage cachedNotification) {
65         final CompositeNode parsedNotification = messageTransformer.toNotification(cachedNotification);
66         Preconditions.checkNotNull(parsedNotification, "%s: Unable to parse received notification: %s", id, cachedNotification);
67         return parsedNotification;
68     }
69
70     private void queueNotification(final NetconfMessage notification) {
71         Preconditions.checkState(passNotifications == false);
72
73         logger.debug("{}: Caching notification {}, remote schema not yet fully built", id, notification);
74         if(logger.isTraceEnabled()) {
75             logger.trace("{}: Caching notification {}", id, XmlUtil.toString(notification.getDocument()));
76         }
77
78         queue.add(notification);
79     }
80
81     private synchronized void passNotification(final CompositeNode parsedNotification) {
82         logger.debug("{}: Forwarding notification {}", id, parsedNotification);
83
84         if(filter == null || filter.filterNotification(parsedNotification).isPresent()) {
85             salFacade.onNotification(parsedNotification);
86         }
87     }
88
89     synchronized void addNotificationFilter(final NotificationFilter filter) {
90         this.filter = filter;
91     }
92
93     synchronized void onRemoteSchemaDown() {
94         queue.clear();
95         passNotifications = false;
96     }
97
98     static interface NotificationFilter {
99
100         Optional<CompositeNode> filterNotification(CompositeNode notification);
101     }
102 }