2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf;
10 import com.google.common.base.Preconditions;
11 import java.util.LinkedList;
12 import java.util.List;
13 import java.util.Optional;
14 import org.opendaylight.mdsal.dom.api.DOMNotification;
15 import org.opendaylight.netconf.api.NetconfMessage;
16 import org.opendaylight.netconf.api.xml.XmlUtil;
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;
24 * Handles incoming notifications. Either caches them(until onRemoteSchemaUp is called) or passes to sal Facade.
26 final class NotificationHandler {
28 private static final Logger LOG = LoggerFactory.getLogger(NotificationHandler.class);
30 private final RemoteDeviceHandler<?> salFacade;
31 private final List<NetconfMessage> queue = new LinkedList<>();
32 private final RemoteDeviceId id;
33 private boolean passNotifications = false;
35 private NotificationFilter filter;
36 private MessageTransformer<NetconfMessage> messageTransformer;
38 NotificationHandler(final RemoteDeviceHandler<?> salFacade, final RemoteDeviceId id) {
39 this.salFacade = Preconditions.checkNotNull(salFacade);
40 this.id = Preconditions.checkNotNull(id);
43 synchronized void handleNotification(final NetconfMessage notification) {
44 if (passNotifications) {
45 passNotification(transformNotification(notification));
47 queueNotification(notification);
52 * Forward all cached notifications and pass all notifications from this point directly to sal facade.
53 * @param transformer Message transformer
55 synchronized void onRemoteSchemaUp(final MessageTransformer<NetconfMessage> transformer) {
56 this.messageTransformer = Preconditions.checkNotNull(transformer);
58 passNotifications = true;
60 for (final NetconfMessage cachedNotification : queue) {
61 passNotification(transformNotification(cachedNotification));
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;
74 private void queueNotification(final NetconfMessage notification) {
75 Preconditions.checkState(!passNotifications);
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()));
82 queue.add(notification);
85 private synchronized void passNotification(final DOMNotification parsedNotification) {
86 LOG.debug("{}: Forwarding notification {}", id, parsedNotification);
88 if (filter == null || filter.filterNotification(parsedNotification).isPresent()) {
89 salFacade.onNotification(parsedNotification);
93 synchronized void addNotificationFilter(final NotificationFilter newFilter) {
94 this.filter = newFilter;
97 synchronized void onRemoteSchemaDown() {
99 passNotifications = false;
100 messageTransformer = null;
103 interface NotificationFilter {
105 Optional<DOMNotification> filterNotification(DOMNotification notification);