[ONOS-3618] Implement REST API for Meter query, insert, delete

* Implement encode & decode method for MeterBandCodec & MeterCodec
* Implement MetersWebResource
* Add unit test for MeterBandCodec & MeterCodec
* Add unit test for MetersWebResource
* Add meter insertion json example
* Add Swagger doc

Change-Id: I07284c6678c08b3cb9e109e86ffb2cf28bf36447
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
index d68b287..633a356 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
@@ -50,6 +50,8 @@
 import org.onosproject.net.intent.HostToHostIntent;
 import org.onosproject.net.intent.Intent;
 import org.onosproject.net.intent.PointToPointIntent;
+import org.onosproject.net.meter.Band;
+import org.onosproject.net.meter.Meter;
 import org.onosproject.net.statistic.Load;
 import org.onosproject.net.topology.Topology;
 import org.onosproject.net.topology.TopologyCluster;
@@ -102,6 +104,8 @@
         registerCodec(Driver.class, new DriverCodec());
         registerCodec(GroupBucket.class, new GroupBucketCodec());
         registerCodec(Load.class, new LoadCodec());
+        registerCodec(Meter.class, new MeterCodec());
+        registerCodec(Band.class, new MeterBandCodec());
         registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec());
         registerCodec(PortStatistics.class, new PortStatisticsCodec());
         registerCodec(Metric.class, new MetricCodec());
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
new file mode 100644
index 0000000..239082b
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/MeterBandCodec.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2014-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.codec.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.meter.Band;
+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";
+    private static final String RATE = "rate";
+    private static final String BURST_SIZE = "burstSize";
+    private static final String PREC = "prec";
+    private static final String PACKETS = "packets";
+    private static final String BYTES = "bytes";
+    private static final String MISSING_MEMBER_MESSAGE = " member is required in Band";
+
+    @Override
+    public ObjectNode encode(Band band, CodecContext context) {
+        checkNotNull(band, "Band cannot be null");
+
+        ObjectNode result = context.mapper().createObjectNode()
+                .put(TYPE, band.type().toString())
+                .put(RATE, band.rate())
+                .put(PACKETS, band.packets())
+                .put(BYTES, band.bytes())
+                .put(BURST_SIZE, band.burst());
+
+        if (band.dropPrecedence() != null) {
+            result.put(PREC, band.dropPrecedence());
+        }
+
+        return result;
+    }
+
+    @Override
+    public Band decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        // parse rate
+        long rate = nullIsIllegal(json.get(RATE), RATE + MISSING_MEMBER_MESSAGE).asLong();
+
+        // parse burst size
+        long burstSize = nullIsIllegal(json.get(BURST_SIZE), BURST_SIZE + MISSING_MEMBER_MESSAGE).asLong();
+
+        // parse precedence
+        Short precedence = null;
+
+        // parse band type
+        String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
+        Band.Type type;
+        switch (typeStr) {
+            case "DROP":
+                type = Band.Type.DROP;
+                break;
+            case "REMARK":
+                type = Band.Type.REMARK;
+                precedence = (short) nullIsIllegal(json.get(PREC), PREC + MISSING_MEMBER_MESSAGE).asInt();
+                break;
+            default:
+                log.warn("The requested type {} is not defined for band.", typeStr);
+                return null;
+        }
+
+        Band band = DefaultBand.builder()
+                .ofType(type)
+                .burstSize(burstSize)
+                .withRate(rate)
+                .dropPrecedence(precedence)
+                .build();
+
+        return 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
new file mode 100644
index 0000000..468d237
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/MeterCodec.java
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2014-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.codec.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.meter.Band;
+import org.onosproject.net.meter.DefaultMeter;
+import org.onosproject.net.meter.Meter;
+import org.onosproject.net.meter.MeterId;
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+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";
+    private static final String STATE = "state";
+    private static final String LIFE = "life";
+    private static final String PACKETS = "packets";
+    private static final String BYTES = "bytes";
+    private static final String REFERENCE_COUNT = "referenceCount";
+    private static final String APP_ID = "appId";
+    private static final String BURST = "burst";
+    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 MISSING_MEMBER_MESSAGE = " member is required in Meter";
+
+    @Override
+    public ObjectNode encode(Meter meter, CodecContext context) {
+        checkNotNull(meter, "Meter cannot be null");
+        ObjectNode result = context.mapper().createObjectNode()
+                .put(ID, meter.id().toString())
+                .put(LIFE, meter.life())
+                .put(PACKETS, meter.packetsSeen())
+                .put(BYTES, meter.bytesSeen())
+                .put(REFERENCE_COUNT, meter.referenceCount())
+                .put(UNIT, meter.unit().toString())
+                .put(BURST, meter.isBurst())
+                .put(DEVICE_ID, meter.deviceId().toString());
+
+        if (meter.appId() != null) {
+            result.put(APP_ID, meter.appId().toString());
+        }
+
+        if (meter.state() != null) {
+            result.put(STATE, meter.state().toString());
+        }
+
+        ArrayNode bands = context.mapper().createArrayNode();
+        meter.bands().forEach(band -> {
+            ObjectNode bandJson = context.codec(Band.class).encode(band, context);
+            bands.add(bandJson);
+        });
+        result.set(BANDS, bands);
+        return result;
+    }
+
+    @Override
+    public Meter decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        final JsonCodec<Band> meterBandCodec = context.codec(Band.class);
+        CoreService coreService = context.getService(CoreService.class);
+
+        // parse meter id
+        int meterIdInt = nullIsIllegal(json.get(ID), ID + MISSING_MEMBER_MESSAGE).asInt();
+        MeterId meterId = MeterId.meterId(meterIdInt);
+
+        // 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);
+
+        // parse burst
+        boolean burst = false;
+        JsonNode burstJson = json.get("burst");
+        if (burstJson != null) {
+            burst = burstJson.asBoolean();
+        }
+
+        // parse unit type
+        String unit = nullIsIllegal(json.get(UNIT), UNIT + MISSING_MEMBER_MESSAGE).asText();
+        Meter.Unit meterUnit;
+
+        switch (unit) {
+            case "KB_PER_SEC":
+                meterUnit = Meter.Unit.KB_PER_SEC;
+                break;
+            case "PKTS_PER_SEC":
+                meterUnit = Meter.Unit.PKTS_PER_SEC;
+                break;
+            default:
+                log.warn("The requested unit {} is not defined for meter.", unit);
+                return null;
+        }
+
+        // parse meter bands
+        List<Band> bandList = new ArrayList<>();
+        JsonNode bandsJson = json.get(BANDS);
+        checkNotNull(bandsJson);
+        if (bandsJson != null) {
+            IntStream.range(0, bandsJson.size()).forEach(i -> {
+                ObjectNode bandJson = get(bandsJson, i);
+                bandList.add(meterBandCodec.decode(bandJson, context));
+            });
+        }
+
+        Meter meter;
+        if (burst) {
+            meter = DefaultMeter.builder()
+                    .withId(meterId)
+                    .fromApp(appId)
+                    .forDevice(deviceId)
+                    .withUnit(meterUnit)
+                    .withBands(bandList)
+                    .burst().build();
+        } else {
+            meter = DefaultMeter.builder()
+                    .withId(meterId)
+                    .fromApp(appId)
+                    .forDevice(deviceId)
+                    .withUnit(meterUnit)
+                    .withBands(bandList).build();
+        }
+
+        return meter;
+    }
+}
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/MeterBandJsonMatcher.java b/core/common/src/test/java/org/onosproject/codec/impl/MeterBandJsonMatcher.java
new file mode 100644
index 0000000..2f08a5c
--- /dev/null
+++ b/core/common/src/test/java/org/onosproject/codec/impl/MeterBandJsonMatcher.java
@@ -0,0 +1,107 @@
+/*
+ * 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.codec.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.net.meter.Band;
+
+/**
+ * Hamcrest matcher for bands.
+ */
+public final class MeterBandJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+    private final Band band;
+
+    private MeterBandJsonMatcher(Band band) {
+        this.band = band;
+    }
+
+    /**
+     * Matches the contents of a meter band.
+     *
+     * @param bandJson JSON representation of band to match
+     * @param description Description object used for recording errors
+     * @return true if contents match, false otherwise
+     */
+    @Override
+    protected boolean matchesSafely(JsonNode bandJson, Description description) {
+        // check type
+        final String jsonType = bandJson.get("type").textValue();
+        if (!band.type().name().equals(jsonType)) {
+            description.appendText("type was " + jsonType);
+            return false;
+        }
+
+        // check rate
+        final long jsonRate = bandJson.get("rate").longValue();
+        if (band.rate() != jsonRate) {
+            description.appendText("rate was " + jsonRate);
+            return false;
+        }
+
+        // check burst size
+        final long jsonBurstSize = bandJson.get("burstSize").longValue();
+        if (band.burst() != jsonBurstSize) {
+            description.appendText("burst size was " + jsonBurstSize);
+            return false;
+        }
+
+        // check precedence
+        final JsonNode jsonNodePrec = bandJson.get("prec");
+        if (jsonNodePrec != null) {
+            if (band.dropPrecedence() != jsonNodePrec.shortValue()) {
+                description.appendText("drop precedence was " + jsonNodePrec.shortValue());
+                return false;
+            }
+        }
+
+        // check packets
+        final JsonNode jsonNodePackets = bandJson.get("packets");
+        if (jsonNodePackets != null) {
+            if (band.packets() != jsonNodePackets.asLong()) {
+                description.appendText("packets was " + jsonNodePackets.asLong());
+                return false;
+            }
+        }
+
+        final JsonNode jsonNodeBytes = bandJson.get("bytes");
+        if (jsonNodeBytes != null) {
+            if (band.bytes() != jsonNodeBytes.asLong()) {
+                description.appendText("bytes was " + jsonNodeBytes.asLong());
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(band.toString());
+    }
+
+    /**
+     * Factory to allocate a band matcher.
+     *
+     * @param band band object we are looking for
+     * @return matcher
+     */
+    public static MeterBandJsonMatcher matchesMeterBand(Band band) {
+        return new MeterBandJsonMatcher(band);
+    }
+}
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/MeterCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/MeterCodecTest.java
new file mode 100644
index 0000000..dcabcc4
--- /dev/null
+++ b/core/common/src/test/java/org/onosproject/codec/impl/MeterCodecTest.java
@@ -0,0 +1,138 @@
+/*
+ * 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.codec.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.NetTestTools;
+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.MeterId;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.codec.impl.MeterJsonMatcher.matchesMeter;
+import static org.onosproject.net.NetTestTools.APP_ID;
+
+/**
+ * Unit tests for Meter codec.
+ */
+public class MeterCodecTest {
+
+    MockCodecContext context;
+    JsonCodec<Meter> meterCodec;
+    final CoreService mockCoreService = createMock(CoreService.class);
+
+    /**
+     * Sets up for each test.  Creates a context and fetches the flow rule
+     * codec.
+     */
+    @Before
+    public void setUp() {
+        context = new MockCodecContext();
+        meterCodec = context.codec(Meter.class);
+        assertThat(meterCodec, notNullValue());
+
+        expect(mockCoreService.registerApplication(MeterCodec.REST_APP_ID))
+                .andReturn(APP_ID).anyTimes();
+        replay(mockCoreService);
+        context.registerService(CoreService.class, mockCoreService);
+    }
+
+    /**
+     * Tests encoding of a Meter object.
+     */
+    @Test
+    public void testMeterEncode() {
+        Band band1 = DefaultBand.builder()
+                        .ofType(Band.Type.DROP)
+                        .burstSize(10)
+                        .withRate(10).build();
+        Band band2 = DefaultBand.builder()
+                        .ofType(Band.Type.REMARK)
+                        .burstSize(10)
+                        .withRate(10)
+                        .dropPrecedence((short) 10).build();
+
+        Meter meter = DefaultMeter.builder()
+                        .fromApp(APP_ID)
+                        .withId(MeterId.meterId(1L))
+                        .forDevice(NetTestTools.did("d1"))
+                        .withBands(ImmutableList.of(band1, band2))
+                        .withUnit(Meter.Unit.KB_PER_SEC).build();
+
+        ObjectNode meterJson = meterCodec.encode(meter, context);
+        assertThat(meterJson, matchesMeter(meter));
+    }
+
+    /**
+     * Test decoding of a Meter object.
+     */
+    @Test
+    public void testMeterDecode() throws IOException  {
+        Meter meter = getMeter("simple-meter.json");
+        checkCommonData(meter);
+
+        assertThat(meter.bands().size(), is(1));
+        Band band = meter.bands().iterator().next();
+        assertThat(band.type().toString(), is("REMARK"));
+        assertThat(band.rate(), is(10L));
+        assertThat(band.dropPrecedence(), is((short) 20));
+        assertThat(band.burst(), is(30L));
+    }
+
+    /**
+     * Checks that the data shared by all the resource is correct for a given meter.
+     *
+     * @param meter meter to check
+     */
+    private void checkCommonData(Meter meter) {
+        assertThat(meter.id().id(), is(1L));
+        assertThat(meter.deviceId().toString(), is("of:0000000000000001"));
+        assertThat(meter.appId(), is(APP_ID));
+        assertThat(meter.unit().toString(), is("KB_PER_SEC"));
+    }
+
+    /**
+     * Reads in a meter from the given resource and decodes it.
+     *
+     * @param resourceName resource to use to read the JSON for the rule
+     * @return decoded meter
+     * @throws IOException if processing the resource fails
+     */
+    private Meter getMeter(String resourceName) throws IOException {
+        InputStream jsonStream = MeterCodecTest.class.getResourceAsStream(resourceName);
+        JsonNode json = context.mapper().readTree(jsonStream);
+        assertThat(json, notNullValue());
+        Meter meter = meterCodec.decode((ObjectNode) json, context);
+        assertThat(meter, notNullValue());
+        return meter;
+    }
+}
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/MeterJsonMatcher.java b/core/common/src/test/java/org/onosproject/codec/impl/MeterJsonMatcher.java
new file mode 100644
index 0000000..98f7cbc
--- /dev/null
+++ b/core/common/src/test/java/org/onosproject/codec/impl/MeterJsonMatcher.java
@@ -0,0 +1,141 @@
+/*
+ * 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.codec.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.net.meter.Band;
+import org.onosproject.net.meter.Meter;
+
+/**
+ * Hamcrest matcher for meters.
+ */
+public final class MeterJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+    private final Meter meter;
+
+    private MeterJsonMatcher(Meter meter) {
+        this.meter = meter;
+    }
+
+    @Override
+    protected boolean matchesSafely(JsonNode jsonMeter, Description description) {
+        // check id
+        String jsonMeterId = jsonMeter.get("id").asText();
+        String meterId = meter.id().toString();
+        if (!jsonMeterId.equals(meterId)) {
+            description.appendText("meter id was " + jsonMeterId);
+            return false;
+        }
+
+        // check unit
+        String jsonUnit = jsonMeter.get("unit").asText();
+        String unit = meter.unit().toString();
+        if (!jsonUnit.equals(unit)) {
+            description.appendText("unit was " + jsonUnit);
+            return false;
+        }
+
+        // check burst
+        boolean jsonBurst = jsonMeter.get("burst").asBoolean();
+        boolean burst = meter.isBurst();
+        if (jsonBurst != burst) {
+            description.appendText("isBurst was " + jsonBurst);
+            return false;
+        }
+
+        // check state
+        JsonNode jsonNodeState = jsonMeter.get("state");
+        if (jsonNodeState != null) {
+            String state = meter.state().toString();
+            if (!jsonNodeState.asText().equals(state)) {
+                description.appendText("state was " + jsonNodeState.asText());
+                return false;
+            }
+        }
+
+        // check life
+        JsonNode jsonNodeLife = jsonMeter.get("life");
+        if (jsonNodeLife != null) {
+            long life = meter.life();
+            if (jsonNodeLife.asLong() != life) {
+                description.appendText("life was " + jsonNodeLife.asLong());
+                return false;
+            }
+        }
+
+        // check bytes
+        JsonNode jsonNodeBytes = jsonMeter.get("bytes");
+        if (jsonNodeBytes != null) {
+            long bytes = meter.bytesSeen();
+            if (jsonNodeBytes.asLong() != bytes) {
+                description.appendText("bytes was " + jsonNodeBytes.asLong());
+                return false;
+            }
+        }
+
+        // check packets
+        JsonNode jsonNodePackets = jsonMeter.get("packets");
+        if (jsonNodePackets != null) {
+            long packets = meter.packetsSeen();
+            if (jsonNodePackets.asLong() != packets) {
+                description.appendText("packets was " + jsonNodePackets.asLong());
+                return false;
+            }
+        }
+
+        // check size of band array
+        JsonNode jsonBands = jsonMeter.get("bands");
+        if (jsonBands.size() != meter.bands().size()) {
+            description.appendText("bands size was " + jsonBands.size());
+            return false;
+        }
+
+        // check bands
+        for (Band band : meter.bands()) {
+            boolean bandFound = false;
+            for (int bandIndex = 0; bandIndex < jsonBands.size(); bandIndex++) {
+                MeterBandJsonMatcher bandMatcher = MeterBandJsonMatcher.matchesMeterBand(band);
+                if (bandMatcher.matches(jsonBands.get(bandIndex))) {
+                    bandFound = true;
+                    break;
+                }
+            }
+            if (!bandFound) {
+                description.appendText("band not found " + band.toString());
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(meter.toString());
+    }
+
+    /**
+     * Factory to allocate a meter matcher.
+     *
+     * @param meter meter object we are looking for
+     * @return matcher
+     */
+    public static MeterJsonMatcher matchesMeter(Meter meter) {
+        return new MeterJsonMatcher(meter);
+    }
+}
diff --git a/core/common/src/test/resources/org/onosproject/codec/impl/simple-meter.json b/core/common/src/test/resources/org/onosproject/codec/impl/simple-meter.json
new file mode 100644
index 0000000..21eb38a
--- /dev/null
+++ b/core/common/src/test/resources/org/onosproject/codec/impl/simple-meter.json
@@ -0,0 +1,14 @@
+{
+  "id": 1,
+  "deviceId": "of:0000000000000001",
+  "unit": "KB_PER_SEC",
+  "burst": true,
+  "bands": [
+    {
+      "type": "REMARK",
+      "rate": 10,
+      "prec": 20,
+      "burstSize": 30
+    }
+  ]
+}
\ No newline at end of file