61e1af84bc8f4acab96bb82b3c65da55b6c48d3e
[controller.git] / opendaylight / md-sal / messagebus-impl / 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 java.util.ArrayList;
11 import java.util.List;
12 import java.util.concurrent.ConcurrentHashMap;
13
14 import javax.xml.parsers.DocumentBuilder;
15 import javax.xml.parsers.DocumentBuilderFactory;
16 import javax.xml.parsers.ParserConfigurationException;
17 import javax.xml.transform.dom.DOMSource;
18
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 import com.google.common.base.Optional;
37 import com.google.common.base.Preconditions;
38
39 public class ConnectionNotificationTopicRegistration extends NotificationTopicRegistration {
40
41     private static final Logger LOG = LoggerFactory.getLogger(ConnectionNotificationTopicRegistration.class);
42
43     public static final SchemaPath EVENT_SOURCE_STATUS_PATH = SchemaPath.create(true, QName.create(EventSourceStatusNotification.QNAME, "event-source-status"));
44     private static final NodeIdentifier EVENT_SOURCE_STATUS_ARG = new NodeIdentifier(EventSourceStatusNotification.QNAME);
45     private static final String XMLNS_ATTRIBUTE_KEY = "xmlns";
46     private static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/";
47
48     private final DOMNotificationListener domNotificationListener;
49     private ConcurrentHashMap<SchemaPath, ArrayList<TopicId>> notificationTopicMap = new ConcurrentHashMap<>();
50
51     public ConnectionNotificationTopicRegistration(String SourceName, DOMNotificationListener domNotificationListener) {
52         super(NotificationSourceType.ConnectionStatusChange, SourceName, 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() throws Exception {
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(SchemaPath notificationPath, TopicId topicId) {
89         if(checkNotificationPath(notificationPath) == false){
90             LOG.debug("Bad SchemaPath for notification try to register");
91             return false;
92         }
93         ArrayList<TopicId> topicIds = getNotificationTopicIds(notificationPath);
94         if(topicIds == null){
95             topicIds = new ArrayList<>();
96             topicIds.add(topicId);
97         } else {
98             if(topicIds.contains(topicId) == false){
99                 topicIds.add(topicId);
100             }
101         }
102         notificationTopicMap.put(notificationPath, topicIds);
103         return true;
104     }
105
106     @Override
107     ArrayList<TopicId> getNotificationTopicIds(SchemaPath notificationPath) {
108         return notificationTopicMap.get(notificationPath);
109     }
110
111     @Override
112     synchronized void unRegisterNotificationTopic(TopicId topicId) {
113         List<SchemaPath> notificationPathToRemove = new ArrayList<>();
114         for(SchemaPath notifKey : notificationTopicMap.keySet()){
115             ArrayList<TopicId> topicList = notificationTopicMap.get(notifKey);
116             if(topicList != null){
117                 topicList.remove(topicId);
118                 if(topicList.isEmpty()){
119                     notificationPathToRemove.add(notifKey);
120                 }
121             }
122         }
123         for(SchemaPath notifKey : notificationPathToRemove){
124             notificationTopicMap.remove(notifKey);
125         }
126     }
127
128     private void publishNotification(EventSourceStatus eventSourceStatus){
129
130         final EventSourceStatusNotification notification = new EventSourceStatusNotificationBuilder()
131                     .setStatus(eventSourceStatus)
132                     .build();
133         domNotificationListener.onNotification(createNotification(notification));
134     }
135
136     private DOMNotification createNotification(EventSourceStatusNotification notification){
137         final ContainerNode cn = Builders.containerBuilder()
138                 .withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
139                 .withChild(encapsulate(notification))
140                 .build();
141         DOMNotification dn = new DOMNotification() {
142
143             @Override
144             public SchemaPath getType() {
145                 return EVENT_SOURCE_STATUS_PATH;
146             }
147
148             @Override
149             public ContainerNode getBody() {
150                 return cn;
151             }
152         };
153         return dn;
154     }
155
156     private AnyXmlNode encapsulate(EventSourceStatusNotification notification){
157
158         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
159         DocumentBuilder docBuilder;
160
161         try {
162             docBuilder = docFactory.newDocumentBuilder();
163         } catch (ParserConfigurationException e) {
164             throw new IllegalStateException("Can not create XML DocumentBuilder");
165         }
166
167         Document doc = docBuilder.newDocument();
168
169         final Optional<String> namespace = Optional.of(EVENT_SOURCE_STATUS_ARG.getNodeType().getNamespace().toString());
170         final Element rootElement = createElement(doc , "EventSourceStatusNotification", namespace);
171
172         final Element sourceElement = doc.createElement("status");
173         sourceElement.appendChild(doc.createTextNode(notification.getStatus().name()));
174         rootElement.appendChild(sourceElement);
175
176
177         return Builders.anyXmlBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
178                      .withValue(new DOMSource(rootElement))
179                      .build();
180
181     }
182
183     // Helper to create root XML element with correct namespace and attribute
184     private Element createElement(final Document document, final String qName, final Optional<String> namespaceURI) {
185         if(namespaceURI.isPresent()) {
186             final Element element = document.createElementNS(namespaceURI.get(), qName);
187             String name = XMLNS_ATTRIBUTE_KEY;
188             if(element.getPrefix() != null) {
189                 name += ":" + element.getPrefix();
190             }
191             element.setAttributeNS(XMLNS_URI, name, namespaceURI.get());
192             return element;
193         }
194         return document.createElement(qName);
195     }
196 }

©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.