Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.drivers.server; |
| 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.behaviour.ControllerInfo; |
| 23 | import org.onosproject.protocol.rest.RestSBController; |
| 24 | import org.onosproject.protocol.rest.RestSBDevice; |
| 25 | |
| 26 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 28 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 29 | |
| 30 | import java.io.ByteArrayInputStream; |
| 31 | import java.io.InputStream; |
| 32 | import java.util.List; |
| 33 | import java.util.Set; |
| 34 | import java.util.Map; |
| 35 | import java.util.concurrent.ConcurrentHashMap; |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame^] | 36 | import java.util.function.Consumer; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 37 | import javax.ws.rs.core.MediaType; |
| 38 | import javax.ws.rs.core.Response; |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame^] | 39 | import javax.ws.rs.sse.InboundSseEvent; |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 40 | |
| 41 | import static org.hamcrest.Matchers.notNullValue; |
| 42 | import static org.junit.Assert.assertThat; |
| 43 | |
| 44 | /** |
| 45 | * Test class for REST SB controller. |
| 46 | */ |
| 47 | public class RestSBControllerMock implements RestSBController { |
| 48 | /** |
| 49 | * Local memory of devices. |
| 50 | */ |
| 51 | private Map<DeviceId, RestSBDevice> deviceMap = new ConcurrentHashMap<>(); |
| 52 | |
| 53 | /** |
| 54 | * Objects to populate our memory. |
| 55 | */ |
| 56 | private DeviceId restDeviceId1; |
| 57 | private RestSBDevice restDevice1; |
| 58 | private static List<ControllerInfo> controllers; |
| 59 | |
| 60 | /** |
| 61 | * Parameters to be exchanged with the server's agent. |
| 62 | */ |
| 63 | private static final String PARAM_CTRL = "controllers"; |
| 64 | private static final String PARAM_CTRL_IP = "ip"; |
| 65 | private static final String PARAM_CTRL_PORT = "port"; |
| 66 | private static final String PARAM_CTRL_TYPE = "type"; |
| 67 | |
| 68 | public RestSBControllerMock() { |
| 69 | restDeviceId1 = TestConfig.REST_DEV_ID1; |
| 70 | assertThat(restDeviceId1, notNullValue()); |
| 71 | |
| 72 | restDevice1 = TestConfig.REST_DEV1; |
| 73 | assertThat(restDevice1, notNullValue()); |
| 74 | |
| 75 | controllers = TestConfig.CONTROLLERS; |
| 76 | assertThat(controllers, notNullValue()); |
| 77 | |
| 78 | deviceMap.put(restDeviceId1, restDevice1); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void addProxiedDevice(DeviceId deviceId, RestSBDevice proxy) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void removeProxiedDevice(DeviceId deviceId) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public Set<DeviceId> getProxiedDevices(DeviceId proxyId) { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public RestSBDevice getProxySBDevice(DeviceId deviceId) { |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Map<DeviceId, RestSBDevice> getDevices() { |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public RestSBDevice getDevice(DeviceId deviceInfo) { |
| 108 | return null; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public RestSBDevice getDevice(IpAddress ip, int port) { |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void addDevice(RestSBDevice device) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void removeDevice(DeviceId deviceId) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public InputStream get(DeviceId device, String request, MediaType mediaType) { |
| 128 | /** |
| 129 | * We fake the HTTP get in order to |
| 130 | * emulate the expected response. |
| 131 | */ |
| 132 | ObjectMapper mapper = new ObjectMapper(); |
| 133 | |
| 134 | // Create the object node to host the data |
| 135 | ObjectNode sendObjNode = mapper.createObjectNode(); |
| 136 | |
| 137 | // Insert header |
| 138 | ArrayNode ctrlsArrayNode = sendObjNode.putArray(PARAM_CTRL); |
| 139 | |
| 140 | // Add each controller's information object |
| 141 | for (ControllerInfo ctrl : controllers) { |
| 142 | ObjectNode ctrlObjNode = mapper.createObjectNode(); |
| 143 | ctrlObjNode.put(PARAM_CTRL_IP, ctrl.ip().toString()); |
| 144 | ctrlObjNode.put(PARAM_CTRL_PORT, ctrl.port()); |
| 145 | ctrlObjNode.put(PARAM_CTRL_TYPE, ctrl.type()); |
| 146 | ctrlsArrayNode.add(ctrlObjNode); |
| 147 | } |
| 148 | |
| 149 | return new ByteArrayInputStream(sendObjNode.toString().getBytes()); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public int post(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
| 154 | return Response.Status.OK.getStatusCode(); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public int put(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
| 159 | return Response.Status.OK.getStatusCode(); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public int patch(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
| 164 | return Response.Status.OK.getStatusCode(); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public int delete(DeviceId device, String request, InputStream payload, MediaType mediaType) { |
| 169 | return Response.Status.OK.getStatusCode(); |
| 170 | } |
| 171 | |
| 172 | @Override |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame^] | 173 | public <T> T post(DeviceId device, String request, InputStream payload, |
| 174 | MediaType mediaType, Class<T> responseClass) { |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 175 | return null; |
Sean Condon | 5548ce6 | 2018-07-30 16:00:10 +0100 | [diff] [blame^] | 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void startServerSentEvents(DeviceId deviceId, String eventsUrl) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public int getServerSentEvents(DeviceId deviceId, String request, |
| 185 | Consumer<InboundSseEvent> onEvent, Consumer<Throwable> onError) { |
| 186 | return 204; |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public int cancelServerSentEvents(DeviceId deviceId) { |
| 191 | return 200; |
| 192 | } |
Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [diff] [blame] | 193 | } |