Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 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 | */ |
Jian Li | 8ae9120 | 2016-03-24 14:36:16 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 17 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 18 | import com.eclipsesource.json.Json; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 19 | import com.eclipsesource.json.JsonArray; |
| 20 | import com.eclipsesource.json.JsonObject; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.ImmutableSet; |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.osgi.ServiceDirectory; |
| 26 | import org.onlab.osgi.TestServiceDirectory; |
| 27 | import org.onlab.rest.BaseResource; |
| 28 | import org.onosproject.codec.CodecService; |
| 29 | import org.onosproject.codec.impl.CodecManager; |
| 30 | import org.onosproject.net.Link; |
| 31 | import org.onosproject.net.link.LinkService; |
| 32 | import org.onosproject.net.statistic.DefaultLoad; |
| 33 | import org.onosproject.net.statistic.StatisticService; |
| 34 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 35 | import javax.ws.rs.client.WebTarget; |
| 36 | import java.io.UnsupportedEncodingException; |
| 37 | import java.net.URLDecoder; |
| 38 | import java.util.HashMap; |
| 39 | import java.util.stream.IntStream; |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 40 | |
| 41 | import static org.easymock.EasyMock.createMock; |
| 42 | import static org.easymock.EasyMock.expect; |
| 43 | import static org.easymock.EasyMock.replay; |
| 44 | import static org.hamcrest.Matchers.containsString; |
| 45 | import static org.hamcrest.Matchers.hasSize; |
| 46 | import static org.hamcrest.Matchers.is; |
| 47 | import static org.hamcrest.Matchers.lessThanOrEqualTo; |
| 48 | import static org.hamcrest.Matchers.notNullValue; |
| 49 | import static org.junit.Assert.assertThat; |
| 50 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 51 | import static org.onosproject.net.NetTestTools.link; |
| 52 | |
| 53 | /** |
| 54 | * Unit tests for statistics REST APIs. |
| 55 | */ |
| 56 | public class StatisticsResourceTest extends ResourceTest { |
| 57 | |
| 58 | Link link1 = link("src1", 1, "dst1", 1); |
| 59 | Link link2 = link("src2", 2, "dst2", 2); |
| 60 | Link link3 = link("src3", 3, "dst3", 3); |
| 61 | |
| 62 | LinkService mockLinkService; |
| 63 | StatisticService mockStatisticService; |
| 64 | |
| 65 | /** |
| 66 | * Initializes test mocks and environment. |
| 67 | */ |
| 68 | @Before |
| 69 | public void setUpTest() { |
| 70 | mockLinkService = createMock(LinkService.class); |
| 71 | expect(mockLinkService.getLinks()) |
| 72 | .andReturn(ImmutableList.of(link1, link2, link3)); |
| 73 | expect(mockLinkService.getLinks(connectPoint("0000000000000001", 2))) |
| 74 | .andReturn(ImmutableSet.of(link3)); |
| 75 | |
| 76 | mockStatisticService = createMock(StatisticService.class); |
| 77 | expect(mockStatisticService.load(link1)) |
| 78 | .andReturn(new DefaultLoad(2, 1, 1)); |
| 79 | expect(mockStatisticService.load(link2)) |
| 80 | .andReturn(new DefaultLoad(22, 11, 1)); |
| 81 | expect(mockStatisticService.load(link3)) |
| 82 | .andReturn(new DefaultLoad(222, 111, 1)); |
| 83 | |
| 84 | replay(mockLinkService, mockStatisticService); |
| 85 | |
| 86 | // Register the services needed for the test |
| 87 | CodecManager codecService = new CodecManager(); |
| 88 | codecService.activate(); |
| 89 | ServiceDirectory testDirectory = |
| 90 | new TestServiceDirectory() |
| 91 | .add(LinkService.class, mockLinkService) |
| 92 | .add(StatisticService.class, mockStatisticService) |
| 93 | .add(CodecService.class, codecService); |
| 94 | |
| 95 | BaseResource.setServiceDirectory(testDirectory); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Checks that the values in a JSON representation of a Load are |
| 100 | * correct. |
| 101 | * |
| 102 | * @param load JSON for the Loan object |
| 103 | * @param rate expected vale fo rate |
| 104 | * @param latest expected value for latest |
| 105 | * @param valid expected value for valid flag |
| 106 | * @param device expected device ID |
| 107 | */ |
| 108 | private void checkValues(JsonObject load, int rate, int latest, |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 109 | boolean valid, String device) throws UnsupportedEncodingException { |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 110 | assertThat(load, notNullValue()); |
| 111 | assertThat(load.get("rate").asInt(), is(rate)); |
| 112 | assertThat(load.get("latest").asInt(), is(latest)); |
| 113 | assertThat(load.get("valid").asBoolean(), is(valid)); |
| 114 | assertThat(load.get("time").asLong(), |
| 115 | lessThanOrEqualTo((System.currentTimeMillis()))); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 116 | assertThat(URLDecoder.decode(load.get("link").asString(), "UTF-8"), |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 117 | containsString("device=of:" + device)); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Tests GET of a single Load statistics object. |
| 122 | */ |
| 123 | @Test |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 124 | public void testSingleLoadGet() throws UnsupportedEncodingException { |
| 125 | final WebTarget wt = target(); |
| 126 | final String response = wt.path("statistics/flows/link") |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 127 | .queryParam("device", "of:0000000000000001") |
| 128 | .queryParam("port", "2") |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 129 | .request() |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 130 | .get(String.class); |
| 131 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 132 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 133 | assertThat(result, notNullValue()); |
| 134 | |
| 135 | assertThat(result.names(), hasSize(1)); |
| 136 | assertThat(result.names().get(0), is("loads")); |
| 137 | |
| 138 | final JsonArray jsonLoads = result.get("loads").asArray(); |
| 139 | assertThat(jsonLoads, notNullValue()); |
| 140 | assertThat(jsonLoads.size(), is(1)); |
| 141 | |
| 142 | JsonObject load1 = jsonLoads.get(0).asObject(); |
| 143 | checkValues(load1, 111, 222, true, "src3"); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Tests GET of all Load statistics objects. |
| 148 | */ |
| 149 | @Test |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 150 | public void testLoadsGet() throws UnsupportedEncodingException { |
| 151 | final WebTarget wt = target(); |
| 152 | final String response = wt.path("statistics/flows/link/").request().get(String.class); |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 153 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 154 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | b9af946 | 2015-07-17 11:12:09 -0700 | [diff] [blame] | 155 | assertThat(result, notNullValue()); |
| 156 | |
| 157 | assertThat(result.names(), hasSize(1)); |
| 158 | assertThat(result.names().get(0), is("loads")); |
| 159 | |
| 160 | final JsonArray jsonLoads = result.get("loads").asArray(); |
| 161 | assertThat(jsonLoads, notNullValue()); |
| 162 | assertThat(jsonLoads.size(), is(3)); |
| 163 | |
| 164 | // Hash the loads by the current field to allow easy lookup if the |
| 165 | // order changes. |
| 166 | HashMap<Integer, JsonObject> currentMap = new HashMap<>(); |
| 167 | IntStream.range(0, jsonLoads.size()) |
| 168 | .forEach(index -> currentMap.put( |
| 169 | jsonLoads.get(index).asObject().get("latest").asInt(), |
| 170 | jsonLoads.get(index).asObject())); |
| 171 | |
| 172 | JsonObject load1 = currentMap.get(2); |
| 173 | checkValues(load1, 1, 2, true, "src1"); |
| 174 | |
| 175 | JsonObject load2 = currentMap.get(22); |
| 176 | checkValues(load2, 11, 22, true, "src2"); |
| 177 | |
| 178 | JsonObject load3 = currentMap.get(222); |
| 179 | checkValues(load3, 111, 222, true, "src3"); |
| 180 | |
| 181 | } |
| 182 | } |