Andrea Campanella | 945ded2 | 2016-01-07 13:17:43 -0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.protocol.rest.ctl; |
| 18 | |
| 19 | import com.sun.jersey.api.client.Client; |
| 20 | import com.sun.jersey.api.client.ClientResponse; |
| 21 | import com.sun.jersey.api.client.WebResource; |
| 22 | import org.apache.commons.io.IOUtils; |
| 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.protocol.rest.RestSBController; |
| 30 | import org.onosproject.protocol.rest.RestSBDevice; |
| 31 | import org.osgi.service.component.ComponentContext; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
| 37 | import java.io.ByteArrayInputStream; |
| 38 | import java.io.IOException; |
| 39 | import java.io.InputStream; |
| 40 | import java.nio.charset.StandardCharsets; |
| 41 | import java.util.Map; |
| 42 | import java.util.concurrent.ConcurrentHashMap; |
| 43 | |
| 44 | /** |
| 45 | * The implementation of RestSBController. |
| 46 | */ |
| 47 | @Component(immediate = true) |
| 48 | @Service |
| 49 | public 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) { |
| 131 | WebResource webResource = getWebResource(device, request); |
| 132 | ClientResponse response = null; |
| 133 | if (payload != null) { |
| 134 | try { |
| 135 | response = webResource.accept(mediaType) |
| 136 | .put(ClientResponse.class, IOUtils.toString(payload, StandardCharsets.UTF_8)); |
| 137 | } catch (IOException e) { |
| 138 | log.error("Cannot do PUT {} request on device {} because can't read payload", |
| 139 | request, device); |
| 140 | } |
| 141 | } else { |
| 142 | response = webResource.accept(mediaType) |
| 143 | .put(ClientResponse.class); |
| 144 | } |
| 145 | return checkReply(response); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public InputStream get(DeviceId device, String request, String mediaType) { |
| 150 | WebResource webResource = getWebResource(device, request); |
| 151 | String type; |
| 152 | switch (mediaType) { |
| 153 | case XML: |
| 154 | type = MediaType.APPLICATION_XML; |
| 155 | break; |
| 156 | case JSON: |
| 157 | type = MediaType.APPLICATION_JSON; |
| 158 | break; |
| 159 | default: |
| 160 | throw new IllegalArgumentException("Unsupported media type " + mediaType); |
| 161 | |
| 162 | } |
| 163 | return new ByteArrayInputStream(webResource.accept(type).get(ClientResponse.class) |
| 164 | .getEntity(String.class) |
| 165 | .getBytes(StandardCharsets.UTF_8)); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public boolean delete(DeviceId device, String request, InputStream payload, String mediaType) { |
| 170 | WebResource webResource = getWebResource(device, request); |
| 171 | ClientResponse response = null; |
| 172 | if (payload != null) { |
| 173 | try { |
| 174 | response = webResource.accept(mediaType) |
| 175 | .delete(ClientResponse.class, IOUtils.toString(payload, StandardCharsets.UTF_8)); |
| 176 | } catch (IOException e) { |
| 177 | log.error("Cannot do PUT {} request on device {} because can't read payload", |
| 178 | request, device); |
| 179 | } |
| 180 | } else { |
| 181 | response = webResource.accept(mediaType) |
| 182 | .delete(ClientResponse.class); |
| 183 | } |
| 184 | return checkReply(response); |
| 185 | } |
| 186 | |
| 187 | private WebResource getWebResource(DeviceId device, String request) { |
| 188 | return Client.create().resource(deviceMap.get(device).protocol() + COLON + |
| 189 | DOUBLESLASH + |
| 190 | deviceMap.get(device).ip().toString() + |
| 191 | COLON + deviceMap.get(device).port() + |
| 192 | SLASH + request); |
| 193 | } |
| 194 | |
| 195 | private boolean checkReply(ClientResponse response) { |
| 196 | if (response != null) { |
| 197 | if (response.getStatus() == STATUS_OK || |
| 198 | response.getStatus() == STATUS_CREATED || |
| 199 | response.getStatus() == STATUS_ACCEPTED) { |
| 200 | return true; |
| 201 | } else { |
| 202 | log.error("Failed request: HTTP error code : " |
| 203 | + response.getStatus()); |
| 204 | return false; |
| 205 | } |
| 206 | } |
| 207 | log.error("Null reply from device"); |
| 208 | return false; |
| 209 | } |
| 210 | } |