blob: 6e8df34a2a402c0ec27cb4ff49d0ec656d8d2ef3 [file] [log] [blame]
Georgios Katsikas83600982017-05-28 20:41:45 +02001/*
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
17package org.onosproject.drivers.server;
18
19import org.onlab.packet.IpAddress;
20
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.behaviour.ControllerInfo;
23import org.onosproject.protocol.rest.RestSBController;
24import org.onosproject.protocol.rest.RestSBDevice;
25
26import com.fasterxml.jackson.databind.ObjectMapper;
27import com.fasterxml.jackson.databind.node.ArrayNode;
28import com.fasterxml.jackson.databind.node.ObjectNode;
29
30import java.io.ByteArrayInputStream;
31import java.io.InputStream;
32import java.util.List;
33import java.util.Set;
34import java.util.Map;
35import java.util.concurrent.ConcurrentHashMap;
Sean Condon5548ce62018-07-30 16:00:10 +010036import java.util.function.Consumer;
Georgios Katsikas83600982017-05-28 20:41:45 +020037import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
Sean Condon5548ce62018-07-30 16:00:10 +010039import javax.ws.rs.sse.InboundSseEvent;
Georgios Katsikas83600982017-05-28 20:41:45 +020040
41import static org.hamcrest.Matchers.notNullValue;
42import static org.junit.Assert.assertThat;
Georgios Katsikas740d3282020-03-18 12:05:03 +010043import static org.onosproject.drivers.server.Constants.PARAM_CTRL;
44import static org.onosproject.drivers.server.Constants.PARAM_CTRL_IP;
45import static org.onosproject.drivers.server.Constants.PARAM_CTRL_PORT;
46import static org.onosproject.drivers.server.Constants.PARAM_CTRL_TYPE;
Georgios Katsikas83600982017-05-28 20:41:45 +020047
48/**
49 * Test class for REST SB controller.
50 */
51public 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 Katsikas83600982017-05-28 20:41:45 +020064 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 Condon5548ce62018-07-30 16:00:10 +0100169 public <T> T post(DeviceId device, String request, InputStream payload,
170 MediaType mediaType, Class<T> responseClass) {
Georgios Katsikas83600982017-05-28 20:41:45 +0200171 return null;
Sean Condon5548ce62018-07-30 16:00:10 +0100172 }
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 Katsikas740d3282020-03-18 12:05:03 +0100189
Georgios Katsikas83600982017-05-28 20:41:45 +0200190}