BUG-2314 Migrate netconf-connector to NormalizedNode
[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.schema.ContainerNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Handles incoming notifications. Either caches them(until onRemoteSchemaUp is called) or passes to sal Facade.
26  */
27 final class NotificationHandler {
28
29     private static final Logger logger = LoggerFactory.getLogger(NotificationHandler.class);
30
31     private final RemoteDeviceHandler<?> salFacade;
32     private final List<NetconfMessage> queue = new LinkedList<>();
33     private final RemoteDeviceId id;
34     private boolean passNotifications = false;
35
36     private NotificationFilter filter;
37     private MessageTransformer<NetconfMessage> messageTransformer;
38
39     NotificationHandler(final RemoteDeviceHandler<?> salFacade, final RemoteDeviceId id) {
40         this.salFacade = Preconditions.checkNotNull(salFacade);
41         this.id = Preconditions.checkNotNull(id);
42     }
43
44     synchronized void handleNotification(final NetconfMessage notification) {
45         if(passNotifications) {
46             passNotification(transformNotification(notification));
47         } else {
48             queueNotification(notification);
49         }
50     }
51
52     /**
53      * Forward all cached notifications and pass all notifications from this point directly to sal facade.
54      * @param messageTransformer
55      */
56     synchronized void onRemoteSchemaUp(final MessageTransformer<NetconfMessage> messageTransformer) {
57         this.messageTransformer = Preconditions.checkNotNull(messageTransformer);
58
59         passNotifications = true;
60
61         for (final NetconfMessage cachedNotification : queue) {
62             passNotification(transformNotification(cachedNotification));
63         }
64
65         queue.clear();
66     }
67
68     private ContainerNode transformNotification(final NetconfMessage cachedNotification) {
69         final ContainerNode parsedNotification = messageTransformer.toNotification(cachedNotification);
70         Preconditions.checkNotNull(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         logger.debug("{}: Caching notification {}, remote schema not yet fully built", id, notification);
78         if(logger.isTraceEnabled()) {
79             logger.trace("{}: Caching notification {}", id, XmlUtil.toString(notification.getDocument()));
80         }
81
82         queue.add(notification);
83     }
84
85     private synchronized void passNotification(final ContainerNode parsedNotification) {
86         logger.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     static interface NotificationFilter {
104
105         Optional<NormalizedNode<?, ?>> filterNotification(NormalizedNode<?, ?> notification);
106     }
107 }