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.Preconditions;
11 import java.util.LinkedList;
12 import java.util.List;
13 import org.opendaylight.controller.netconf.api.NetconfMessage;
14 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
15 import org.opendaylight.controller.sal.connect.api.MessageTransformer;
16 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
17 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
18 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Handles incoming notifications. Either caches them(until onRemoteSchemaUp is called) or passes to sal Facade.
25 final class NotificationHandler {
27 private static final Logger logger = LoggerFactory.getLogger(NotificationHandler.class);
29 private final RemoteDeviceHandler<?> salFacade;
30 private final List<NetconfMessage> queue = new LinkedList<>();
31 private final MessageTransformer<NetconfMessage> messageTransformer;
32 private final RemoteDeviceId id;
33 private boolean passNotifications = false;
35 NotificationHandler(final RemoteDeviceHandler<?> salFacade, final MessageTransformer<NetconfMessage> messageTransformer, final RemoteDeviceId id) {
36 this.salFacade = Preconditions.checkNotNull(salFacade);
37 this.messageTransformer = Preconditions.checkNotNull(messageTransformer);
38 this.id = Preconditions.checkNotNull(id);
41 synchronized void handleNotification(final NetconfMessage notification) {
42 if(passNotifications) {
43 passNotification(messageTransformer.toNotification(notification));
45 queueNotification(notification);
50 * Forward all cached notifications and pass all notifications from this point directly to sal facade.
52 synchronized void onRemoteSchemaUp() {
53 passNotifications = true;
55 for (final NetconfMessage cachedNotification : queue) {
56 passNotification(messageTransformer.toNotification(cachedNotification));
62 private void queueNotification(final NetconfMessage notification) {
63 Preconditions.checkState(passNotifications == false);
65 logger.debug("{}: Caching notification {}, remote schema not yet fully built", id, notification);
66 if(logger.isTraceEnabled()) {
67 logger.trace("{}: Caching notification {}", id, XmlUtil.toString(notification.getDocument()));
70 queue.add(notification);
73 private void passNotification(final CompositeNode parsedNotification) {
74 logger.debug("{}: Forwarding notification {}", id, parsedNotification);
75 Preconditions.checkNotNull(parsedNotification);
76 salFacade.onNotification(parsedNotification);