blob: a527e0e62432c5082e48ca1f35ce9d27f07b3ee2 [file] [log] [blame]
Andrea Campanella945ded22016-01-07 13:17:43 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Andrea Campanella945ded22016-01-07 13:17:43 -08003 *
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.protocol.rest.ctl;
18
Michele Santuaric372c222017-01-12 09:41:25 +010019import com.google.common.collect.ImmutableSet;
Andrea Campanella945ded22016-01-07 13:17:43 -080020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
Sean Condon5548ce62018-07-30 16:00:10 +010023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Andrea Campanella945ded22016-01-07 13:17:43 -080025import org.apache.felix.scr.annotations.Service;
Sean Condon5548ce62018-07-30 16:00:10 +010026import org.onosproject.event.EventDeliveryService;
27import org.onosproject.event.ListenerRegistry;
28import org.onosproject.event.ListenerService;
Michele Santuaric372c222017-01-12 09:41:25 +010029import org.onosproject.net.DeviceId;
Hesam Rahimi4a409b42016-08-12 18:37:33 -040030import org.onosproject.protocol.http.ctl.HttpSBControllerImpl;
Andrea Campanella945ded22016-01-07 13:17:43 -080031import org.onosproject.protocol.rest.RestSBController;
Michele Santuaric372c222017-01-12 09:41:25 +010032import org.onosproject.protocol.rest.RestSBDevice;
Sean Condon5548ce62018-07-30 16:00:10 +010033import org.onosproject.protocol.rest.RestSBEventListener;
34import org.onosproject.protocol.rest.RestSBServerSentEvent;
Andrea Campanella945ded22016-01-07 13:17:43 -080035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Michele Santuaric372c222017-01-12 09:41:25 +010038import javax.ws.rs.client.WebTarget;
Sean Condon5548ce62018-07-30 16:00:10 +010039import javax.ws.rs.sse.InboundSseEvent;
Michele Santuaric372c222017-01-12 09:41:25 +010040import java.util.Map;
41import java.util.Set;
42import java.util.concurrent.ConcurrentHashMap;
43import java.util.stream.Collectors;
44
Andrea Campanella945ded22016-01-07 13:17:43 -080045/**
46 * The implementation of RestSBController.
47 */
48@Component(immediate = true)
49@Service
Sean Condon5548ce62018-07-30 16:00:10 +010050public class RestSBControllerImpl extends HttpSBControllerImpl
51 implements RestSBController, ListenerService<RestSBServerSentEvent, RestSBEventListener> {
Andrea Campanella945ded22016-01-07 13:17:43 -080052
53 private static final Logger log =
54 LoggerFactory.getLogger(RestSBControllerImpl.class);
Andrea Campanella945ded22016-01-07 13:17:43 -080055
Sean Condon5548ce62018-07-30 16:00:10 +010056 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected EventDeliveryService eventDispatcher;
58
59 protected final ListenerRegistry<RestSBServerSentEvent, RestSBEventListener> listenerRegistry =
60 new ListenerRegistry<>();
61
Michele Santuaric372c222017-01-12 09:41:25 +010062 private final Map<DeviceId, RestSBDevice> proxiedDeviceMap = new ConcurrentHashMap<>();
63
Andrea Campanella945ded22016-01-07 13:17:43 -080064 @Activate
Andrea Campanellace279ee2016-01-25 10:21:45 -080065 public void activate() {
Sean Condon5548ce62018-07-30 16:00:10 +010066 eventDispatcher.addSink(RestSBServerSentEvent.class, listenerRegistry);
Andrea Campanella945ded22016-01-07 13:17:43 -080067 log.info("Started");
68 }
69
70 @Deactivate
71 public void deactivate() {
Hesam Rahimi4a409b42016-08-12 18:37:33 -040072 this.getClientMap().clear();
73 this.getDeviceMap().clear();
Sean Condon5548ce62018-07-30 16:00:10 +010074 this.getSseEventSourceMap().clear();
Andrea Campanella945ded22016-01-07 13:17:43 -080075 log.info("Stopped");
76 }
77
Michele Santuaric372c222017-01-12 09:41:25 +010078
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 Condon5548ce62018-07-30 16:00:10 +0100138
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 Campanella945ded22016-01-07 13:17:43 -0800170}