moving meter service to incubator and initial implementation of
meter manager.
Change-Id: I6ef0d9476b58d00b37f7ef156ac7bdacca20185b
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/Band.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/Band.java
new file mode 100644
index 0000000..ecd6504
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/Band.java
@@ -0,0 +1,119 @@
+/*
+ * 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.incubator.net.meter;
+
+/**
+ * Represents a band used within a meter.
+ */
+public interface Band {
+
+ /**
+ * Specifies the type of band.
+ */
+ enum Type {
+ /**
+ * Simple rate limiter which drops packets
+ * when the rate is exceeded.
+ */
+ DROP,
+
+ /**
+ * defines a simple DiffServ policer that remark
+ * the drop precedence of the DSCP field in the
+ * IP header of the packets that exceed the band
+ * rate value.
+ */
+ REMARK
+ }
+
+ /**
+ * The rate at which this meter applies.
+ *
+ * @return the long value of the rate
+ */
+ long rate();
+
+ /**
+ * The burst size at which the meter applies.
+ *
+ * @return the long value of the size
+ */
+ 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();
+
+ interface Builder {
+
+ /**
+ * Assigns a rate to this band. The units for this rate
+ * are defined in the encapsulating meter.
+ *
+ * @param rate a long value
+ * @return this
+ */
+ Builder withRate(long rate);
+
+ /**
+ * Assigns a burst size to this band. Only meaningful if
+ * the encapsulating meter is of burst type.
+ *
+ * @param burstSize a long value.
+ * @return this
+ */
+ Builder burstSize(long burstSize);
+
+ /**
+ * Assigns the drop precedence for this band. Only meaningful if
+ * the band is of REMARK type.
+ *
+ * @param prec a short value
+ * @return this
+ */
+ Builder dropPrecedence(short prec);
+
+ /**
+ * Assigns the @See Type of this band.
+ *
+ * @param type a band type
+ * @return this
+ */
+ Builder ofType(Type type);
+
+ /**
+ * Builds the band.
+ *
+ * @return a band
+ */
+ Band build();
+
+ }
+
+
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/DefaultBand.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/DefaultBand.java
new file mode 100644
index 0000000..bff757b
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/DefaultBand.java
@@ -0,0 +1,103 @@
+/*
+ * 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.incubator.net.meter;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * A default implementation for a Band.
+ */
+public final class DefaultBand implements Band {
+
+ private final Type type;
+ private final long rate;
+ private final long burstSize;
+ private final short prec;
+
+ public DefaultBand(Type type, long rate,
+ long burstSize, short prec) {
+ this.type = type;
+ this.rate = rate;
+ this.burstSize = burstSize;
+ this.prec = prec;
+ }
+
+ @Override
+ public long rate() {
+ return rate;
+ }
+
+ @Override
+ public long burst() {
+ return burstSize;
+ }
+
+ @Override
+ public short dropPrecedence() {
+ return prec;
+ }
+
+ @Override
+ public Type type() {
+ return type;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static final class Builder implements Band.Builder {
+
+ private long rate;
+ private long burstSize;
+ private Short prec;
+ private Type type;
+
+ @Override
+ public Band.Builder withRate(long rate) {
+ this.rate = rate;
+ return this;
+ }
+
+ @Override
+ public Band.Builder burstSize(long burstSize) {
+ this.burstSize = burstSize;
+ return this;
+ }
+
+ @Override
+ public Band.Builder dropPrecedence(short prec) {
+ this.prec = prec;
+ return this;
+ }
+
+ @Override
+ public Band.Builder ofType(Type type) {
+ this.type = type;
+ return this;
+ }
+
+ @Override
+ public Band build() {
+ checkArgument(prec != null && type == Type.REMARK,
+ "Only REMARK bands can have a precendence.");
+
+ return new DefaultBand(type, rate, burstSize, prec);
+ }
+
+
+ }
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/DefaultMeter.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/DefaultMeter.java
new file mode 100644
index 0000000..953feb2
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/DefaultMeter.java
@@ -0,0 +1,158 @@
+/*
+ * 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.incubator.net.meter;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.DeviceId;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Optional;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * A default implementation of a meter.
+ */
+public final class DefaultMeter implements Meter {
+
+
+ private final MeterId id;
+ private final ApplicationId appId;
+ private final Unit unit;
+ private final boolean burst;
+ private final Collection<Band> bands;
+ private final DeviceId deviceId;
+ private final Optional<MeterContext> context;
+
+ private DefaultMeter(DeviceId deviceId, MeterId id, ApplicationId appId,
+ Unit unit, boolean burst,
+ Collection<Band> bands, Optional<MeterContext> context) {
+ this.deviceId = deviceId;
+ this.id = id;
+ this.appId = appId;
+ this.unit = unit;
+ this.burst = burst;
+ this.bands = bands;
+ this.context = context;
+ }
+
+ @Override
+ public DeviceId deviceId() {
+ return deviceId;
+ }
+
+ @Override
+ public MeterId id() {
+ return id;
+ }
+
+ @Override
+ public ApplicationId appId() {
+ return appId;
+ }
+
+ @Override
+ public Unit unit() {
+ return unit;
+ }
+
+ @Override
+ public boolean isBurst() {
+ return burst;
+ }
+
+ @Override
+ public Collection<Band> bands() {
+ return bands;
+ }
+
+ @Override
+ public Optional<MeterContext> context() {
+ return null;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static final class Builder implements Meter.Builder {
+
+ private MeterId id;
+ private ApplicationId appId;
+ private Unit unit = Unit.KB_PER_SEC;
+ private boolean burst = false;
+ private Collection<Band> bands;
+ private DeviceId deviceId;
+ private Optional<MeterContext> context;
+
+
+ @Override
+ public Meter.Builder forDevice(DeviceId deviceId) {
+ this.deviceId = deviceId;
+ return this;
+ }
+
+ @Override
+ public Meter.Builder withId(int id) {
+ this.id = MeterId.meterId(id);
+ return this;
+ }
+
+ @Override
+ public Meter.Builder fromApp(ApplicationId appId) {
+ this.appId = appId;
+ return this;
+ }
+
+ @Override
+ public Meter.Builder withUnit(Unit unit) {
+ this.unit = unit;
+ return this;
+ }
+
+ @Override
+ public Meter.Builder burst() {
+ this.burst = true;
+ return this;
+ }
+
+ @Override
+ public Meter.Builder withBands(Collection<Band> bands) {
+ this.bands = Collections.unmodifiableCollection(bands);
+ return this;
+ }
+
+ @Override
+ public Meter.Builder withContext(MeterContext context) {
+ this.context = Optional.<MeterContext>ofNullable(context);
+ return this;
+ }
+
+ @Override
+ public Meter build() {
+ checkNotNull(deviceId, "Must specify a device");
+ checkNotNull(bands, "Must have bands.");
+ checkArgument(bands.size() > 0, "Must have at least one band.");
+ checkNotNull(appId, "Must have an application id");
+ checkNotNull(id, "Must specify a meter id");
+ return new DefaultMeter(deviceId, id, appId, unit, burst, bands, context);
+ }
+
+
+ }
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/Meter.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/Meter.java
new file mode 100644
index 0000000..5c59d51
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/Meter.java
@@ -0,0 +1,155 @@
+/*
+ * 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.incubator.net.meter;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.DeviceId;
+
+import java.util.Collection;
+import java.util.Optional;
+
+/**
+ * Represents a generalized meter to be deployed on a device.
+ */
+public interface Meter {
+
+ enum Unit {
+ /**
+ * Packets per second.
+ */
+ PKTS_PER_SEC,
+
+ /**
+ * Kilo bits per second.
+ */
+ KB_PER_SEC
+ }
+
+ /**
+ * The target device for this meter.
+ *
+ * @return a device id
+ */
+ DeviceId deviceId();
+
+ /**
+ * This meters id.
+ *
+ * @return a meter id
+ */
+ MeterId id();
+
+ /**
+ * The id of the application which created this meter.
+ *
+ * @return an application id
+ */
+ ApplicationId appId();
+
+ /**
+ * The unit used within this meter.
+ *
+ * @return
+ */
+ Unit unit();
+
+ /**
+ * Signals whether this meter applies to bursts only.
+ *
+ * @return a boolean
+ */
+ boolean isBurst();
+
+ /**
+ * The collection of bands to apply on the dataplane.
+ *
+ * @return a collection of bands.
+ */
+ Collection<Band> bands();
+
+ /**
+ * Obtains an optional context.
+ *
+ * @return optional; which will be empty if there is no context.
+ * Otherwise it will return the context.
+ */
+ Optional<MeterContext> context();
+
+ /**
+ * A meter builder.
+ */
+ interface Builder {
+
+ /**
+ * Assigns the target device for this meter.
+ *
+ * @param deviceId a device id
+ * @return this
+ */
+ Builder forDevice(DeviceId deviceId);
+
+ /**
+ * Assigns the id to this meter.
+ *
+ * @param id an integer
+ * @return this
+ */
+ Builder withId(int id);
+
+ /**
+ * Assigns the application that built this meter.
+ *
+ * @param appId an application id
+ * @return this
+ */
+ Builder fromApp(ApplicationId appId);
+
+ /**
+ * Assigns the @See Unit to use for this meter.
+ * Defaults to kb/s
+ *
+ * @param unit a unit
+ * @return this
+ */
+ Builder withUnit(Unit unit);
+
+ /**
+ * Sets this meter as applicable to burst traffic only.
+ * Defaults to false.
+ *
+ * @return this
+ */
+ Builder burst();
+
+ /**
+ * Assigns bands to this meter. There must be at least one band.
+ *
+ * @param bands a collection of bands
+ * @return this
+ */
+ Builder withBands(Collection<Band> bands);
+
+ Builder withContext(MeterContext context);
+
+ /**
+ * Builds the meter based on the specified parameters.
+ *
+ * @return a meter
+ */
+ Meter build();
+ }
+
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterContext.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterContext.java
new file mode 100644
index 0000000..4c814f8
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterContext.java
@@ -0,0 +1,39 @@
+/*
+ * 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.incubator.net.meter;
+
+/**
+ * Created by ash on 01/08/15.
+ */
+public interface MeterContext {
+
+ /**
+ * Invoked on successful installation of the meter.
+ *
+ * @param op a meter operation
+ */
+ default void onSuccess(MeterOperation op) {
+ }
+
+ /**
+ * Invoked when error is encountered while installing a meter.
+ *
+ * @param op a meter operation
+ * @param reason the reason why it failed
+ */
+ default void onError(MeterOperation op, MeterFailReason reason) {
+ }
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterEvent.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterEvent.java
new file mode 100644
index 0000000..a952a14
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterEvent.java
@@ -0,0 +1,82 @@
+/*
+ * 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.incubator.net.meter;
+
+import org.onosproject.event.AbstractEvent;
+
+/**
+ * Entity that represents Meter events.
+ */
+public class MeterEvent extends AbstractEvent<MeterEvent.Type, Meter> {
+
+
+ enum Type {
+
+ /**
+ * Signals that a new meter has been added.
+ */
+ METER_ADDED,
+
+ /**
+ * Signals that a meter has been removed.
+ */
+ METER_REMOVED,
+
+ /**
+ * Signals that a meter has been added.
+ */
+ METER_UPDATED,
+
+ /**
+ * Signals that a meter addition failed.
+ */
+ METER_ADD_FAILED,
+
+ /**
+ * Signals that a meter removal failed.
+ */
+ METER_REMOVE_FAILED,
+
+ /**
+ * Signals that a meter update failed.
+ */
+ METER_UPDATE_FAILED
+ }
+
+
+ /**
+ * Creates an event of a given type and for the specified meter and the
+ * current time.
+ *
+ * @param type meter event type
+ * @param meter event subject
+ */
+ public MeterEvent(Type type, Meter meter) {
+ super(type, meter);
+ }
+
+ /**
+ * Creates an event of a given type and for the specified meter and time.
+ *
+ * @param type meter event type
+ * @param meter event subject
+ * @param time occurrence time
+ */
+ public MeterEvent(Type type, Meter meter, long time) {
+ super(type, meter, time);
+ }
+
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterFailReason.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterFailReason.java
new file mode 100644
index 0000000..d98e44a
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterFailReason.java
@@ -0,0 +1,89 @@
+/*
+ * 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.incubator.net.meter;
+
+/**
+ * Enum used to represent a meter failure condition.
+ */
+public enum MeterFailReason {
+ /**
+ * A meter with the same identifier already exists.
+ * Essentially a duplicate meter exists.
+ */
+ EXISTING_METER,
+
+ /**
+ * The device does not support any more meters.
+ */
+ OUT_OF_METERS,
+
+ /**
+ * The device does not support any more bands for this meter.
+ */
+ OUT_OF_BANDS,
+
+ /**
+ * The meter that was attempted to be modified is 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
+
+
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterId.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterId.java
new file mode 100644
index 0000000..ea7cf36
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterId.java
@@ -0,0 +1,73 @@
+/*
+ * 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.incubator.net.meter;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * A representation of a meter id.
+ * Uniquely identifies a meter for a given device.
+ */
+public final class MeterId {
+
+ static final long MAX = 0xFFFF0000;
+
+ private final int id;
+
+ public static final MeterId SLOWPATH = new MeterId(0xFFFFFFFD);
+ public static final MeterId CONTROLLER = new MeterId(0xFFFFFFFE);
+ public static final MeterId ALL = new MeterId(0xFFFFFFFF);
+
+ private MeterId(int id) {
+ checkArgument(id <= MAX, "id cannot be larger than 0xFFFF0000");
+ this.id = id;
+ }
+
+ /**
+ * The integer representation of the meter id.
+ *
+ * @return an integer
+ */
+ public int id() {
+ return id;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ MeterId meterId = (MeterId) o;
+
+ return id == meterId.id;
+
+ }
+
+ @Override
+ public int hashCode() {
+ return id;
+ }
+
+ public static MeterId meterId(int id) {
+ return new MeterId(id);
+
+ }
+
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterListener.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterListener.java
new file mode 100644
index 0000000..1cb667a
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterListener.java
@@ -0,0 +1,24 @@
+/*
+ * 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.incubator.net.meter;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Entity capable of receiving Meter related events.
+ */
+public interface MeterListener extends EventListener<MeterEvent> {
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterOperation.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterOperation.java
new file mode 100644
index 0000000..ff7e988
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterOperation.java
@@ -0,0 +1,68 @@
+/*
+ * 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.incubator.net.meter;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * Representation of an operation on the meter table.
+ */
+public class MeterOperation {
+
+ /**
+ * Tyoe of meter operation.
+ */
+ public enum Type {
+ ADD,
+ REMOVE,
+ MODIFY
+ }
+
+ private final Meter meter;
+ private final Type type;
+
+
+ public MeterOperation(Meter meter, Type type) {
+ this.meter = meter;
+ this.type = type;
+ }
+
+ /**
+ * Returns the type of operation.
+ *
+ * @return type
+ */
+ public Type type() {
+ return type;
+ }
+
+ /**
+ * Returns the meter.
+ *
+ * @return a meter
+ */
+ public Meter meter() {
+ return meter;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("meter", meter)
+ .add("type", type)
+ .toString();
+ }
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterOperations.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterOperations.java
new file mode 100644
index 0000000..8dbad89
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterOperations.java
@@ -0,0 +1,50 @@
+/*
+ * 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.incubator.net.meter;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Immutable collection of meter operation to be used between
+ * core and provider layers of group subsystem.
+ *
+ */
+public final class MeterOperations {
+ private final List<MeterOperation> operations;
+
+ /**
+ * Creates a immutable list of meter operation.
+ *
+ * @param operations list of meter operation
+ */
+ public MeterOperations(List<MeterOperation> operations) {
+ this.operations = ImmutableList.copyOf(checkNotNull(operations));
+ }
+
+ /**
+ * Returns immutable list of Meter operation.
+ *
+ * @return list of Meter operation
+ */
+ public List<MeterOperation> operations() {
+ return operations;
+ }
+
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterProvider.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterProvider.java
new file mode 100644
index 0000000..d8a743c1
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterProvider.java
@@ -0,0 +1,48 @@
+/*
+ * 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.incubator.net.meter;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.provider.Provider;
+
+/**
+ * Abstraction of a Meter provider.
+ */
+public interface MeterProvider extends Provider {
+
+ /**
+ * Performs a batch of meter operation on the specified device with the
+ * specified parameters.
+ *
+ * @param deviceId device identifier on which the batch of group
+ * operations to be executed
+ * @param meterOps immutable list of meter operation
+ */
+ void performMeterOperation(DeviceId deviceId,
+ MeterOperations meterOps);
+
+
+ /**
+ * Performs a meter operation on the specified device with the
+ * specified parameters.
+ *
+ * @param deviceId device identifier on which the batch of group
+ * operations to be executed
+ * @param meterOp a meter operation
+ */
+ void performMeterOperation(DeviceId deviceId,
+ MeterOperation meterOp);
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterProviderRegistry.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterProviderRegistry.java
new file mode 100644
index 0000000..b20e918
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterProviderRegistry.java
@@ -0,0 +1,27 @@
+/*
+ * 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.incubator.net.meter;
+
+
+import org.onosproject.net.provider.ProviderRegistry;
+
+/**
+ * Abstraction for a meter provider registry.
+ */
+public interface MeterProviderRegistry
+ extends ProviderRegistry<MeterProvider, MeterProviderService> {
+}
+
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterProviderService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterProviderService.java
new file mode 100644
index 0000000..c1dd30cb1
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterProviderService.java
@@ -0,0 +1,49 @@
+/*
+ * 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.incubator.net.meter;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.provider.ProviderService;
+
+import java.util.Collection;
+
+/**
+ * Service through which meter providers can inject information
+ * into the core.
+ */
+public interface MeterProviderService extends ProviderService<MeterProvider> {
+
+ /**
+ * Notifies the core that a meter operaton failed for a
+ * specific reason.
+ * @param operation the failed operation
+ * @param reason the failure reason
+ */
+ void meterOperationFailed(MeterOperation operation,
+ MeterFailReason reason);
+
+ /**
+ * Pushes the collection of meters observed on the data plane as
+ * well as their associated statistics.
+ *
+ * @param deviceId a device id
+ * @param meterEntries a collection of meter entries
+ */
+ void pushMeterMetrics(DeviceId deviceId,
+ Collection<Meter> meterEntries);
+
+
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterService.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterService.java
new file mode 100644
index 0000000..b8702dd
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterService.java
@@ -0,0 +1,70 @@
+/*
+ * 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.incubator.net.meter;
+
+import org.onosproject.event.ListenerService;
+
+/**
+ * Service for add/updating and removing meters. Meters are
+ * are assigned to flow to rate limit them and provide a certain
+ * quality of service.
+ */
+public interface MeterService
+ extends ListenerService<MeterEvent, MeterListener> {
+
+ /**
+ * Adds a meter to the system and performs it installation.
+ *
+ * @param meter a meter.
+ */
+ void addMeter(Meter meter);
+
+ /**
+ * Updates a meter by adding statistic information to the meter.
+ *
+ * @param meter an updated meter
+ */
+ void updateMeter(Meter meter);
+
+ /**
+ * Remove a meter from the system and the dataplane.
+ *
+ * @param meter a meter to remove
+ */
+ void removeMeter(Meter meter);
+
+ /**
+ * Remove a meter from the system and the dataplane by meter id.
+ *
+ * @param id a meter id
+ */
+ void removeMeter(MeterId id);
+
+ /**
+ * Fetch the meter by the meter id.
+ *
+ * @param id a meter id
+ * @return a meter
+ */
+ Meter getMeter(MeterId id);
+
+ /**
+ * Allocate a meter id which must be used to create the meter.
+ *
+ * @return a meter id
+ */
+ MeterId allocateMeterId();
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterStoreDelegate.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterStoreDelegate.java
new file mode 100644
index 0000000..facb453
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/MeterStoreDelegate.java
@@ -0,0 +1,24 @@
+/*
+ * 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.incubator.net.meter;
+
+import org.onosproject.store.StoreDelegate;
+
+/**
+ * Meter store delegate abstraction.
+ */
+public interface MeterStoreDelegate extends StoreDelegate<MeterEvent> {
+}
diff --git a/incubator/api/src/main/java/org/onosproject/incubator/net/meter/package-info.java b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/package-info.java
new file mode 100644
index 0000000..b1cb85e
--- /dev/null
+++ b/incubator/api/src/main/java/org/onosproject/incubator/net/meter/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Flow meter model and related services.
+ */
+package org.onosproject.incubator.net.meter;
\ No newline at end of file
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/meter/impl/MeterManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/meter/impl/MeterManager.java
new file mode 100644
index 0000000..6039247
--- /dev/null
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/meter/impl/MeterManager.java
@@ -0,0 +1,146 @@
+/*
+ * 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.incubator.net.meter.impl;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.incubator.net.meter.Meter;
+import org.onosproject.incubator.net.meter.MeterEvent;
+import org.onosproject.incubator.net.meter.MeterFailReason;
+import org.onosproject.incubator.net.meter.MeterId;
+import org.onosproject.incubator.net.meter.MeterListener;
+import org.onosproject.incubator.net.meter.MeterOperation;
+import org.onosproject.incubator.net.meter.MeterProvider;
+import org.onosproject.incubator.net.meter.MeterProviderRegistry;
+import org.onosproject.incubator.net.meter.MeterProviderService;
+import org.onosproject.incubator.net.meter.MeterService;
+import org.onosproject.incubator.net.meter.MeterStoreDelegate;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.provider.AbstractListenerProviderRegistry;
+import org.onosproject.net.provider.AbstractProviderService;
+import org.onosproject.store.service.AtomicCounter;
+import org.onosproject.store.service.StorageService;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+
+/**
+ * Provides implementation of the meter service APIs.
+ */
+@Component(immediate = true)
+@Service
+public class MeterManager extends AbstractListenerProviderRegistry<MeterEvent, MeterListener,
+ MeterProvider, MeterProviderService>
+ implements MeterService, MeterProviderRegistry {
+
+ private final String meterIdentifier = "meter-id-counter";
+ private final Logger log = getLogger(getClass());
+ private final MeterStoreDelegate delegate = new InternalMeterStoreDelegate();
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected StorageService storageService;
+
+ private AtomicCounter meterIdCounter;
+
+ @Activate
+ public void activate() {
+ meterIdCounter = storageService.atomicCounterBuilder()
+ .withName(meterIdentifier)
+ .build();
+ log.info("Started");
+ }
+
+ @Deactivate
+ public void deactivate() {
+ log.info("Stopped");
+ }
+
+ @Override
+ protected MeterProviderService createProviderService(MeterProvider provider) {
+ return new InternalMeterProviderService(provider);
+ }
+
+ @Override
+ public void addMeter(Meter meter) {
+
+ }
+
+ @Override
+ public void updateMeter(Meter meter) {
+
+ }
+
+ @Override
+ public void removeMeter(Meter meter) {
+
+ }
+
+ @Override
+ public void removeMeter(MeterId id) {
+
+ }
+
+ @Override
+ public Meter getMeter(MeterId id) {
+ return null;
+ }
+
+ @Override
+ public MeterId allocateMeterId() {
+ // FIXME: This will break one day.
+ return MeterId.meterId((int) meterIdCounter.getAndIncrement());
+ }
+
+ private class InternalMeterProviderService
+ extends AbstractProviderService<MeterProvider>
+ implements MeterProviderService {
+
+ /**
+ * Creates a provider service on behalf of the specified provider.
+ *
+ * @param provider provider to which this service is being issued
+ */
+ protected InternalMeterProviderService(MeterProvider provider) {
+ super(provider);
+ }
+
+ @Override
+ public void meterOperationFailed(MeterOperation operation, MeterFailReason reason) {
+
+ }
+
+ @Override
+ public void pushMeterMetrics(DeviceId deviceId, Collection<Meter> meterEntries) {
+
+ }
+ }
+
+ private class InternalMeterStoreDelegate implements MeterStoreDelegate {
+
+ @Override
+ public void notify(MeterEvent event) {
+
+ }
+ }
+
+}