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 static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
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;
27 * Handles incoming notifications. Either caches them(until onRemoteSchemaUp is called) or passes to sal Facade.
29 final class NotificationHandler {
30 private static final Logger LOG = LoggerFactory.getLogger(NotificationHandler.class);
32 private final RemoteDeviceHandler salFacade;
33 // FIXME: better implementation?
34 private final List<NetconfMessage> queue = new LinkedList<>();
35 private final RemoteDeviceId id;
37 private boolean passNotifications = false;
38 private NotificationFilter filter;
39 private NotificationTransformer messageTransformer;
41 NotificationHandler(final RemoteDeviceHandler salFacade, final RemoteDeviceId id) {
42 this.salFacade = requireNonNull(salFacade);
43 this.id = requireNonNull(id);
46 synchronized void handleNotification(final NetconfMessage notification) {
47 if (passNotifications) {
48 passNotification(transformNotification(notification));
50 queueNotification(notification);
55 * Forward all cached notifications and pass all notifications from this point directly to sal facade.
56 * @param transformer Message transformer
58 synchronized void onRemoteSchemaUp(final NotificationTransformer transformer) {
59 messageTransformer = requireNonNull(transformer);
61 passNotifications = true;
63 for (final NetconfMessage cachedNotification : queue) {
64 passNotification(transformNotification(cachedNotification));
70 private DOMNotification transformNotification(final NetconfMessage cachedNotification) {
71 return checkNotNull(messageTransformer.toNotification(cachedNotification),
72 "%s: Unable to parse received notification: %s", id, cachedNotification);
75 private void queueNotification(final NetconfMessage notification) {
76 checkState(!passNotifications);
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()));
83 queue.add(notification);
86 private synchronized void passNotification(final DOMNotification parsedNotification) {
87 LOG.debug("{}: Forwarding notification {}", id, parsedNotification);
89 if (filter == null || filter.test(parsedNotification)) {
90 salFacade.onNotification(parsedNotification);
94 synchronized void addNotificationFilter(final NotificationFilter newFilter) {
98 synchronized void onRemoteSchemaDown() {
100 passNotifications = false;
101 messageTransformer = null;
105 interface NotificationFilter extends Predicate<DOMNotification> {