Remove redundant meter id when create a meter entry in REST API
Change-Id: Iec38ea9a612878a2a6f2766c154c8ed8a8b31ef2
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 7c19202..99030ca 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
@@ -55,6 +55,7 @@
import org.onosproject.net.intent.PointToPointIntent;
import org.onosproject.net.meter.Band;
import org.onosproject.net.meter.Meter;
+import org.onosproject.net.meter.MeterRequest;
import org.onosproject.net.statistic.Load;
import org.onosproject.net.topology.Topology;
import org.onosproject.net.topology.TopologyCluster;
@@ -107,6 +108,7 @@
registerCodec(Driver.class, new DriverCodec());
registerCodec(GroupBucket.class, new GroupBucketCodec());
registerCodec(Load.class, new LoadCodec());
+ registerCodec(MeterRequest.class, new MeterRequestCodec());
registerCodec(Meter.class, new MeterCodec());
registerCodec(Band.class, new MeterBandCodec());
registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec());
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 468d237..a7879e5 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
@@ -15,26 +15,15 @@
*/
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;
@@ -57,7 +46,6 @@
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) {
@@ -88,79 +76,4 @@
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/main/java/org/onosproject/codec/impl/MeterRequestCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/MeterRequestCodec.java
new file mode 100644
index 0000000..1517acb
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/MeterRequestCodec.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2016 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 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.DefaultMeterRequest;
+import org.onosproject.net.meter.Meter;
+import org.onosproject.net.meter.MeterRequest;
+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;
+
+/**
+ * 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 MISSING_MEMBER_MESSAGE = " member is required in MeterRequest";
+
+ @Override
+ public MeterRequest 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 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));
+ });
+ }
+
+ 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();
+ }
+
+ return meterRequest;
+ }
+}
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
index 476785d..0d1ac59 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/MeterCodecTest.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/MeterCodecTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 Open Networking Laboratory
+ * Copyright 2015-2016 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.
@@ -15,7 +15,6 @@
*/
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;
@@ -29,14 +28,10 @@
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;
@@ -91,48 +86,4 @@
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/MeterRequestCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/MeterRequestCodecTest.java
new file mode 100644
index 0000000..94e3b03
--- /dev/null
+++ b/core/common/src/test/java/org/onosproject/codec/impl/MeterRequestCodecTest.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2016 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 org.junit.Before;
+import org.junit.Test;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.meter.Band;
+import org.onosproject.net.meter.MeterRequest;
+
+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.net.NetTestTools.APP_ID;
+
+/**
+ * Unit tests for MeterRequest codec.
+ */
+public class MeterRequestCodecTest {
+ MockCodecContext context;
+ JsonCodec<MeterRequest> meterRequestCodec;
+ final CoreService mockCoreService = createMock(CoreService.class);
+
+ /**
+ * Sets up for each test. Creates a context and fetches the meterRequest
+ * codec.
+ */
+ @Before
+ public void setUp() {
+ context = new MockCodecContext();
+ meterRequestCodec = context.codec(MeterRequest.class);
+ assertThat(meterRequestCodec, notNullValue());
+
+ expect(mockCoreService.registerApplication(MeterCodec.REST_APP_ID))
+ .andReturn(APP_ID).anyTimes();
+ replay(mockCoreService);
+ context.registerService(CoreService.class, mockCoreService);
+ }
+
+ /**
+ * Test decoding of a MeterRequest object.
+ */
+ @Test
+ public void testMeterRequestDecode() throws IOException {
+ MeterRequest meterRequest = getMeterRequest("simple-meter-request.json");
+ checkCommonData(meterRequest);
+
+ assertThat(meterRequest.bands().size(), is(1));
+ Band band = meterRequest.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 meterRequest.
+ *
+ * @param meterRequest meterRequest to check
+ */
+ private void checkCommonData(MeterRequest meterRequest) {
+ assertThat(meterRequest.deviceId().toString(), is("of:0000000000000001"));
+ assertThat(meterRequest.appId(), is(APP_ID));
+ assertThat(meterRequest.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 meterRequest
+ * @throws IOException if processing the resource fails
+ */
+ private MeterRequest getMeterRequest(String resourceName) throws IOException {
+ InputStream jsonStream = MeterRequestCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ assertThat(json, notNullValue());
+ MeterRequest meterRequest = meterRequestCodec.decode((ObjectNode) json, context);
+ assertThat(meterRequest, notNullValue());
+ return meterRequest;
+ }
+}
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-request.json
similarity index 94%
rename from core/common/src/test/resources/org/onosproject/codec/impl/simple-meter.json
rename to core/common/src/test/resources/org/onosproject/codec/impl/simple-meter-request.json
index 21eb38a..9ef6b4d 100644
--- 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-request.json
@@ -1,5 +1,4 @@
{
- "id": 1,
"deviceId": "of:0000000000000001",
"unit": "KB_PER_SEC",
"burst": true,