Add a power management application
Change-Id: I7f3271b6f45d0e0b990049db1333843a0cd5f06a
diff --git a/apps/powermanagement/BUCK b/apps/powermanagement/BUCK
new file mode 100644
index 0000000..41d7956
--- /dev/null
+++ b/apps/powermanagement/BUCK
@@ -0,0 +1,30 @@
+COMPILE_DEPS = [
+ '//lib:CORE_DEPS',
+ '//lib:JACKSON',
+ '//core/store/serializers:onos-core-serializers',
+ '//utils/rest:onlab-rest',
+ '//lib:javax.ws.rs-api',
+]
+
+TEST_DEPS = [
+ '//lib:TEST_REST',
+ '//core/api:onos-api-tests',
+]
+
+osgi_jar_with_tests (
+ deps = COMPILE_DEPS,
+ test_deps = TEST_DEPS,
+ web_context = '/onos/powermanagement',
+ api_title = 'Power Management API',
+ api_version = '1.0',
+ api_description = 'REST API for Power Management',
+ api_package = 'org.onosproject.powermanagement',
+)
+
+onos_app (
+ title = 'Power Management',
+ category = 'Monitoring',
+ url = 'http://onosproject.org',
+ description = 'This application provides northbound interfaces for monitoring and ' +
+ 'configuring power.',
+)
diff --git a/apps/powermanagement/src/main/java/org/onosproject/powermanagement/PowerConfigWebApplication.java b/apps/powermanagement/src/main/java/org/onosproject/powermanagement/PowerConfigWebApplication.java
new file mode 100644
index 0000000..878186d
--- /dev/null
+++ b/apps/powermanagement/src/main/java/org/onosproject/powermanagement/PowerConfigWebApplication.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.powermanagement;
+
+import org.onlab.rest.AbstractWebApplication;
+
+import java.util.Set;
+
+/**
+ * Power Management Web application.
+ */
+public class PowerConfigWebApplication extends AbstractWebApplication {
+ @Override
+ public Set<Class<?>> getClasses() {
+ return getClasses(PowerConfigWebResource.class);
+ }
+}
diff --git a/apps/powermanagement/src/main/java/org/onosproject/powermanagement/PowerConfigWebResource.java b/apps/powermanagement/src/main/java/org/onosproject/powermanagement/PowerConfigWebResource.java
new file mode 100644
index 0000000..afcdfbd
--- /dev/null
+++ b/apps/powermanagement/src/main/java/org/onosproject/powermanagement/PowerConfigWebResource.java
@@ -0,0 +1,277 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.powermanagement;
+
+import org.onosproject.net.Device;
+import org.onosproject.net.Direction;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.behaviour.PowerConfig;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.rest.AbstractWebResource;
+
+import static org.onosproject.net.DeviceId.deviceId;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Range;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.slf4j.Logger;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Manage inventory of infrastructure devices with Power Config behaviour.
+ */
+@Path("devices")
+public class PowerConfigWebResource extends AbstractWebResource {
+
+ private static final String JSON_INVALID = "Invalid json input";
+ private static final String DEVICE_NOT_FOUND = "Device is not found";
+ private static final String POWERCONFIG_UNSUPPORTED = "Power Config is not supported";
+ private static final String DIRECTION_UNSUPPORTED = "Direction is not supported";
+
+ private static final String DEVICES = "powerConfigDevices";
+ private static final String PORTS = "ports";
+ private static final String DEVICE_ID = "deviceId";
+ private static final String DEVICE_IDS = "powerConfigDeviceIds";
+ private static final String POWERCONFIG_SUPPORTED = "powerConfigSupported";
+ private static final String DIRECTION = "direction";
+ private static final String PORT_ID = "portId";
+ private static final String TARGET_POWER = "targetPower";
+ private static final String CURRENT_POWER = "currentPower";
+ private static final String INPUT_POWER_RANGE = "inputPowerRange";
+ private static final String TARGET_POWER_RANGE = "targetPowerRange";
+
+ private final ObjectMapper mapper = new ObjectMapper();
+
+ private static final Logger log = getLogger(PowerConfigWebResource.class);
+
+ /**
+ * Gets all power config devices.
+ * Returns array of all discovered power config devices.
+ *
+ * @return 200 OK with a collection of devices
+ * @onos.rsModel PowerConfigDevicesGet
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getDevices() {
+ ObjectNode root = mapper().createObjectNode();
+ ArrayNode deviceIdsNode = root.putArray(DEVICE_IDS);
+
+ Iterable<Device> devices = get(DeviceService.class).getDevices();
+ if (devices != null) {
+ for (Device d : devices) {
+ if (getPowerConfig(d.id().toString()) != null) {
+ deviceIdsNode.add(d.id().toString());
+ }
+ }
+ }
+
+ return ok(root).build();
+ }
+
+ /**
+ * Applies the target power for the specified device.
+ *
+ * @param stream JSON representation of device, port, component and target
+ * power info
+ * @return status of the request - CREATED if the JSON is correct,
+ * BAD_REQUEST if the JSON is invalid
+ * @onos.rsModel PowerConfigPut
+ */
+ @PUT
+ @Consumes(MediaType.APPLICATION_JSON)
+ public Response setTargetPower(InputStream stream) {
+ try {
+ ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
+ decode(jsonTree);
+ return Response.ok().build();
+ } catch (IOException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ /**
+ * Gets the details of a power config device.
+ * Returns the details of the specified power config device.
+ *
+ * @param id device identifier
+ * @return 200 OK with a device
+ * @onos.rsModel PowerConfigDeviceGet
+ */
+ @GET
+ @Path("{id}")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getDevice(@PathParam("id") String id) {
+ ObjectNode result = mapper.createObjectNode();
+ result.put(POWERCONFIG_SUPPORTED, (getPowerConfig(id) != null) ? true : false);
+ return ok(result).build();
+ }
+
+ private PowerConfig<Object> getPowerConfig(String id) {
+ Device device = get(DeviceService.class).getDevice(deviceId(id));
+ if (device == null) {
+ throw new IllegalArgumentException(DEVICE_NOT_FOUND);
+ }
+ if (device.is(PowerConfig.class)) {
+ return device.as(PowerConfig.class);
+ }
+ return null;
+ }
+
+ /**
+ * Gets the ports of a power config device.
+ * Returns the details of the specified power config device ports.
+ *
+ * @onos.rsModel PowerConfigDeviceGetPorts
+ * @param id device identifier
+ * @param direction port direction
+ * @param channel port channel
+ * @return 200 OK with a collection of ports of the given device
+ */
+ @GET
+ @Path("{id}/ports")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getDevicePorts(@PathParam("id") String id,
+ @QueryParam("direction") String direction,
+ @QueryParam("channel") String channel) {
+ PowerConfig<Object> powerConfig = getPowerConfig(id);
+ if (powerConfig == null) {
+ throw new IllegalArgumentException(POWERCONFIG_UNSUPPORTED);
+ }
+ if (direction == null && channel == null) {
+ direction = "ALL";
+ // TODO: Fallback to all channels?
+ }
+ ObjectNode result = encode(powerConfig, direction, channel);
+ return ok(result).build();
+ }
+
+ private ObjectNode encode(PowerConfig<Object> powerConfig, String direction, String channel) {
+ checkNotNull(powerConfig, "PowerConfig cannot be null");
+ ObjectNode powerConfigPorts = mapper.createObjectNode();
+ Multimap<PortNumber, Object> portsMap = HashMultimap.create();
+
+ if (direction != null) {
+ for (PortNumber port : powerConfig.getPorts(direction)) {
+ portsMap.put(port, Direction.valueOf(direction.toUpperCase()));
+ }
+ }
+
+ if (channel != null) {
+ for (PortNumber port : powerConfig.getPorts(channel)) {
+ // TODO: channel to be handled
+ portsMap.put(port, channel);
+ }
+ }
+
+ for (Map.Entry<PortNumber, Object> entry : portsMap.entries()) {
+ PortNumber port = entry.getKey();
+ ObjectNode powerConfigComponents = mapper.createObjectNode();
+ for (Object component : portsMap.get(port)) {
+ // TODO: channel to be handled
+ String componentName = "unknown";
+ if (component instanceof Direction) {
+ componentName = component.toString();
+ }
+ ObjectNode powerConfigNode = mapper.createObjectNode()
+ .put(CURRENT_POWER, powerConfig.currentPower(port, component).orElse(0L))
+ .put(TARGET_POWER, powerConfig.getTargetPower(port, component).orElse(0L))
+ .put(INPUT_POWER_RANGE, powerConfig.getInputPowerRange(port,
+ component).orElse(Range.closed(0L, 0L)).toString())
+ .put(TARGET_POWER_RANGE, powerConfig.getTargetPowerRange(port,
+ component).orElse(Range.closed(0L, 0L)).toString());
+ powerConfigComponents.set(componentName, powerConfigNode);
+ }
+ powerConfigPorts.set(port.toString(), powerConfigComponents);
+ }
+
+ ObjectNode result = mapper.createObjectNode();
+ result.set("powerConfigPorts", powerConfigPorts);
+ return result;
+ }
+
+ public void decode(ObjectNode json) {
+ if (json == null || !json.isObject()) {
+ throw new IllegalArgumentException(JSON_INVALID);
+ }
+
+ JsonNode devicesNode = json.get(DEVICES);
+ if (!devicesNode.isObject()) {
+ throw new IllegalArgumentException(JSON_INVALID);
+ }
+
+ Iterator<Entry<String, JsonNode>> deviceEntries = devicesNode.fields();
+ while (deviceEntries.hasNext()) {
+ Entry<String, JsonNode> deviceEntryNext = deviceEntries.next();
+ String deviceId = deviceEntryNext.getKey();
+ PowerConfig<Object> powerConfig = getPowerConfig(deviceId);
+ JsonNode portsNode = deviceEntryNext.getValue();
+ if (!portsNode.isObject()) {
+ throw new IllegalArgumentException(JSON_INVALID);
+ }
+
+ Iterator<Entry<String, JsonNode>> portEntries = portsNode.fields();
+ while (portEntries.hasNext()) {
+ Entry<String, JsonNode> portEntryNext = portEntries.next();
+ PortNumber portNumber = PortNumber.portNumber(portEntryNext.getKey());
+ JsonNode componentsNode = portEntryNext.getValue();
+ Iterator<Entry<String, JsonNode>> componentEntries = componentsNode.fields();
+ while (componentEntries.hasNext()) {
+ Direction direction = null;
+ Entry<String, JsonNode> componentEntryNext = componentEntries.next();
+ try {
+ direction = Direction.valueOf(componentEntryNext.getKey().toUpperCase());
+ } catch (IllegalArgumentException e) {
+ // TODO: Handle other components
+ }
+
+ JsonNode powerNode = componentEntryNext.getValue();
+ if (!powerNode.isObject()) {
+ throw new IllegalArgumentException(JSON_INVALID);
+ }
+ Long targetPower = powerNode.get(TARGET_POWER).asLong();
+ if (direction != null) {
+ powerConfig.setTargetPower(portNumber, direction, targetPower);
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/apps/powermanagement/src/main/java/org/onosproject/powermanagement/package-info.java b/apps/powermanagement/src/main/java/org/onosproject/powermanagement/package-info.java
new file mode 100644
index 0000000..4a69745
--- /dev/null
+++ b/apps/powermanagement/src/main/java/org/onosproject/powermanagement/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Application to monitor and configure power.
+ */
+package org.onosproject.powermanagement;
diff --git a/apps/powermanagement/src/main/resources/definitions/PowerConfigDeviceGet.json b/apps/powermanagement/src/main/resources/definitions/PowerConfigDeviceGet.json
new file mode 100644
index 0000000..e3d3068
--- /dev/null
+++ b/apps/powermanagement/src/main/resources/definitions/PowerConfigDeviceGet.json
@@ -0,0 +1,13 @@
+{
+ "type": "object",
+ "title": "powerConfigSupported",
+ "required": [
+ "powerConfigSupported"
+ ],
+ "properties": {
+ "powerConfigSupported": {
+ "type": "boolean",
+ "example": true
+ }
+ }
+}
diff --git a/apps/powermanagement/src/main/resources/definitions/PowerConfigDeviceGetPorts.json b/apps/powermanagement/src/main/resources/definitions/PowerConfigDeviceGetPorts.json
new file mode 100644
index 0000000..97dd7bb
--- /dev/null
+++ b/apps/powermanagement/src/main/resources/definitions/PowerConfigDeviceGetPorts.json
@@ -0,0 +1,38 @@
+{
+ "type": "object",
+ "title": "powerConfigPorts",
+ "additionalProperties": {
+ "type": "object",
+ "title": "powerConfigPort",
+ "additionalProperties": {
+ "type": "object",
+ "title": "powerConfigComponent",
+ "required": [
+ "currentPower",
+ "targetPower",
+ "inputPowerRange",
+ "targetPowerRange"
+ ],
+ "properties": {
+ "currentPower": {
+ "type": "integer",
+ "format": "int64",
+ "example": -755
+ },
+ "targetPower": {
+ "type": "integer",
+ "format": "int64",
+ "example": -800
+ },
+ "inputPowerRange": {
+ "type": "string",
+ "example": "[-6000..2800]"
+ },
+ "targetPowerRange": {
+ "type": "string",
+ "example": "[-6000..2800]"
+ }
+ }
+ }
+ }
+}
diff --git a/apps/powermanagement/src/main/resources/definitions/PowerConfigDevicesGet.json b/apps/powermanagement/src/main/resources/definitions/PowerConfigDevicesGet.json
new file mode 100644
index 0000000..f070cb6
--- /dev/null
+++ b/apps/powermanagement/src/main/resources/definitions/PowerConfigDevicesGet.json
@@ -0,0 +1,20 @@
+{
+ "type": "object",
+ "title": "powerConfigDeviceIds",
+ "required": [
+ "powerConfigDeviceIds"
+ ],
+ "properties": {
+ "powerConfigDeviceIds": {
+ "type": "array",
+ "xml": {
+ "name": "powerConfigDeviceId",
+ "wrapped": true
+ },
+ "items": {
+ "type": "string",
+ "example": "of:0000000000000001"
+ }
+ }
+ }
+}
diff --git a/apps/powermanagement/src/main/resources/definitions/PowerConfigPut.json b/apps/powermanagement/src/main/resources/definitions/PowerConfigPut.json
new file mode 100644
index 0000000..c5a3070
--- /dev/null
+++ b/apps/powermanagement/src/main/resources/definitions/PowerConfigPut.json
@@ -0,0 +1,31 @@
+{
+ "type": "object",
+ "title": "powerConfigPut",
+ "required": [
+ "powerConfigDevices"
+ ],
+ "properties": {
+ "powerConfigDevices": {
+ "type": "object",
+ "title": "powerConfigDevicePut",
+ "additionalProperties": {
+ "type": "object",
+ "title": "powerConfigPortPut",
+ "additionalProperties": {
+ "type": "object",
+ "title": "powerConfigComponentPut",
+ "required": [
+ "targetPower"
+ ],
+ "properties": {
+ "targetPower": {
+ "type": "integer",
+ "format": "int64",
+ "example": -1000
+ }
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/apps/powermanagement/src/main/webapp/WEB-INF/web.xml b/apps/powermanagement/src/main/webapp/WEB-INF/web.xml
new file mode 100644
index 0000000..f3440ee
--- /dev/null
+++ b/apps/powermanagement/src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Copyright 2017-present Open Networking Foundation
+ ~
+ ~ Licensed under the Apache License, Version 2.0 (the "License");
+ ~ you may not use this file except in compliance with the License.
+ ~ You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ id="ONOS" version="2.5">
+ <display-name>Power Management REST API</display-name>
+
+ <security-constraint>
+ <web-resource-collection>
+ <web-resource-name>Secured</web-resource-name>
+ <url-pattern>/*</url-pattern>
+ </web-resource-collection>
+ <auth-constraint>
+ <role-name>admin</role-name>
+ </auth-constraint>
+ </security-constraint>
+
+ <security-role>
+ <role-name>admin</role-name>
+ </security-role>
+
+ <login-config>
+ <auth-method>BASIC</auth-method>
+ <realm-name>karaf</realm-name>
+ </login-config>
+
+ <servlet>
+ <servlet-name>JAX-RS Service</servlet-name>
+ <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
+ <init-param>
+ <param-name>javax.ws.rs.Application</param-name>
+ <param-value>org.onosproject.powermanagement.PowerConfigWebApplication</param-value>
+ </init-param>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+
+ <servlet-mapping>
+ <servlet-name>JAX-RS Service</servlet-name>
+ <url-pattern>/*</url-pattern>
+ </servlet-mapping>
+
+</web-app>
diff --git a/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisPowerConfig.java b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisPowerConfig.java
index 381d3a4..61a3727 100644
--- a/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisPowerConfig.java
+++ b/drivers/polatis/netconf/src/main/java/org/onosproject/drivers/polatis/netconf/PolatisPowerConfig.java
@@ -208,9 +208,9 @@
.append(xmlOpen(KEY_PORT))
.append(xml(KEY_PORTID, Long.toString(port.toLong())))
.append(xml(KEY_ATTEN_MODE, VALUE_ATTEN_MODE))
- .append(xml(KEY_ATTEN_LEVEL, Long.toString(power)))
+ .append(xml(KEY_ATTEN_LEVEL, Double.toString((double) power / VOA_MULTIPLIER)))
.append(xmlClose(KEY_PORT))
- .append(xmlClose(KEY_VOA_XMLNS))
+ .append(xmlClose(KEY_VOA))
.toString();
return netconfEditConfig(handler(), CFG_MODE_MERGE, cfg);
}
diff --git a/modules.defs b/modules.defs
index ffd756a..5ea50a2 100644
--- a/modules.defs
+++ b/modules.defs
@@ -225,6 +225,7 @@
'//apps/p4-tutorial/icmpdropper:onos-apps-p4-tutorial-icmpdropper-oar',
'//apps/cfm:onos-apps-cfm-oar',
'//apps/routeradvertisement:onos-apps-routeradvertisement-oar',
+ '//apps/powermanagement:onos-apps-powermanagement-oar',
]
PROTOCOL_APPS = [