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