Laszlo Papp | da059e7 | 2017-10-23 11:39:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | package org.onosproject.powermanagement; |
| 17 | |
| 18 | import org.onosproject.net.Device; |
| 19 | import org.onosproject.net.Direction; |
| 20 | import org.onosproject.net.PortNumber; |
| 21 | import org.onosproject.net.behaviour.PowerConfig; |
| 22 | import org.onosproject.net.device.DeviceService; |
| 23 | import org.onosproject.rest.AbstractWebResource; |
| 24 | |
| 25 | import static org.onosproject.net.DeviceId.deviceId; |
| 26 | |
| 27 | import com.fasterxml.jackson.databind.JsonNode; |
| 28 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 30 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 31 | |
| 32 | import com.google.common.collect.HashMultimap; |
| 33 | import com.google.common.collect.Multimap; |
| 34 | import com.google.common.collect.Range; |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | |
| 37 | import javax.ws.rs.Consumes; |
| 38 | import javax.ws.rs.GET; |
| 39 | import javax.ws.rs.PUT; |
| 40 | import javax.ws.rs.Path; |
| 41 | import javax.ws.rs.PathParam; |
| 42 | import javax.ws.rs.Produces; |
| 43 | import javax.ws.rs.QueryParam; |
| 44 | import javax.ws.rs.core.MediaType; |
| 45 | import javax.ws.rs.core.Response; |
| 46 | |
| 47 | import java.io.IOException; |
| 48 | import java.io.InputStream; |
| 49 | |
| 50 | import java.util.Iterator; |
| 51 | import java.util.Map; |
| 52 | import java.util.Map.Entry; |
| 53 | |
| 54 | import org.slf4j.Logger; |
| 55 | import static org.slf4j.LoggerFactory.getLogger; |
| 56 | |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 57 | import static org.onlab.util.Tools.readTreeFromStream; |
| 58 | |
Laszlo Papp | da059e7 | 2017-10-23 11:39:31 +0100 | [diff] [blame] | 59 | /** |
| 60 | * Manage inventory of infrastructure devices with Power Config behaviour. |
| 61 | */ |
| 62 | @Path("devices") |
| 63 | public class PowerConfigWebResource extends AbstractWebResource { |
| 64 | |
| 65 | private static final String JSON_INVALID = "Invalid json input"; |
| 66 | private static final String DEVICE_NOT_FOUND = "Device is not found"; |
| 67 | private static final String POWERCONFIG_UNSUPPORTED = "Power Config is not supported"; |
| 68 | private static final String DIRECTION_UNSUPPORTED = "Direction is not supported"; |
| 69 | |
| 70 | private static final String DEVICES = "powerConfigDevices"; |
| 71 | private static final String PORTS = "ports"; |
| 72 | private static final String DEVICE_ID = "deviceId"; |
| 73 | private static final String DEVICE_IDS = "powerConfigDeviceIds"; |
| 74 | private static final String POWERCONFIG_SUPPORTED = "powerConfigSupported"; |
| 75 | private static final String DIRECTION = "direction"; |
| 76 | private static final String PORT_ID = "portId"; |
| 77 | private static final String TARGET_POWER = "targetPower"; |
| 78 | private static final String CURRENT_POWER = "currentPower"; |
| 79 | private static final String INPUT_POWER_RANGE = "inputPowerRange"; |
| 80 | private static final String TARGET_POWER_RANGE = "targetPowerRange"; |
| 81 | |
| 82 | private final ObjectMapper mapper = new ObjectMapper(); |
| 83 | |
| 84 | private static final Logger log = getLogger(PowerConfigWebResource.class); |
| 85 | |
| 86 | /** |
| 87 | * Gets all power config devices. |
| 88 | * Returns array of all discovered power config devices. |
| 89 | * |
| 90 | * @return 200 OK with a collection of devices |
| 91 | * @onos.rsModel PowerConfigDevicesGet |
| 92 | */ |
| 93 | @GET |
| 94 | @Produces(MediaType.APPLICATION_JSON) |
| 95 | public Response getDevices() { |
| 96 | ObjectNode root = mapper().createObjectNode(); |
| 97 | ArrayNode deviceIdsNode = root.putArray(DEVICE_IDS); |
| 98 | |
| 99 | Iterable<Device> devices = get(DeviceService.class).getDevices(); |
| 100 | if (devices != null) { |
| 101 | for (Device d : devices) { |
| 102 | if (getPowerConfig(d.id().toString()) != null) { |
| 103 | deviceIdsNode.add(d.id().toString()); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return ok(root).build(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Applies the target power for the specified device. |
| 113 | * |
| 114 | * @param stream JSON representation of device, port, component and target |
| 115 | * power info |
| 116 | * @return status of the request - CREATED if the JSON is correct, |
| 117 | * BAD_REQUEST if the JSON is invalid |
| 118 | * @onos.rsModel PowerConfigPut |
| 119 | */ |
| 120 | @PUT |
| 121 | @Consumes(MediaType.APPLICATION_JSON) |
| 122 | public Response setTargetPower(InputStream stream) { |
| 123 | try { |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 124 | ObjectNode jsonTree = readTreeFromStream(mapper(), stream); |
Laszlo Papp | da059e7 | 2017-10-23 11:39:31 +0100 | [diff] [blame] | 125 | decode(jsonTree); |
| 126 | return Response.ok().build(); |
| 127 | } catch (IOException e) { |
| 128 | throw new IllegalArgumentException(e); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Gets the details of a power config device. |
| 134 | * Returns the details of the specified power config device. |
| 135 | * |
| 136 | * @param id device identifier |
| 137 | * @return 200 OK with a device |
| 138 | * @onos.rsModel PowerConfigDeviceGet |
| 139 | */ |
| 140 | @GET |
| 141 | @Path("{id}") |
| 142 | @Produces(MediaType.APPLICATION_JSON) |
| 143 | public Response getDevice(@PathParam("id") String id) { |
| 144 | ObjectNode result = mapper.createObjectNode(); |
| 145 | result.put(POWERCONFIG_SUPPORTED, (getPowerConfig(id) != null) ? true : false); |
| 146 | return ok(result).build(); |
| 147 | } |
| 148 | |
| 149 | private PowerConfig<Object> getPowerConfig(String id) { |
| 150 | Device device = get(DeviceService.class).getDevice(deviceId(id)); |
| 151 | if (device == null) { |
| 152 | throw new IllegalArgumentException(DEVICE_NOT_FOUND); |
| 153 | } |
| 154 | if (device.is(PowerConfig.class)) { |
| 155 | return device.as(PowerConfig.class); |
| 156 | } |
| 157 | return null; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Gets the ports of a power config device. |
| 162 | * Returns the details of the specified power config device ports. |
| 163 | * |
| 164 | * @onos.rsModel PowerConfigDeviceGetPorts |
| 165 | * @param id device identifier |
| 166 | * @param direction port direction |
| 167 | * @param channel port channel |
| 168 | * @return 200 OK with a collection of ports of the given device |
| 169 | */ |
| 170 | @GET |
| 171 | @Path("{id}/ports") |
| 172 | @Produces(MediaType.APPLICATION_JSON) |
| 173 | public Response getDevicePorts(@PathParam("id") String id, |
| 174 | @QueryParam("direction") String direction, |
| 175 | @QueryParam("channel") String channel) { |
| 176 | PowerConfig<Object> powerConfig = getPowerConfig(id); |
| 177 | if (powerConfig == null) { |
| 178 | throw new IllegalArgumentException(POWERCONFIG_UNSUPPORTED); |
| 179 | } |
| 180 | if (direction == null && channel == null) { |
| 181 | direction = "ALL"; |
| 182 | // TODO: Fallback to all channels? |
| 183 | } |
| 184 | ObjectNode result = encode(powerConfig, direction, channel); |
| 185 | return ok(result).build(); |
| 186 | } |
| 187 | |
| 188 | private ObjectNode encode(PowerConfig<Object> powerConfig, String direction, String channel) { |
| 189 | checkNotNull(powerConfig, "PowerConfig cannot be null"); |
| 190 | ObjectNode powerConfigPorts = mapper.createObjectNode(); |
| 191 | Multimap<PortNumber, Object> portsMap = HashMultimap.create(); |
| 192 | |
| 193 | if (direction != null) { |
| 194 | for (PortNumber port : powerConfig.getPorts(direction)) { |
| 195 | portsMap.put(port, Direction.valueOf(direction.toUpperCase())); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (channel != null) { |
| 200 | for (PortNumber port : powerConfig.getPorts(channel)) { |
| 201 | // TODO: channel to be handled |
| 202 | portsMap.put(port, channel); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | for (Map.Entry<PortNumber, Object> entry : portsMap.entries()) { |
| 207 | PortNumber port = entry.getKey(); |
| 208 | ObjectNode powerConfigComponents = mapper.createObjectNode(); |
| 209 | for (Object component : portsMap.get(port)) { |
| 210 | // TODO: channel to be handled |
| 211 | String componentName = "unknown"; |
| 212 | if (component instanceof Direction) { |
| 213 | componentName = component.toString(); |
| 214 | } |
| 215 | ObjectNode powerConfigNode = mapper.createObjectNode() |
Andrea Campanella | dadf640 | 2019-08-07 15:24:11 +0200 | [diff] [blame^] | 216 | .put(CURRENT_POWER, powerConfig.currentPower(port, component).orElse(0.0)) |
| 217 | .put(TARGET_POWER, powerConfig.getTargetPower(port, component).orElse(0.0)) |
Laszlo Papp | da059e7 | 2017-10-23 11:39:31 +0100 | [diff] [blame] | 218 | .put(INPUT_POWER_RANGE, powerConfig.getInputPowerRange(port, |
Andrea Campanella | dadf640 | 2019-08-07 15:24:11 +0200 | [diff] [blame^] | 219 | component).orElse(Range.closed(0.0, 0.0)).toString()) |
Laszlo Papp | da059e7 | 2017-10-23 11:39:31 +0100 | [diff] [blame] | 220 | .put(TARGET_POWER_RANGE, powerConfig.getTargetPowerRange(port, |
Andrea Campanella | dadf640 | 2019-08-07 15:24:11 +0200 | [diff] [blame^] | 221 | component).orElse(Range.closed(0.0, 0.0)).toString()); |
Laszlo Papp | da059e7 | 2017-10-23 11:39:31 +0100 | [diff] [blame] | 222 | powerConfigComponents.set(componentName, powerConfigNode); |
| 223 | } |
| 224 | powerConfigPorts.set(port.toString(), powerConfigComponents); |
| 225 | } |
| 226 | |
| 227 | ObjectNode result = mapper.createObjectNode(); |
| 228 | result.set("powerConfigPorts", powerConfigPorts); |
| 229 | return result; |
| 230 | } |
| 231 | |
| 232 | public void decode(ObjectNode json) { |
| 233 | if (json == null || !json.isObject()) { |
| 234 | throw new IllegalArgumentException(JSON_INVALID); |
| 235 | } |
| 236 | |
| 237 | JsonNode devicesNode = json.get(DEVICES); |
| 238 | if (!devicesNode.isObject()) { |
| 239 | throw new IllegalArgumentException(JSON_INVALID); |
| 240 | } |
| 241 | |
| 242 | Iterator<Entry<String, JsonNode>> deviceEntries = devicesNode.fields(); |
| 243 | while (deviceEntries.hasNext()) { |
| 244 | Entry<String, JsonNode> deviceEntryNext = deviceEntries.next(); |
| 245 | String deviceId = deviceEntryNext.getKey(); |
| 246 | PowerConfig<Object> powerConfig = getPowerConfig(deviceId); |
| 247 | JsonNode portsNode = deviceEntryNext.getValue(); |
| 248 | if (!portsNode.isObject()) { |
| 249 | throw new IllegalArgumentException(JSON_INVALID); |
| 250 | } |
| 251 | |
| 252 | Iterator<Entry<String, JsonNode>> portEntries = portsNode.fields(); |
| 253 | while (portEntries.hasNext()) { |
| 254 | Entry<String, JsonNode> portEntryNext = portEntries.next(); |
| 255 | PortNumber portNumber = PortNumber.portNumber(portEntryNext.getKey()); |
| 256 | JsonNode componentsNode = portEntryNext.getValue(); |
| 257 | Iterator<Entry<String, JsonNode>> componentEntries = componentsNode.fields(); |
| 258 | while (componentEntries.hasNext()) { |
| 259 | Direction direction = null; |
| 260 | Entry<String, JsonNode> componentEntryNext = componentEntries.next(); |
| 261 | try { |
| 262 | direction = Direction.valueOf(componentEntryNext.getKey().toUpperCase()); |
| 263 | } catch (IllegalArgumentException e) { |
| 264 | // TODO: Handle other components |
| 265 | } |
| 266 | |
| 267 | JsonNode powerNode = componentEntryNext.getValue(); |
| 268 | if (!powerNode.isObject()) { |
| 269 | throw new IllegalArgumentException(JSON_INVALID); |
| 270 | } |
Andrea Campanella | dadf640 | 2019-08-07 15:24:11 +0200 | [diff] [blame^] | 271 | Double targetPower = powerNode.get(TARGET_POWER).asDouble(); |
Laszlo Papp | da059e7 | 2017-10-23 11:39:31 +0100 | [diff] [blame] | 272 | if (direction != null) { |
| 273 | powerConfig.setTargetPower(portNumber, direction, targetPower); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |