blob: 5a75ef9e68798b91825dcd4913f169f1b3f39bfe [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;
Sean Condon5548ce62018-07-30 16:00:10 +010020import org.onosproject.event.EventDeliveryService;
21import org.onosproject.event.ListenerRegistry;
22import org.onosproject.event.ListenerService;
Michele Santuaric372c222017-01-12 09:41:25 +010023import org.onosproject.net.DeviceId;
Hesam Rahimi4a409b42016-08-12 18:37:33 -040024import org.onosproject.protocol.http.ctl.HttpSBControllerImpl;
Andrea Campanella945ded22016-01-07 13:17:43 -080025import org.onosproject.protocol.rest.RestSBController;
Michele Santuaric372c222017-01-12 09:41:25 +010026import org.onosproject.protocol.rest.RestSBDevice;
Sean Condon5548ce62018-07-30 16:00:10 +010027import org.onosproject.protocol.rest.RestSBEventListener;
28import org.onosproject.protocol.rest.RestSBServerSentEvent;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070029import org.osgi.service.component.annotations.Activate;
30import org.osgi.service.component.annotations.Component;
31import org.osgi.service.component.annotations.Deactivate;
32import org.osgi.service.component.annotations.Reference;
33import org.osgi.service.component.annotations.ReferenceCardinality;
Andrea Campanella945ded22016-01-07 13:17:43 -080034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Michele Santuaric372c222017-01-12 09:41:25 +010037import javax.ws.rs.client.WebTarget;
Sean Condon5548ce62018-07-30 16:00:10 +010038import javax.ws.rs.sse.InboundSseEvent;
Michele Santuaric372c222017-01-12 09:41:25 +010039import java.util.Map;
40import java.util.Set;
41import java.util.concurrent.ConcurrentHashMap;
42import java.util.stream.Collectors;
43
Andrea Campanella945ded22016-01-07 13:17:43 -080044/**
45 * The implementation of RestSBController.
46 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070047@Component(immediate = true, service = { RestSBController.class, ListenerService.class })
Sean Condon5548ce62018-07-30 16:00:10 +010048public class RestSBControllerImpl extends HttpSBControllerImpl
49 implements RestSBController, ListenerService<RestSBServerSentEvent, RestSBEventListener> {
Andrea Campanella945ded22016-01-07 13:17:43 -080050
51 private static final Logger log =
52 LoggerFactory.getLogger(RestSBControllerImpl.class);
Andrea Campanella945ded22016-01-07 13:17:43 -080053
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sean Condon5548ce62018-07-30 16:00:10 +010055 protected EventDeliveryService eventDispatcher;
56
57 protected final ListenerRegistry<RestSBServerSentEvent, RestSBEventListener> listenerRegistry =
58 new ListenerRegistry<>();
59
Michele Santuaric372c222017-01-12 09:41:25 +010060 private final Map<DeviceId, RestSBDevice> proxiedDeviceMap = new ConcurrentHashMap<>();
61
Andrea Campanella945ded22016-01-07 13:17:43 -080062 @Activate
Andrea Campanellace279ee2016-01-25 10:21:45 -080063 public void activate() {
Sean Condon5548ce62018-07-30 16:00:10 +010064 eventDispatcher.addSink(RestSBServerSentEvent.class, listenerRegistry);
Andrea Campanella945ded22016-01-07 13:17:43 -080065 log.info("Started");
66 }
67
68 @Deactivate
69 public void deactivate() {
Hesam Rahimi4a409b42016-08-12 18:37:33 -040070 this.getClientMap().clear();
71 this.getDeviceMap().clear();
Sean Condon5548ce62018-07-30 16:00:10 +010072 this.getSseEventSourceMap().clear();
Andrea Campanella945ded22016-01-07 13:17:43 -080073 log.info("Stopped");
74 }
75
Michele Santuaric372c222017-01-12 09:41:25 +010076
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 Condon5548ce62018-07-30 16:00:10 +0100136
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 Campanella945ded22016-01-07 13:17:43 -0800168}