blob: af64224d082fcac2279fc684a841e9e50fea579c [file] [log] [blame]
Ray Milkeyb9af9462015-07-17 11:12:09 -07001/*
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 */
16package org.onosproject.rest;
17
18import java.util.HashMap;
19import java.util.stream.IntStream;
20
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.osgi.ServiceDirectory;
24import org.onlab.osgi.TestServiceDirectory;
25import org.onlab.rest.BaseResource;
26import org.onosproject.codec.CodecService;
27import org.onosproject.codec.impl.CodecManager;
28import org.onosproject.net.Link;
29import org.onosproject.net.link.LinkService;
30import org.onosproject.net.statistic.DefaultLoad;
31import org.onosproject.net.statistic.StatisticService;
32
33import com.eclipsesource.json.JsonArray;
34import com.eclipsesource.json.JsonObject;
35import com.google.common.collect.ImmutableList;
36import com.google.common.collect.ImmutableSet;
37import com.sun.jersey.api.client.WebResource;
38
39import static org.easymock.EasyMock.createMock;
40import static org.easymock.EasyMock.expect;
41import static org.easymock.EasyMock.replay;
42import static org.hamcrest.Matchers.containsString;
43import static org.hamcrest.Matchers.hasSize;
44import static org.hamcrest.Matchers.is;
45import static org.hamcrest.Matchers.lessThanOrEqualTo;
46import static org.hamcrest.Matchers.notNullValue;
47import static org.junit.Assert.assertThat;
48import static org.onosproject.net.NetTestTools.connectPoint;
49import static org.onosproject.net.NetTestTools.link;
50
51/**
52 * Unit tests for statistics REST APIs.
53 */
54public 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}