[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);
+
 }
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
index 2cf658f..37251f6 100644
--- 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
@@ -29,6 +29,7 @@
 import org.onosproject.net.meter.Meter;
 import org.onosproject.net.meter.MeterEvent;
 import org.onosproject.net.meter.MeterFailReason;
+import org.onosproject.net.meter.MeterFeatures;
 import org.onosproject.net.meter.MeterId;
 import org.onosproject.net.meter.MeterKey;
 import org.onosproject.net.meter.MeterListener;
@@ -226,6 +227,16 @@
                 }
             });
         }
+
+        @Override
+        public void pushMeterFeatures(DeviceId deviceId, MeterFeatures meterfeatures) {
+            store.storeMeterFeatures(meterfeatures);
+        }
+
+        @Override
+        public void deleteMeterFeatures(DeviceId deviceId) {
+            store.deleteMeterFeatures(deviceId);
+        }
     }
 
     private class InternalMeterStoreDelegate implements MeterStoreDelegate {
diff --git a/incubator/net/src/test/java/org/onosproject/incubator/net/meter/impl/MeterManagerTest.java b/incubator/net/src/test/java/org/onosproject/incubator/net/meter/impl/MeterManagerTest.java
index 05832c7..a81004f 100644
--- a/incubator/net/src/test/java/org/onosproject/incubator/net/meter/impl/MeterManagerTest.java
+++ b/incubator/net/src/test/java/org/onosproject/incubator/net/meter/impl/MeterManagerTest.java
@@ -35,8 +35,10 @@
 import org.onosproject.net.meter.Band;
 import org.onosproject.net.meter.DefaultBand;
 import org.onosproject.net.meter.DefaultMeter;
+import org.onosproject.net.meter.DefaultMeterFeatures;
 import org.onosproject.net.meter.DefaultMeterRequest;
 import org.onosproject.net.meter.Meter;
+import org.onosproject.net.meter.MeterFeaturesKey;
 import org.onosproject.net.meter.MeterId;
 import org.onosproject.net.meter.MeterOperation;
 import org.onosproject.net.meter.MeterOperations;
@@ -51,13 +53,12 @@
 import org.onosproject.store.service.TestStorageService;
 
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
 import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 import static org.onosproject.net.NetTestTools.APP_ID;
 import static org.onosproject.net.NetTestTools.did;
 import static org.onosproject.net.NetTestTools.injectEventDispatcher;
@@ -96,6 +97,24 @@
         TestUtils.setField(meterStore, "clusterService", new TestClusterService());
         TestUtils.setField(meterStore, "mastershipService", new TestMastershipService());
         meterStore.activate();
+        meterStore.storeMeterFeatures(DefaultMeterFeatures.builder().forDevice(did("1"))
+                .withMaxMeters(255L)
+                .withBandTypes(new HashSet<>())
+                .withUnits(new HashSet<>())
+                .hasStats(false)
+                .hasBurst(false)
+                .withMaxBands((byte) 0)
+                .withMaxColors((byte) 0)
+                .build());
+        meterStore.storeMeterFeatures(DefaultMeterFeatures.builder().forDevice(did("2"))
+                .withMaxMeters(2)
+                .withBandTypes(new HashSet<>())
+                .withUnits(new HashSet<>())
+                .hasBurst(false)
+                .hasStats(false)
+                .withMaxBands((byte) 0)
+                .withMaxColors((byte) 0)
+                .build());
 
         manager = new MeterManager();
         manager.store = meterStore;
@@ -195,6 +214,14 @@
         assertThat(manager.getMeter(did("2"), MeterId.meterId(1)), is(m2));
     }
 
+    @Test
+    public void testMeterFeatures() {
+        assertEquals(meterStore.getMaxMeters(MeterFeaturesKey.key(did("1"))), 255L);
+        assertEquals(meterStore.getMaxMeters(MeterFeaturesKey.key(did("2"))), 2);
+    }
+
+
+
     public class TestApplicationId extends DefaultApplicationId {
         public TestApplicationId(int id, String name) {
             super(id, name);
diff --git a/incubator/store/src/main/java/org/onosproject/incubator/store/meter/impl/DistributedMeterStore.java b/incubator/store/src/main/java/org/onosproject/incubator/store/meter/impl/DistributedMeterStore.java
index be7de21..b681c27 100644
--- a/incubator/store/src/main/java/org/onosproject/incubator/store/meter/impl/DistributedMeterStore.java
+++ b/incubator/store/src/main/java/org/onosproject/incubator/store/meter/impl/DistributedMeterStore.java
@@ -26,18 +26,22 @@
 import org.onosproject.cluster.ClusterService;
 import org.onosproject.cluster.NodeId;
 import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.DeviceId;
 import org.onosproject.net.meter.Band;
 import org.onosproject.net.meter.DefaultBand;
 import org.onosproject.net.meter.DefaultMeter;
 import org.onosproject.net.meter.Meter;
 import org.onosproject.net.meter.MeterEvent;
 import org.onosproject.net.meter.MeterFailReason;
+import org.onosproject.net.meter.MeterFeatures;
+import org.onosproject.net.meter.MeterFeaturesKey;
 import org.onosproject.net.meter.MeterKey;
 import org.onosproject.net.meter.MeterOperation;
 import org.onosproject.net.meter.MeterState;
 import org.onosproject.net.meter.MeterStore;
 import org.onosproject.net.meter.MeterStoreDelegate;
 import org.onosproject.net.meter.MeterStoreResult;
+import org.onosproject.net.meter.DefaultMeterFeatures;
 import org.onosproject.store.AbstractStore;
 import org.onosproject.store.serializers.KryoNamespaces;
 import org.onosproject.store.service.ConsistentMap;
@@ -54,6 +58,7 @@
 import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 
+import static org.onosproject.net.meter.MeterFailReason.TIMEOUT;
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
@@ -68,6 +73,7 @@
     private Logger log = getLogger(getClass());
 
     private static final String METERSTORE = "onos-meter-store";
+    private static final String METERFEATURESSTORE = "onos-meter-features-store";
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     private StorageService storageService;
@@ -81,6 +87,8 @@
     private ConsistentMap<MeterKey, MeterData> meters;
     private NodeId local;
 
+    private ConsistentMap<MeterFeaturesKey, MeterFeatures> meterFeatures;
+
     private MapEventListener<MeterKey, MeterData> mapListener = new InternalMapEventListener();
 
     private Map<MeterKey, CompletableFuture<MeterStoreResult>> futures =
@@ -106,6 +114,16 @@
 
         meters.addListener(mapListener);
 
+        meterFeatures = storageService.<MeterFeaturesKey, MeterFeatures>consistentMapBuilder()
+                .withName(METERFEATURESSTORE)
+                .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API),
+                        MeterFeaturesKey.class,
+                        MeterFeatures.class,
+                        DefaultMeterFeatures.class,
+                        Band.Type.class,
+                        Meter.Unit.class,
+                        MeterFailReason.class)).build();
+
         log.info("Started");
     }
 
@@ -157,6 +175,30 @@
     }
 
     @Override
+    public MeterStoreResult storeMeterFeatures(MeterFeatures meterfeatures) {
+        MeterStoreResult result = MeterStoreResult.success();
+        MeterFeaturesKey key = MeterFeaturesKey.key(meterfeatures.deviceId());
+        try {
+            meterFeatures.putIfAbsent(key, meterfeatures);
+        } catch (StorageException e) {
+            result = MeterStoreResult.fail(TIMEOUT);
+        }
+        return result;
+    }
+
+    @Override
+    public MeterStoreResult deleteMeterFeatures(DeviceId deviceId) {
+        MeterStoreResult result = MeterStoreResult.success();
+        MeterFeaturesKey key = MeterFeaturesKey.key(deviceId);
+        try {
+            meterFeatures.remove(key);
+        } catch (StorageException e) {
+            result = MeterStoreResult.fail(TIMEOUT);
+        }
+        return result;
+    }
+
+    @Override
     public CompletableFuture<MeterStoreResult> updateMeter(Meter meter) {
         CompletableFuture<MeterStoreResult> future = new CompletableFuture<>();
         MeterKey key = MeterKey.key(meter.deviceId(), meter.id());
@@ -214,6 +256,12 @@
         meters.remove(key);
     }
 
+    @Override
+    public long getMaxMeters(MeterFeaturesKey key) {
+        MeterFeatures features = Versioned.valueOrElse(meterFeatures.get(key), null);
+        return features == null ? 0L : features.maxMeter();
+    }
+
     private class InternalMapEventListener implements MapEventListener<MeterKey, MeterData> {
         @Override
         public void event(MapEvent<MeterKey, MeterData> event) {
diff --git a/protocols/openflow/api/src/main/java/org/onosproject/openflow/controller/OpenFlowSwitch.java b/protocols/openflow/api/src/main/java/org/onosproject/openflow/controller/OpenFlowSwitch.java
index 206ffad..2ac0e02 100644
--- a/protocols/openflow/api/src/main/java/org/onosproject/openflow/controller/OpenFlowSwitch.java
+++ b/protocols/openflow/api/src/main/java/org/onosproject/openflow/controller/OpenFlowSwitch.java
@@ -18,6 +18,7 @@
 import org.onosproject.net.Device;
 import org.projectfloodlight.openflow.protocol.OFFactory;
 import org.projectfloodlight.openflow.protocol.OFMessage;
+import org.projectfloodlight.openflow.protocol.OFMeterFeatures;
 import org.projectfloodlight.openflow.protocol.OFPortDesc;
 
 import java.util.List;
@@ -74,6 +75,12 @@
     List<OFPortDesc> getPorts();
 
     /**
+     * Fetches the meter features of this switch.
+     * @return unmodifiable meter features
+     */
+    OFMeterFeatures getMeterFeatures();
+
+    /**
      * Provides the factory for this OF version.
      * @return OF version specific factory.
      */
diff --git a/protocols/openflow/api/src/main/java/org/onosproject/openflow/controller/driver/AbstractOpenFlowSwitch.java b/protocols/openflow/api/src/main/java/org/onosproject/openflow/controller/driver/AbstractOpenFlowSwitch.java
index 128863e..eb6571e 100644
--- a/protocols/openflow/api/src/main/java/org/onosproject/openflow/controller/driver/AbstractOpenFlowSwitch.java
+++ b/protocols/openflow/api/src/main/java/org/onosproject/openflow/controller/driver/AbstractOpenFlowSwitch.java
@@ -30,6 +30,7 @@
 import org.projectfloodlight.openflow.protocol.OFFactory;
 import org.projectfloodlight.openflow.protocol.OFFeaturesReply;
 import org.projectfloodlight.openflow.protocol.OFMessage;
+import org.projectfloodlight.openflow.protocol.OFMeterFeatures;
 import org.projectfloodlight.openflow.protocol.OFMeterFeaturesStatsReply;
 import org.projectfloodlight.openflow.protocol.OFNiciraControllerRoleRequest;
 import org.projectfloodlight.openflow.protocol.OFPortDesc;
@@ -472,6 +473,15 @@
     }
 
     @Override
+    public OFMeterFeatures getMeterFeatures() {
+        if (this.meterfeatures != null) {
+            return this.meterfeatures.getFeatures();
+        } else {
+            return null;
+        }
+    }
+
+    @Override
     public String manufacturerDescription() {
         return this.desc.getMfrDesc();
     }
diff --git a/protocols/openflow/api/src/test/java/org/onosproject/openflow/controller/OpenFlowSwitchAdapter.java b/protocols/openflow/api/src/test/java/org/onosproject/openflow/controller/OpenFlowSwitchAdapter.java
index 4a511b6..c3b228d 100644
--- a/protocols/openflow/api/src/test/java/org/onosproject/openflow/controller/OpenFlowSwitchAdapter.java
+++ b/protocols/openflow/api/src/test/java/org/onosproject/openflow/controller/OpenFlowSwitchAdapter.java
@@ -18,6 +18,7 @@
 import org.onosproject.net.Device;
 import org.projectfloodlight.openflow.protocol.OFFactory;
 import org.projectfloodlight.openflow.protocol.OFMessage;
+import org.projectfloodlight.openflow.protocol.OFMeterFeatures;
 import org.projectfloodlight.openflow.protocol.OFPortDesc;
 
 import java.util.List;
@@ -57,6 +58,11 @@
     }
 
     @Override
+    public OFMeterFeatures getMeterFeatures() {
+        return null;
+    }
+
+    @Override
     public OFFactory factory() {
         return null;
     }
diff --git a/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/OpenflowSwitchDriverAdapter.java b/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/OpenflowSwitchDriverAdapter.java
index 11c47e1..74751db 100644
--- a/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/OpenflowSwitchDriverAdapter.java
+++ b/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/OpenflowSwitchDriverAdapter.java
@@ -31,6 +31,7 @@
 import org.projectfloodlight.openflow.protocol.OFFactory;
 import org.projectfloodlight.openflow.protocol.OFFeaturesReply;
 import org.projectfloodlight.openflow.protocol.OFMessage;
+import org.projectfloodlight.openflow.protocol.OFMeterFeatures;
 import org.projectfloodlight.openflow.protocol.OFMeterFeaturesStatsReply;
 import org.projectfloodlight.openflow.protocol.OFPortDesc;
 import org.projectfloodlight.openflow.protocol.OFPortDescStatsReply;
@@ -241,6 +242,11 @@
     }
 
     @Override
+    public OFMeterFeatures getMeterFeatures() {
+        return null;
+    }
+
+    @Override
     public OFFactory factory() {
         // return what-ever triggers requestPending = true
         return OFFactories.getFactory(OFVersion.OF_10);
diff --git a/providers/openflow/device/src/test/java/org/onosproject/provider/of/device/impl/OpenFlowDeviceProviderTest.java b/providers/openflow/device/src/test/java/org/onosproject/provider/of/device/impl/OpenFlowDeviceProviderTest.java
index f8e4ffd..8f5a9c1 100644
--- a/providers/openflow/device/src/test/java/org/onosproject/provider/of/device/impl/OpenFlowDeviceProviderTest.java
+++ b/providers/openflow/device/src/test/java/org/onosproject/provider/of/device/impl/OpenFlowDeviceProviderTest.java
@@ -46,6 +46,7 @@
 import org.onosproject.openflow.controller.RoleState;
 import org.projectfloodlight.openflow.protocol.OFFactory;
 import org.projectfloodlight.openflow.protocol.OFMessage;
+import org.projectfloodlight.openflow.protocol.OFMeterFeatures;
 import org.projectfloodlight.openflow.protocol.OFPortDesc;
 import org.projectfloodlight.openflow.protocol.OFPortReason;
 import org.projectfloodlight.openflow.protocol.OFPortStatus;
@@ -237,7 +238,6 @@
             public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
 
             }
-
         }
     }
 
@@ -363,6 +363,11 @@
         }
 
         @Override
+        public OFMeterFeatures getMeterFeatures() {
+            return null;
+        }
+
+        @Override
         public OFFactory factory() {
             return factory;
         }
diff --git a/providers/openflow/group/src/test/java/org/onosproject/provider/of/group/impl/OpenFlowGroupProviderTest.java b/providers/openflow/group/src/test/java/org/onosproject/provider/of/group/impl/OpenFlowGroupProviderTest.java
index cc2cd39..cd1e7bb 100644
--- a/providers/openflow/group/src/test/java/org/onosproject/provider/of/group/impl/OpenFlowGroupProviderTest.java
+++ b/providers/openflow/group/src/test/java/org/onosproject/provider/of/group/impl/OpenFlowGroupProviderTest.java
@@ -54,6 +54,7 @@
 import org.projectfloodlight.openflow.protocol.OFGroupStatsReply;
 import org.projectfloodlight.openflow.protocol.OFGroupType;
 import org.projectfloodlight.openflow.protocol.OFMessage;
+import org.projectfloodlight.openflow.protocol.OFMeterFeatures;
 import org.projectfloodlight.openflow.protocol.OFPortDesc;
 import org.projectfloodlight.openflow.protocol.OFVersion;
 import org.projectfloodlight.openflow.protocol.errormsg.OFGroupModFailedErrorMsg;
@@ -358,6 +359,11 @@
         }
 
         @Override
+        public OFMeterFeatures getMeterFeatures() {
+            return null;
+        }
+
+        @Override
         public OFFactory factory() {
             return OFFactories.getFactory(OFVersion.OF_13);
         }
diff --git a/providers/openflow/meter/src/main/java/org/onosproject/provider/of/meter/impl/OpenFlowMeterProvider.java b/providers/openflow/meter/src/main/java/org/onosproject/provider/of/meter/impl/OpenFlowMeterProvider.java
index cff47bb..b343c56 100644
--- a/providers/openflow/meter/src/main/java/org/onosproject/provider/of/meter/impl/OpenFlowMeterProvider.java
+++ b/providers/openflow/meter/src/main/java/org/onosproject/provider/of/meter/impl/OpenFlowMeterProvider.java
@@ -34,6 +34,7 @@
 import org.onosproject.net.meter.DefaultMeter;
 import org.onosproject.net.meter.Meter;
 import org.onosproject.net.meter.MeterFailReason;
+import org.onosproject.net.meter.MeterFeatures;
 import org.onosproject.net.meter.MeterId;
 import org.onosproject.net.meter.MeterOperation;
 import org.onosproject.net.meter.MeterOperations;
@@ -51,11 +52,13 @@
 import org.onosproject.openflow.controller.OpenFlowSwitch;
 import org.onosproject.openflow.controller.OpenFlowSwitchListener;
 import org.onosproject.openflow.controller.RoleState;
+import org.onosproject.provider.of.meter.util.MeterFeaturesBuilder;
 import org.projectfloodlight.openflow.protocol.OFErrorMsg;
 import org.projectfloodlight.openflow.protocol.OFErrorType;
 import org.projectfloodlight.openflow.protocol.OFMessage;
 import org.projectfloodlight.openflow.protocol.OFMeterBandStats;
 import org.projectfloodlight.openflow.protocol.OFMeterConfigStatsReply;
+import org.projectfloodlight.openflow.protocol.OFMeterFeatures;
 import org.projectfloodlight.openflow.protocol.OFMeterStats;
 import org.projectfloodlight.openflow.protocol.OFMeterStatsReply;
 import org.projectfloodlight.openflow.protocol.OFPortStatus;
@@ -74,6 +77,8 @@
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.stream.Collectors;
 
+import static org.onosproject.net.DeviceId.deviceId;
+import static org.onosproject.openflow.controller.Dpid.uri;
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
@@ -255,6 +260,25 @@
 
     }
 
+    private MeterFeatures buildMeterFeatures(Dpid dpid, OFMeterFeatures mf) {
+        if (mf != null) {
+            return new MeterFeaturesBuilder(mf, deviceId(uri(dpid)))
+                    .build();
+        } else {
+            // This will usually happen for OpenFlow devices prior to 1.3
+            return MeterFeaturesBuilder.noMeterFeatures(deviceId(uri(dpid)));
+        }
+    }
+
+    private void pushMeterFeatures(Dpid dpid, OFMeterFeatures meterFeatures) {
+        providerService.pushMeterFeatures(deviceId(uri(dpid)),
+                buildMeterFeatures(dpid, meterFeatures));
+    }
+
+    private void destroyMeterFeatures(Dpid dpid) {
+        providerService.deleteMeterFeatures(deviceId(uri(dpid)));
+    }
+
     private Map<Long, Meter> collectMeters(DeviceId deviceId,
                                            OFMeterConfigStatsReply reply) {
         return Maps.newHashMap();
@@ -383,11 +407,13 @@
         @Override
         public void switchAdded(Dpid dpid) {
             createStatsCollection(controller.getSwitch(dpid));
+            pushMeterFeatures(dpid, controller.getSwitch(dpid).getMeterFeatures());
         }
 
         @Override
         public void switchRemoved(Dpid dpid) {
             stopCollectorIfNeeded(collectors.remove(dpid));
+            destroyMeterFeatures(dpid);
         }
 
         @Override
diff --git a/providers/openflow/meter/src/main/java/org/onosproject/provider/of/meter/util/MeterFeaturesBuilder.java b/providers/openflow/meter/src/main/java/org/onosproject/provider/of/meter/util/MeterFeaturesBuilder.java
new file mode 100644
index 0000000..7497448
--- /dev/null
+++ b/providers/openflow/meter/src/main/java/org/onosproject/provider/of/meter/util/MeterFeaturesBuilder.java
@@ -0,0 +1,113 @@
+/*
+ * 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.provider.of.meter.util;
+
+import com.google.common.collect.Sets;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.meter.Band;
+import org.onosproject.net.meter.DefaultMeterFeatures;
+import org.onosproject.net.meter.Meter;
+import org.onosproject.net.meter.MeterFeatures;
+import org.projectfloodlight.openflow.protocol.OFMeterFeatures;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.net.meter.Band.Type.DROP;
+import static org.onosproject.net.meter.Band.Type.REMARK;
+import static org.onosproject.net.meter.Meter.Unit.KB_PER_SEC;
+import static org.onosproject.net.meter.Meter.Unit.PKTS_PER_SEC;
+import static org.projectfloodlight.openflow.protocol.ver13.OFMeterBandTypeSerializerVer13.DROP_VAL;
+import static org.projectfloodlight.openflow.protocol.ver13.OFMeterBandTypeSerializerVer13.DSCP_REMARK_VAL;
+import static org.projectfloodlight.openflow.protocol.ver13.OFMeterFlagsSerializerVer13.*;
+
+/**
+ * OpenFlow builder of MeterFeatures.
+ */
+public class MeterFeaturesBuilder {
+    private static final Logger log = LoggerFactory.getLogger(MeterFeaturesBuilder.class);
+
+    private final OFMeterFeatures ofMeterFeatures;
+    private DeviceId deviceId;
+
+    public MeterFeaturesBuilder(OFMeterFeatures features, DeviceId deviceId) {
+        this.ofMeterFeatures = checkNotNull(features);
+        this.deviceId = deviceId;
+    }
+
+    /**
+     * To build a MeterFeatures using the openflow object
+     * provided by the southbound.
+     *
+     * @return the meter features object
+     */
+    public MeterFeatures build() {
+        /*
+         * We set the basic values before to extract the other information.
+         */
+        MeterFeatures.Builder builder = DefaultMeterFeatures.builder()
+                .forDevice(deviceId)
+                .withMaxBands(ofMeterFeatures.getMaxBands())
+                .withMaxColors(ofMeterFeatures.getMaxColor())
+                .withMaxMeters(ofMeterFeatures.getMaxMeter());
+        /*
+         * We extract the supported band types.
+         */
+        Set<Band.Type> bands = Sets.newHashSet();
+        if ((DROP_VAL & ofMeterFeatures.getCapabilities()) != 0) {
+            bands.add(DROP);
+        }
+        if ((DSCP_REMARK_VAL & ofMeterFeatures.getCapabilities()) != 0) {
+            bands.add(REMARK);
+        }
+        builder.withBandTypes(bands);
+        /*
+         * We extract the supported units;
+         */
+        Set<Meter.Unit> units = Sets.newHashSet();
+        if ((PKTPS_VAL & ofMeterFeatures.getCapabilities()) != 0) {
+            units.add(PKTS_PER_SEC);
+        }
+        if ((KBPS_VAL & ofMeterFeatures.getCapabilities()) != 0) {
+            units.add(KB_PER_SEC);
+        }
+        if (units.isEmpty()) {
+            units.add(PKTS_PER_SEC);
+        }
+        builder.withUnits(units);
+        /*
+         * Burst is supported ?
+         */
+        builder.hasBurst((BURST_VAL & ofMeterFeatures.getCapabilities()) != 0);
+        /*
+         * Stats are supported ?
+         */
+        builder.hasStats((STATS_VAL & ofMeterFeatures.getCapabilities()) != 0);
+        return builder.build();
+    }
+
+    /**
+     * To build an empty meter features.
+     * @param deviceId the device id
+     * @return the meter features
+     */
+    public static MeterFeatures noMeterFeatures(DeviceId deviceId) {
+        return DefaultMeterFeatures.noMeterFeatures(deviceId);
+    }
+
+}
diff --git a/providers/openflow/meter/src/main/java/org/onosproject/provider/of/meter/util/package-info.java b/providers/openflow/meter/src/main/java/org/onosproject/provider/of/meter/util/package-info.java
new file mode 100644
index 0000000..b1830c7
--- /dev/null
+++ b/providers/openflow/meter/src/main/java/org/onosproject/provider/of/meter/util/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * 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.
+ */
+
+/**
+ * Provider that uses OpenFlow controller as a means of device metering management.
+ * Utilities package
+ */
+package org.onosproject.provider.of.meter.util;
diff --git a/providers/openflow/packet/src/test/java/org/onosproject/provider/of/packet/impl/OpenFlowPacketProviderTest.java b/providers/openflow/packet/src/test/java/org/onosproject/provider/of/packet/impl/OpenFlowPacketProviderTest.java
index 1b4fe93..c78a9fb 100644
--- a/providers/openflow/packet/src/test/java/org/onosproject/provider/of/packet/impl/OpenFlowPacketProviderTest.java
+++ b/providers/openflow/packet/src/test/java/org/onosproject/provider/of/packet/impl/OpenFlowPacketProviderTest.java
@@ -48,6 +48,7 @@
 import org.onosproject.openflow.controller.RoleState;
 import org.projectfloodlight.openflow.protocol.OFFactory;
 import org.projectfloodlight.openflow.protocol.OFMessage;
+import org.projectfloodlight.openflow.protocol.OFMeterFeatures;
 import org.projectfloodlight.openflow.protocol.OFPacketIn;
 import org.projectfloodlight.openflow.protocol.OFPacketInReason;
 import org.projectfloodlight.openflow.protocol.OFPortDesc;
@@ -375,6 +376,11 @@
         }
 
         @Override
+        public OFMeterFeatures getMeterFeatures() {
+            return null;
+        }
+
+        @Override
         public OFFactory factory() {
             return factory;
         }