Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 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.protocol.rest.ctl; |
| 18 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 20 | import org.onosproject.event.EventDeliveryService; |
| 21 | import org.onosproject.event.ListenerRegistry; |
| 22 | import org.onosproject.event.ListenerService; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 24 | import org.onosproject.protocol.http.ctl.HttpSBControllerImpl; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 25 | import org.onosproject.protocol.rest.RestSBController; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 26 | import org.onosproject.protocol.rest.RestSBDevice; |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 27 | import org.onosproject.protocol.rest.RestSBEventListener; |
| 28 | import org.onosproject.protocol.rest.RestSBServerSentEvent; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 29 | import org.osgi.service.component.annotations.Activate; |
| 30 | import org.osgi.service.component.annotations.Component; |
| 31 | import org.osgi.service.component.annotations.Deactivate; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 37 | import javax.ws.rs.client.WebTarget; |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 38 | import javax.ws.rs.sse.InboundSseEvent; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 39 | import java.util.Map; |
| 40 | import java.util.Set; |
| 41 | import java.util.concurrent.ConcurrentHashMap; |
| 42 | import java.util.stream.Collectors; |
| 43 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 44 | /** |
| 45 | * The implementation of RestSBController. |
| 46 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 47 | @Component(immediate = true, service = { RestSBController.class, ListenerService.class }) |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 48 | public class RestSBControllerImpl extends HttpSBControllerImpl |
| 49 | implements RestSBController, ListenerService<RestSBServerSentEvent, RestSBEventListener> { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 50 | |
| 51 | private static final Logger log = |
| 52 | LoggerFactory.getLogger(RestSBControllerImpl.class); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 53 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 55 | protected EventDeliveryService eventDispatcher; |
| 56 | |
| 57 | protected final ListenerRegistry<RestSBServerSentEvent, RestSBEventListener> listenerRegistry = |
| 58 | new ListenerRegistry<>(); |
| 59 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 60 | private final Map<DeviceId, RestSBDevice> proxiedDeviceMap = new ConcurrentHashMap<>(); |
| 61 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 62 | @Activate |
Andrea Campanella | ce279ee | 2016-01-25 10:21:45 -0800 | [diff] [blame] | 63 | public void activate() { |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 64 | eventDispatcher.addSink(RestSBServerSentEvent.class, listenerRegistry); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 65 | log.info("Started"); |
| 66 | } |
| 67 | |
| 68 | @Deactivate |
| 69 | public void deactivate() { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 70 | this.getClientMap().clear(); |
| 71 | this.getDeviceMap().clear(); |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 72 | this.getSseEventSourceMap().clear(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 73 | log.info("Stopped"); |
| 74 | } |
| 75 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | public void addProxiedDevice(DeviceId deviceId, RestSBDevice proxy) { |
| 79 | proxiedDeviceMap.put(deviceId, proxy); |
| 80 | log.debug("Added device: {} to proxy {}", deviceId, proxy.deviceId()); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void removeProxiedDevice(DeviceId deviceId) { |
| 85 | log.debug("Removed device: {} from proxy {}", deviceId, proxiedDeviceMap.get(deviceId).deviceId()); |
| 86 | proxiedDeviceMap.remove(deviceId); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | @Override |
| 91 | public Set<DeviceId> getProxiedDevices(DeviceId proxyId) { |
| 92 | return ImmutableSet.copyOf( |
| 93 | proxiedDeviceMap.keySet().stream().filter( |
| 94 | v -> proxiedDeviceMap.get(v).deviceId().equals(proxyId) |
| 95 | ).collect(Collectors.toSet())); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public RestSBDevice getProxySBDevice(DeviceId deviceId) { |
| 100 | return proxiedDeviceMap.get(deviceId); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | protected WebTarget getWebTarget(DeviceId device, String request) { |
| 105 | DeviceId deviceId = device; |
| 106 | if (proxiedDeviceMap.containsKey(device)) { |
| 107 | deviceId = proxiedDeviceMap.get(device).deviceId(); |
| 108 | } |
| 109 | return super.getWebTarget(deviceId, request); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | protected String getUrlString(DeviceId id, String request) { |
| 114 | DeviceId deviceId = id; |
| 115 | if (proxiedDeviceMap.containsKey(id)) { |
| 116 | deviceId = proxiedDeviceMap.get(id).deviceId(); |
| 117 | } |
| 118 | |
| 119 | RestSBDevice device = super.getDeviceMap().get(deviceId); |
| 120 | if (device != null) { |
| 121 | if (device.url() != null && !device.url().isEmpty()) { |
| 122 | return device.protocol() + super.COLON + super.DOUBLESLASH + device.ip().toString() + |
| 123 | super.COLON + device.port() |
| 124 | + device.url() + request; |
| 125 | } else { |
| 126 | return device.protocol() + super.COLON + |
| 127 | super.DOUBLESLASH + |
| 128 | device.ip().toString() + |
| 129 | super.COLON + device.port() + request; |
| 130 | } |
| 131 | } else { |
| 132 | return super.getUrlString(deviceId, request); |
| 133 | } |
| 134 | |
| 135 | } |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 136 | |
| 137 | @Override |
| 138 | public void startServerSentEvents(DeviceId deviceId, String eventsUrl) { |
| 139 | this.getServerSentEvents(deviceId, eventsUrl, |
| 140 | (event) -> sendEvent(event, deviceId), |
| 141 | (error) -> log.error("Unable to handle {} SSEvent from {}. {}", |
| 142 | eventsUrl, deviceId, error)); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void addListener(RestSBEventListener listener) { |
| 147 | listenerRegistry.addListener(listener); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void removeListener(RestSBEventListener listener) { |
| 152 | listenerRegistry.removeListener(listener); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Safely posts the specified event to the local event dispatcher. |
| 157 | * If there is no event dispatcher or if the event is null, this method |
| 158 | * is a noop. |
| 159 | * @param sseEvent event to be posted; may be null |
| 160 | * @param deviceId the device that sent the event |
| 161 | */ |
| 162 | protected void sendEvent(InboundSseEvent sseEvent, DeviceId deviceId) { |
| 163 | if (sseEvent != null && eventDispatcher != null) { |
| 164 | eventDispatcher.post(new RestSBServerSentEvent( |
| 165 | RestSBServerSentEvent.Type.SSE_INBOUND, deviceId, sseEvent)); |
| 166 | } |
| 167 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 168 | } |