BUG-997 Use shared schema context factory in netconf-connector
[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.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;
21
22 /**
23  * Handles incoming notifications. Either caches them(until onRemoteSchemaUp is called) or passes to sal Facade.
24  */
25 final class NotificationHandler {
26
27     private static final Logger logger = LoggerFactory.getLogger(NotificationHandler.class);
28
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;
34
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);
39     }
40
41     synchronized void handleNotification(final NetconfMessage notification) {
42         if(passNotifications) {
43             passNotification(messageTransformer.toNotification(notification));
44         } else {
45             queueNotification(notification);
46         }
47     }
48
49     /**
50      * Forward all cached notifications and pass all notifications from this point directly to sal facade.
51      */
52     synchronized void onRemoteSchemaUp() {
53         passNotifications = true;
54
55         for (final NetconfMessage cachedNotification : queue) {
56             passNotification(messageTransformer.toNotification(cachedNotification));
57         }
58
59         queue.clear();
60     }
61
62     private void queueNotification(final NetconfMessage notification) {
63         Preconditions.checkState(passNotifications == false);
64
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()));
68         }
69
70         queue.add(notification);
71     }
72
73     private void passNotification(final CompositeNode parsedNotification) {
74         logger.debug("{}: Forwarding notification {}", id, parsedNotification);
75         Preconditions.checkNotNull(parsedNotification);
76         salFacade.onNotification(parsedNotification);
77     }
78 }