Ray Milkey
Committed by Gerrit Code Review

Implement REST APIs for link flow statistics

Change-Id: I65ca3cec9dd1771a70811afd319619827f7b9010
...@@ -47,6 +47,7 @@ import org.onosproject.net.intent.Constraint; ...@@ -47,6 +47,7 @@ import org.onosproject.net.intent.Constraint;
47 import org.onosproject.net.intent.HostToHostIntent; 47 import org.onosproject.net.intent.HostToHostIntent;
48 import org.onosproject.net.intent.Intent; 48 import org.onosproject.net.intent.Intent;
49 import org.onosproject.net.intent.PointToPointIntent; 49 import org.onosproject.net.intent.PointToPointIntent;
50 +import org.onosproject.net.statistic.Load;
50 import org.onosproject.net.topology.Topology; 51 import org.onosproject.net.topology.Topology;
51 import org.onosproject.net.topology.TopologyCluster; 52 import org.onosproject.net.topology.TopologyCluster;
52 import org.slf4j.Logger; 53 import org.slf4j.Logger;
...@@ -97,6 +98,7 @@ public class CodecManager implements CodecService { ...@@ -97,6 +98,7 @@ public class CodecManager implements CodecService {
97 registerCodec(Group.class, new GroupCodec()); 98 registerCodec(Group.class, new GroupCodec());
98 registerCodec(Driver.class, new DriverCodec()); 99 registerCodec(Driver.class, new DriverCodec());
99 registerCodec(GroupBucket.class, new GroupBucketCodec()); 100 registerCodec(GroupBucket.class, new GroupBucketCodec());
101 + registerCodec(Load.class, new LoadCodec());
100 log.info("Started"); 102 log.info("Started");
101 } 103 }
102 104
......
1 +/*
2 + * Copyright 2015 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 org.onosproject.codec.CodecContext;
19 +import org.onosproject.codec.JsonCodec;
20 +import org.onosproject.net.statistic.Load;
21 +
22 +import com.fasterxml.jackson.databind.node.ObjectNode;
23 +
24 +import static com.google.common.base.Preconditions.checkNotNull;
25 +
26 +/**
27 + * Codec for the Load class.
28 + */
29 +public class LoadCodec extends JsonCodec<Load> {
30 +
31 + private static final String RATE = "rate";
32 + private static final String LATEST = "latest";
33 + private static final String VALID = "valid";
34 + private static final String TIME = "time";
35 +
36 + @Override
37 + public ObjectNode encode(Load load, CodecContext context) {
38 + checkNotNull(load, "Load cannot be null");
39 + return context.mapper().createObjectNode()
40 + .put(RATE, load.rate())
41 + .put(LATEST, load.latest())
42 + .put(VALID, load.isValid())
43 + .put(TIME, load.time());
44 + }
45 +}
1 +/*
2 + * Copyright 2015 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 org.junit.Test;
19 +import org.onosproject.net.statistic.DefaultLoad;
20 +import org.onosproject.net.statistic.Load;
21 +
22 +import com.fasterxml.jackson.databind.JsonNode;
23 +
24 +import static org.hamcrest.MatcherAssert.assertThat;
25 +import static org.hamcrest.Matchers.greaterThanOrEqualTo;
26 +import static org.hamcrest.Matchers.is;
27 +
28 +/**
29 + * Unit tests for Load codec.
30 + */
31 +public class LoadCodecTest {
32 +
33 + /**
34 + * Tests encoding of a Load object.
35 + */
36 + @Test
37 + public void testLoadEncode() {
38 + final long startTime = System.currentTimeMillis();
39 + final Load load = new DefaultLoad(20, 10, 1);
40 + final JsonNode node = new LoadCodec()
41 + .encode(load, new MockCodecContext());
42 + assertThat(node.get("valid").asBoolean(), is(true));
43 + assertThat(node.get("latest").asLong(), is(20L));
44 + assertThat(node.get("rate").asLong(), is(10L));
45 + assertThat(node.get("time").asLong(), greaterThanOrEqualTo(startTime));
46 + }
47 +}
1 +/*
2 + * Copyright 2015 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.rest.resources;
17 +
18 +import java.util.Spliterator;
19 +import java.util.Spliterators;
20 +import java.util.stream.StreamSupport;
21 +
22 +import javax.ws.rs.GET;
23 +import javax.ws.rs.Path;
24 +import javax.ws.rs.Produces;
25 +import javax.ws.rs.QueryParam;
26 +import javax.ws.rs.core.Context;
27 +import javax.ws.rs.core.MediaType;
28 +import javax.ws.rs.core.Response;
29 +import javax.ws.rs.core.UriBuilder;
30 +import javax.ws.rs.core.UriInfo;
31 +
32 +import org.onosproject.codec.JsonCodec;
33 +import org.onosproject.net.ConnectPoint;
34 +import org.onosproject.net.Link;
35 +import org.onosproject.net.link.LinkService;
36 +import org.onosproject.net.statistic.Load;
37 +import org.onosproject.net.statistic.StatisticService;
38 +import org.onosproject.rest.AbstractWebResource;
39 +
40 +import com.fasterxml.jackson.databind.node.ArrayNode;
41 +import com.fasterxml.jackson.databind.node.ObjectNode;
42 +
43 +import static org.onosproject.net.DeviceId.deviceId;
44 +import static org.onosproject.net.PortNumber.portNumber;
45 +
46 +/**
47 + * Statistics REST APIs.
48 + */
49 +@Path("statistics")
50 +public class StatisticsWebResource extends AbstractWebResource {
51 + @Context
52 + UriInfo uriInfo;
53 +
54 + /**
55 + * Gets the Load statistics for all links, or for a specific link.
56 + *
57 + * @param deviceId (optional) device ID for a specific link
58 + * @param port (optional) port number for a specified link
59 + * @return JSON encoded array lof Load objects
60 + */
61 + @GET
62 + @Path("flows/link")
63 + @Produces(MediaType.APPLICATION_JSON)
64 + public Response getLoads(@QueryParam("device") String deviceId,
65 + @QueryParam("port") String port) {
66 + Iterable<Link> links;
67 +
68 + if (deviceId == null || port == null) {
69 + links = get(LinkService.class).getLinks();
70 + } else {
71 + ConnectPoint connectPoint = new ConnectPoint(deviceId(deviceId),
72 + portNumber(port));
73 + links = get(LinkService.class).getLinks(connectPoint);
74 + }
75 + ObjectNode result = mapper().createObjectNode();
76 + ArrayNode loads = mapper().createArrayNode();
77 + JsonCodec<Load> loadCodec = codec(Load.class);
78 + StatisticService statsService = getService(StatisticService.class);
79 +
80 +
81 + StreamSupport.stream(Spliterators.spliteratorUnknownSize(
82 + links.iterator(), Spliterator.ORDERED), false)
83 + .forEach(link -> {
84 + ObjectNode loadNode = loadCodec.encode(statsService.load(link), this);
85 +
86 + UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
87 + .path("links")
88 + .queryParam("device", link.src().deviceId().toString())
89 + .queryParam("port", link.src().port().toString());
90 + loadNode.put("link", locationBuilder.build().toString());
91 + loads.add(loadNode);
92 + });
93 + result.set("loads", loads);
94 + return ok(result).build();
95 + }
96 +}
...@@ -76,7 +76,8 @@ ...@@ -76,7 +76,8 @@
76 org.onosproject.rest.resources.FlowsWebResource, 76 org.onosproject.rest.resources.FlowsWebResource,
77 org.onosproject.rest.resources.TopologyWebResource, 77 org.onosproject.rest.resources.TopologyWebResource,
78 org.onosproject.rest.resources.ConfigWebResource, 78 org.onosproject.rest.resources.ConfigWebResource,
79 - org.onosproject.rest.resources.PathsWebResource 79 + org.onosproject.rest.resources.PathsWebResource,
80 + org.onosproject.rest.resources.StatisticsWebResource
80 </param-value> 81 </param-value>
81 </init-param> 82 </init-param>
82 <load-on-startup>1</load-on-startup> 83 <load-on-startup>1</load-on-startup>
......
1 +/*
2 + * Copyright 2015 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.rest;
17 +
18 +import java.util.HashMap;
19 +import java.util.stream.IntStream;
20 +
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onlab.osgi.ServiceDirectory;
24 +import org.onlab.osgi.TestServiceDirectory;
25 +import org.onlab.rest.BaseResource;
26 +import org.onosproject.codec.CodecService;
27 +import org.onosproject.codec.impl.CodecManager;
28 +import org.onosproject.net.Link;
29 +import org.onosproject.net.link.LinkService;
30 +import org.onosproject.net.statistic.DefaultLoad;
31 +import org.onosproject.net.statistic.StatisticService;
32 +
33 +import com.eclipsesource.json.JsonArray;
34 +import com.eclipsesource.json.JsonObject;
35 +import com.google.common.collect.ImmutableList;
36 +import com.google.common.collect.ImmutableSet;
37 +import com.sun.jersey.api.client.WebResource;
38 +
39 +import static org.easymock.EasyMock.createMock;
40 +import static org.easymock.EasyMock.expect;
41 +import static org.easymock.EasyMock.replay;
42 +import static org.hamcrest.Matchers.containsString;
43 +import static org.hamcrest.Matchers.hasSize;
44 +import static org.hamcrest.Matchers.is;
45 +import static org.hamcrest.Matchers.lessThanOrEqualTo;
46 +import static org.hamcrest.Matchers.notNullValue;
47 +import static org.junit.Assert.assertThat;
48 +import static org.onosproject.net.NetTestTools.connectPoint;
49 +import static org.onosproject.net.NetTestTools.link;
50 +
51 +/**
52 + * Unit tests for statistics REST APIs.
53 + */
54 +public class StatisticsResourceTest extends ResourceTest {
55 +
56 + Link link1 = link("src1", 1, "dst1", 1);
57 + Link link2 = link("src2", 2, "dst2", 2);
58 + Link link3 = link("src3", 3, "dst3", 3);
59 +
60 + LinkService mockLinkService;
61 + StatisticService mockStatisticService;
62 +
63 + /**
64 + * Initializes test mocks and environment.
65 + */
66 + @Before
67 + public void setUpTest() {
68 + mockLinkService = createMock(LinkService.class);
69 + expect(mockLinkService.getLinks())
70 + .andReturn(ImmutableList.of(link1, link2, link3));
71 + expect(mockLinkService.getLinks(connectPoint("0000000000000001", 2)))
72 + .andReturn(ImmutableSet.of(link3));
73 +
74 + mockStatisticService = createMock(StatisticService.class);
75 + expect(mockStatisticService.load(link1))
76 + .andReturn(new DefaultLoad(2, 1, 1));
77 + expect(mockStatisticService.load(link2))
78 + .andReturn(new DefaultLoad(22, 11, 1));
79 + expect(mockStatisticService.load(link3))
80 + .andReturn(new DefaultLoad(222, 111, 1));
81 +
82 + replay(mockLinkService, mockStatisticService);
83 +
84 + // Register the services needed for the test
85 + CodecManager codecService = new CodecManager();
86 + codecService.activate();
87 + ServiceDirectory testDirectory =
88 + new TestServiceDirectory()
89 + .add(LinkService.class, mockLinkService)
90 + .add(StatisticService.class, mockStatisticService)
91 + .add(CodecService.class, codecService);
92 +
93 + BaseResource.setServiceDirectory(testDirectory);
94 + }
95 +
96 + /**
97 + * Checks that the values in a JSON representation of a Load are
98 + * correct.
99 + *
100 + * @param load JSON for the Loan object
101 + * @param rate expected vale fo rate
102 + * @param latest expected value for latest
103 + * @param valid expected value for valid flag
104 + * @param device expected device ID
105 + */
106 + private void checkValues(JsonObject load, int rate, int latest,
107 + boolean valid, String device) {
108 + assertThat(load, notNullValue());
109 + assertThat(load.get("rate").asInt(), is(rate));
110 + assertThat(load.get("latest").asInt(), is(latest));
111 + assertThat(load.get("valid").asBoolean(), is(valid));
112 + assertThat(load.get("time").asLong(),
113 + lessThanOrEqualTo((System.currentTimeMillis())));
114 + assertThat(load.get("link").asString(),
115 + containsString("device=of:" + device));
116 + }
117 +
118 + /**
119 + * Tests GET of a single Load statistics object.
120 + */
121 + @Test
122 + public void testSingleLoadGet() {
123 + final WebResource rs = resource();
124 + final String response = rs.path("statistics/flows/link")
125 + .queryParam("device", "of:0000000000000001")
126 + .queryParam("port", "2")
127 + .get(String.class);
128 +
129 + final JsonObject result = JsonObject.readFrom(response);
130 + assertThat(result, notNullValue());
131 +
132 + assertThat(result.names(), hasSize(1));
133 + assertThat(result.names().get(0), is("loads"));
134 +
135 + final JsonArray jsonLoads = result.get("loads").asArray();
136 + assertThat(jsonLoads, notNullValue());
137 + assertThat(jsonLoads.size(), is(1));
138 +
139 + JsonObject load1 = jsonLoads.get(0).asObject();
140 + checkValues(load1, 111, 222, true, "src3");
141 + }
142 +
143 + /**
144 + * Tests GET of all Load statistics objects.
145 + */
146 + @Test
147 + public void testLoadsGet() {
148 + final WebResource rs = resource();
149 + final String response = rs.path("statistics/flows/link/").get(String.class);
150 +
151 + final JsonObject result = JsonObject.readFrom(response);
152 + assertThat(result, notNullValue());
153 +
154 + assertThat(result.names(), hasSize(1));
155 + assertThat(result.names().get(0), is("loads"));
156 +
157 + final JsonArray jsonLoads = result.get("loads").asArray();
158 + assertThat(jsonLoads, notNullValue());
159 + assertThat(jsonLoads.size(), is(3));
160 +
161 + // Hash the loads by the current field to allow easy lookup if the
162 + // order changes.
163 + HashMap<Integer, JsonObject> currentMap = new HashMap<>();
164 + IntStream.range(0, jsonLoads.size())
165 + .forEach(index -> currentMap.put(
166 + jsonLoads.get(index).asObject().get("latest").asInt(),
167 + jsonLoads.get(index).asObject()));
168 +
169 + JsonObject load1 = currentMap.get(2);
170 + checkValues(load1, 1, 2, true, "src1");
171 +
172 + JsonObject load2 = currentMap.get(22);
173 + checkValues(load2, 11, 22, true, "src2");
174 +
175 + JsonObject load3 = currentMap.get(222);
176 + checkValues(load3, 111, 222, true, "src3");
177 +
178 + }
179 +}