[ONOS-5599] Store meter features into MeterStore

Change-Id: I22f7366b87cad6fc706b6ff7b0ccff1a0e85ad6a
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;