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.MilliPct; |
| 38 | import org.onosproject.incubator.net.l2monitoring.soam.SoamConfigException; |
| 39 | import org.onosproject.incubator.net.l2monitoring.soam.SoamId; |
| 40 | import org.onosproject.incubator.net.l2monitoring.soam.SoamService; |
| 41 | import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate; |
| 42 | import org.onosproject.incubator.net.l2monitoring.soam.loss.DefaultLmEntry; |
| 43 | import org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementCreate; |
| 44 | import org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementEntry; |
| 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; |
| 52 | import java.io.BufferedReader; |
| 53 | import java.io.ByteArrayInputStream; |
| 54 | import java.io.IOException; |
| 55 | import java.io.InputStreamReader; |
| 56 | import java.time.Instant; |
| 57 | import java.util.ArrayList; |
| 58 | import java.util.List; |
| 59 | |
| 60 | import static org.easymock.EasyMock.createMock; |
| 61 | import static org.easymock.EasyMock.expect; |
| 62 | import static org.easymock.EasyMock.replay; |
| 63 | import static org.hamcrest.Matchers.is; |
| 64 | import static org.junit.Assert.assertEquals; |
| 65 | import static org.junit.Assert.assertThat; |
| 66 | |
| 67 | public class LmWebResourceTest extends CfmResourceTest { |
| 68 | private final CfmMepService mepService = createMock(CfmMepService.class); |
| 69 | private final SoamService soamService = createMock(SoamService.class); |
| 70 | |
| 71 | private static final MdId MDNAME1 = MdIdCharStr.asMdId("md-1"); |
| 72 | private static final MaIdShort MANAME1 = MaIdCharStr.asMaId("ma-1-1"); |
| 73 | private static final MepId MEPID1 = MepId.valueOf((short) 1); |
| 74 | private static final SoamId LMID1 = SoamId.valueOf(1); |
| 75 | private static final SoamId LMID2 = SoamId.valueOf(2); |
| 76 | |
| 77 | private LossMeasurementEntry lm1; |
| 78 | private LossMeasurementEntry lm2; |
| 79 | |
| 80 | private final Instant now = Instant.now(); |
| 81 | |
| 82 | @Before |
| 83 | public void setUpTest() throws CfmConfigException, SoamConfigException { |
| 84 | CfmCodecContext context = new CfmCodecContext(); |
| 85 | ServiceDirectory testDirectory = new TestServiceDirectory() |
| 86 | .add(CfmMepService.class, mepService) |
| 87 | .add(SoamService.class, soamService) |
| 88 | .add(CodecService.class, context.codecManager()); |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 89 | setServiceDirectory(testDirectory); |
Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame] | 90 | |
| 91 | lm1 = DefaultLmEntry.builder( |
| 92 | DelayMeasurementCreate.Version.Y17312008, |
| 93 | MepId.valueOf((short) 10), |
| 94 | Mep.Priority.PRIO1, |
| 95 | LossMeasurementCreate.LmType.LMLMM, |
| 96 | LMID1) |
| 97 | .build(); |
| 98 | lm2 = DefaultLmEntry.builder( |
| 99 | DelayMeasurementCreate.Version.Y17312011, |
| 100 | MepId.valueOf((short) 10), |
| 101 | Mep.Priority.PRIO2, |
| 102 | LossMeasurementCreate.LmType.LMLMM, |
| 103 | LMID2) |
| 104 | .measuredAvailabilityBackwardStatus(LossMeasurementEntry.AvailabilityType.AVAILABLE) |
| 105 | .measuredAvailabilityForwardStatus(LossMeasurementEntry.AvailabilityType.UNKNOWN) |
| 106 | .measuredBackwardFlr(MilliPct.ofPercent(49.9f)) |
| 107 | .measuredForwardFlr(MilliPct.ofRatio(0.51f)) |
| 108 | .measuredBackwardLastTransitionTime(now) |
| 109 | .measuredForwardLastTransitionTime(now) |
| 110 | .build(); |
| 111 | |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void testGetAllLmsForMep() throws CfmConfigException, SoamConfigException { |
| 116 | List<LossMeasurementEntry> lmList = new ArrayList<>(); |
| 117 | lmList.add(lm1); |
| 118 | lmList.add(lm2); |
| 119 | |
| 120 | expect(soamService.getAllLms(MDNAME1, MANAME1, MEPID1)).andReturn(lmList).anyTimes(); |
| 121 | replay(soamService); |
| 122 | |
| 123 | final WebTarget wt = target(); |
| 124 | final String response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 125 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/lm") |
| 126 | .request().get(String.class); |
| 127 | |
| 128 | assertThat(response, is("{\"lms\":[[" + |
| 129 | "{" + |
| 130 | "\"lmId\":\"1\"," + |
| 131 | "\"lmCfgType\":\"LMLMM\"," + |
| 132 | "\"version\":\"Y17312008\"," + |
| 133 | "\"remoteMepId\":10," + |
| 134 | "\"priority\":\"PRIO1\"," + |
| 135 | "\"countersEnabled\":[]," + |
| 136 | "\"measurementHistories\":[]," + |
| 137 | "\"availabilityHistories\":[]" + |
| 138 | "},{" + |
| 139 | "\"lmId\":\"2\"," + |
| 140 | "\"measuredForwardFlr\":51.0," + |
| 141 | "\"measuredBackwardFlr\":49.9," + |
| 142 | "\"measuredAvailabilityForwardStatus\":\"UNKNOWN\"," + |
| 143 | "\"measuredAvailabilityBackwardStatus\":\"AVAILABLE\"," + |
| 144 | "\"measuredForwardLastTransitionTime\":\"" + now + "\"," + |
| 145 | "\"measuredBackwardLastTransitionTime\":\"" + now + "\"," + |
| 146 | "\"lmCfgType\":\"LMLMM\"," + |
| 147 | "\"version\":\"Y17312011\"," + |
| 148 | "\"remoteMepId\":10," + |
| 149 | "\"priority\":\"PRIO2\"," + |
| 150 | "\"countersEnabled\":[]," + |
| 151 | "\"measurementHistories\":[]," + |
| 152 | "\"availabilityHistories\":[]" + |
| 153 | "}]]}")); |
| 154 | } |
| 155 | |
| 156 | @Test |
| 157 | public void testGetAllLmsForMepEmpty() throws CfmConfigException, SoamConfigException { |
| 158 | List<LossMeasurementEntry> lmList = new ArrayList<>(); |
| 159 | |
| 160 | expect(soamService.getAllLms(MDNAME1, MANAME1, MEPID1)).andReturn(lmList).anyTimes(); |
| 161 | replay(soamService); |
| 162 | |
| 163 | final WebTarget wt = target(); |
| 164 | final String response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 165 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/lm") |
| 166 | .request().get(String.class); |
| 167 | |
| 168 | assertThat(response, is("{\"lms\":[[]]}")); |
| 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void testGetLm() throws CfmConfigException, SoamConfigException { |
| 173 | |
| 174 | expect(soamService.getLm(MDNAME1, MANAME1, MEPID1, LMID1)).andReturn(lm1).anyTimes(); |
| 175 | replay(soamService); |
| 176 | |
| 177 | final WebTarget wt = target(); |
| 178 | final String response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 179 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/lm/" + LMID1.value()) |
| 180 | .request().get(String.class); |
| 181 | |
| 182 | assertThat(response, is("{\"lm\":" + |
| 183 | "{" + |
| 184 | "\"lmId\":\"1\"," + |
| 185 | "\"lmCfgType\":\"LMLMM\"," + |
| 186 | "\"version\":\"Y17312008\"," + |
| 187 | "\"remoteMepId\":10," + |
| 188 | "\"priority\":\"PRIO1\"," + |
| 189 | "\"countersEnabled\":[]," + |
| 190 | "\"measurementHistories\":[]," + |
| 191 | "\"availabilityHistories\":[]" + |
| 192 | "}}")); |
| 193 | } |
| 194 | |
| 195 | @Test |
| 196 | public void testGetLmEmpty() throws CfmConfigException, SoamConfigException, IOException { |
| 197 | SoamId lmId3 = SoamId.valueOf(3); |
| 198 | expect(soamService.getLm(MDNAME1, MANAME1, MEPID1, lmId3)) |
| 199 | .andReturn(null).anyTimes(); |
| 200 | replay(soamService); |
| 201 | |
| 202 | final WebTarget wt = target(); |
| 203 | try { |
| 204 | final String response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 205 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/lm/" + lmId3.value()) |
| 206 | .request().get(String.class); |
| 207 | } catch (InternalServerErrorException e) { |
| 208 | ByteArrayInputStream is = (ByteArrayInputStream) e.getResponse().getEntity(); |
| 209 | BufferedReader br = new BufferedReader(new InputStreamReader(is)); |
| 210 | String line = null; |
| 211 | StringBuffer sb = new StringBuffer(); |
| 212 | while ((line = br.readLine()) != null) { |
| 213 | sb.append(line); |
| 214 | } |
| 215 | assertEquals("{ \"failure\":\"LM md-1/ma-1-1/1/3 not found\" }", |
| 216 | sb.toString()); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | @Test |
| 221 | public void testAbortLm() { |
| 222 | |
| 223 | final WebTarget wt = target(); |
| 224 | final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 225 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/lm/" + LMID1.value()) |
| 226 | .request().delete(); |
| 227 | assertEquals(200, response.getStatus()); |
| 228 | } |
| 229 | |
| 230 | @Test |
| 231 | public void testCreateLm() throws CfmConfigException, SoamConfigException { |
| 232 | MepEntry mep1 = DefaultMepEntry.builder(MEPID1, DeviceId.deviceId("netconf:1.2.3.4:830"), |
| 233 | PortNumber.portNumber(1), Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).buildEntry(); |
| 234 | |
| 235 | expect(mepService.getMep(MDNAME1, MANAME1, MEPID1)).andReturn(mep1).anyTimes(); |
| 236 | replay(mepService); |
| 237 | |
| 238 | ObjectMapper mapper = new ObjectMapper(); |
| 239 | CfmCodecContext context = new CfmCodecContext(); |
| 240 | ObjectNode node = mapper.createObjectNode(); |
| 241 | node.set("lm", context.codec(LossMeasurementCreate.class).encode(lm1, context)); |
| 242 | |
| 243 | final WebTarget wt = target(); |
| 244 | final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 245 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/lm") |
| 246 | .request().post(Entity.json(node.toString())); |
| 247 | assertEquals(201, response.getStatus()); |
| 248 | } |
| 249 | |
| 250 | @Test |
| 251 | public void testClearLmHistory() { |
| 252 | |
| 253 | final WebTarget wt = target(); |
| 254 | final Response response = wt.path("md/" + MDNAME1.mdName() + "/ma/" + |
| 255 | MANAME1.maName() + "/mep/" + MEPID1.value() + "/lm/" + LMID1.value() + |
| 256 | "/clear-history") |
| 257 | .request().put(Entity.json("")); |
| 258 | |
| 259 | assertEquals(200, response.getStatus()); |
| 260 | } |
| 261 | } |