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