[ONOS-5599] Store meter features into MeterStore

Change-Id: I22f7366b87cad6fc706b6ff7b0ccff1a0e85ad6a
diff --git a/core/api/src/main/java/org/onosproject/net/meter/Band.java b/core/api/src/main/java/org/onosproject/net/meter/Band.java
index 35a816f..29d9c82 100644
--- a/core/api/src/main/java/org/onosproject/net/meter/Band.java
+++ b/core/api/src/main/java/org/onosproject/net/meter/Band.java
@@ -40,7 +40,12 @@
          * IP header of the packets that exceed the band
          * rate value.
          */
-        REMARK
+        REMARK,
+
+        /**
+         * defines an experimental meter band.
+         */
+        EXPERIMENTAL
     }
 
     /**
diff --git a/core/api/src/main/java/org/onosproject/net/meter/DefaultMeterFeatures.java b/core/api/src/main/java/org/onosproject/net/meter/DefaultMeterFeatures.java
new file mode 100644
index 0000000..21ee2b2
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/meter/DefaultMeterFeatures.java
@@ -0,0 +1,183 @@
+/*
+ * Copyright 2015-present 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.net.meter;
+
+import com.google.common.base.MoreObjects;
+import org.onosproject.net.DeviceId;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Default implementation of MeterFeatures.
+ */
+public final class DefaultMeterFeatures implements MeterFeatures {
+    private DeviceId deviceId;
+    private long maxMeter;
+    private Set<Band.Type> bandTypes;
+    private Set<Meter.Unit> units;
+    private boolean burst;
+    private boolean stats;
+    private short maxBands;
+    private short maxColor;
+
+    private DefaultMeterFeatures(DeviceId did, long maxMeter,
+                                 Set<Band.Type> bandTypes, Set<Meter.Unit> units,
+                                 boolean burst, boolean stats,
+                                 short maxBands, short maxColor) {
+        this.deviceId = did;
+        this.maxMeter = maxMeter;
+        this.bandTypes = bandTypes;
+        this.burst = burst;
+        this.stats = stats;
+        this.units = units;
+        this.maxBands = maxBands;
+        this.maxColor = maxColor;
+    }
+
+    @Override
+    public DeviceId deviceId() {
+        return deviceId;
+    }
+
+    @Override
+    public long maxMeter() {
+        return maxMeter;
+    }
+
+    @Override
+    public Set<Band.Type> bandTypes() {
+        return bandTypes;
+    }
+
+    @Override
+    public Set<Meter.Unit> unitTypes() {
+        return units;
+    }
+
+    @Override
+    public boolean isBurstSupported() {
+        return burst;
+    }
+
+    @Override
+    public boolean isStatsSupported() {
+        return stats;
+    }
+
+    @Override
+    public short maxBands() {
+        return maxBands;
+    }
+
+    @Override
+    public short maxColor() {
+        return maxColor;
+    }
+
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static MeterFeatures noMeterFeatures(DeviceId deviceId) {
+        return DefaultMeterFeatures.builder().forDevice(deviceId)
+                .build();
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("deviceId", deviceId())
+                .add("maxMeter", maxMeter())
+                .add("maxBands", maxBands())
+                .add("maxColor", maxColor())
+                .add("bands", bandTypes())
+                .add("burst", isBurstSupported())
+                .add("stats", isStatsSupported())
+                .add("units", unitTypes())
+                .toString();
+    }
+
+    /**
+     * A DefaultMeterFeatures builder.
+     */
+    public static final class Builder implements MeterFeatures.Builder {
+        private DeviceId did;
+        private long mmeter = 0L;
+        private short mbands = 0;
+        private short mcolors = 0;
+        private Set<Band.Type> bandTypes = new HashSet<>();
+        private Set<Meter.Unit> units1 = new HashSet<>();
+        private boolean burst = false;
+        private boolean stats = false;
+
+        @Override
+        public MeterFeatures.Builder forDevice(DeviceId deviceId) {
+            did = deviceId;
+            return this;
+        }
+
+        @Override
+        public MeterFeatures.Builder withMaxMeters(long maxMeter) {
+            mmeter = maxMeter;
+            return this;
+        }
+
+        @Override
+        public MeterFeatures.Builder withMaxBands(short maxBands) {
+            mbands = maxBands;
+            return this;
+        }
+
+        @Override
+        public MeterFeatures.Builder withMaxColors(short maxColors) {
+            mcolors = maxColors;
+            return this;
+        }
+
+        @Override
+        public MeterFeatures.Builder withBandTypes(Set<Band.Type> types) {
+            bandTypes = types;
+            return this;
+        }
+
+        @Override
+        public MeterFeatures.Builder withUnits(Set<Meter.Unit> units) {
+            units1 = units;
+            return this;
+        }
+
+        @Override
+        public MeterFeatures.Builder hasBurst(boolean hasBurst) {
+            burst = hasBurst;
+            return this;
+        }
+
+        @Override
+        public MeterFeatures.Builder hasStats(boolean hasStats) {
+            stats = hasStats;
+            return this;
+        }
+
+        @Override
+        public MeterFeatures build() {
+            checkNotNull(did, "Must specify a device");
+            return new DefaultMeterFeatures(did, mmeter, bandTypes, units1, burst, stats, mbands, mcolors);
+        }
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/meter/MeterFeatures.java b/core/api/src/main/java/org/onosproject/net/meter/MeterFeatures.java
new file mode 100644
index 0000000..1b8e869
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/meter/MeterFeatures.java
@@ -0,0 +1,158 @@
+/*
+ * Copyright 2015-present 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.net.meter;
+
+import org.onosproject.net.DeviceId;
+
+import java.util.Set;
+
+/**
+ * Meter Features of a device.
+ */
+public interface MeterFeatures {
+
+    /**
+     * Return the device id to which this meter features apply.
+     *
+     * @return the device id
+     */
+    DeviceId deviceId();
+
+    /**
+     * Returns the maximum number of meters accepted by the device.
+     *
+     * @return the maximum meter value.
+     */
+    long maxMeter();
+
+    /**
+     * Returns band types supported.
+     *
+     * @return the band types supported.
+     */
+    Set<Band.Type> bandTypes();
+
+    /**
+     * Returns unit types available for meters.
+     *
+     * @return the unit types available.
+     */
+    Set<Meter.Unit> unitTypes();
+
+    /**
+     * Returns if burst size is available.
+     *
+     * @return burst availability
+     */
+    boolean isBurstSupported();
+
+    /**
+     * Returns if statistics collection is available.
+     *
+     * @return statistics availability
+     */
+    boolean isStatsSupported();
+
+    /**
+     * Returns the maximum bands per meter.
+     *
+     * @return the max bands value
+     */
+    short maxBands();
+
+    /**
+     * Returns the maximum colors value for DiffServ operation.
+     *
+     * @return the maximum colors value.
+     */
+    short maxColor();
+
+    /**
+     * A meter features builder.
+     */
+    interface Builder {
+        /**
+         * Assigns the target device for this meter features.
+         *
+         * @param deviceId a device id
+         * @return this builder
+         */
+        Builder forDevice(DeviceId deviceId);
+
+        /**
+         * Assigns the max meters value for this meter features.
+         *
+         * @param maxMeter the maximum meters available
+         * @return this builder
+         */
+        Builder withMaxMeters(long maxMeter);
+
+        /**
+         * Assigns the max bands value for this meter features.
+         *
+         * @param maxBands the maximum bands available.
+         * @return this builder
+         */
+        Builder withMaxBands(short maxBands);
+
+        /**
+         * Assigns the max colors value for this meter features.
+         *
+         * @param maxColors the maximum colors available.
+         * @return this builder
+         */
+        Builder withMaxColors(short maxColors);
+
+        /**
+         * Assigns the band types for this meter features.
+         *
+         * @param types the band types available.
+         * @return this builder
+         */
+        Builder withBandTypes(Set<Band.Type> types);
+
+        /**
+         * Assigns the capabilities for this meter features.
+         *
+         * @param units the units available
+         * @return this
+         */
+        Builder withUnits(Set<Meter.Unit> units);
+
+        /**
+         * Assigns the burst capabilities.
+         *
+         * @param hasBurst if the burst is supported
+         * @return this builder
+         */
+        Builder hasBurst(boolean hasBurst);
+
+        /**
+         * Assigns the stats capabilities.
+         *
+         * @param hasStats if the statistics are supported
+         * @return this builder
+         */
+        Builder hasStats(boolean hasStats);
+
+        /**
+         * Builds the Meter Features based on the specified parameters.
+         *
+         * @return the meter features
+         */
+        MeterFeatures build();
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/meter/MeterFeaturesKey.java b/core/api/src/main/java/org/onosproject/net/meter/MeterFeaturesKey.java
new file mode 100644
index 0000000..be49f96
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/meter/MeterFeaturesKey.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2016-present 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.net.meter;
+
+import org.onosproject.net.DeviceId;
+
+import java.util.Objects;
+
+/**
+ * A meter features key represents a meter features uniquely.
+ * Right now only deviceId is used but this class might be useful in
+ * virtualization in which a unique deviceId could have multiple features (guess).
+ */
+public final class MeterFeaturesKey {
+    private final DeviceId deviceId;
+
+    private MeterFeaturesKey(DeviceId deviceId) {
+        this.deviceId = deviceId;
+    }
+
+    public static MeterFeaturesKey key(DeviceId did) {
+        return new MeterFeaturesKey(did);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        MeterFeaturesKey mfk = (MeterFeaturesKey) obj;
+        return Objects.equals(deviceId, mfk.deviceId());
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(deviceId);
+    }
+
+    @Override
+    public String toString() {
+        return "mfk@" + deviceId.toString();
+    }
+
+    public DeviceId deviceId() {
+        return deviceId;
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/meter/MeterProviderService.java b/core/api/src/main/java/org/onosproject/net/meter/MeterProviderService.java
index 9f5a5bf..242c5d0 100644
--- a/core/api/src/main/java/org/onosproject/net/meter/MeterProviderService.java
+++ b/core/api/src/main/java/org/onosproject/net/meter/MeterProviderService.java
@@ -45,5 +45,21 @@
     void pushMeterMetrics(DeviceId deviceId,
                           Collection<Meter> meterEntries);
 
+    /**
+     * Pushes the meter features collected from the device.
+     *
+     * @param deviceId the device Id
+     * @param meterfeatures the meter features
+     */
+    void pushMeterFeatures(DeviceId deviceId,
+                           MeterFeatures meterfeatures);
+
+
+    /**
+     * Delete meter features collected from the device.
+     *
+     * @param deviceId the device id
+     */
+    void deleteMeterFeatures(DeviceId deviceId);
 
 }
diff --git a/core/api/src/main/java/org/onosproject/net/meter/MeterStore.java b/core/api/src/main/java/org/onosproject/net/meter/MeterStore.java
index d5247c4..5a5016e 100644
--- a/core/api/src/main/java/org/onosproject/net/meter/MeterStore.java
+++ b/core/api/src/main/java/org/onosproject/net/meter/MeterStore.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.net.meter;
 
+import org.onosproject.net.DeviceId;
 import org.onosproject.store.Store;
 
 import java.util.Collection;
@@ -41,6 +42,24 @@
      */
     CompletableFuture<MeterStoreResult> deleteMeter(Meter meter);
 
+
+    /**
+     * Adds the meter features to the store.
+     *
+     * @param meterfeatures the meter features
+     * @return the result of the store operation
+     */
+    MeterStoreResult storeMeterFeatures(MeterFeatures meterfeatures);
+
+    /**
+     * Deletes the meter features from the store.
+     *
+     * @param deviceId the device id
+     * @return a future indicating the result of the store operation
+     */
+    MeterStoreResult deleteMeterFeatures(DeviceId deviceId);
+
+
     /**
      * Updates a meter whose meter id is the same as the passed meter.
      *
@@ -87,4 +106,12 @@
      */
     void deleteMeterNow(Meter m);
 
+    /**
+     * Retrieve maximum meters available for the device.
+     *
+     * @param key the meter features key
+     * @return the maximum number of meters supported by the device
+     */
+    long getMaxMeters(MeterFeaturesKey key);
+
 }