Bump versions to 2.0.0-SNAPSHOT
[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 static java.util.Objects.requireNonNull;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.Set;
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;
36
37 /**
38  * Topic registration on event-source-status-notification.
39  */
40 @Deprecated(forRemoval = true)
41 class ConnectionNotificationTopicRegistration extends NotificationTopicRegistration {
42     private static final Logger LOG = LoggerFactory.getLogger(ConnectionNotificationTopicRegistration.class);
43
44     public static final Absolute EVENT_SOURCE_STATUS_PATH =
45             Absolute.of(QName.create(EventSourceStatusNotification.QNAME, "event-source-status"));
46     private static final NodeIdentifier EVENT_SOURCE_STATUS_ARG = NodeIdentifier.create(
47             EventSourceStatusNotification.QNAME);
48
49     private final DOMNotificationListener domNotificationListener;
50
51     ConnectionNotificationTopicRegistration(final String sourceName,
52                                             final DOMNotificationListener domNotificationListener) {
53         super(NotificationSourceType.ConnectionStatusChange, sourceName,
54                 EVENT_SOURCE_STATUS_PATH.lastNodeIdentifier().getNamespace().toString());
55         this.domNotificationListener = requireNonNull(domNotificationListener);
56         LOG.info("Connection notification source has been initialized.");
57         setActive(true);
58         setReplaySupported(false);
59     }
60
61     @Override
62     public void close() {
63         if (isActive()) {
64             LOG.debug("Connection notification - publish Deactive");
65             publishNotification(EventSourceStatus.Deactive);
66             notificationTopicMap.clear();
67             setActive(false);
68         }
69     }
70
71     @Override
72     void activateNotificationSource() {
73         LOG.debug("Connection notification - publish Active");
74         publishNotification(EventSourceStatus.Active);
75     }
76
77     @Override
78     void deActivateNotificationSource() {
79         LOG.debug("Connection notification - publish Inactive");
80         publishNotification(EventSourceStatus.Inactive);
81     }
82
83     @Override
84     void reActivateNotificationSource() {
85         LOG.debug("Connection notification - reactivate - publish active");
86         publishNotification(EventSourceStatus.Active);
87     }
88
89     @Override
90     boolean registerNotificationTopic(final SchemaPath notificationPath, final TopicId topicId) {
91         if (!checkNotificationPath(notificationPath)) {
92             LOG.debug("Bad SchemaPath for notification try to register");
93             return false;
94         }
95         Set<TopicId> topicIds = getTopicsForNotification(notificationPath);
96         topicIds.add(topicId);
97         notificationTopicMap.put(notificationPath, topicIds);
98         return true;
99     }
100
101     @Override
102     synchronized void unRegisterNotificationTopic(final TopicId topicId) {
103         List<SchemaPath> notificationPathToRemove = new ArrayList<>();
104         for (SchemaPath notifKey : notificationTopicMap.keySet()) {
105             Set<TopicId> topicList = notificationTopicMap.get(notifKey);
106             if (topicList != null) {
107                 topicList.remove(topicId);
108                 if (topicList.isEmpty()) {
109                     notificationPathToRemove.add(notifKey);
110                 }
111             }
112         }
113         for (SchemaPath notifKey : notificationPathToRemove) {
114             notificationTopicMap.remove(notifKey);
115         }
116     }
117
118     private void publishNotification(final EventSourceStatus eventSourceStatus) {
119
120         final EventSourceStatusNotification notification = new EventSourceStatusNotificationBuilder()
121                 .setStatus(eventSourceStatus).build();
122         domNotificationListener.onNotification(createNotification(notification));
123     }
124
125     private static DOMNotification createNotification(final EventSourceStatusNotification notification) {
126         final ContainerNode cn = Builders.containerBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
127                 .withChild(encapsulate(notification)).build();
128         DOMNotification dn = new DOMNotification() {
129
130             @Override
131             public Absolute getType() {
132                 return EVENT_SOURCE_STATUS_PATH;
133             }
134
135             @Override
136             public ContainerNode getBody() {
137                 return cn;
138             }
139         };
140         return dn;
141     }
142
143     private static DOMSourceAnyxmlNode encapsulate(final EventSourceStatusNotification notification) {
144         Document doc = UntrustedXML.newDocumentBuilder().newDocument();
145
146         final Element rootElement = XmlUtil.createElement(doc, "EventSourceStatusNotification",
147             Optional.of(EVENT_SOURCE_STATUS_ARG.getNodeType().getNamespace().toString()));
148
149         final Element sourceElement = doc.createElement("status");
150         sourceElement.appendChild(doc.createTextNode(notification.getStatus().name()));
151         rootElement.appendChild(sourceElement);
152
153         return Builders.anyXmlBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
154                 .withValue(new DOMSource(rootElement)).build();
155     }
156 }