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; |
| 23 | import org.apache.felix.scr.annotations.Service; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 25 | import org.onosproject.protocol.http.ctl.HttpSBControllerImpl; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 26 | import org.onosproject.protocol.rest.RestSBController; |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 27 | import org.onosproject.protocol.rest.RestSBDevice; |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 31 | import javax.ws.rs.client.WebTarget; |
| 32 | import java.util.Map; |
| 33 | import java.util.Set; |
| 34 | import java.util.concurrent.ConcurrentHashMap; |
| 35 | import java.util.stream.Collectors; |
| 36 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 37 | /** |
| 38 | * The implementation of RestSBController. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | @Service |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 42 | public class RestSBControllerImpl extends HttpSBControllerImpl implements RestSBController { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 43 | |
| 44 | private static final Logger log = |
| 45 | LoggerFactory.getLogger(RestSBControllerImpl.class); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 46 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 47 | private final Map<DeviceId, RestSBDevice> proxiedDeviceMap = new ConcurrentHashMap<>(); |
| 48 | |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 49 | @Activate |
Andrea Campanella | ce279ee | 2016-01-25 10:21:45 -0800 | [diff] [blame] | 50 | public void activate() { |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 51 | log.info("Started"); |
| 52 | } |
| 53 | |
| 54 | @Deactivate |
| 55 | public void deactivate() { |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 56 | this.getClientMap().clear(); |
| 57 | this.getDeviceMap().clear(); |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 58 | log.info("Stopped"); |
| 59 | } |
| 60 | |
Michele Santuari | c372c22 | 2017-01-12 09:41:25 +0100 | [diff] [blame] | 61 | |
| 62 | @Override |
| 63 | public void addProxiedDevice(DeviceId deviceId, RestSBDevice proxy) { |
| 64 | proxiedDeviceMap.put(deviceId, proxy); |
| 65 | log.debug("Added device: {} to proxy {}", deviceId, proxy.deviceId()); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void removeProxiedDevice(DeviceId deviceId) { |
| 70 | log.debug("Removed device: {} from proxy {}", deviceId, proxiedDeviceMap.get(deviceId).deviceId()); |
| 71 | proxiedDeviceMap.remove(deviceId); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | @Override |
| 76 | public Set<DeviceId> getProxiedDevices(DeviceId proxyId) { |
| 77 | return ImmutableSet.copyOf( |
| 78 | proxiedDeviceMap.keySet().stream().filter( |
| 79 | v -> proxiedDeviceMap.get(v).deviceId().equals(proxyId) |
| 80 | ).collect(Collectors.toSet())); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public RestSBDevice getProxySBDevice(DeviceId deviceId) { |
| 85 | return proxiedDeviceMap.get(deviceId); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected WebTarget getWebTarget(DeviceId device, String request) { |
| 90 | DeviceId deviceId = device; |
| 91 | if (proxiedDeviceMap.containsKey(device)) { |
| 92 | deviceId = proxiedDeviceMap.get(device).deviceId(); |
| 93 | } |
| 94 | return super.getWebTarget(deviceId, request); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | protected String getUrlString(DeviceId id, String request) { |
| 99 | DeviceId deviceId = id; |
| 100 | if (proxiedDeviceMap.containsKey(id)) { |
| 101 | deviceId = proxiedDeviceMap.get(id).deviceId(); |
| 102 | } |
| 103 | |
| 104 | RestSBDevice device = super.getDeviceMap().get(deviceId); |
| 105 | if (device != null) { |
| 106 | if (device.url() != null && !device.url().isEmpty()) { |
| 107 | return device.protocol() + super.COLON + super.DOUBLESLASH + device.ip().toString() + |
| 108 | super.COLON + device.port() |
| 109 | + device.url() + request; |
| 110 | } else { |
| 111 | return device.protocol() + super.COLON + |
| 112 | super.DOUBLESLASH + |
| 113 | device.ip().toString() + |
| 114 | super.COLON + device.port() + request; |
| 115 | } |
| 116 | } else { |
| 117 | return super.getUrlString(deviceId, request); |
| 118 | } |
| 119 | |
| 120 | } |
Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame] | 121 | } |