blob: 44b49604f52b67323fceafc6c3c2a3cbe5b9d28c [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
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 */
16package org.onosproject.soam.impl;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.osgi.ServiceDirectory;
23import org.onlab.osgi.TestServiceDirectory;
Sean Condon0e89bda2017-03-21 14:23:19 +000024import org.onosproject.cfm.CfmCodecContext;
25import org.onosproject.cfm.impl.CfmResourceTest;
26import org.onosproject.codec.CodecService;
27import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepEntry;
28import org.onosproject.incubator.net.l2monitoring.cfm.Mep;
29import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry;
30import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
31import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort;
32import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId;
33import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
34import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
35import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
36import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService;
37import org.onosproject.incubator.net.l2monitoring.soam.MilliPct;
38import org.onosproject.incubator.net.l2monitoring.soam.SoamConfigException;
39import org.onosproject.incubator.net.l2monitoring.soam.SoamId;
40import org.onosproject.incubator.net.l2monitoring.soam.SoamService;
41import org.onosproject.incubator.net.l2monitoring.soam.delay.DelayMeasurementCreate;
42import org.onosproject.incubator.net.l2monitoring.soam.loss.DefaultLmEntry;
43import org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementCreate;
44import org.onosproject.incubator.net.l2monitoring.soam.loss.LossMeasurementEntry;
45import org.onosproject.net.DeviceId;
46import org.onosproject.net.PortNumber;
47
48import javax.ws.rs.InternalServerErrorException;
49import javax.ws.rs.client.Entity;
50import javax.ws.rs.client.WebTarget;
51import javax.ws.rs.core.Response;
52import java.io.BufferedReader;
53import java.io.ByteArrayInputStream;
54import java.io.IOException;
55import java.io.InputStreamReader;
56import java.time.Instant;
57import java.util.ArrayList;
58import java.util.List;
59
60import static org.easymock.EasyMock.createMock;
61import static org.easymock.EasyMock.expect;
62import static org.easymock.EasyMock.replay;
63import static org.hamcrest.Matchers.is;
64import static org.junit.Assert.assertEquals;
65import static org.junit.Assert.assertThat;
66
67public 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 Milkey094a1352018-01-22 14:03:54 -080089 setServiceDirectory(testDirectory);
Sean Condon0e89bda2017-03-21 14:23:19 +000090
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}