Decouple message bus from netconf connector
[controller.git] / opendaylight / netconf / messagebus-netconf / src / main / java / org / opendaylight / controller / 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.controller.messagebus.eventsources.netconf;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.concurrent.ConcurrentHashMap;
15 import javax.xml.parsers.DocumentBuilder;
16 import javax.xml.parsers.DocumentBuilderFactory;
17 import javax.xml.parsers.ParserConfigurationException;
18 import javax.xml.transform.dom.DOMSource;
19 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
21 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
22 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatus;
23 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatusNotification;
24 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatusNotificationBuilder;
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.AnyXmlNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35
36 public class ConnectionNotificationTopicRegistration extends NotificationTopicRegistration {
37
38     private static final Logger LOG = LoggerFactory.getLogger(ConnectionNotificationTopicRegistration.class);
39
40     public static final SchemaPath EVENT_SOURCE_STATUS_PATH = SchemaPath
41         .create(true, QName.create(EventSourceStatusNotification.QNAME, "event-source-status"));
42     private static final NodeIdentifier EVENT_SOURCE_STATUS_ARG = new NodeIdentifier(
43         EventSourceStatusNotification.QNAME);
44     private static final String XMLNS_ATTRIBUTE_KEY = "xmlns";
45     private static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
46
47     private final DOMNotificationListener domNotificationListener;
48     private ConcurrentHashMap<SchemaPath, ArrayList<TopicId>> notificationTopicMap = new ConcurrentHashMap<>();
49
50     public ConnectionNotificationTopicRegistration(String SourceName, 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 public void close() throws Exception {
60         if (isActive()) {
61             LOG.debug("Connection notification - publish Deactive");
62             publishNotification(EventSourceStatus.Deactive);
63             notificationTopicMap.clear();
64             setActive(false);
65         }
66     }
67
68     @Override void activateNotificationSource() {
69         LOG.debug("Connection notification - publish Active");
70         publishNotification(EventSourceStatus.Active);
71     }
72
73     @Override void deActivateNotificationSource() {
74         LOG.debug("Connection notification - publish Inactive");
75         publishNotification(EventSourceStatus.Inactive);
76     }
77
78     @Override void reActivateNotificationSource() {
79         LOG.debug("Connection notification - reactivate - publish active");
80         publishNotification(EventSourceStatus.Active);
81     }
82
83     @Override boolean registerNotificationTopic(SchemaPath notificationPath, TopicId topicId) {
84         if (checkNotificationPath(notificationPath) == false) {
85             LOG.debug("Bad SchemaPath for notification try to register");
86             return false;
87         }
88         ArrayList<TopicId> topicIds = getNotificationTopicIds(notificationPath);
89         if (topicIds == null) {
90             topicIds = new ArrayList<>();
91             topicIds.add(topicId);
92         } else {
93             if (topicIds.contains(topicId) == false) {
94                 topicIds.add(topicId);
95             }
96         }
97         notificationTopicMap.put(notificationPath, topicIds);
98         return true;
99     }
100
101     @Override ArrayList<TopicId> getNotificationTopicIds(SchemaPath notificationPath) {
102         return notificationTopicMap.get(notificationPath);
103     }
104
105     @Override synchronized void unRegisterNotificationTopic(TopicId topicId) {
106         List<SchemaPath> notificationPathToRemove = new ArrayList<>();
107         for (SchemaPath notifKey : notificationTopicMap.keySet()) {
108             ArrayList<TopicId> topicList = notificationTopicMap.get(notifKey);
109             if (topicList != null) {
110                 topicList.remove(topicId);
111                 if (topicList.isEmpty()) {
112                     notificationPathToRemove.add(notifKey);
113                 }
114             }
115         }
116         for (SchemaPath notifKey : notificationPathToRemove) {
117             notificationTopicMap.remove(notifKey);
118         }
119     }
120
121     private void publishNotification(EventSourceStatus eventSourceStatus) {
122
123         final EventSourceStatusNotification notification = new EventSourceStatusNotificationBuilder()
124             .setStatus(eventSourceStatus).build();
125         domNotificationListener.onNotification(createNotification(notification));
126     }
127
128     private DOMNotification createNotification(EventSourceStatusNotification notification) {
129         final ContainerNode cn = Builders.containerBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
130             .withChild(encapsulate(notification)).build();
131         DOMNotification dn = new DOMNotification() {
132
133             @Override public SchemaPath getType() {
134                 return EVENT_SOURCE_STATUS_PATH;
135             }
136
137             @Override public ContainerNode getBody() {
138                 return cn;
139             }
140         };
141         return dn;
142     }
143
144     private AnyXmlNode encapsulate(EventSourceStatusNotification notification) {
145
146         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
147         DocumentBuilder docBuilder;
148
149         try {
150             docBuilder = docFactory.newDocumentBuilder();
151         } catch (ParserConfigurationException e) {
152             throw new IllegalStateException("Can not create XML DocumentBuilder");
153         }
154
155         Document doc = docBuilder.newDocument();
156
157         final Optional<String> namespace = Optional.of(EVENT_SOURCE_STATUS_ARG.getNodeType().getNamespace().toString());
158         final Element rootElement = createElement(doc, "EventSourceStatusNotification", namespace);
159
160         final Element sourceElement = doc.createElement("status");
161         sourceElement.appendChild(doc.createTextNode(notification.getStatus().name()));
162         rootElement.appendChild(sourceElement);
163
164         return Builders.anyXmlBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
165             .withValue(new DOMSource(rootElement)).build();
166
167     }
168
169     // Helper to create root XML element with correct namespace and attribute
170     private Element createElement(final Document document, final String qName, final Optional<String> namespaceURI) {
171         if (namespaceURI.isPresent()) {
172             final Element element = document.createElementNS(namespaceURI.get(), qName);
173             String name = XMLNS_ATTRIBUTE_KEY;
174             if (element.getPrefix() != null) {
175                 name += ":" + element.getPrefix();
176             }
177             element.setAttributeNS(XMLNS_URI, name, namespaceURI.get());
178             return element;
179         }
180         return document.createElement(qName);
181     }
182 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.