Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [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.http; |
| 18 | |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 19 | import java.io.InputStream; |
| 20 | import java.util.Map; |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 21 | import java.util.function.Consumer; |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 22 | |
| 23 | import javax.ws.rs.core.MediaType; |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 24 | import javax.ws.rs.sse.InboundSseEvent; |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 25 | |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.protocol.rest.RestSBDevice; |
| 29 | |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 30 | /** |
| 31 | * Abstraction of an HTTP controller. Serves as a one stop shop for obtaining |
| 32 | * HTTP southbound devices and (un)register listeners. |
| 33 | */ |
| 34 | public interface HttpSBController { |
| 35 | |
| 36 | /** |
| 37 | * Returns all the devices known to this controller. |
| 38 | * |
| 39 | * @return map of devices |
| 40 | */ |
| 41 | Map<DeviceId, RestSBDevice> getDevices(); |
| 42 | |
| 43 | /** |
| 44 | * Returns a device by node identifier. |
| 45 | * |
| 46 | * @param deviceInfo node identifier |
| 47 | * @return RestSBDevice rest device |
| 48 | */ |
| 49 | RestSBDevice getDevice(DeviceId deviceInfo); |
| 50 | |
| 51 | /** |
| 52 | * Returns a device by Ip and Port. |
| 53 | * |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 54 | * @param ip device ip |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 55 | * @param port device port |
| 56 | * @return RestSBDevice rest device |
| 57 | */ |
| 58 | RestSBDevice getDevice(IpAddress ip, int port); |
| 59 | |
| 60 | /** |
| 61 | * Adds a device to the device map. |
| 62 | * |
| 63 | * @param device to be added |
| 64 | */ |
| 65 | void addDevice(RestSBDevice device); |
| 66 | |
| 67 | /** |
| 68 | * Removes the device from the devices map. |
| 69 | * |
| 70 | * @param deviceId to be removed |
| 71 | */ |
| 72 | void removeDevice(DeviceId deviceId); |
| 73 | |
| 74 | /** |
| 75 | * Does a HTTP POST request with specified parameters to the device. |
| 76 | * |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 77 | * @param device device to make the request to |
| 78 | * @param request url of the request |
| 79 | * @param payload payload of the request as an InputStream |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 80 | * @param mediaType type of content in the payload i.e. application/json |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 81 | * @return status Commonly used status codes defined by HTTP |
| 82 | */ |
| 83 | int post(DeviceId device, String request, InputStream payload, MediaType mediaType); |
| 84 | |
| 85 | /** |
| 86 | * Does a HTTP PUT request with specified parameters to the device. |
| 87 | * |
| 88 | * @param device device to make the request to |
| 89 | * @param request resource path of the request |
| 90 | * @param payload payload of the request as an InputStream |
| 91 | * @param mediaType type of content in the payload i.e. application/json |
| 92 | * @return status Commonly used status codes defined by HTTP |
| 93 | */ |
| 94 | int put(DeviceId device, String request, InputStream payload, MediaType mediaType); |
| 95 | |
| 96 | /** |
| 97 | * Does a HTTP PATCH request with specified parameters to the device. |
| 98 | * |
| 99 | * @param device device to make the request to |
| 100 | * @param request url of the request |
| 101 | * @param payload payload of the request as an InputStream |
| 102 | * @param mediaType format to retrieve the content in |
| 103 | * @return status Commonly used status codes defined by HTTP |
| 104 | */ |
| 105 | int patch(DeviceId device, String request, InputStream payload, MediaType mediaType); |
| 106 | |
| 107 | /** |
| 108 | * Does a HTTP DELETE request with specified parameters to the device. |
| 109 | * |
| 110 | * @param device device to make the request to |
| 111 | * @param request url of the request |
| 112 | * @param payload payload of the request as an InputStream |
| 113 | * @param mediaType type of content in the payload i.e. application/json |
| 114 | * @return status Commonly used status codes defined by HTTP |
| 115 | */ |
| 116 | int delete(DeviceId device, String request, InputStream payload, MediaType mediaType); |
| 117 | |
| 118 | /** |
| 119 | * |
| 120 | * Does a HTTP GET request with specified parameters to the device. |
| 121 | * |
| 122 | * @param device device to make the request to |
| 123 | * @param request url of the request |
| 124 | * @param mediaType format to retrieve the content in |
| 125 | * @return an inputstream of data from the reply. |
| 126 | */ |
| 127 | InputStream get(DeviceId device, String request, MediaType mediaType); |
| 128 | |
| 129 | /** |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 130 | * Does a HTTP POST request with specified parameters to the device and |
| 131 | * extracts an object of type T from the response entity field. |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 132 | * |
| 133 | * @param <T> post return type |
| 134 | * @param device device to make the request to |
| 135 | * @param request url of the request |
| 136 | * @param payload payload of the request as an InputStream |
| 137 | * @param mediaType type of content in the payload i.e. application/json |
| 138 | * @param responseClass the type of response object we are interested in, |
| 139 | * such as String, InputStream. |
| 140 | * @return Object of type requested via responseClass. |
| 141 | */ |
| 142 | <T> T post(DeviceId device, String request, InputStream payload, MediaType mediaType, Class<T> responseClass); |
| 143 | |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 144 | /** |
| 145 | * Does a HTTP GET against a Server Sent Events (SSE_INBOUND) resource on the device. |
| 146 | * |
| 147 | * This is a low level function that can take callbacks. |
| 148 | * For a higher level function that emits events based on this callback |
| 149 | * see startServerSentEvents() in the RestSBController |
| 150 | * |
| 151 | * @param deviceId device to make the request to |
| 152 | * @param request url of the request |
| 153 | * @param onEvent A consumer of inbound SSE_INBOUND events |
| 154 | * @param onError A consumer of inbound SSE_INBOUND errors |
| 155 | * @return status Commonly used status codes defined by HTTP |
| 156 | */ |
| 157 | int getServerSentEvents(DeviceId deviceId, String request, |
| 158 | Consumer<InboundSseEvent> onEvent, Consumer<Throwable> onError); |
Matteo Gerola | 7e180c2 | 2017-03-30 11:57:58 +0200 | [diff] [blame] | 159 | |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame] | 160 | /** |
| 161 | * Cancels a Server Sent Events listener to a device. |
| 162 | * |
| 163 | * @param deviceId device to cancel the listener for |
| 164 | * @return status Commonly used status codes defined by HTTP |
| 165 | */ |
| 166 | int cancelServerSentEvents(DeviceId deviceId); |
Hesam Rahimi | 4a409b4 | 2016-08-12 18:37:33 -0400 | [diff] [blame] | 167 | } |