alshabib
Committed by Gerrit Code Review

OpenFlow Meter Provider - still need to parse meter stats

Change-Id: I4b313ea003b3801f81ec7d4823bf69de37fd8643
......@@ -32,7 +32,7 @@ public interface Band {
/**
* defines a simple DiffServ policer that remark
* the drop precedence of the DSCP eld in the
* the drop precedence of the DSCP field in the
* IP header of the packets that exceed the band
* rate value.
*/
......@@ -42,16 +42,32 @@ public interface Band {
/**
* The rate at which this meter applies.
*
* @return the integer value of the rate
* @return the long value of the rate
*/
int rate();
long rate();
/**
* The burst size at which the meter applies.
*
* @return the integer value of the size
* @return the long value of the size
*/
int burst();
long burst();
/**
* Only meaningful in the case of a REMARK band type.
* indicates by which amount the drop precedence of
* the packet should be increase if the band is exceeded.
*
* @return a short value
*/
short dropPrecedence();
/**
* Signals the type of band to create.
*
* @return a band type
*/
Type type();
}
......
......@@ -38,5 +38,52 @@ public enum MeterFailReason {
/**
* The meter that was attempted to be modified is unknown.
*/
UNKNOWN
UNKNOWN,
/**
* The operation for this meter installation timed out.
*/
TIMEOUT,
/**
* Invalid meter definition.
*/
INVALID_METER,
/**
* The target device is unknown.
*/
UNKNOWN_DEVICE,
/**
* Unknown command.
*/
UNKNOWN_COMMAND,
/**
* Unknown flags.
*/
UNKNOWN_FLAGS,
/**
* Bad rate value.
*/
BAD_RATE,
/**
* Bad burst size value.
*/
BAD_BURST,
/**
* Bad band.
*/
BAD_BAND,
/**
* Bad value value.
*/
BAD_BAND_VALUE
}
......
......@@ -41,7 +41,7 @@ public final class MeterId {
*
* @return an integer
*/
int id() {
public int id() {
return id;
}
......
......@@ -29,10 +29,10 @@ public interface MeterProviderService extends ProviderService<MeterProvider> {
/**
* Notifies the core that a meter operaton failed for a
* specific reason.
* @param deviceId
* @param operation
* @param operation the failed operation
* @param reason the failure reason
*/
void meterOperationFailed(DeviceId deviceId, MeterOperation operation,
void meterOperationFailed(MeterOperation operation,
MeterFailReason reason);
/**
......
......@@ -29,4 +29,5 @@
<artifact>mvn:${project.groupId}/onos-of-provider-packet/${project.version}</artifact>
<artifact>mvn:${project.groupId}/onos-of-provider-flow/${project.version}</artifact>
<artifact>mvn:${project.groupId}/onos-of-provider-group/${project.version}</artifact>
<artifact>mvn:${project.groupId}/onos-of-provider-meter/${project.version}</artifact>
</app>
......
......@@ -29,5 +29,6 @@
<bundle>mvn:${project.groupId}/onos-of-provider-packet/${project.version}</bundle>
<bundle>mvn:${project.groupId}/onos-of-provider-flow/${project.version}</bundle>
<bundle>mvn:${project.groupId}/onos-of-provider-group/${project.version}</bundle>
<bundle>mvn:${project.groupId}/onos-of-provider-meter/${project.version}</bundle>
</feature>
</features>
......
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.provider.of.meter.impl;
import org.onosproject.net.meter.Band;
import org.onosproject.net.meter.Meter;
import org.onosproject.net.meter.MeterId;
import org.projectfloodlight.openflow.protocol.OFFactory;
import org.projectfloodlight.openflow.protocol.OFMeterFlags;
import org.projectfloodlight.openflow.protocol.OFMeterMod;
import org.projectfloodlight.openflow.protocol.OFMeterModCommand;
import org.projectfloodlight.openflow.protocol.meterband.OFMeterBand;
import org.projectfloodlight.openflow.protocol.meterband.OFMeterBandDrop;
import org.projectfloodlight.openflow.protocol.meterband.OFMeterBandDscpRemark;
import org.slf4j.Logger;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Builder for a meter modification.
*/
public final class MeterModBuilder {
private final Logger log = getLogger(getClass());
private final long xid;
private final OFFactory factory;
private Meter.Unit unit = Meter.Unit.KB_PER_SEC;
private boolean burst = false;
private Integer id;
private Collection<Band> bands;
public MeterModBuilder(long xid, OFFactory factory) {
this.xid = xid;
this.factory = factory;
}
public static MeterModBuilder builder(long xid, OFFactory factory) {
return new MeterModBuilder(xid, factory);
}
public MeterModBuilder withRateUnit(Meter.Unit unit) {
this.unit = unit;
return this;
}
public MeterModBuilder burst() {
this.burst = true;
return this;
}
public MeterModBuilder withId(MeterId meterId) {
this.id = meterId.id();
return this;
}
public MeterModBuilder withBands(Collection<Band> bands) {
this.bands = bands;
return this;
}
public OFMeterMod add() {
validate();
OFMeterMod.Builder builder = builderMeterMod();
builder.setCommand(OFMeterModCommand.ADD.ordinal());
return builder.build();
}
public OFMeterMod remove() {
validate();
OFMeterMod.Builder builder = builderMeterMod();
builder.setCommand(OFMeterModCommand.DELETE.ordinal());
return builder.build();
}
public OFMeterMod modify() {
validate();
OFMeterMod.Builder builder = builderMeterMod();
builder.setCommand(OFMeterModCommand.MODIFY.ordinal());
return builder.build();
}
private OFMeterMod.Builder builderMeterMod() {
OFMeterMod.Builder builder = factory.buildMeterMod();
int flags = 0;
if (burst) {
// covering loxi short comings.
flags |= 1 << OFMeterFlags.BURST.ordinal();
}
switch (unit) {
case PKTS_PER_SEC:
flags |= 1 << OFMeterFlags.PKTPS.ordinal();
break;
case KB_PER_SEC:
flags |= 1 << OFMeterFlags.KBPS.ordinal();
break;
default:
log.warn("Unknown unit type {}", unit);
}
builder.setBands(buildBands());
builder.setFlags(flags)
.setMeterId(id)
.setXid(xid);
return builder;
}
private List<OFMeterBand> buildBands() {
return bands.stream().map(b -> {
switch (b.type()) {
case DROP:
OFMeterBandDrop.Builder dropBuilder =
factory.meterBands().buildDrop();
if (burst) {
dropBuilder.setBurstSize(b.burst());
}
dropBuilder.setRate(b.rate());
return dropBuilder.build();
case REMARK:
OFMeterBandDscpRemark.Builder remarkBand =
factory.meterBands().buildDscpRemark();
if (burst) {
remarkBand.setBurstSize(b.burst());
}
remarkBand.setRate(b.rate());
remarkBand.setPrecLevel(b.dropPrecedence());
return remarkBand.build();
default:
log.warn("Unknown band type {}", b.type());
return null;
}
}).filter(value -> value != null).collect(Collectors.toList());
}
private void validate() {
checkNotNull(id, "id cannot be null");
checkNotNull(bands, "Must have bands");
checkArgument(bands.size() > 0, "Must have at lease one band");
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* 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.provider.of.meter.impl;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timeout;
import org.jboss.netty.util.TimerTask;
import org.onlab.util.Timer;
import org.onosproject.openflow.controller.OpenFlowSwitch;
import org.onosproject.openflow.controller.RoleState;
import org.projectfloodlight.openflow.protocol.OFMeterStatsRequest;
import org.slf4j.Logger;
import java.util.concurrent.TimeUnit;
import static org.slf4j.LoggerFactory.getLogger;
/*
* Sends Meter Stats Request and collect the Meter statistics with a time interval.
*/
public class MeterStatsCollector implements TimerTask {
private final HashedWheelTimer timer = Timer.getTimer();
private final OpenFlowSwitch sw;
private final Logger log = getLogger(getClass());
private final int refreshInterval;
private Timeout timeout;
private boolean stopTimer = false;
/**
* Creates a GroupStatsCollector object.
*
* @param sw Open Flow switch
* @param interval time interval for collecting group statistic
*/
public MeterStatsCollector(OpenFlowSwitch sw, int interval) {
this.sw = sw;
this.refreshInterval = interval;
}
@Override
public void run(Timeout timeout) throws Exception {
log.trace("Collecting stats for {}", sw.getStringId());
sendMeterStatistic();
if (!this.stopTimer) {
log.trace("Scheduling stats collection in {} seconds for {}",
this.refreshInterval, this.sw.getStringId());
timeout.getTimer().newTimeout(this, refreshInterval,
TimeUnit.SECONDS);
}
}
private void sendMeterStatistic() {
if (log.isTraceEnabled()) {
log.trace("sendMeterStatistics {}:{}", sw.getStringId(), sw.getRole());
}
if (sw.getRole() != RoleState.MASTER) {
return;
}
OFMeterStatsRequest.Builder builder =
sw.factory().buildMeterStatsRequest();
builder.setXid(0).setMeterId(0xFFFFFFFF);
sw.sendMsg(builder.build());
}
/**
* Starts the collector.
*/
public void start() {
log.info("Starting Meter Stats collection thread for {}", sw.getStringId());
timeout = timer.newTimeout(this, 1, TimeUnit.SECONDS);
}
/**
* Stops the collector.
*/
public void stop() {
log.info("Stopping Meter Stats collection thread for {}", sw.getStringId());
this.stopTimer = true;
timeout.cancel();
}
}