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.controller.sal.connect.netconf;
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;
25 * Handles incoming notifications. Either caches them(until onRemoteSchemaUp is called) or passes to sal Facade.
27 final class NotificationHandler {
29 private static final Logger logger = LoggerFactory.getLogger(NotificationHandler.class);
31 private final RemoteDeviceHandler<?> salFacade;
32 private final List<NetconfMessage> queue = new LinkedList<>();
33 private final RemoteDeviceId id;
34 private boolean passNotifications = false;
36 private NotificationFilter filter;
37 private MessageTransformer<NetconfMessage> messageTransformer;
39 NotificationHandler(final RemoteDeviceHandler<?> salFacade, final RemoteDeviceId id) {
40 this.salFacade = Preconditions.checkNotNull(salFacade);
41 this.id = Preconditions.checkNotNull(id);
44 synchronized void handleNotification(final NetconfMessage notification) {
45 if(passNotifications) {
46 passNotification(transformNotification(notification));
48 queueNotification(notification);
53 * Forward all cached notifications and pass all notifications from this point directly to sal facade.
54 * @param messageTransformer
56 synchronized void onRemoteSchemaUp(final MessageTransformer<NetconfMessage> messageTransformer) {
57 this.messageTransformer = Preconditions.checkNotNull(messageTransformer);
59 passNotifications = true;
61 for (final NetconfMessage cachedNotification : queue) {
62 passNotification(transformNotification(cachedNotification));
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;
74 private void queueNotification(final NetconfMessage notification) {
75 Preconditions.checkState(passNotifications == false);
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()));
82 queue.add(notification);
85 private synchronized void passNotification(final ContainerNode parsedNotification) {
86 logger.debug("{}: Forwarding notification {}", id, parsedNotification);
88 if(filter == null || filter.filterNotification(parsedNotification).isPresent()) {
89 salFacade.onNotification(parsedNotification);
93 synchronized void addNotificationFilter(final NotificationFilter filter) {
97 synchronized void onRemoteSchemaDown() {
99 passNotifications = false;
100 messageTransformer = null;
103 static interface NotificationFilter {
105 Optional<NormalizedNode<?, ?>> filterNotification(NormalizedNode<?, ?> notification);