[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/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);
+ }
+}