blob: cb9900db6080a0a7f61c886000c0f85fa86fc2b5 [file] [log] [blame]
Tomek OsiƄski7a1db182018-02-03 17:15:06 +01001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.xmpp.pubsub.ctl;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.dom4j.Element;
26import org.onosproject.net.DeviceId;
27import org.onosproject.xmpp.core.XmppController;
28import org.onosproject.xmpp.core.XmppDeviceId;
29import org.onosproject.xmpp.core.XmppIqListener;
30import org.onosproject.xmpp.pubsub.XmppPubSubConstants;
31import org.onosproject.xmpp.pubsub.XmppPubSubController;
32import org.onosproject.xmpp.pubsub.XmppPublishEventsListener;
33import org.onosproject.xmpp.pubsub.XmppSubscribeEventsListener;
34import org.onosproject.xmpp.pubsub.model.XmppEventNotification;
35import org.onosproject.xmpp.pubsub.model.XmppPubSubError;
36import org.onosproject.xmpp.pubsub.model.XmppPublish;
37import org.onosproject.xmpp.pubsub.model.XmppRetract;
38import org.onosproject.xmpp.pubsub.model.XmppSubscribe;
39import org.onosproject.xmpp.pubsub.model.XmppUnsubscribe;
40import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
42import org.xmpp.packet.IQ;
43import org.xmpp.packet.JID;
44
45import java.util.Set;
46import java.util.concurrent.CopyOnWriteArraySet;
47
48import static com.google.common.base.Preconditions.checkNotNull;
49import static org.onosproject.xmpp.pubsub.XmppPubSubConstants.PUBSUB_ELEMENT;
50import static org.onosproject.xmpp.pubsub.XmppPubSubConstants.PUBSUB_NAMESPACE;
51
52/**
53 * The main class implementing XMPP Publish/Subscribe extension.
54 * It listens to IQ stanzas and generates PubSub events based on the payload.
55 */
56@Component(immediate = true)
57@Service
58public class XmppPubSubControllerImpl implements XmppPubSubController {
59
60 private static final Logger log =
61 LoggerFactory.getLogger(XmppPubSubControllerImpl.class);
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected XmppController xmppController;
65
66 protected Set<XmppPublishEventsListener> xmppPublishEventsListeners =
67 new CopyOnWriteArraySet<XmppPublishEventsListener>();
68 protected Set<XmppSubscribeEventsListener> xmppSubscribeEventsListeners =
69 new CopyOnWriteArraySet<XmppSubscribeEventsListener>();
70
71 protected XmppIqListener iqListener = new InternalXmppIqListener();
72
73 @Activate
74 public void activate() {
75 xmppController.addXmppIqListener(iqListener, PUBSUB_NAMESPACE);
76 log.info("Started.");
77 }
78
79 @Deactivate
80 public void deactivate() {
81 xmppController.removeXmppIqListener(iqListener, PUBSUB_NAMESPACE);
82 log.info("Stopped");
83 }
84
85 @Override
86 public void notify(DeviceId deviceId, XmppEventNotification eventNotification) {
87 XmppDeviceId xmppDeviceId = asXmppDeviceId(deviceId);
88 xmppController.getDevice(xmppDeviceId).sendPacket(eventNotification);
89 }
90
91 @Override
92 public void notifyError(DeviceId deviceId, XmppPubSubError error) {
93 XmppDeviceId xmppDeviceId = asXmppDeviceId(deviceId);
94 xmppController.getDevice(xmppDeviceId).sendError(error.asPacketError());
95 }
96
97 private XmppDeviceId asXmppDeviceId(DeviceId deviceId) {
98 String[] parts = deviceId.toString().split(":");
99 JID jid = new JID(parts[1]);
100 return new XmppDeviceId(jid);
101 }
102
103 @Override
104 public void addXmppPublishEventsListener(XmppPublishEventsListener xmppPublishEventsListener) {
105 xmppPublishEventsListeners.add(xmppPublishEventsListener);
106 }
107
108 @Override
109 public void removeXmppPublishEventsListener(XmppPublishEventsListener xmppPublishEventsListener) {
110 xmppPublishEventsListeners.remove(xmppPublishEventsListener);
111 }
112
113 @Override
114 public void addXmppSubscribeEventsListener(XmppSubscribeEventsListener xmppSubscribeEventsListener) {
115 xmppSubscribeEventsListeners.add(xmppSubscribeEventsListener);
116 }
117
118 @Override
119 public void removeXmppSubscribeEventsListener(XmppSubscribeEventsListener xmppSubscribeEventsListener) {
120 xmppSubscribeEventsListeners.remove(xmppSubscribeEventsListener);
121 }
122
123 private class InternalXmppIqListener implements XmppIqListener {
124 @Override
125 public void handleIqStanza(IQ iq) {
126 if (isPubSub(iq)) {
127 notifyListeners(iq);
128 }
129 }
130 }
131
132 private void notifyListeners(IQ iq) {
133 XmppPubSubConstants.Method method = getMethod(iq);
134 checkNotNull(method);
135 switch (method) {
136 case SUBSCRIBE:
137 XmppSubscribe subscribe = new XmppSubscribe(iq);
138 notifyXmppSubscribe(subscribe);
139 break;
140 case UNSUBSCRIBE:
141 XmppUnsubscribe unsubscribe = new XmppUnsubscribe(iq);
142 notifyXmppUnsubscribe(unsubscribe);
143 break;
144 case PUBLISH:
145 XmppPublish publish = new XmppPublish(iq);
146 notifyXmppPublish(publish);
147 break;
148 case RETRACT:
149 XmppRetract retract = new XmppRetract(iq);
150 notifyXmppRetract(retract);
151 break;
152 default:
153 break;
154 }
155 }
156
157 private void notifyXmppRetract(XmppRetract retractEvent) {
158 for (XmppPublishEventsListener listener : xmppPublishEventsListeners) {
159 listener.handleRetract(retractEvent);
160 }
161 }
162
163 private void notifyXmppPublish(XmppPublish publishEvent) {
164 for (XmppPublishEventsListener listener : xmppPublishEventsListeners) {
165 listener.handlePublish(publishEvent);
166 }
167 }
168
169 private void notifyXmppUnsubscribe(XmppUnsubscribe unsubscribeEvent) {
170 for (XmppSubscribeEventsListener listener : xmppSubscribeEventsListeners) {
171 listener.handleUnsubscribe(unsubscribeEvent);
172 }
173 }
174
175 private void notifyXmppSubscribe(XmppSubscribe subscribeEvent) {
176 for (XmppSubscribeEventsListener listener : xmppSubscribeEventsListeners) {
177 listener.handleSubscribe(subscribeEvent);
178 }
179 }
180
181 private boolean isPubSub(IQ iq) {
182 Element pubsub = iq.getElement().element(PUBSUB_ELEMENT);
183 if (pubsub != null && pubsub.getNamespaceURI().equals(PUBSUB_NAMESPACE)) {
184 return true;
185 }
186 return false;
187 }
188
189 public static XmppPubSubConstants.Method getMethod(IQ iq) {
190 Element pubsubElement = iq.getChildElement();
191 Element methodElement = getChildElement(pubsubElement);
192 String name = methodElement.getName();
193 switch (name) {
194 case "subscribe":
195 return XmppPubSubConstants.Method.SUBSCRIBE;
196 case "unsubscribe":
197 return XmppPubSubConstants.Method.UNSUBSCRIBE;
198 case "publish":
199 return XmppPubSubConstants.Method.PUBLISH;
200 case "retract":
201 return XmppPubSubConstants.Method.RETRACT;
202 default:
203 break;
204 }
205 return null;
206 }
207
208 public static Element getChildElement(Element element) {
209 Element child = (Element) element.elements().get(0); // the first element is related to pubsub operation
210 return child;
211 }
212
213}