Jian Li
Committed by Gerrit Code Review

Remove redundant meter id when create a meter entry in REST API

Change-Id: Iec38ea9a612878a2a6f2766c154c8ed8a8b31ef2
...@@ -55,6 +55,7 @@ import org.onosproject.net.intent.Intent; ...@@ -55,6 +55,7 @@ import org.onosproject.net.intent.Intent;
55 import org.onosproject.net.intent.PointToPointIntent; 55 import org.onosproject.net.intent.PointToPointIntent;
56 import org.onosproject.net.meter.Band; 56 import org.onosproject.net.meter.Band;
57 import org.onosproject.net.meter.Meter; 57 import org.onosproject.net.meter.Meter;
58 +import org.onosproject.net.meter.MeterRequest;
58 import org.onosproject.net.statistic.Load; 59 import org.onosproject.net.statistic.Load;
59 import org.onosproject.net.topology.Topology; 60 import org.onosproject.net.topology.Topology;
60 import org.onosproject.net.topology.TopologyCluster; 61 import org.onosproject.net.topology.TopologyCluster;
...@@ -107,6 +108,7 @@ public class CodecManager implements CodecService { ...@@ -107,6 +108,7 @@ public class CodecManager implements CodecService {
107 registerCodec(Driver.class, new DriverCodec()); 108 registerCodec(Driver.class, new DriverCodec());
108 registerCodec(GroupBucket.class, new GroupBucketCodec()); 109 registerCodec(GroupBucket.class, new GroupBucketCodec());
109 registerCodec(Load.class, new LoadCodec()); 110 registerCodec(Load.class, new LoadCodec());
111 + registerCodec(MeterRequest.class, new MeterRequestCodec());
110 registerCodec(Meter.class, new MeterCodec()); 112 registerCodec(Meter.class, new MeterCodec());
111 registerCodec(Band.class, new MeterBandCodec()); 113 registerCodec(Band.class, new MeterBandCodec());
112 registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec()); 114 registerCodec(TableStatisticsEntry.class, new TableStatisticsEntryCodec());
......
...@@ -15,26 +15,15 @@ ...@@ -15,26 +15,15 @@
15 */ 15 */
16 package org.onosproject.codec.impl; 16 package org.onosproject.codec.impl;
17 17
18 -import com.fasterxml.jackson.databind.JsonNode;
19 import com.fasterxml.jackson.databind.node.ArrayNode; 18 import com.fasterxml.jackson.databind.node.ArrayNode;
20 import com.fasterxml.jackson.databind.node.ObjectNode; 19 import com.fasterxml.jackson.databind.node.ObjectNode;
21 import org.onosproject.codec.CodecContext; 20 import org.onosproject.codec.CodecContext;
22 import org.onosproject.codec.JsonCodec; 21 import org.onosproject.codec.JsonCodec;
23 -import org.onosproject.core.ApplicationId;
24 -import org.onosproject.core.CoreService;
25 -import org.onosproject.net.DeviceId;
26 import org.onosproject.net.meter.Band; 22 import org.onosproject.net.meter.Band;
27 -import org.onosproject.net.meter.DefaultMeter;
28 import org.onosproject.net.meter.Meter; 23 import org.onosproject.net.meter.Meter;
29 -import org.onosproject.net.meter.MeterId;
30 import org.slf4j.Logger; 24 import org.slf4j.Logger;
31 25
32 -import java.util.ArrayList;
33 -import java.util.List;
34 -import java.util.stream.IntStream;
35 -
36 import static com.google.common.base.Preconditions.checkNotNull; 26 import static com.google.common.base.Preconditions.checkNotNull;
37 -import static org.onlab.util.Tools.nullIsIllegal;
38 import static org.slf4j.LoggerFactory.getLogger; 27 import static org.slf4j.LoggerFactory.getLogger;
39 28
40 29
...@@ -57,7 +46,6 @@ public final class MeterCodec extends JsonCodec<Meter> { ...@@ -57,7 +46,6 @@ public final class MeterCodec extends JsonCodec<Meter> {
57 private static final String UNIT = "unit"; 46 private static final String UNIT = "unit";
58 private static final String BANDS = "bands"; 47 private static final String BANDS = "bands";
59 public static final String REST_APP_ID = "org.onosproject.rest"; 48 public static final String REST_APP_ID = "org.onosproject.rest";
60 - private static final String MISSING_MEMBER_MESSAGE = " member is required in Meter";
61 49
62 @Override 50 @Override
63 public ObjectNode encode(Meter meter, CodecContext context) { 51 public ObjectNode encode(Meter meter, CodecContext context) {
...@@ -88,79 +76,4 @@ public final class MeterCodec extends JsonCodec<Meter> { ...@@ -88,79 +76,4 @@ public final class MeterCodec extends JsonCodec<Meter> {
88 result.set(BANDS, bands); 76 result.set(BANDS, bands);
89 return result; 77 return result;
90 } 78 }
91 -
92 - @Override
93 - public Meter decode(ObjectNode json, CodecContext context) {
94 - if (json == null || !json.isObject()) {
95 - return null;
96 - }
97 -
98 - final JsonCodec<Band> meterBandCodec = context.codec(Band.class);
99 - CoreService coreService = context.getService(CoreService.class);
100 -
101 - // parse meter id
102 - int meterIdInt = nullIsIllegal(json.get(ID), ID + MISSING_MEMBER_MESSAGE).asInt();
103 - MeterId meterId = MeterId.meterId(meterIdInt);
104 -
105 - // parse device id
106 - DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
107 - DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
108 -
109 - // application id
110 - ApplicationId appId = coreService.registerApplication(REST_APP_ID);
111 -
112 - // parse burst
113 - boolean burst = false;
114 - JsonNode burstJson = json.get("burst");
115 - if (burstJson != null) {
116 - burst = burstJson.asBoolean();
117 - }
118 -
119 - // parse unit type
120 - String unit = nullIsIllegal(json.get(UNIT), UNIT + MISSING_MEMBER_MESSAGE).asText();
121 - Meter.Unit meterUnit;
122 -
123 - switch (unit) {
124 - case "KB_PER_SEC":
125 - meterUnit = Meter.Unit.KB_PER_SEC;
126 - break;
127 - case "PKTS_PER_SEC":
128 - meterUnit = Meter.Unit.PKTS_PER_SEC;
129 - break;
130 - default:
131 - log.warn("The requested unit {} is not defined for meter.", unit);
132 - return null;
133 - }
134 -
135 - // parse meter bands
136 - List<Band> bandList = new ArrayList<>();
137 - JsonNode bandsJson = json.get(BANDS);
138 - checkNotNull(bandsJson);
139 - if (bandsJson != null) {
140 - IntStream.range(0, bandsJson.size()).forEach(i -> {
141 - ObjectNode bandJson = get(bandsJson, i);
142 - bandList.add(meterBandCodec.decode(bandJson, context));
143 - });
144 - }
145 -
146 - Meter meter;
147 - if (burst) {
148 - meter = DefaultMeter.builder()
149 - .withId(meterId)
150 - .fromApp(appId)
151 - .forDevice(deviceId)
152 - .withUnit(meterUnit)
153 - .withBands(bandList)
154 - .burst().build();
155 - } else {
156 - meter = DefaultMeter.builder()
157 - .withId(meterId)
158 - .fromApp(appId)
159 - .forDevice(deviceId)
160 - .withUnit(meterUnit)
161 - .withBands(bandList).build();
162 - }
163 -
164 - return meter;
165 - }
166 } 79 }
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.codec.impl;
17 +
18 +import com.fasterxml.jackson.databind.JsonNode;
19 +import com.fasterxml.jackson.databind.node.ObjectNode;
20 +import org.onosproject.codec.CodecContext;
21 +import org.onosproject.codec.JsonCodec;
22 +import org.onosproject.core.ApplicationId;
23 +import org.onosproject.core.CoreService;
24 +import org.onosproject.net.DeviceId;
25 +import org.onosproject.net.meter.Band;
26 +import org.onosproject.net.meter.DefaultMeterRequest;
27 +import org.onosproject.net.meter.Meter;
28 +import org.onosproject.net.meter.MeterRequest;
29 +import org.slf4j.Logger;
30 +
31 +import java.util.ArrayList;
32 +import java.util.List;
33 +import java.util.stream.IntStream;
34 +
35 +import static com.google.common.base.Preconditions.checkNotNull;
36 +import static org.onlab.util.Tools.nullIsIllegal;
37 +import static org.slf4j.LoggerFactory.getLogger;
38 +
39 +/**
40 + * MeterRequest JSON codec.
41 + */
42 +public final class MeterRequestCodec extends JsonCodec<MeterRequest> {
43 + private final Logger log = getLogger(getClass());
44 +
45 + // JSON field names
46 + private static final String DEVICE_ID = "deviceId";
47 + private static final String UNIT = "unit";
48 + private static final String BANDS = "bands";
49 + public static final String REST_APP_ID = "org.onosproject.rest";
50 + private static final String MISSING_MEMBER_MESSAGE = " member is required in MeterRequest";
51 +
52 + @Override
53 + public MeterRequest decode(ObjectNode json, CodecContext context) {
54 + if (json == null || !json.isObject()) {
55 + return null;
56 + }
57 +
58 + final JsonCodec<Band> meterBandCodec = context.codec(Band.class);
59 + CoreService coreService = context.getService(CoreService.class);
60 +
61 + // parse device id
62 + DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
63 + DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
64 +
65 + // application id
66 + ApplicationId appId = coreService.registerApplication(REST_APP_ID);
67 +
68 + // parse burst
69 + boolean burst = false;
70 + JsonNode burstJson = json.get("burst");
71 + if (burstJson != null) {
72 + burst = burstJson.asBoolean();
73 + }
74 +
75 + // parse unit type
76 + String unit = nullIsIllegal(json.get(UNIT), UNIT + MISSING_MEMBER_MESSAGE).asText();
77 + Meter.Unit meterUnit;
78 +
79 + switch (unit) {
80 + case "KB_PER_SEC":
81 + meterUnit = Meter.Unit.KB_PER_SEC;
82 + break;
83 + case "PKTS_PER_SEC":
84 + meterUnit = Meter.Unit.PKTS_PER_SEC;
85 + break;
86 + default:
87 + log.warn("The requested unit {} is not defined for meter.", unit);
88 + return null;
89 + }
90 +
91 + // parse meter bands
92 + List<Band> bandList = new ArrayList<>();
93 + JsonNode bandsJson = json.get(BANDS);
94 + checkNotNull(bandsJson);
95 + if (bandsJson != null) {
96 + IntStream.range(0, bandsJson.size()).forEach(i -> {
97 + ObjectNode bandJson = get(bandsJson, i);
98 + bandList.add(meterBandCodec.decode(bandJson, context));
99 + });
100 + }
101 +
102 + MeterRequest meterRequest;
103 + if (burst) {
104 + meterRequest = DefaultMeterRequest.builder()
105 + .fromApp(appId)
106 + .forDevice(deviceId)
107 + .withUnit(meterUnit)
108 + .withBands(bandList)
109 + .burst().add();
110 + } else {
111 + meterRequest = DefaultMeterRequest.builder()
112 + .fromApp(appId)
113 + .forDevice(deviceId)
114 + .withUnit(meterUnit)
115 + .withBands(bandList).add();
116 + }
117 +
118 + return meterRequest;
119 + }
120 +}
1 /* 1 /*
2 - * Copyright 2015 Open Networking Laboratory 2 + * Copyright 2015-2016 Open Networking Laboratory
3 * 3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License. 5 * you may not use this file except in compliance with the License.
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
15 */ 15 */
16 package org.onosproject.codec.impl; 16 package org.onosproject.codec.impl;
17 17
18 -import com.fasterxml.jackson.databind.JsonNode;
19 import com.fasterxml.jackson.databind.node.ObjectNode; 18 import com.fasterxml.jackson.databind.node.ObjectNode;
20 import com.google.common.collect.ImmutableList; 19 import com.google.common.collect.ImmutableList;
21 import org.junit.Before; 20 import org.junit.Before;
...@@ -29,14 +28,10 @@ import org.onosproject.net.meter.DefaultMeter; ...@@ -29,14 +28,10 @@ import org.onosproject.net.meter.DefaultMeter;
29 import org.onosproject.net.meter.Meter; 28 import org.onosproject.net.meter.Meter;
30 import org.onosproject.net.meter.MeterId; 29 import org.onosproject.net.meter.MeterId;
31 30
32 -import java.io.IOException;
33 -import java.io.InputStream;
34 -
35 import static org.easymock.EasyMock.createMock; 31 import static org.easymock.EasyMock.createMock;
36 import static org.easymock.EasyMock.expect; 32 import static org.easymock.EasyMock.expect;
37 import static org.easymock.EasyMock.replay; 33 import static org.easymock.EasyMock.replay;
38 import static org.hamcrest.MatcherAssert.assertThat; 34 import static org.hamcrest.MatcherAssert.assertThat;
39 -import static org.hamcrest.Matchers.is;
40 import static org.hamcrest.Matchers.notNullValue; 35 import static org.hamcrest.Matchers.notNullValue;
41 import static org.onosproject.codec.impl.MeterJsonMatcher.matchesMeter; 36 import static org.onosproject.codec.impl.MeterJsonMatcher.matchesMeter;
42 import static org.onosproject.net.NetTestTools.APP_ID; 37 import static org.onosproject.net.NetTestTools.APP_ID;
...@@ -91,48 +86,4 @@ public class MeterCodecTest { ...@@ -91,48 +86,4 @@ public class MeterCodecTest {
91 ObjectNode meterJson = meterCodec.encode(meter, context); 86 ObjectNode meterJson = meterCodec.encode(meter, context);
92 assertThat(meterJson, matchesMeter(meter)); 87 assertThat(meterJson, matchesMeter(meter));
93 } 88 }
94 -
95 - /**
96 - * Test decoding of a Meter object.
97 - */
98 - @Test
99 - public void testMeterDecode() throws IOException {
100 - Meter meter = getMeter("simple-meter.json");
101 - checkCommonData(meter);
102 -
103 - assertThat(meter.bands().size(), is(1));
104 - Band band = meter.bands().iterator().next();
105 - assertThat(band.type().toString(), is("REMARK"));
106 - assertThat(band.rate(), is(10L));
107 - assertThat(band.dropPrecedence(), is((short) 20));
108 - assertThat(band.burst(), is(30L));
109 - }
110 -
111 - /**
112 - * Checks that the data shared by all the resource is correct for a given meter.
113 - *
114 - * @param meter meter to check
115 - */
116 - private void checkCommonData(Meter meter) {
117 - assertThat(meter.id().id(), is(1L));
118 - assertThat(meter.deviceId().toString(), is("of:0000000000000001"));
119 - assertThat(meter.appId(), is(APP_ID));
120 - assertThat(meter.unit().toString(), is("KB_PER_SEC"));
121 - }
122 -
123 - /**
124 - * Reads in a meter from the given resource and decodes it.
125 - *
126 - * @param resourceName resource to use to read the JSON for the rule
127 - * @return decoded meter
128 - * @throws IOException if processing the resource fails
129 - */
130 - private Meter getMeter(String resourceName) throws IOException {
131 - InputStream jsonStream = MeterCodecTest.class.getResourceAsStream(resourceName);
132 - JsonNode json = context.mapper().readTree(jsonStream);
133 - assertThat(json, notNullValue());
134 - Meter meter = meterCodec.decode((ObjectNode) json, context);
135 - assertThat(meter, notNullValue());
136 - return meter;
137 - }
138 } 89 }
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.codec.impl;
17 +
18 +import com.fasterxml.jackson.databind.JsonNode;
19 +import com.fasterxml.jackson.databind.node.ObjectNode;
20 +import org.junit.Before;
21 +import org.junit.Test;
22 +import org.onosproject.codec.JsonCodec;
23 +import org.onosproject.core.CoreService;
24 +import org.onosproject.net.meter.Band;
25 +import org.onosproject.net.meter.MeterRequest;
26 +
27 +import java.io.IOException;
28 +import java.io.InputStream;
29 +
30 +import static org.easymock.EasyMock.createMock;
31 +import static org.easymock.EasyMock.expect;
32 +import static org.easymock.EasyMock.replay;
33 +import static org.hamcrest.MatcherAssert.assertThat;
34 +import static org.hamcrest.Matchers.is;
35 +import static org.hamcrest.Matchers.notNullValue;
36 +import static org.onosproject.net.NetTestTools.APP_ID;
37 +
38 +/**
39 + * Unit tests for MeterRequest codec.
40 + */
41 +public class MeterRequestCodecTest {
42 + MockCodecContext context;
43 + JsonCodec<MeterRequest> meterRequestCodec;
44 + final CoreService mockCoreService = createMock(CoreService.class);
45 +
46 + /**
47 + * Sets up for each test. Creates a context and fetches the meterRequest
48 + * codec.
49 + */
50 + @Before
51 + public void setUp() {
52 + context = new MockCodecContext();
53 + meterRequestCodec = context.codec(MeterRequest.class);
54 + assertThat(meterRequestCodec, notNullValue());
55 +
56 + expect(mockCoreService.registerApplication(MeterCodec.REST_APP_ID))
57 + .andReturn(APP_ID).anyTimes();
58 + replay(mockCoreService);
59 + context.registerService(CoreService.class, mockCoreService);
60 + }
61 +
62 + /**
63 + * Test decoding of a MeterRequest object.
64 + */
65 + @Test
66 + public void testMeterRequestDecode() throws IOException {
67 + MeterRequest meterRequest = getMeterRequest("simple-meter-request.json");
68 + checkCommonData(meterRequest);
69 +
70 + assertThat(meterRequest.bands().size(), is(1));
71 + Band band = meterRequest.bands().iterator().next();
72 + assertThat(band.type().toString(), is("REMARK"));
73 + assertThat(band.rate(), is(10L));
74 + assertThat(band.dropPrecedence(), is((short) 20));
75 + assertThat(band.burst(), is(30L));
76 + }
77 +
78 + /**
79 + * Checks that the data shared by all the resource is correct for a given meterRequest.
80 + *
81 + * @param meterRequest meterRequest to check
82 + */
83 + private void checkCommonData(MeterRequest meterRequest) {
84 + assertThat(meterRequest.deviceId().toString(), is("of:0000000000000001"));
85 + assertThat(meterRequest.appId(), is(APP_ID));
86 + assertThat(meterRequest.unit().toString(), is("KB_PER_SEC"));
87 + }
88 +
89 + /**
90 + * Reads in a meter from the given resource and decodes it.
91 + *
92 + * @param resourceName resource to use to read the JSON for the rule
93 + * @return decoded meterRequest
94 + * @throws IOException if processing the resource fails
95 + */
96 + private MeterRequest getMeterRequest(String resourceName) throws IOException {
97 + InputStream jsonStream = MeterRequestCodecTest.class.getResourceAsStream(resourceName);
98 + JsonNode json = context.mapper().readTree(jsonStream);
99 + assertThat(json, notNullValue());
100 + MeterRequest meterRequest = meterRequestCodec.decode((ObjectNode) json, context);
101 + assertThat(meterRequest, notNullValue());
102 + return meterRequest;
103 + }
104 +}
1 { 1 {
2 - "id": 1,
3 "deviceId": "of:0000000000000001", 2 "deviceId": "of:0000000000000001",
4 "unit": "KB_PER_SEC", 3 "unit": "KB_PER_SEC",
5 "burst": true, 4 "burst": true,
......
...@@ -141,8 +141,7 @@ public class MetersWebResource extends AbstractWebResource { ...@@ -141,8 +141,7 @@ public class MetersWebResource extends AbstractWebResource {
141 throw new IllegalArgumentException(DEVICE_INVALID); 141 throw new IllegalArgumentException(DEVICE_INVALID);
142 } 142 }
143 jsonTree.put("deviceId", deviceId); 143 jsonTree.put("deviceId", deviceId);
144 - final Meter tmpMeter = codec(Meter.class).decode(jsonTree, this); 144 + final MeterRequest meterRequest = codec(MeterRequest.class).decode(jsonTree, this);
145 - final MeterRequest meterRequest = meterToMeterRequest(tmpMeter, "ADD");
146 final Meter meter = meterService.submit(meterRequest); 145 final Meter meter = meterService.submit(meterRequest);
147 location = new URI(Long.toString(meter.id().id())); 146 location = new URI(Long.toString(meter.id().id()));
148 } catch (IOException | URISyntaxException ex) { 147 } catch (IOException | URISyntaxException ex) {
......
...@@ -2,17 +2,12 @@ ...@@ -2,17 +2,12 @@
2 "type": "object", 2 "type": "object",
3 "title": "meter", 3 "title": "meter",
4 "required": [ 4 "required": [
5 - "id",
6 "deviceId", 5 "deviceId",
7 "unit", 6 "unit",
8 "burst", 7 "burst",
9 "bands" 8 "bands"
10 ], 9 ],
11 "properties": { 10 "properties": {
12 - "id": {
13 - "type": "string",
14 - "example": "1"
15 - },
16 "deviceId": { 11 "deviceId": {
17 "type": "string", 12 "type": "string",
18 "example": "of:0000000000000001" 13 "example": "of:0000000000000001"
......