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