blob: 86f8020937c2b25341696036e0acc9930e6cc92a [file] [log] [blame]
Andrea Campanella945ded22016-01-07 13:17:43 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
23import org.apache.felix.scr.annotations.Service;
Michele Santuaric372c222017-01-12 09:41:25 +010024import org.onosproject.net.DeviceId;
Hesam Rahimi4a409b42016-08-12 18:37:33 -040025import org.onosproject.protocol.http.ctl.HttpSBControllerImpl;
Andrea Campanella945ded22016-01-07 13:17:43 -080026import org.onosproject.protocol.rest.RestSBController;
Michele Santuaric372c222017-01-12 09:41:25 +010027import org.onosproject.protocol.rest.RestSBDevice;
Andrea Campanella945ded22016-01-07 13:17:43 -080028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
Michele Santuaric372c222017-01-12 09:41:25 +010031import javax.ws.rs.client.WebTarget;
32import java.util.Map;
33import java.util.Set;
34import java.util.concurrent.ConcurrentHashMap;
35import java.util.stream.Collectors;
36
Andrea Campanella945ded22016-01-07 13:17:43 -080037/**
38 * The implementation of RestSBController.
39 */
40@Component(immediate = true)
41@Service
Hesam Rahimi4a409b42016-08-12 18:37:33 -040042public class RestSBControllerImpl extends HttpSBControllerImpl implements RestSBController {
Andrea Campanella945ded22016-01-07 13:17:43 -080043
44 private static final Logger log =
45 LoggerFactory.getLogger(RestSBControllerImpl.class);
Andrea Campanella945ded22016-01-07 13:17:43 -080046
Michele Santuaric372c222017-01-12 09:41:25 +010047 private final Map<DeviceId, RestSBDevice> proxiedDeviceMap = new ConcurrentHashMap<>();
48
Andrea Campanella945ded22016-01-07 13:17:43 -080049 @Activate
Andrea Campanellace279ee2016-01-25 10:21:45 -080050 public void activate() {
Andrea Campanella945ded22016-01-07 13:17:43 -080051 log.info("Started");
52 }
53
54 @Deactivate
55 public void deactivate() {
Hesam Rahimi4a409b42016-08-12 18:37:33 -040056 this.getClientMap().clear();
57 this.getDeviceMap().clear();
Andrea Campanella945ded22016-01-07 13:17:43 -080058 log.info("Stopped");
59 }
60
Michele Santuaric372c222017-01-12 09:41:25 +010061
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 Campanella945ded22016-01-07 13:17:43 -0800121}