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 static java.util.Objects.requireNonNull;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Optional;
16 import javax.xml.transform.dom.DOMSource;
17 import org.opendaylight.mdsal.dom.api.DOMNotification;
18 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
19 import org.opendaylight.netconf.api.xml.XmlUtil;
20 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
21 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatus;
22 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatusNotification;
23 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatusNotificationBuilder;
24 import org.opendaylight.yangtools.util.xml.UntrustedXML;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
29 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
38 * Topic registration on event-source-status-notification.
40 class ConnectionNotificationTopicRegistration extends NotificationTopicRegistration {
41 private static final Logger LOG = LoggerFactory.getLogger(ConnectionNotificationTopicRegistration.class);
43 public static final Absolute EVENT_SOURCE_STATUS_PATH =
44 Absolute.of(QName.create(EventSourceStatusNotification.QNAME, "event-source-status"));
45 private static final NodeIdentifier EVENT_SOURCE_STATUS_ARG = NodeIdentifier.create(
46 EventSourceStatusNotification.QNAME);
48 private final DOMNotificationListener domNotificationListener;
50 ConnectionNotificationTopicRegistration(final String sourceName,
51 final DOMNotificationListener domNotificationListener) {
52 super(NotificationSourceType.ConnectionStatusChange, sourceName,
53 EVENT_SOURCE_STATUS_PATH.lastNodeIdentifier().getNamespace().toString());
54 this.domNotificationListener = requireNonNull(domNotificationListener);
55 LOG.info("Connection notification source has been initialized.");
57 setReplaySupported(false);
63 LOG.debug("Connection notification - publish Deactive");
64 publishNotification(EventSourceStatus.Deactive);
65 notificationTopicMap.clear();
71 void activateNotificationSource() {
72 LOG.debug("Connection notification - publish Active");
73 publishNotification(EventSourceStatus.Active);
77 void deActivateNotificationSource() {
78 LOG.debug("Connection notification - publish Inactive");
79 publishNotification(EventSourceStatus.Inactive);
83 void reActivateNotificationSource() {
84 LOG.debug("Connection notification - reactivate - publish active");
85 publishNotification(EventSourceStatus.Active);
89 boolean registerNotificationTopic(final SchemaPath notificationPath, final TopicId topicId) {
90 if (!checkNotificationPath(notificationPath)) {
91 LOG.debug("Bad SchemaPath for notification try to register");
94 Set<TopicId> topicIds = getTopicsForNotification(notificationPath);
95 topicIds.add(topicId);
96 notificationTopicMap.put(notificationPath, topicIds);
101 synchronized void unRegisterNotificationTopic(final TopicId topicId) {
102 List<SchemaPath> notificationPathToRemove = new ArrayList<>();
103 for (SchemaPath notifKey : notificationTopicMap.keySet()) {
104 Set<TopicId> topicList = notificationTopicMap.get(notifKey);
105 if (topicList != null) {
106 topicList.remove(topicId);
107 if (topicList.isEmpty()) {
108 notificationPathToRemove.add(notifKey);
112 for (SchemaPath notifKey : notificationPathToRemove) {
113 notificationTopicMap.remove(notifKey);
117 private void publishNotification(final EventSourceStatus eventSourceStatus) {
119 final EventSourceStatusNotification notification = new EventSourceStatusNotificationBuilder()
120 .setStatus(eventSourceStatus).build();
121 domNotificationListener.onNotification(createNotification(notification));
124 private static DOMNotification createNotification(final EventSourceStatusNotification notification) {
125 final ContainerNode cn = Builders.containerBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
126 .withChild(encapsulate(notification)).build();
127 DOMNotification dn = new DOMNotification() {
130 public Absolute getType() {
131 return EVENT_SOURCE_STATUS_PATH;
135 public ContainerNode getBody() {
142 private static DOMSourceAnyxmlNode encapsulate(final EventSourceStatusNotification notification) {
143 Document doc = UntrustedXML.newDocumentBuilder().newDocument();
145 final Element rootElement = XmlUtil.createElement(doc, "EventSourceStatusNotification",
146 Optional.of(EVENT_SOURCE_STATUS_ARG.getNodeType().getNamespace().toString()));
148 final Element sourceElement = doc.createElement("status");
149 sourceElement.appendChild(doc.createTextNode(notification.getStatus().name()));
150 rootElement.appendChild(sourceElement);
152 return Builders.anyXmlBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
153 .withValue(new DOMSource(rootElement)).build();