Bug 8351: Enforce check-style rules for restconf - sal-rest-docgen
[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.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Set;
15 import javax.xml.transform.dom.DOMSource;
16 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
18 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
19 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatus;
20 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatusNotification;
21 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceStatusNotificationBuilder;
22 import org.opendaylight.yangtools.util.xml.UntrustedXML;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33
34 /**
35  * Topic registration on event-source-status-notification.
36  */
37 class ConnectionNotificationTopicRegistration extends NotificationTopicRegistration {
38
39     private static final Logger LOG = LoggerFactory.getLogger(ConnectionNotificationTopicRegistration.class);
40
41     public static final SchemaPath EVENT_SOURCE_STATUS_PATH = SchemaPath
42         .create(true, QName.create(EventSourceStatusNotification.QNAME, "event-source-status"));
43     private static final NodeIdentifier EVENT_SOURCE_STATUS_ARG = NodeIdentifier.create(
44         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
50     public ConnectionNotificationTopicRegistration(final String SourceName,
51             final DOMNotificationListener domNotificationListener) {
52         super(NotificationSourceType.ConnectionStatusChange, SourceName,
53             EVENT_SOURCE_STATUS_PATH.getLastComponent().getNamespace().toString());
54         this.domNotificationListener = Preconditions.checkNotNull(domNotificationListener);
55         LOG.info("Connection notification source has been initialized.");
56         setActive(true);
57         setReplaySupported(false);
58     }
59
60     @Override
61     public void close() {
62         if (isActive()) {
63             LOG.debug("Connection notification - publish Deactive");
64             publishNotification(EventSourceStatus.Deactive);
65             notificationTopicMap.clear();
66             setActive(false);
67         }
68     }
69
70     @Override
71     void activateNotificationSource() {
72         LOG.debug("Connection notification - publish Active");
73         publishNotification(EventSourceStatus.Active);
74     }
75
76     @Override
77     void deActivateNotificationSource() {
78         LOG.debug("Connection notification - publish Inactive");
79         publishNotification(EventSourceStatus.Inactive);
80     }
81
82     @Override
83     void reActivateNotificationSource() {
84         LOG.debug("Connection notification - reactivate - publish active");
85         publishNotification(EventSourceStatus.Active);
86     }
87
88     @Override
89     boolean registerNotificationTopic(final SchemaPath notificationPath, final TopicId topicId) {
90         if (!checkNotificationPath(notificationPath)) {
91             LOG.debug("Bad SchemaPath for notification try to register");
92             return false;
93         }
94         Set<TopicId> topicIds = getTopicsForNotification(notificationPath);
95         topicIds.add(topicId);
96         notificationTopicMap.put(notificationPath, topicIds);
97         return true;
98     }
99
100     @Override
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);
109                 }
110             }
111         }
112         for (SchemaPath notifKey : notificationPathToRemove) {
113             notificationTopicMap.remove(notifKey);
114         }
115     }
116
117     private void publishNotification(final EventSourceStatus eventSourceStatus) {
118
119         final EventSourceStatusNotification notification = new EventSourceStatusNotificationBuilder()
120             .setStatus(eventSourceStatus).build();
121         domNotificationListener.onNotification(createNotification(notification));
122     }
123
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() {
128
129             @Override public SchemaPath getType() {
130                 return EVENT_SOURCE_STATUS_PATH;
131             }
132
133             @Override public ContainerNode getBody() {
134                 return cn;
135             }
136         };
137         return dn;
138     }
139
140     private static AnyXmlNode encapsulate(final EventSourceStatusNotification notification) {
141         Document doc = UntrustedXML.newDocumentBuilder().newDocument();
142
143         final Optional<String> namespace = Optional.of(EVENT_SOURCE_STATUS_ARG.getNodeType().getNamespace().toString());
144         final Element rootElement = createElement(doc, "EventSourceStatusNotification", namespace);
145
146         final Element sourceElement = doc.createElement("status");
147         sourceElement.appendChild(doc.createTextNode(notification.getStatus().name()));
148         rootElement.appendChild(sourceElement);
149
150         return Builders.anyXmlBuilder().withNodeIdentifier(EVENT_SOURCE_STATUS_ARG)
151             .withValue(new DOMSource(rootElement)).build();
152     }
153
154     // Helper to create root XML element with correct namespace and attribute
155     private static Element createElement(final Document document, final String qName,
156             final Optional<String> namespaceURI) {
157         if (namespaceURI.isPresent()) {
158             final Element element = document.createElementNS(namespaceURI.get(), qName);
159             String name = XMLNS_ATTRIBUTE_KEY;
160             if (element.getPrefix() != null) {
161                 name += ":" + element.getPrefix();
162             }
163             element.setAttributeNS(XMLNS_URI, name, namespaceURI.get());
164             return element;
165         }
166         return document.createElement(qName);
167     }
168 }