Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.soam.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onlab.osgi.TestServiceDirectory; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 24 | import org.onosproject.cfm.CfmCodecContext; |
| 25 | import org.onosproject.cfm.impl.CfmResourceTest; |
| 26 | import org.onosproject.codec.CodecService; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepEntry; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 33 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 34 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 35 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 36 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService; |
| 37 | import org.onosproject.incubator.net.l2monitoring.soam.SoamConfigException; |
| 38 | import org.onosproject.incubator.net.l2monitoring.soam.SoamId; |
| 39 | import org.onosproject.incubator.net.l2monitoring.soam.SoamService; |
| 40 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DefaultDelayMeasurementEntry; |
| 41 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DefaultDelayMeasurementStatCurrent; |
| 42 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate; |
| 43 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementEntry; |
| 44 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementStatCurrent; |
| 45 | import org.onosproject.net.DeviceId; |
| 46 | import org.onosproject.net.PortNumber; |
| 47 | |
| 48 | import javax.ws.rs.InternalServerErrorException; |
| 49 | import javax.ws.rs.client.Entity; |
| 50 | import javax.ws.rs.client.WebTarget; |
| 51 | import javax.ws.rs.core.Response; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 52 | import java.io.BufferedReader; |
| 53 | import java.io.ByteArrayInputStream; |
| 54 | import java.io.IOException; |
| 55 | import java.io.InputStreamReader; |
| 56 | import java.time.Duration; |
| 57 | import java.time.Instant; |
| 58 | import java.util.ArrayList; |
| 59 | import java.util.List; |
| 60 | |
| 61 | import static junit.framework.TestCase.fail; |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame^] | 62 | import static org.easymock.EasyMock.createMock; |
| 63 | import static org.easymock.EasyMock.expect; |
| 64 | import static org.easymock.EasyMock.replay; |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 65 | import static org.hamcrest.Matchers.is; |
| 66 | import static org.junit.Assert.assertEquals; |
| 67 | import static org.junit.Assert.assertThat; |
| 68 | |
| 69 | public class DmWebResourceTest extends CfmResourceTest { |
| 70 | private final CfmMepService mepService = createMock(CfmMepService.class); |
| 71 | private final SoamService soamService = createMock(SoamService.class); |
| 72 | |
| 73 | private static final MdId MDNAME1 = MdIdCharStr.asMdId("md-1"); |
| 74 | private static final MaIdShort MANAME1 = MaIdCharStr.asMaId("ma-1-1"); |
| 75 | private static final MepId MEPID1 = MepId.valueOf((short) 1); |
| 76 | private static final SoamId DM1 = SoamId.valueOf(1); |
| 77 | private static final SoamId DM2 = SoamId.valueOf(2); |
| 78 | |
| 79 | private DelayMeasurementEntry dm1; |
| 80 | private DelayMeasurementEntry dm2; |
| 81 | |
| 82 | private final Instant now = Instant.now(); |
| 83 | |
| 84 | @Before |
| 85 | public void setUpTest() throws CfmConfigException, SoamConfigException { |
| 86 | CfmCodecContext context = new CfmCodecContext(); |
| 87 | ServiceDirectory testDirectory = new TestServiceDirectory() |
| 88 | .add(CfmMepService.class, mepService) |
| 89 | .add(SoamService.class, soamService) |
| 90 | .add(CodecService.class, context.codecManager()); |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame^] | 91 | setServiceDirectory(testDirectory); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 92 | |
| 93 | DelayMeasurementStatCurrent.DmStatCurrentBuilder dmCurrBuilder1 = |
| 94 | (DelayMeasurementStatCurrent.DmStatCurrentBuilder) |
| 95 | DefaultDelayMeasurementStatCurrent |
| 96 | .builder(Duration.ofMinutes(1), false) |
| 97 | .startTime(now) |
| 98 | .frameDelayBackwardAvg(Duration.ofMillis(10)) |
| 99 | .frameDelayForwardAvg(Duration.ofMillis(11)) |
| 100 | .frameDelayRangeBackwardAvg(Duration.ofMillis(12)); |
| 101 | |
| 102 | dm1 = DefaultDelayMeasurementEntry.builder(DM1, |
| 103 | DelayMeasurementCreate.DmType.DMDMM, |
| 104 | DelayMeasurementCreate.Version.Y17312008, |
| 105 | MepId.valueOf((short) 2), |
| 106 | Mep.Priority.PRIO1) |
| 107 | .sessionStatus(DelayMeasurementEntry.SessionStatus.ACTIVE) |
| 108 | .frameDelayTwoWay(Duration.ofMillis(40)) |
| 109 | .frameDelayBackward(Duration.ofMillis(30)) |
| 110 | .frameDelayForward(Duration.ofMillis(10)) |
| 111 | .interFrameDelayVariationTwoWay(Duration.ofMillis(8)) |
| 112 | .interFrameDelayVariationBackward(Duration.ofMillis(3)) |
| 113 | .interFrameDelayVariationForward(Duration.ofMillis(5)) |
| 114 | .currentResult((DelayMeasurementStatCurrent) dmCurrBuilder1.build()) |
| 115 | .build(); |
| 116 | |
| 117 | dm2 = DefaultDelayMeasurementEntry.builder(DM2, |
| 118 | DelayMeasurementCreate.DmType.DMDMM, |
| 119 | DelayMeasurementCreate.Version.Y17312011, |
| 120 | MepId.valueOf((short) 2), |
| 121 | Mep.Priority.PRIO2) |
| 122 | .build(); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void testGetAllDmsForMep() throws CfmConfigException, SoamConfigException { |
| 127 | |
| 128 | List<DelayMeasurementEntry> dmList = new ArrayList<>(); |
| 129 | dmList.add(dm1); |
| 130 | dmList.add(dm2); |
| 131 | |
| 132 | expect(soamService.getAllDms(MDNAME1, MANAME1, MEPID1)).andReturn(dmList).anyTimes(); |
| 133 | replay(soamService); |
| 134 | |
| 135 | final WebTarget wt = target(); |
| 136 | final String response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 137 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/dm") |
| 138 | .request().get(String.class); |
| 139 | |
| 140 | assertThat(response, is("{\"dms\":[[" + |
| 141 | "{" + |
| 142 | "\"dmId\":\"1\"," + |
| 143 | "\"sessionStatus\":\"ACTIVE\"," + |
| 144 | "\"frameDelayTwoWay\":\"PT0.04S\"," + |
| 145 | "\"frameDelayForward\":\"PT0.01S\"," + |
| 146 | "\"frameDelayBackward\":\"PT0.03S\"," + |
| 147 | "\"interFrameDelayVariationTwoWay\":\"PT0.008S\"," + |
| 148 | "\"interFrameDelayVariationForward\":\"PT0.005S\"," + |
| 149 | "\"interFrameDelayVariationBackward\":\"PT0.003S\"," + |
| 150 | "\"dmCfgType\":\"DMDMM\"," + |
| 151 | "\"version\":\"Y17312008\"," + |
| 152 | "\"remoteMepId\":2," + |
| 153 | "\"priority\":\"PRIO1\"," + |
| 154 | "\"measurementsEnabled\":[]," + |
| 155 | "\"current\":{" + |
| 156 | "\"startTime\":\"" + now + "\"," + |
| 157 | "\"elapsedTime\":\"PT1M\"," + |
| 158 | "\"suspectStatus\":\"false\"," + |
| 159 | "\"frameDelayForwardAvg\":\"PT0.011S\"," + |
| 160 | "\"frameDelayBackwardAvg\":\"PT0.01S\"," + |
| 161 | "\"frameDelayRangeBackwardAvg\":\"PT0.012S\"" + |
| 162 | "}," + |
| 163 | "\"historic\":[]" + |
| 164 | "},{" + |
| 165 | "\"dmId\":\"2\"," + |
| 166 | "\"dmCfgType\":\"DMDMM\"," + |
| 167 | "\"version\":\"Y17312011\"," + |
| 168 | "\"remoteMepId\":2," + |
| 169 | "\"priority\":\"PRIO2\"," + |
| 170 | "\"measurementsEnabled\":[]," + |
| 171 | "\"historic\":[]}]]" + |
| 172 | "}")); |
| 173 | } |
| 174 | |
| 175 | @Test |
| 176 | public void testGetAllDmsForMepEmpty() throws CfmConfigException, SoamConfigException { |
| 177 | |
| 178 | List<DelayMeasurementEntry> dmListEmpty = new ArrayList<>(); |
| 179 | |
| 180 | expect(soamService.getAllDms(MDNAME1, MANAME1, MEPID1)).andReturn(dmListEmpty).anyTimes(); |
| 181 | replay(soamService); |
| 182 | |
| 183 | final WebTarget wt = target(); |
| 184 | final String response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 185 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/dm") |
| 186 | .request().get(String.class); |
| 187 | |
| 188 | assertThat(response, is("{\"dms\":[[]]}")); |
| 189 | } |
| 190 | |
| 191 | @Test |
| 192 | public void testGetDm() throws CfmConfigException, SoamConfigException { |
| 193 | |
| 194 | expect(soamService.getDm(MDNAME1, MANAME1, MEPID1, DM1)).andReturn(dm1).anyTimes(); |
| 195 | replay(soamService); |
| 196 | |
| 197 | final WebTarget wt = target(); |
| 198 | final String response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 199 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/dm/" + DM1.value()) |
| 200 | .request().get(String.class); |
| 201 | |
| 202 | assertThat(response, is("{\"dm\":" + |
| 203 | "{" + |
| 204 | "\"dmId\":\"1\"," + |
| 205 | "\"sessionStatus\":\"ACTIVE\"," + |
| 206 | "\"frameDelayTwoWay\":\"PT0.04S\"," + |
| 207 | "\"frameDelayForward\":\"PT0.01S\"," + |
| 208 | "\"frameDelayBackward\":\"PT0.03S\"," + |
| 209 | "\"interFrameDelayVariationTwoWay\":\"PT0.008S\"," + |
| 210 | "\"interFrameDelayVariationForward\":\"PT0.005S\"," + |
| 211 | "\"interFrameDelayVariationBackward\":\"PT0.003S\"," + |
| 212 | "\"dmCfgType\":\"DMDMM\"," + |
| 213 | "\"version\":\"Y17312008\"," + |
| 214 | "\"remoteMepId\":2," + |
| 215 | "\"priority\":\"PRIO1\"," + |
| 216 | "\"measurementsEnabled\":[]," + |
| 217 | "\"current\":{" + |
| 218 | "\"startTime\":\"" + now + "\"," + |
| 219 | "\"elapsedTime\":\"PT1M\"," + |
| 220 | "\"suspectStatus\":\"false\"," + |
| 221 | "\"frameDelayForwardAvg\":\"PT0.011S\"," + |
| 222 | "\"frameDelayBackwardAvg\":\"PT0.01S\"," + |
| 223 | "\"frameDelayRangeBackwardAvg\":\"PT0.012S\"" + |
| 224 | "}," + |
| 225 | "\"historic\":[]" + |
| 226 | "}}")); |
| 227 | } |
| 228 | |
| 229 | @Test |
| 230 | public void testGetDmInvalid() throws CfmConfigException, SoamConfigException, IOException { |
| 231 | |
| 232 | SoamId dm3 = SoamId.valueOf(3); |
| 233 | |
| 234 | expect(soamService.getDm(MDNAME1, MANAME1, MEPID1, dm3)).andReturn(null).anyTimes(); |
| 235 | replay(soamService); |
| 236 | |
| 237 | final WebTarget wt = target(); |
| 238 | try { |
| 239 | final String response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 240 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/dm/" + dm3.value()) |
| 241 | .request().get(String.class); |
| 242 | fail("Expecting excpetion"); |
| 243 | } catch (InternalServerErrorException e) { |
| 244 | ByteArrayInputStream is = (ByteArrayInputStream) e.getResponse().getEntity(); |
| 245 | BufferedReader br = new BufferedReader(new InputStreamReader(is)); |
| 246 | String line = null; |
| 247 | StringBuffer sb = new StringBuffer(); |
| 248 | while ((line = br.readLine()) != null) { |
| 249 | sb.append(line); |
| 250 | } |
| 251 | assertEquals("{ \"failure\":\"DM md-1/ma-1-1/1/3 not found\" }", sb.toString()); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | @Test |
| 256 | public void testAbortDm() { |
| 257 | |
| 258 | final WebTarget wt = target(); |
| 259 | final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 260 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/dm/" + DM1.value()) |
| 261 | .request().delete(); |
| 262 | |
| 263 | assertEquals(200, response.getStatus()); |
| 264 | } |
| 265 | |
| 266 | @Test |
| 267 | public void testCreateDm() throws CfmConfigException, SoamConfigException { |
| 268 | MepEntry mep1 = DefaultMepEntry.builder(MEPID1, DeviceId.deviceId("netconf:1.2.3.4:830"), |
| 269 | PortNumber.portNumber(1), Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).buildEntry(); |
| 270 | |
| 271 | expect(mepService.getMep(MDNAME1, MANAME1, MEPID1)).andReturn(mep1).anyTimes(); |
| 272 | replay(mepService); |
| 273 | |
| 274 | ObjectMapper mapper = new ObjectMapper(); |
| 275 | CfmCodecContext context = new CfmCodecContext(); |
| 276 | ObjectNode node = mapper.createObjectNode(); |
| 277 | node.set("dm", context.codec(DelayMeasurementCreate.class).encode(dm1, context)); |
| 278 | |
| 279 | final WebTarget wt = target(); |
| 280 | final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 281 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/dm") |
| 282 | .request().post(Entity.json(node.toString())); |
| 283 | |
| 284 | assertEquals(201, response.getStatus()); |
| 285 | } |
| 286 | |
| 287 | @Test |
| 288 | public void testClearDmHistory() { |
| 289 | |
| 290 | final WebTarget wt = target(); |
| 291 | final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 292 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/dm/" + DM1.value() + |
| 293 | "/clear-history") |
| 294 | .request().put(Entity.json("")); |
| 295 | |
| 296 | assertEquals(200, response.getStatus()); |
| 297 | } |
| 298 | |
| 299 | } |