blob: 4c4829fa74ef22214893a55fed0dbbab6fa7261a [file] [log] [blame]
Rohit Singh16d4df92019-07-15 19:33:05 +05301/*
2 * Copyright 2019-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 * This Work is contributed by Sterlite Technologies
17 */
18package org.onosproject.rest.resources;
19
20import com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.ObjectMapper;
22import com.fasterxml.jackson.databind.node.ArrayNode;
23import com.fasterxml.jackson.databind.node.ObjectNode;
24import org.onosproject.net.Device;
25import org.onosproject.net.Direction;
26import org.onosproject.net.ModulationScheme;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.behaviour.ModulationConfig;
29import org.onosproject.net.device.DeviceService;
30import org.onosproject.rest.AbstractWebResource;
31import org.slf4j.Logger;
32
33import javax.ws.rs.Consumes;
34import javax.ws.rs.GET;
35import javax.ws.rs.PUT;
36import javax.ws.rs.Path;
37import javax.ws.rs.PathParam;
38import javax.ws.rs.Produces;
39import javax.ws.rs.QueryParam;
40import javax.ws.rs.core.Context;
41import javax.ws.rs.core.MediaType;
42import javax.ws.rs.core.Response;
43import javax.ws.rs.core.UriInfo;
44import java.io.IOException;
45import java.io.InputStream;
46import java.util.Iterator;
47import java.util.Map;
48
49import static com.google.common.base.Preconditions.checkNotNull;
50import static org.onlab.util.Tools.readTreeFromStream;
51import static org.onosproject.net.DeviceId.deviceId;
52import static org.slf4j.LoggerFactory.getLogger;
53
54/**
55 * Query and program flow rules.
56 */
57
58@Path("modulation")
59public class ModulationWebResource extends AbstractWebResource {
60
61 private final Logger log = getLogger(getClass());
62 @Context
63 private UriInfo uriInfo;
64
65 private static final String DEVICE_NOT_FOUND = "Device is not found";
66 private static final String APP_ID_NOT_FOUND = "Application Id is not found";
67 private static final String MODULATIONCONFIG_UNSUPPORTED = "Modulation Config is not supported";
68
69 private static final String DEVICES = "modulationConfigDevices";
70 private static final String DEVICE_ID = "deviceId";
71 private static final String DEVICE_IDS = "modulationConfigDeviceIds";
72 private static final String MODULATIONCONFIG_SUPPORTED = "modulationConfigSupported";
73 private static final String TARGET_MODULATION = "targetModulation";
74 private static final String TARGET_BITRATE = "targetBitRate";
75 private static final String JSON_INVALID = "Invalid json input";
76
77 private final ObjectMapper mapper = new ObjectMapper();
78
79 /**
80 * Gets all modulation config devices.
81 * Returns array of all discovered modulation config devices.
82 *
83 * @return 200 OK with a collection of devices supporting modulation
84 */
85 @GET
86 @Produces(MediaType.APPLICATION_JSON)
87 public Response getModulationSupportedDevices() {
88 ObjectNode root = mapper().createObjectNode();
89 ArrayNode deviceIdsNode = root.putArray(DEVICE_IDS);
90
91 Iterable<Device> devices = get(DeviceService.class).getDevices();
92 if (devices != null) {
93 for (Device d : devices) {
94 if (getModulationConfig(d.id().toString()) != null) {
95 deviceIdsNode.add(d.id().toString());
96 }
97 }
98 }
99
100 return ok(root).build();
101 }
102
103 private ModulationConfig<Object> getModulationConfig(String id) {
104 Device device = get(DeviceService.class).getDevice(deviceId(id));
105 if (device == null) {
106 throw new IllegalArgumentException(DEVICE_NOT_FOUND);
107 }
108 if (device.is(ModulationConfig.class)) {
109 return device.as(ModulationConfig.class);
110 }
111 return null;
112 }
113
114
115 /**
116 * Gets the details of a modulation config device.
117 * Returns the details of the specified modulation config device.
118 *
119 * @param id device identifier
120 * @return 200 OK with a device
121 */
122 @GET
123 @Path("{id}")
124 @Produces(MediaType.APPLICATION_JSON)
125 public Response isModulationSupported(@PathParam("id") String id) {
126 ObjectNode result = mapper.createObjectNode();
127 result.put(MODULATIONCONFIG_SUPPORTED, (getModulationConfig(id) != null) ? true : false);
128 return ok(result).build();
129 }
130
131
132 /**
133 * Returns the supported modulation scheme for specified ports of device.
134 * <p>
135 * ModulationConfigDeviceGetPorts
136 *
137 * @param id device identifier
138 * @param portId line port identifier
139 * @return 200 OK with a modulationGetResponse.
140 */
141 @GET
142 @Path("{id}/port")
143 @Produces(MediaType.APPLICATION_JSON)
144 public Response getConfigurablePortModulation(@PathParam("id") String id,
145 @QueryParam("port_id") String portId) {
146 ModulationConfig<Object> modulationConfig = getModulationConfig(id);
147 if (modulationConfig == null) {
148 throw new IllegalArgumentException(MODULATIONCONFIG_UNSUPPORTED);
149 }
150
151 ObjectNode result = encode(id, modulationConfig, portId);
152 return ok(result).build();
153 }
154
155
156 /**
157 * Applies the target modulation for the specified device.
158 *
159 * @param stream JSON representation of device, port, component and target
160 * bitrate info
161 * @return status of the request - CREATED if the JSON is correct,
162 * BAD_REQUEST if the JSON is invalid
163 * ModulationConfigPut
164 */
165 @PUT
166 @Consumes(MediaType.APPLICATION_JSON)
167 public Response setModulationScheme(InputStream stream) {
168 try {
169 ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
170 decode(jsonTree);
171 return Response.ok().build();
172 } catch (IOException e) {
173 throw new IllegalArgumentException(e);
174 }
175 }
176
177 private ObjectNode encode(String deviceId, ModulationConfig<Object> modulationConfig, String portId) {
178 checkNotNull(modulationConfig, "ModulationConfig cannot be null");
179
180 Direction component = Direction.ALL;
181 PortNumber port = PortNumber.portNumber(portId);
182 log.debug("Port Details for port_id : {} is \n {} ", portId, port);
183
184 ModulationScheme modulation = modulationConfig.getModulationScheme(port, component).get();
185 log.debug("Modulation fetched from driver : " + modulation.name());
186
187 ObjectNode responseNode = mapper.createObjectNode();
188 responseNode.put("deviceId", deviceId);
189 responseNode.put("portId", portId);
190 responseNode.put("modulation", modulation.name());
191
192 return responseNode;
193 }
194
195 public void decode(ObjectNode json) {
196 if (json == null || !json.isObject()) {
197 throw new IllegalArgumentException(JSON_INVALID);
198 }
199
200 JsonNode devicesNode = json.get(DEVICES);
201 if (!devicesNode.isObject()) {
202 throw new IllegalArgumentException(JSON_INVALID);
203 }
204
205 Iterator<Map.Entry<String, JsonNode>> deviceEntries = devicesNode.fields();
206 while (deviceEntries.hasNext()) {
207 Map.Entry<String, JsonNode> deviceEntryNext = deviceEntries.next();
208 String deviceId = deviceEntryNext.getKey();
209 ModulationConfig<Object> modulationConfig = getModulationConfig(deviceId);
210 JsonNode portsNode = deviceEntryNext.getValue();
211 if (!portsNode.isObject()) {
212 throw new IllegalArgumentException(JSON_INVALID);
213 }
214
215 Iterator<Map.Entry<String, JsonNode>> portEntries = portsNode.fields();
216 while (portEntries.hasNext()) {
217 Map.Entry<String, JsonNode> portEntryNext = portEntries.next();
218 PortNumber portNumber = PortNumber.portNumber(portEntryNext.getKey());
219 JsonNode componentsNode = portEntryNext.getValue();
220 Iterator<Map.Entry<String, JsonNode>> componentEntries = componentsNode.fields();
221 while (componentEntries.hasNext()) {
222 Direction direction = null;
223 Map.Entry<String, JsonNode> componentEntryNext = componentEntries.next();
224 try {
225 direction = Direction.valueOf(componentEntryNext.getKey().toUpperCase());
226 } catch (IllegalArgumentException e) {
227 // TODO: Handle other components
228 }
229
230 JsonNode bitRateNode = componentEntryNext.getValue();
231 if (!bitRateNode.isObject()) {
232 throw new IllegalArgumentException(JSON_INVALID);
233 }
234 Long targetBitRate = bitRateNode.get(TARGET_BITRATE).asLong();
235 if (direction != null) {
236 modulationConfig.setModulationScheme(portNumber, direction, targetBitRate);
237 }
238 }
239 }
240 }
241 }
242
243 /**
244 * Sets the modulation for specified device and port.
245 * Returns the details of the specified modulation config device ports.
246 * <p>
247 * ModulationConfigDeviceGetPorts
248 *
249 * @param id device identifier
250 * @param direction port direction(transmitter or receiver port)
251 * @param portId port channel
252 * @param bitrate port bitrate
253 * @return status of the request - CREATED if the JSON is correct,
254 * BAD_REQUEST if the JSON is invalid
255 */
256 @PUT
257 @Path("set-modulation/{id}")
258 @Consumes(MediaType.APPLICATION_JSON)
259 public Response setPortModulation(@PathParam("id") String id,
260 @QueryParam("port_id") String portId,
261 @QueryParam("direction") String direction,
262 @QueryParam("bitrate") long bitrate) {
263
264 ModulationConfig<Object> modulationConfig = getModulationConfig(id);
265 PortNumber portNumber = PortNumber.portNumber(portId);
266 log.info("Port Details for port_id : {} is \n {} ", portId, portNumber);
267 if (direction != null) {
268 Direction component = Direction.valueOf(direction.toUpperCase());
269 modulationConfig.setModulationScheme(portNumber, component, bitrate);
270 } else {
271 log.error("Direction cannot be null");
272 return Response.status(400, "Direction cannot be Null").build();
273 }
274 return Response.ok().build();
275 }
276
277
278}