blob: eee1f979ce93fd9b2dfc3fbfe4638180a6ec0839 [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.incubator.net.l2monitoring.cfm;
17
18import java.time.Duration;
19
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.VlanId;
25import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMep.DefaultMepBuilder;
26import org.onosproject.incubator.net.l2monitoring.cfm.Mep.FngAddress;
27import org.onosproject.incubator.net.l2monitoring.cfm.Mep.FngAddressType;
28import org.onosproject.incubator.net.l2monitoring.cfm.Mep.LowestFaultDefect;
29import org.onosproject.incubator.net.l2monitoring.cfm.Mep.MepDirection;
30import org.onosproject.incubator.net.l2monitoring.cfm.Mep.Priority;
31import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
32import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
33import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
34import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.PortNumber;
37
38import static org.junit.Assert.*;
39
40public class MepTest {
41 private Mep mep1;
42
43 @Before
44 public void setUp() throws Exception {
45 try {
46 mep1 = DefaultMep.builder(MepId.valueOf((short) 1),
47 DeviceId.deviceId("of:12345678"),
48 PortNumber.portNumber(0),
49 MepDirection.UP_MEP,
50 MdIdCharStr.asMdId("md-1"),
51 MaIdCharStr.asMaId("ma-1-1"))
52 .primaryVid(VlanId.vlanId((short) 1000))
53 .administrativeState(true)
54 .cciEnabled(true)
55 .ccmLtmPriority(Priority.PRIO6)
56 .fngAddress(FngAddress.ipV4Address(IpAddress.valueOf("10.2.3.1")))
57 .lowestFaultPriorityDefect(LowestFaultDefect.ERROR_FD_PLUS)
58 .defectPresentTime(Duration.ofSeconds(1))
59 .defectAbsentTime(Duration.ofSeconds(2))
60 .build();
61 } catch (CfmConfigException e) {
62 throw new Exception(e);
63 }
64 }
65
66 @After
67 public void tearDown() throws Exception {
68 }
69
70 @Test
71 public void testPortError() {
72 try {
73 DefaultMep.builder(MepId.valueOf((short) 1),
74 DeviceId.deviceId("of:12345678"),
75 PortNumber.FLOOD,
76 MepDirection.UP_MEP,
77 MdIdCharStr.asMdId("md-1"),
78 MaIdCharStr.asMaId("ma-1-1"))
79 .build();
80 fail("Port = FLOOD should throw exception");
81 } catch (CfmConfigException e) {
82 assertTrue(e.getMessage().contains("Port must be physical"));
83 }
84 }
85
86 @Test
87 public void testMdNameErrorNull() {
88 try {
89 DefaultMep.builder(MepId.valueOf((short) 1),
90 DeviceId.deviceId("of:12345678"),
91 PortNumber.portNumber(0),
92 MepDirection.UP_MEP,
93 null,
94 MaIdCharStr.asMaId("ma-1-1"))
95 .build();
96 fail("Null md Name should throw exception");
97 } catch (CfmConfigException e) {
98 assertTrue(e.getMessage().contains("MdId is null"));
99 }
100 }
101
102 @Test
103 public void testMaNameErrorNull() {
104 try {
105 DefaultMep.builder(MepId.valueOf((short) 1),
106 DeviceId.deviceId("of:12345678"),
107 PortNumber.portNumber(0),
108 MepDirection.UP_MEP,
109 MdIdCharStr.asMdId("md-1"),
110 null)
111 .build();
112 fail("Null ma Name should throw exception");
113 } catch (CfmConfigException e) {
114 assertTrue(e.getMessage().contains("MaId is null"));
115 }
116 }
117
118 @Test
119 public void testMepCopyConstructor() throws CfmConfigException {
120 Mep mep2 = (new DefaultMepBuilder(mep1)).build();
121 assertEquals(1, mep2.mepId().value());
122 assertEquals("md-1", mep2.mdId().mdName());
123 }
124
125 @Test
126 public void testMepId() {
127 assertEquals(1, mep1.mepId().value());
128 }
129
130 @Test
131 public void testDeviceId() {
132 assertEquals("of:12345678", mep1.deviceId().toString());
133 }
134
135 @Test
136 public void testPort() {
137 assertEquals(0, mep1.port().toLong());
138 }
139
140 @Test
141 public void testDirection() {
142 assertEquals(MepDirection.UP_MEP, mep1.direction());
143 }
144
145 @Test
146 public void testPrimaryVid() {
147 assertEquals(1000, mep1.primaryVid().id().intValue());
148 }
149
150 @Test
151 public void testAdministrativeState() {
152 assertTrue(mep1.administrativeState());
153 }
154
155 @Test
156 public void testWithAdministrativeState() {
157 Mep mep2 = mep1.withAdministrativeState(false);
158 assertFalse(mep2.administrativeState());
159 assertEquals(1, mep2.mepId().value());
160 }
161
162 @Test
163 public void testCciEnabled() {
164 assertTrue(mep1.cciEnabled());
165 }
166
167 @Test
168 public void testWithCciEnabled() {
169 Mep mep2 = mep1.withCciEnabled(false);
170 assertFalse(mep2.cciEnabled());
171 }
172
173 @Test
174 public void testCcmLtmPriority() {
175 assertEquals(6, mep1.ccmLtmPriority().ordinal());
176 }
177
178 @Test
179 public void testWithCcmLtmPriority() throws CfmConfigException {
180 Mep mep2 = mep1.withCcmLtmPriority(Priority.PRIO5);
181 assertEquals(5, mep2.ccmLtmPriority().ordinal());
182 assertEquals(1, mep2.mepId().value());
183 }
184
185 @Test
186 public void testWithFngAddress() {
187 Mep mep2 = mep1.withFngAddress(FngAddress.ipV4Address(IpAddress.valueOf("10.2.3.2")));
188 assertEquals(FngAddressType.IPV4, mep2.fngAddress().addressType());
189 assertEquals(IpAddress.valueOf("10.2.3.2"), mep2.fngAddress().ipAddress());
190 }
191
192 @Test
193 public void testFngAddress() {
194 assertEquals(FngAddressType.IPV4, mep1.fngAddress().addressType());
195 assertEquals(IpAddress.valueOf("10.2.3.1"), mep1.fngAddress().ipAddress());
196 }
197
198 @Test
199 public void testLowestFaultPriorityDefect() {
200 assertEquals(LowestFaultDefect.ERROR_FD_PLUS, mep1.lowestFaultPriorityDefect());
201 }
202
203 @Test
204 public void testWithLowestFaultPriorityDefect() {
205 Mep mep2 = mep1.withLowestFaultPriorityDefect(LowestFaultDefect.XCON_FD_ONLY);
206 assertEquals(LowestFaultDefect.XCON_FD_ONLY, mep2.lowestFaultPriorityDefect());
207 }
208
209 @Test
210 public void testDefectPresetTime() {
211 assertEquals(1000, mep1.defectPresentTime().toMillis());
212 }
213
214 @Test
215 public void testWithDefectPresentTime() {
216 Mep mep2 = mep1.withDefectPresentTime(Duration.ofMillis(1500L));
217 assertEquals(1500, mep2.defectPresentTime().toMillis());
218 }
219
220 @Test
221 public void testDefectAbsentTime() {
222 assertEquals(2000, mep1.defectAbsentTime().toMillis());
223 }
224
225 @Test
226 public void testWithDefectAbsentTime() {
227 Mep mep2 = mep1.withDefectAbsentTime(Duration.ofMillis(2500L));
228 assertEquals(2500, mep2.defectAbsentTime().toMillis());
229 }
230
231 @Test
232 public void testEqualsObject() throws CfmConfigException {
233 Mep mep2 = mep1.withPrimaryVid(VlanId.vlanId((short) 5));
234 assertNotEquals(mep1, mep2);
235 }
236
237 @Test
238 public void testMepToString() {
239 assertEquals(mep1.toString(), "DefaultMep{" +
240 "mepId=1, " +
241 "deviceId=of:12345678, " +
242 "port=0, " +
243 "direction=UP_MEP, " +
244 "mdId=md-1, " +
245 "maId=ma-1-1, " +
246 "primaryVid=1000, " +
247 "administrativeState=true, " +
248 "cciEnabled=true, " +
249 "ccmLtmPriority=PRIO6, " +
250 "fngAddress=FngAddress{addressType=IPV4, ipAddress=10.2.3.1}, " +
251 "lowestFaultPriorityDefect=ERROR_FD_PLUS, " +
252 "defectPresentTime=PT1S, " +
253 "defectAbsentTime=PT2S}");
254 }
255
256 @Test
257 public void testEquality() throws CfmConfigException {
258 assertFalse(mep1.equals(null));
259 assertTrue(mep1.equals(mep1));
260 assertFalse(mep1.equals(new String("test")));
261
262 Mep mep2 = new DefaultMepBuilder(mep1).build();
263 assertTrue(mep1.equals(mep2));
264 assertEquals(mep1.hashCode(), mep2.hashCode());
265
266 Mep mep3 = DefaultMep.builder(MepId.valueOf((short) 2),
267 DeviceId.deviceId("of:12345680"),
268 PortNumber.portNumber(0),
269 MepDirection.UP_MEP,
270 MdIdCharStr.asMdId("md-3"),
271 MaIdCharStr.asMaId("ma-3-3"))
272 .build();
273 assertFalse(mep1.equals(mep3));
274 }
275}