blob: b659d2f9dfaf9f0ef25e2811ae159dc0893b27c2 [file] [log] [blame]
Andrea Campanella945ded22016-01-07 13:17:43 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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
17package org.onosproject.protocol.rest.ctl;
18
19import com.sun.jersey.api.client.Client;
20import com.sun.jersey.api.client.ClientResponse;
21import com.sun.jersey.api.client.WebResource;
22import org.apache.commons.io.IOUtils;
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Service;
27import org.onlab.packet.IpAddress;
28import org.onosproject.net.DeviceId;
29import org.onosproject.protocol.rest.RestSBController;
30import org.onosproject.protocol.rest.RestSBDevice;
31import org.osgi.service.component.ComponentContext;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import javax.ws.rs.core.MediaType;
36import javax.ws.rs.core.Response;
37import java.io.ByteArrayInputStream;
38import java.io.IOException;
39import java.io.InputStream;
40import java.nio.charset.StandardCharsets;
41import java.util.Map;
42import java.util.concurrent.ConcurrentHashMap;
43
44/**
45 * The implementation of RestSBController.
46 */
47@Component(immediate = true)
48@Service
49public class RestSBControllerImpl implements RestSBController {
50
51 private static final Logger log =
52 LoggerFactory.getLogger(RestSBControllerImpl.class);
53 private static final String APPLICATION = "application/";
54 private static final String XML = "xml";
55 private static final String JSON = "json";
56 private static final String DOUBLESLASH = "//";
57 private static final String COLON = ":";
58 private static final int STATUS_OK = Response.Status.OK.getStatusCode();
59 private static final int STATUS_CREATED = Response.Status.CREATED.getStatusCode();
60 private static final int STATUS_ACCEPTED = Response.Status.ACCEPTED.getStatusCode();
61 private static final String SLASH = "/";
62
63 private final Map<DeviceId, RestSBDevice> deviceMap = new ConcurrentHashMap<>();
64 Client client;
65
66 @Activate
67 public void activate(ComponentContext context) {
68 client = Client.create();
69 log.info("Started");
70 }
71
72 @Deactivate
73 public void deactivate() {
74 deviceMap.clear();
75 log.info("Stopped");
76 }
77
78 @Override
79 public Map<DeviceId, RestSBDevice> getDevices() {
80 return deviceMap;
81 }
82
83 @Override
84 public RestSBDevice getDevice(DeviceId deviceInfo) {
85 return deviceMap.get(deviceInfo);
86 }
87
88 @Override
89 public RestSBDevice getDevice(IpAddress ip, int port) {
90 for (DeviceId info : deviceMap.keySet()) {
91 if (IpAddress.valueOf(info.uri().getHost()).equals(ip) &&
92 info.uri().getPort() == port) {
93 return deviceMap.get(info);
94 }
95 }
96 return null;
97 }
98
99 @Override
100 public void addDevice(RestSBDevice device) {
101 deviceMap.put(device.deviceId(), device);
102 }
103
104 @Override
105 public void removeDevice(RestSBDevice device) {
106 deviceMap.remove(device.deviceId());
107 }
108
109 @Override
110 public boolean post(DeviceId device, String request, InputStream payload, String mediaType) {
111 WebResource webResource = getWebResource(device, request);
112
113 ClientResponse response = null;
114 if (payload != null) {
115 try {
116 response = webResource.accept(mediaType)
117 .post(ClientResponse.class, IOUtils.toString(payload, StandardCharsets.UTF_8));
118 } catch (IOException e) {
119 log.error("Cannot do POST {} request on device {} because can't read payload",
120 request, device);
121 }
122 } else {
123 response = webResource.accept(mediaType)
124 .post(ClientResponse.class);
125 }
126 return checkReply(response);
127 }
128
129 @Override
130 public boolean put(DeviceId device, String request, InputStream payload, String mediaType) {
Andrea Campanellad8d92db2016-01-14 16:24:41 -0800131
Andrea Campanella945ded22016-01-07 13:17:43 -0800132 WebResource webResource = getWebResource(device, request);
133 ClientResponse response = null;
134 if (payload != null) {
135 try {
136 response = webResource.accept(mediaType)
137 .put(ClientResponse.class, IOUtils.toString(payload, StandardCharsets.UTF_8));
138 } catch (IOException e) {
139 log.error("Cannot do PUT {} request on device {} because can't read payload",
140 request, device);
141 }
142 } else {
143 response = webResource.accept(mediaType)
144 .put(ClientResponse.class);
145 }
146 return checkReply(response);
147 }
148
149 @Override
150 public InputStream get(DeviceId device, String request, String mediaType) {
151 WebResource webResource = getWebResource(device, request);
152 String type;
153 switch (mediaType) {
154 case XML:
155 type = MediaType.APPLICATION_XML;
156 break;
157 case JSON:
158 type = MediaType.APPLICATION_JSON;
159 break;
160 default:
161 throw new IllegalArgumentException("Unsupported media type " + mediaType);
162
163 }
164 return new ByteArrayInputStream(webResource.accept(type).get(ClientResponse.class)
165 .getEntity(String.class)
166 .getBytes(StandardCharsets.UTF_8));
167 }
168
169 @Override
170 public boolean delete(DeviceId device, String request, InputStream payload, String mediaType) {
171 WebResource webResource = getWebResource(device, request);
172 ClientResponse response = null;
173 if (payload != null) {
174 try {
175 response = webResource.accept(mediaType)
176 .delete(ClientResponse.class, IOUtils.toString(payload, StandardCharsets.UTF_8));
177 } catch (IOException e) {
178 log.error("Cannot do PUT {} request on device {} because can't read payload",
179 request, device);
180 }
181 } else {
182 response = webResource.accept(mediaType)
183 .delete(ClientResponse.class);
184 }
185 return checkReply(response);
186 }
187
188 private WebResource getWebResource(DeviceId device, String request) {
189 return Client.create().resource(deviceMap.get(device).protocol() + COLON +
190 DOUBLESLASH +
191 deviceMap.get(device).ip().toString() +
192 COLON + deviceMap.get(device).port() +
193 SLASH + request);
194 }
195
196 private boolean checkReply(ClientResponse response) {
197 if (response != null) {
198 if (response.getStatus() == STATUS_OK ||
199 response.getStatus() == STATUS_CREATED ||
200 response.getStatus() == STATUS_ACCEPTED) {
201 return true;
202 } else {
203 log.error("Failed request: HTTP error code : "
204 + response.getStatus());
205 return false;
206 }
207 }
208 log.error("Null reply from device");
209 return false;
210 }
211}