Tomek OsiĆski | 7a1db18 | 2018-02-03 17:15:06 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.xmpp.pubsub.ctl; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.dom4j.Element; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.xmpp.core.XmppController; |
| 28 | import org.onosproject.xmpp.core.XmppDeviceId; |
| 29 | import org.onosproject.xmpp.core.XmppIqListener; |
| 30 | import org.onosproject.xmpp.pubsub.XmppPubSubConstants; |
| 31 | import org.onosproject.xmpp.pubsub.XmppPubSubController; |
| 32 | import org.onosproject.xmpp.pubsub.XmppPublishEventsListener; |
| 33 | import org.onosproject.xmpp.pubsub.XmppSubscribeEventsListener; |
| 34 | import org.onosproject.xmpp.pubsub.model.XmppEventNotification; |
| 35 | import org.onosproject.xmpp.pubsub.model.XmppPubSubError; |
| 36 | import org.onosproject.xmpp.pubsub.model.XmppPublish; |
| 37 | import org.onosproject.xmpp.pubsub.model.XmppRetract; |
| 38 | import org.onosproject.xmpp.pubsub.model.XmppSubscribe; |
| 39 | import org.onosproject.xmpp.pubsub.model.XmppUnsubscribe; |
| 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | import org.xmpp.packet.IQ; |
| 43 | import org.xmpp.packet.JID; |
| 44 | |
| 45 | import java.util.Set; |
| 46 | import java.util.concurrent.CopyOnWriteArraySet; |
| 47 | |
| 48 | import static com.google.common.base.Preconditions.checkNotNull; |
| 49 | import static org.onosproject.xmpp.pubsub.XmppPubSubConstants.PUBSUB_ELEMENT; |
| 50 | import 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 |
| 58 | public 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 | } |