blob: 915c1bb38c1b1f011e004cfe331778a2a6bf6919 [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;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
38
39import static org.hamcrest.Matchers.notNullValue;
40import static org.junit.Assert.assertThat;
41
42/**
43 * Test class for REST SB controller.
44 */
45public class RestSBControllerMock implements RestSBController {
46 /**
47 * Local memory of devices.
48 */
49 private Map<DeviceId, RestSBDevice> deviceMap = new ConcurrentHashMap<>();
50
51 /**
52 * Objects to populate our memory.
53 */
54 private DeviceId restDeviceId1;
55 private RestSBDevice restDevice1;
56 private static List<ControllerInfo> controllers;
57
58 /**
59 * Parameters to be exchanged with the server's agent.
60 */
61 private static final String PARAM_CTRL = "controllers";
62 private static final String PARAM_CTRL_IP = "ip";
63 private static final String PARAM_CTRL_PORT = "port";
64 private static final String PARAM_CTRL_TYPE = "type";
65
66 public RestSBControllerMock() {
67 restDeviceId1 = TestConfig.REST_DEV_ID1;
68 assertThat(restDeviceId1, notNullValue());
69
70 restDevice1 = TestConfig.REST_DEV1;
71 assertThat(restDevice1, notNullValue());
72
73 controllers = TestConfig.CONTROLLERS;
74 assertThat(controllers, notNullValue());
75
76 deviceMap.put(restDeviceId1, restDevice1);
77 }
78
79 @Override
80 public void addProxiedDevice(DeviceId deviceId, RestSBDevice proxy) {
81 return;
82 }
83
84 @Override
85 public void removeProxiedDevice(DeviceId deviceId) {
86 return;
87 }
88
89 @Override
90 public Set<DeviceId> getProxiedDevices(DeviceId proxyId) {
91 return null;
92 }
93
94 @Override
95 public RestSBDevice getProxySBDevice(DeviceId deviceId) {
96 return null;
97 }
98
99 @Override
100 public Map<DeviceId, RestSBDevice> getDevices() {
101 return null;
102 }
103
104 @Override
105 public RestSBDevice getDevice(DeviceId deviceInfo) {
106 return null;
107 }
108
109 @Override
110 public RestSBDevice getDevice(IpAddress ip, int port) {
111 return null;
112 }
113
114 @Override
115 public void addDevice(RestSBDevice device) {
116 return;
117 }
118
119 @Override
120 public void removeDevice(DeviceId deviceId) {
121 return;
122 }
123
124 @Override
125 public InputStream get(DeviceId device, String request, MediaType mediaType) {
126 /**
127 * We fake the HTTP get in order to
128 * emulate the expected response.
129 */
130 ObjectMapper mapper = new ObjectMapper();
131
132 // Create the object node to host the data
133 ObjectNode sendObjNode = mapper.createObjectNode();
134
135 // Insert header
136 ArrayNode ctrlsArrayNode = sendObjNode.putArray(PARAM_CTRL);
137
138 // Add each controller's information object
139 for (ControllerInfo ctrl : controllers) {
140 ObjectNode ctrlObjNode = mapper.createObjectNode();
141 ctrlObjNode.put(PARAM_CTRL_IP, ctrl.ip().toString());
142 ctrlObjNode.put(PARAM_CTRL_PORT, ctrl.port());
143 ctrlObjNode.put(PARAM_CTRL_TYPE, ctrl.type());
144 ctrlsArrayNode.add(ctrlObjNode);
145 }
146
147 return new ByteArrayInputStream(sendObjNode.toString().getBytes());
148 }
149
150 @Override
151 public int post(DeviceId device, String request, InputStream payload, MediaType mediaType) {
152 return Response.Status.OK.getStatusCode();
153 }
154
155 @Override
156 public int put(DeviceId device, String request, InputStream payload, MediaType mediaType) {
157 return Response.Status.OK.getStatusCode();
158 }
159
160 @Override
161 public int patch(DeviceId device, String request, InputStream payload, MediaType mediaType) {
162 return Response.Status.OK.getStatusCode();
163 }
164
165 @Override
166 public int delete(DeviceId device, String request, InputStream payload, MediaType mediaType) {
167 return Response.Status.OK.getStatusCode();
168 }
169
170 @Override
171 public <T> T post(DeviceId device, String request, InputStream payload,
172 MediaType mediaType, Class<T> responseClass) {
173 return null;
174 }
175}