2 * Copyright (c) 2015 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.messagebus.eventsources.netconf;
10 import com.google.common.base.Preconditions;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Optional;
15 import javax.xml.transform.dom.DOMSource;
16 import org.opendaylight.mdsal.dom.api.DOMNotification;
17 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
18 import org.opendaylight.netconf.api.xml.XmlUtil;
19 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
20 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatus;
21 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatusNotification;
22 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatusNotificationBuilder;
23 import org.opendaylight.yangtools.util.xml.UntrustedXML;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
36 * Topic registration on event-source-status-notification.
38 class ConnectionNotificationTopicRegistration extends NotificationTopicRegistration {
40 private static final Logger LOG = LoggerFactory.getLogger(ConnectionNotificationTopicRegistration.class);
42 public static final SchemaPath EVENT_SOURCE_STATUS_PATH = SchemaPath
43 .create(true, QName.create(EventSourceStatusNotification.QNAME, "event-source-status"));
44 private static final NodeIdentifier EVENT_SOURCE_STATUS_ARG = NodeIdentifier.create(
45 EventSourceStatusNotification.QNAME);
47 private final DOMNotificationListener domNotificationListener;
49 ConnectionNotificationTopicRegistration(final String sourceName,
50 final DOMNotificationListener domNotificationListener) {
51 super(NotificationSourceType.ConnectionStatusChange, sourceName,
52 EVENT_SOURCE_STATUS_PATH.getLastComponent().getNamespace().toString());
53 this.domNotificationListener = Preconditions.checkNotNull(domNotificationListener);
54 LOG.info("Connection notification source has been initialized.");
56 setReplaySupported(false);
62 LOG.debug("Connection notification - publish Deactive");
63 publishNotification(EventSourceStatus.Deactive);
64 notificationTopicMap.clear();
70 void activateNotificationSource() {
71 LOG.debug("Connection notification - publish Active");
72 publishNotification(EventSourceStatus.Active);
76 void deActivateNotificationSource() {
77 LOG.debug("Connection notification - publish Inactive");
78 publishNotification(EventSourceStatus.Inactive);
82 void reActivateNotificationSource() {
83 LOG.debug("Connection notification - reactivate - publish active");
84 publishNotification(EventSourceStatus.Active);
88 boolean registerNotificationTopic(final SchemaPath notificationPath, final TopicId topicId) {
89 if (!checkNotificationPath(notificationPath)) {
90 LOG.debug("Bad SchemaPath for notification try to register");
93 Set<TopicId> topicIds = getTopicsForNotification(notificationPath);
94 topicIds.add(topicId);
95 notificationTopicMap.put(notificationPath, topicIds);
100 synchronized void unRegisterNotificationTopic(final TopicId topicId) {
101 List<SchemaPath> notificationPathToRemove = new ArrayList<>();
102 for (SchemaPath notifKey : notificationTopicMap.keySet()) {
103 Set<TopicId> topicList = notificationTopicMap.get(notifKey);
104 if (topicList != null) {
105 topicList.remove(topicId);
106 if (topicList.isEmpty()) {
107 notificationPathToRemove.add(notifKey);
111 for (SchemaPath notifKey : notificationPathToRemove) {
112 notificationTopicMap.remove(notifKey);
116 private void publishNotification(final EventSourceStatus eventSourceStatus) {
118 final EventSourceStatusNotification notification = new EventSourceStatusNotificationBuilder()
119 .setStatus(eventSourceStatus).build();
120 domNotificationListener.onNotification(createNotification(notification));
123 private static DOMNotification createNotification(final EventSourceStatusNotification notification) {
124 final ContainerNode cn = Builders.containerBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
125 .withChild(encapsulate(notification)).build();
126 DOMNotification dn = new DOMNotification() {
129 public SchemaPath getType() {
130 return EVENT_SOURCE_STATUS_PATH;
134 public ContainerNode getBody() {
141 private static AnyXmlNode encapsulate(final EventSourceStatusNotification notification) {
142 Document doc = UntrustedXML.newDocumentBuilder().newDocument();
144 final Element rootElement = XmlUtil.createElement(doc, "EventSourceStatusNotification",
145 Optional.of(EVENT_SOURCE_STATUS_ARG.getNodeType().getNamespace().toString()));
147 final Element sourceElement = doc.createElement("status");
148 sourceElement.appendChild(doc.createTextNode(notification.getStatus().name()));
149 rootElement.appendChild(sourceElement);
151 return Builders.anyXmlBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
152 .withValue(new DOMSource(rootElement)).build();