Search in sources :

Example 16 with Meter

use of org.onosproject.net.meter.Meter in project onos by opennetworkinglab.

the class MetersWebResource method getMeters.

/**
 * Returns all meters of all devices.
 *
 * @return 200 OK with array of all the meters in the system
 * @onos.rsModel Meters
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getMeters() {
    MeterService meterService = get(MeterService.class);
    final Iterable<Meter> meters = meterService.getAllMeters();
    if (meters != null) {
        meters.forEach(meter -> metersNode.add(codec(Meter.class).encode(meter, this)));
    }
    return ok(root).build();
}
Also used : Meter(org.onosproject.net.meter.Meter) MeterService(org.onosproject.net.meter.MeterService) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 17 with Meter

use of org.onosproject.net.meter.Meter in project onos by opennetworkinglab.

the class MetersWebResource method createMeter.

/**
 * Creates new meter rule. Creates and installs a new meter rule for the
 * specified device.
 *
 * @param deviceId device identifier
 * @param stream   meter rule JSON
 * @return status of the request - CREATED if the JSON is correct,
 * BAD_REQUEST if the JSON is invalid
 * @onos.rsModel MeterPost
 */
@POST
@Path("{deviceId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createMeter(@PathParam("deviceId") String deviceId, InputStream stream) {
    try {
        ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
        JsonNode specifiedDeviceId = jsonTree.get("deviceId");
        if ((specifiedDeviceId != null && !specifiedDeviceId.asText().equals(deviceId)) || get(DeviceService.class).getDevice(DeviceId.deviceId(deviceId)) == null) {
            throw new IllegalArgumentException(DEVICE_INVALID);
        }
        jsonTree.put("deviceId", deviceId);
        final MeterRequest meterRequest = codec(MeterRequest.class).decode(jsonTree, this);
        MeterService meterService = get(MeterService.class);
        final Meter meter = meterService.submit(meterRequest);
        UriBuilder locationBuilder = uriInfo.getBaseUriBuilder().path("meters").path(deviceId).path(Long.toString(meter.id().id()));
        return Response.created(locationBuilder.build()).build();
    } catch (IOException ex) {
        throw new IllegalArgumentException(ex);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Meter(org.onosproject.net.meter.Meter) DeviceService(org.onosproject.net.device.DeviceService) JsonNode(com.fasterxml.jackson.databind.JsonNode) MeterRequest(org.onosproject.net.meter.MeterRequest) DefaultMeterRequest(org.onosproject.net.meter.DefaultMeterRequest) IOException(java.io.IOException) UriBuilder(javax.ws.rs.core.UriBuilder) MeterService(org.onosproject.net.meter.MeterService) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 18 with Meter

use of org.onosproject.net.meter.Meter in project onos by opennetworkinglab.

the class MetersWebResource method getMetersByDeviceIdAndScope.

/**
 * Returns a collection of meters by the device id and meter scope.
 *
 * @param deviceId device identifier
 * @param scope scope identifier
 * @return 200 OK with array of meters which belongs to specified device
 * @onos.rsModel Meters
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("scope/{deviceId}/{scope}")
public Response getMetersByDeviceIdAndScope(@PathParam("deviceId") String deviceId, @PathParam("scope") String scope) {
    DeviceId did = DeviceId.deviceId(deviceId);
    MeterScope meterScope = MeterScope.of(scope);
    MeterService meterService = get(MeterService.class);
    final Iterable<Meter> meters = meterService.getMeters(did, meterScope);
    if (meters != null) {
        meters.forEach(meter -> metersNode.add(codec(Meter.class).encode(meter, this)));
    }
    return ok(root).build();
}
Also used : Meter(org.onosproject.net.meter.Meter) DeviceId(org.onosproject.net.DeviceId) MeterScope(org.onosproject.net.meter.MeterScope) MeterService(org.onosproject.net.meter.MeterService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 19 with Meter

use of org.onosproject.net.meter.Meter in project onos by opennetworkinglab.

the class MetersWebResource method getMetersByDeviceId.

/**
 * Returns a collection of meters by the device id.
 *
 * @param deviceId device identifier
 * @return 200 OK with array of meters which belongs to specified device
 * @onos.rsModel Meters
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{deviceId}")
public Response getMetersByDeviceId(@PathParam("deviceId") String deviceId) {
    DeviceId did = DeviceId.deviceId(deviceId);
    MeterService meterService = get(MeterService.class);
    final Iterable<Meter> meters = meterService.getMeters(did);
    if (meters != null) {
        meters.forEach(meter -> metersNode.add(codec(Meter.class).encode(meter, this)));
    }
    return ok(root).build();
}
Also used : Meter(org.onosproject.net.meter.Meter) DeviceId(org.onosproject.net.DeviceId) MeterService(org.onosproject.net.meter.MeterService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 20 with Meter

use of org.onosproject.net.meter.Meter in project onos by opennetworkinglab.

the class OpenFlowMeterProvider method performOperation.

private void performOperation(OpenFlowSwitch sw, MeterOperation op) {
    Meter meter = op.meter();
    MeterModBuilder builder = MeterModBuilder.builder(meter.id().id(), sw.factory());
    if (meter.isBurst()) {
        builder.burst();
    }
    builder.withBands(meter.bands()).withId(meter.id()).withRateUnit(meter.unit());
    switch(op.type()) {
        case ADD:
            pendingOperations.put(op.meter().id().id(), op);
            sw.sendMsg(builder.add());
            break;
        case REMOVE:
            sw.sendMsg(builder.remove());
            break;
        case MODIFY:
            pendingOperations.put(op.meter().id().id(), op);
            sw.sendMsg(builder.modify());
            break;
        default:
            log.warn("Unknown Meter command {}; not sending anything", op.type());
            providerService.meterOperationFailed(op, MeterFailReason.UNKNOWN_COMMAND);
    }
}
Also used : DefaultMeter(org.onosproject.net.meter.DefaultMeter) Meter(org.onosproject.net.meter.Meter)

Aggregations

Meter (org.onosproject.net.meter.Meter)45 DefaultMeter (org.onosproject.net.meter.DefaultMeter)32 Test (org.junit.Test)21 DeviceId (org.onosproject.net.DeviceId)12 MeterService (org.onosproject.net.meter.MeterService)8 PiMeterCellId (org.onosproject.net.pi.runtime.PiMeterCellId)8 MeterCellId (org.onosproject.net.meter.MeterCellId)7 MeterKey (org.onosproject.net.meter.MeterKey)7 Produces (javax.ws.rs.Produces)6 GET (javax.ws.rs.GET)5 Path (javax.ws.rs.Path)5 Band (org.onosproject.net.meter.Band)5 DefaultBand (org.onosproject.net.meter.DefaultBand)4 DefaultMeterRequest (org.onosproject.net.meter.DefaultMeterRequest)4 MeterOperation (org.onosproject.net.meter.MeterOperation)4 MeterRequest (org.onosproject.net.meter.MeterRequest)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Maps (com.google.common.collect.Maps)3 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3