acf72e79186548d4a055032366fb6acf0dd9b203
[netconf.git] / netconf / messagebus-netconf / src / main / java / org / opendaylight / netconf / messagebus / eventsources / netconf / ConnectionNotificationTopicRegistration.java
1 /*
2  * Copyright (c) 2015 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.netconf.messagebus.eventsources.netconf;
9
10 import com.google.common.base.Preconditions;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.Set;
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;
34
35 /**
36  * Topic registration on event-source-status-notification.
37  */
38 class ConnectionNotificationTopicRegistration extends NotificationTopicRegistration {
39
40     private static final Logger LOG = LoggerFactory.getLogger(ConnectionNotificationTopicRegistration.class);
41
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);
46
47     private final DOMNotificationListener domNotificationListener;
48
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.");
55         setActive(true);
56         setReplaySupported(false);
57     }
58
59     @Override
60     public void close() {
61         if (isActive()) {
62             LOG.debug("Connection notification - publish Deactive");
63             publishNotification(EventSourceStatus.Deactive);
64             notificationTopicMap.clear();
65             setActive(false);
66         }
67     }
68
69     @Override
70     void activateNotificationSource() {
71         LOG.debug("Connection notification - publish Active");
72         publishNotification(EventSourceStatus.Active);
73     }
74
75     @Override
76     void deActivateNotificationSource() {
77         LOG.debug("Connection notification - publish Inactive");
78         publishNotification(EventSourceStatus.Inactive);
79     }
80
81     @Override
82     void reActivateNotificationSource() {
83         LOG.debug("Connection notification - reactivate - publish active");
84         publishNotification(EventSourceStatus.Active);
85     }
86
87     @Override
88     boolean registerNotificationTopic(final SchemaPath notificationPath, final TopicId topicId) {
89         if (!checkNotificationPath(notificationPath)) {
90             LOG.debug("Bad SchemaPath for notification try to register");
91             return false;
92         }
93         Set<TopicId> topicIds = getTopicsForNotification(notificationPath);
94         topicIds.add(topicId);
95         notificationTopicMap.put(notificationPath, topicIds);
96         return true;
97     }
98
99     @Override
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);
108                 }
109             }
110         }
111         for (SchemaPath notifKey : notificationPathToRemove) {
112             notificationTopicMap.remove(notifKey);
113         }
114     }
115
116     private void publishNotification(final EventSourceStatus eventSourceStatus) {
117
118         final EventSourceStatusNotification notification = new EventSourceStatusNotificationBuilder()
119                 .setStatus(eventSourceStatus).build();
120         domNotificationListener.onNotification(createNotification(notification));
121     }
122
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() {
127
128             @Override
129             public SchemaPath getType() {
130                 return EVENT_SOURCE_STATUS_PATH;
131             }
132
133             @Override
134             public ContainerNode getBody() {
135                 return cn;
136             }
137         };
138         return dn;
139     }
140
141     private static AnyXmlNode encapsulate(final EventSourceStatusNotification notification) {
142         Document doc = UntrustedXML.newDocumentBuilder().newDocument();
143
144         final Element rootElement = XmlUtil.createElement(doc, "EventSourceStatusNotification",
145             Optional.of(EVENT_SOURCE_STATUS_ARG.getNodeType().getNamespace().toString()));
146
147         final Element sourceElement = doc.createElement("status");
148         sourceElement.appendChild(doc.createTextNode(notification.getStatus().name()));
149         rootElement.appendChild(sourceElement);
150
151         return Builders.anyXmlBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
152                 .withValue(new DOMSource(rootElement)).build();
153     }
154 }