[SDFAB-500][SDFAB-527] Meters cleanup and leftovers

- Improve ONOS cli enabling CRUD of p4rt trtcm
- Improve ONOS rest enabling CRUD of p4rt trtcm
- Improve MeterService with scope defined reads and integrate in cli/rest
- Add support along the stack for BYTE_PER_SEC unit
- Add support along the stack for COMMITTED and PEAK bands
- Fix several bugs in ONOS cli/rest interfaces
- Improve REST codecs
- Fix NPE in MeterDriverProvider
- Improve PiMeterTransalation by enforcing trtcm config
- Implement explicit translation of the bands
- Fix ONOS reconciliation by removing from the mirror the wrong configs
- Remove unnecessary checks in MeterEntryCodec
- Update unit tests

It will follow a 2nd patch to complete SDFAB-527

Change-Id: I855235b17f60cb1d39f5b9a042c1015105a8a269
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/MeterBandCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/MeterBandCodec.java
index 4621e94..3cb68a0 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/MeterBandCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/MeterBandCodec.java
@@ -21,17 +21,14 @@
 import org.onosproject.net.meter.Band;
 import org.onosproject.net.meter.Band.Builder;
 import org.onosproject.net.meter.DefaultBand;
-import org.slf4j.Logger;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.onlab.util.Tools.nullIsIllegal;
-import static org.slf4j.LoggerFactory.getLogger;
 
 /**
  * Meter band JSON codec.
  */
 public final class MeterBandCodec extends JsonCodec<Band> {
-    private final Logger log = getLogger(getClass());
 
     // JSON field names
     private static final String TYPE = "type";
@@ -93,6 +90,18 @@
                 builder.ofType(type);
                 builder.dropPrecedence(precedence);
                 break;
+            case "NONE":
+                type = Band.Type.NONE;
+                builder.ofType(type);
+                break;
+            case "MARK_YELLOW":
+                type = Band.Type.MARK_YELLOW;
+                builder.ofType(type);
+                break;
+            case "MARK_RED":
+                type = Band.Type.MARK_RED;
+                builder.ofType(type);
+                break;
             default:
                 nullIsIllegal(type, "The requested type " + typeStr + " is not defined for band.");
         }
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/MeterCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/MeterCodec.java
index 5cf1d13..26b6677 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/MeterCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/MeterCodec.java
@@ -21,17 +21,14 @@
 import org.onosproject.codec.JsonCodec;
 import org.onosproject.net.meter.Band;
 import org.onosproject.net.meter.Meter;
-import org.slf4j.Logger;
 
 import static com.google.common.base.Preconditions.checkNotNull;
-import static org.slf4j.LoggerFactory.getLogger;
 
 
 /**
  * Meter JSON codec.
  */
 public final class MeterCodec extends JsonCodec<Meter> {
-    private final Logger log = getLogger(getClass());
 
     // JSON field names
     private static final String ID = "id";
@@ -51,7 +48,7 @@
     public ObjectNode encode(Meter meter, CodecContext context) {
         checkNotNull(meter, "Meter cannot be null");
         ObjectNode result = context.mapper().createObjectNode()
-                .put(ID, meter.id().toString())
+                .put(ID, meter.meterCellId().toString())
                 .put(LIFE, meter.life())
                 .put(PACKETS, meter.packetsSeen())
                 .put(BYTES, meter.bytesSeen())
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/MeterRequestCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/MeterRequestCodec.java
index d254428..d46e257 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/MeterRequestCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/MeterRequestCodec.java
@@ -26,29 +26,32 @@
 import org.onosproject.net.meter.DefaultMeterRequest;
 import org.onosproject.net.meter.Meter;
 import org.onosproject.net.meter.MeterRequest;
-import org.slf4j.Logger;
+import org.onosproject.net.meter.MeterScope;
 
 import java.util.ArrayList;
 import java.util.List;
 import java.util.stream.IntStream;
 
 import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Strings.isNullOrEmpty;
 import static org.onlab.util.Tools.nullIsIllegal;
-import static org.slf4j.LoggerFactory.getLogger;
 
 /**
  * MeterRequest JSON codec.
  */
 public final class MeterRequestCodec extends JsonCodec<MeterRequest> {
-    private final Logger log = getLogger(getClass());
 
     // JSON field names
     private static final String DEVICE_ID = "deviceId";
     private static final String UNIT = "unit";
     private static final String BANDS = "bands";
-    public static final String REST_APP_ID = "org.onosproject.rest";
+    private static final String SCOPE = "scope";
+    private static final String INDEX = "index";
+    private static final String REST_APP_ID = "org.onosproject.rest";
     private static final String MISSING_MEMBER_MESSAGE = " member is required in MeterRequest";
 
+    private ApplicationId applicationId;
+
     @Override
     public MeterRequest decode(ObjectNode json, CodecContext context) {
         if (json == null || !json.isObject()) {
@@ -56,14 +59,17 @@
         }
 
         final JsonCodec<Band> meterBandCodec = context.codec(Band.class);
-        CoreService coreService = context.getService(CoreService.class);
+
 
         // parse device id
         DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
                 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
 
         // application id
-        ApplicationId appId = coreService.registerApplication(REST_APP_ID);
+        if (applicationId == null) {
+            CoreService coreService = context.getService(CoreService.class);
+            applicationId = coreService.registerApplication(REST_APP_ID);
+        }
 
         // parse burst
         boolean burst = false;
@@ -83,6 +89,9 @@
             case "PKTS_PER_SEC":
                 meterUnit = Meter.Unit.PKTS_PER_SEC;
                 break;
+            case "BYTES_PER_SEC":
+                meterUnit = Meter.Unit.BYTES_PER_SEC;
+                break;
             default:
                 nullIsIllegal(meterUnit, "The requested unit " + unit + " is not defined for meter.");
         }
@@ -98,22 +107,39 @@
             });
         }
 
-        MeterRequest meterRequest;
-        if (burst) {
-            meterRequest = DefaultMeterRequest.builder()
-                    .fromApp(appId)
-                    .forDevice(deviceId)
-                    .withUnit(meterUnit)
-                    .withBands(bandList)
-                    .burst().add();
-        } else {
-            meterRequest = DefaultMeterRequest.builder()
-                    .fromApp(appId)
-                    .forDevice(deviceId)
-                    .withUnit(meterUnit)
-                    .withBands(bandList).add();
+        // parse scope and index
+        JsonNode scopeJson = json.get(SCOPE);
+        MeterScope scope = null;
+        if (scopeJson != null && !isNullOrEmpty(scopeJson.asText())) {
+            scope = MeterScope.of(scopeJson.asText());
         }
 
-        return meterRequest;
+        JsonNode indexJson = json.get(INDEX);
+        Long index = null;
+        if (indexJson != null && !isNullOrEmpty(indexJson.asText()) && scope != null) {
+            index = indexJson.asLong();
+        }
+
+        // build the final request
+        MeterRequest.Builder meterRequest = DefaultMeterRequest.builder();
+        if (scope != null) {
+            meterRequest.withScope(scope);
+        }
+
+        if (index != null) {
+            meterRequest.withIndex(index);
+        }
+
+        meterRequest.fromApp(applicationId)
+                .forDevice(deviceId)
+                .withUnit(meterUnit)
+                .withBands(bandList);
+
+        if (burst) {
+            meterRequest.burst();
+        }
+
+        return meterRequest.add();
     }
+
 }