2 * Copyright (c) 2015 NEC Corporation. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.vtn.manager.internal.util.flow.cond;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
17 import org.junit.Test;
19 import org.mockito.Mockito;
21 import org.opendaylight.vtn.manager.internal.util.inventory.SalPort;
22 import org.opendaylight.vtn.manager.internal.util.rpc.RpcErrorTag;
23 import org.opendaylight.vtn.manager.internal.util.rpc.RpcException;
25 import org.opendaylight.vtn.manager.internal.TestBase;
27 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.flow.cond.rev150313.VtnFlowConditions;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.flow.cond.rev150313.VtnFlowConditionsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.flow.cond.rev150313.vtn.flow.cond.config.VtnFlowMatch;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.flow.cond.rev150313.vtn.flow.cond.config.VtnFlowMatchKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.flow.cond.rev150313.vtn.flow.conditions.VtnFlowCondition;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.flow.cond.rev150313.vtn.flow.conditions.VtnFlowConditionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.flow.cond.rev150313.vtn.flow.conditions.VtnFlowConditionKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.types.rev150209.VnodeName;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vtn.types.rev150209.VtnErrorTag;
43 * JUnit test for {@link FlowCondUtils}.
45 public class FlowCondUtilsTest extends TestBase {
47 * Test case for the following methods.
50 * <li>{@link FlowCondUtils#getNotFoundException(String)}</li>
51 * <li>{@link FlowCondUtils#getNotFoundException(String, Throwable)}</li>
52 * <li>{@link FlowCondUtils#getMatchIndexMissingException()}</li>
56 public void testException() {
63 Throwable cause = new IllegalStateException();
64 for (String name: names) {
65 String msg = name + ": Flow condition does not exist.";
66 RpcException e = FlowCondUtils.getNotFoundException(name);
67 assertEquals(null, e.getCause());
68 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
69 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
70 assertEquals(msg, e.getMessage());
72 e = FlowCondUtils.getNotFoundException(name, cause);
73 assertSame(cause, e.getCause());
74 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
75 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
76 assertEquals(msg, e.getMessage());
79 String msg = "Match index cannot be null";
80 RpcException e = FlowCondUtils.getMatchIndexMissingException();
81 assertEquals(RpcErrorTag.MISSING_ELEMENT, e.getErrorTag());
82 assertEquals(VtnErrorTag.BADREQUEST, e.getVtnErrorTag());
83 assertEquals(msg, e.getMessage());
84 assertEquals(null, e.getCause());
88 * Test case for the following methods.
91 * <li>{@link FlowCondUtils#checkName(String)}</li>
92 * <li>{@link FlowCondUtils#checkName(VnodeName)}</li>
93 * <li>{@link FlowCondUtils#getVnodeName(String)}</li>
94 * <li>{@link FlowCondUtils#getIdentifier(String)}</li>
95 * <li>{@link FlowCondUtils#getIdentifier(VnodeName)}</li>
96 * <li>{@link FlowCondUtils#getIdentifier(String, Integer)}</li>
97 * <li>{@link FlowCondUtils#getIdentifier(VnodeName, Integer)}</li>
98 * <li>{@link FlowCondUtils#getName(InstanceIdentifier)}</li>
101 * @throws Exception An error occurred.
104 public void testName() throws Exception {
111 "012345678901234567890123456789",
112 "0123456789012345678901234567890",
119 "abcABC_0123_XXXXXXXXXXXXXXXXXXX",
121 Integer[] indices = {
122 1, 2, 3, 444, 1000, 2000, 3333, 44444, 55555, 65534, 65535,
125 for (String name: names) {
126 VnodeName vname = new VnodeName(name);
127 assertEquals(vname, FlowCondUtils.checkName(name));
128 assertEquals(name, FlowCondUtils.checkName(vname));
129 assertEquals(vname, FlowCondUtils.getVnodeName(name));
131 VtnFlowConditionKey key = new VtnFlowConditionKey(vname);
132 InstanceIdentifier<VtnFlowCondition> expected = InstanceIdentifier.
133 builder(VtnFlowConditions.class).
134 child(VtnFlowCondition.class, key).build();
135 assertEquals(expected, FlowCondUtils.getIdentifier(name));
136 assertEquals(expected, FlowCondUtils.getIdentifier(vname));
137 assertEquals(name, FlowCondUtils.getName(expected));
139 for (Integer idx: indices) {
140 VtnFlowMatchKey mkey = new VtnFlowMatchKey(idx);
141 InstanceIdentifier<VtnFlowMatch> ex = InstanceIdentifier.
142 builder(VtnFlowConditions.class).
143 child(VtnFlowCondition.class, key).
144 child(VtnFlowMatch.class, mkey).build();
145 assertEquals(ex, FlowCondUtils.getIdentifier(name, idx));
146 assertEquals(ex, FlowCondUtils.getIdentifier(vname, idx));
147 assertEquals(name, FlowCondUtils.getName(ex));
150 String msg = "Match index cannot be null";
152 FlowCondUtils.getIdentifier(name, null);
154 } catch (RpcException e) {
155 assertEquals(RpcErrorTag.MISSING_ELEMENT, e.getErrorTag());
156 assertEquals(VtnErrorTag.BADREQUEST, e.getVtnErrorTag());
157 assertEquals(msg, e.getMessage());
158 assertEquals(null, e.getCause());
162 FlowCondUtils.getIdentifier(vname, null);
164 } catch (RpcException e) {
165 assertEquals(RpcErrorTag.MISSING_ELEMENT, e.getErrorTag());
166 assertEquals(VtnErrorTag.BADREQUEST, e.getVtnErrorTag());
167 assertEquals(msg, e.getMessage());
168 assertEquals(null, e.getCause());
173 String msg = "Flow condition name cannot be null";
174 RpcErrorTag etag = RpcErrorTag.MISSING_ELEMENT;
175 VtnErrorTag vtag = VtnErrorTag.BADREQUEST;
177 FlowCondUtils.checkName((String)null);
179 } catch (RpcException e) {
180 assertEquals(etag, e.getErrorTag());
181 assertEquals(vtag, e.getVtnErrorTag());
182 assertEquals(msg, e.getMessage());
183 assertEquals(null, e.getCause());
187 FlowCondUtils.checkName((VnodeName)null);
189 } catch (RpcException e) {
190 assertEquals(etag, e.getErrorTag());
191 assertEquals(vtag, e.getVtnErrorTag());
192 assertEquals(msg, e.getMessage());
193 assertEquals(null, e.getCause());
197 FlowCondUtils.getVnodeName(null);
199 } catch (RpcException e) {
200 assertEquals(etag, e.getErrorTag());
201 assertEquals(vtag, e.getVtnErrorTag());
202 assertEquals(msg, e.getMessage());
203 assertEquals(null, e.getCause());
207 FlowCondUtils.getIdentifier((String)null);
209 } catch (RpcException e) {
210 assertEquals(etag, e.getErrorTag());
211 assertEquals(vtag, e.getVtnErrorTag());
212 assertEquals(msg, e.getMessage());
213 assertEquals(null, e.getCause());
217 FlowCondUtils.getIdentifier((String)null, Integer.valueOf(1));
219 } catch (RpcException e) {
220 assertEquals(etag, e.getErrorTag());
221 assertEquals(vtag, e.getVtnErrorTag());
222 assertEquals(msg, e.getMessage());
223 assertEquals(null, e.getCause());
227 msg = "Flow condition name cannot be empty";
228 etag = RpcErrorTag.BAD_ELEMENT;
230 FlowCondUtils.checkName("");
232 } catch (RpcException e) {
233 assertEquals(etag, e.getErrorTag());
234 assertEquals(vtag, e.getVtnErrorTag());
235 assertEquals(msg, e.getMessage());
236 assertEquals(null, e.getCause());
240 FlowCondUtils.getVnodeName("");
242 } catch (RpcException e) {
243 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
244 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
245 assertEquals(": Flow condition does not exist.", e.getMessage());
247 Throwable cause = e.getCause();
248 assertTrue(cause instanceof RpcException);
249 RpcException re = (RpcException)cause;
250 assertEquals(etag, re.getErrorTag());
251 assertEquals(vtag, re.getVtnErrorTag());
252 assertEquals(msg, re.getMessage());
253 assertEquals(null, re.getCause());
257 FlowCondUtils.getIdentifier("");
259 } catch (RpcException e) {
260 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
261 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
262 assertEquals(": Flow condition does not exist.", e.getMessage());
264 Throwable cause = e.getCause();
265 assertTrue(cause instanceof RpcException);
266 RpcException re = (RpcException)cause;
267 assertEquals(etag, re.getErrorTag());
268 assertEquals(vtag, re.getVtnErrorTag());
269 assertEquals(msg, re.getMessage());
270 assertEquals(null, re.getCause());
274 FlowCondUtils.getIdentifier("", Integer.valueOf(1));
276 } catch (RpcException e) {
277 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
278 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
279 assertEquals(": Flow condition does not exist.", e.getMessage());
281 Throwable cause = e.getCause();
282 assertTrue(cause instanceof RpcException);
283 RpcException re = (RpcException)cause;
284 assertEquals(etag, re.getErrorTag());
285 assertEquals(vtag, re.getVtnErrorTag());
286 assertEquals(msg, re.getMessage());
287 assertEquals(null, re.getCause());
291 msg = "Flow condition name is invalid";
292 String[] invalidNames = {
293 "01234567890123456789012345678901",
294 "abcABC_0123_XXXXXXXXXXXXXXXXXXXX",
302 for (String name: invalidNames) {
304 FlowCondUtils.checkName(name);
306 } catch (RpcException e) {
307 assertEquals(etag, e.getErrorTag());
308 assertEquals(vtag, e.getVtnErrorTag());
309 assertEquals(msg, e.getMessage());
310 Throwable cause = e.getCause();
311 assertTrue(cause instanceof IllegalArgumentException);
315 FlowCondUtils.getVnodeName(name);
317 } catch (RpcException e) {
318 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
319 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
320 assertEquals(name + ": Flow condition does not exist.",
323 Throwable cause = e.getCause();
324 assertTrue(cause instanceof RpcException);
325 RpcException re = (RpcException)cause;
326 assertEquals(etag, re.getErrorTag());
327 assertEquals(vtag, re.getVtnErrorTag());
328 assertEquals(msg, re.getMessage());
329 cause = re.getCause();
330 assertTrue(cause instanceof IllegalArgumentException);
334 FlowCondUtils.getIdentifier(name);
336 } catch (RpcException e) {
337 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
338 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
339 assertEquals(name + ": Flow condition does not exist.",
342 Throwable cause = e.getCause();
343 assertTrue(cause instanceof RpcException);
344 RpcException re = (RpcException)cause;
345 assertEquals(etag, re.getErrorTag());
346 assertEquals(vtag, re.getVtnErrorTag());
347 assertEquals(msg, re.getMessage());
348 cause = re.getCause();
349 assertTrue(cause instanceof IllegalArgumentException);
353 FlowCondUtils.getIdentifier(name, Integer.valueOf(1));
355 } catch (RpcException e) {
356 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
357 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
358 assertEquals(name + ": Flow condition does not exist.",
361 Throwable cause = e.getCause();
362 assertTrue(cause instanceof RpcException);
363 RpcException re = (RpcException)cause;
364 assertEquals(etag, re.getErrorTag());
365 assertEquals(vtag, re.getVtnErrorTag());
366 assertEquals(msg, re.getMessage());
367 cause = re.getCause();
368 assertTrue(cause instanceof IllegalArgumentException);
372 // getName() should return null if the given path does not contain a
373 // flow condition key.
374 SalPort sport = new SalPort(1L, 2L);
375 List<InstanceIdentifier<?>> list = new ArrayList<>();
376 list.add(sport.getNodeIdentifier());
377 list.add(sport.getNodeConnectorIdentifier());
378 list.add(sport.getVtnNodeIdentifier());
379 list.add(sport.getVtnPortIdentifier());
380 for (InstanceIdentifier<?> path: list) {
381 assertEquals(null, FlowCondUtils.getName(path));
384 VtnFlowConditionKey nullKey = new VtnFlowConditionKey((VnodeName)null);
385 InstanceIdentifier<VtnFlowCondition> path = InstanceIdentifier.
386 builder(VtnFlowConditions.class).
387 child(VtnFlowCondition.class, nullKey).build();
388 assertEquals(null, FlowCondUtils.getName(path));
390 InstanceIdentifier<VtnFlowMatch> mpath = InstanceIdentifier.
391 builder(VtnFlowConditions.class).
392 child(VtnFlowCondition.class, nullKey).
393 child(VtnFlowMatch.class, new VtnFlowMatchKey(1)).build();
394 assertEquals(null, FlowCondUtils.getName(mpath));
398 * Test case for the following methods.
401 * <li>{@link FlowCondUtils#verifyMatchIndex(Set, Integer)}</li>
402 * <li>{@link FlowCondUtils#verifyMatchIndex(Integer)}</li>
405 * @throws Exception An error occurred.
408 public void testMatchIndex() throws Exception {
409 Integer[] indices = {
410 1, 2, 3, 444, 1000, 2000, 3333, 44444, 55555, 65534, 65535,
413 Set<Integer> idxSet = new HashSet<>();
414 for (Integer index: indices) {
415 FlowCondUtils.verifyMatchIndex(index);
416 FlowCondUtils.verifyMatchIndex(idxSet, index);
420 String msg = "Match index cannot be null";
421 RpcErrorTag etag = RpcErrorTag.MISSING_ELEMENT;
422 VtnErrorTag vtag = VtnErrorTag.BADREQUEST;
424 FlowCondUtils.verifyMatchIndex(null);
426 } catch (RpcException e) {
427 assertEquals(etag, e.getErrorTag());
428 assertEquals(vtag, e.getVtnErrorTag());
429 assertEquals(msg, e.getMessage());
430 assertEquals(null, e.getCause());
434 Integer[] badIndices = {
435 Integer.MIN_VALUE, -10000000, -3000, -200, -3, -2, -1, 0,
436 65536, 65537, 70000, 1000000, Integer.MAX_VALUE,
438 etag = RpcErrorTag.BAD_ELEMENT;
439 for (Integer index: badIndices) {
440 msg = "Invalid match index: " + index;
442 FlowCondUtils.verifyMatchIndex(index);
444 } catch (RpcException e) {
445 assertEquals(etag, e.getErrorTag());
446 assertEquals(vtag, e.getVtnErrorTag());
447 assertEquals(msg, e.getMessage());
449 Throwable cause = e.getCause();
450 assertTrue(cause instanceof IllegalArgumentException);
455 for (Integer index: indices) {
456 msg = "Duplicate match index: " + index;
458 FlowCondUtils.verifyMatchIndex(idxSet, index);
460 } catch (RpcException e) {
461 assertEquals(etag, e.getErrorTag());
462 assertEquals(vtag, e.getVtnErrorTag());
463 assertEquals(msg, e.getMessage());
464 assertEquals(null, e.getCause());
470 * Test case for {@link FlowCondUtils#isEmpty(VtnFlowConditions)}.
473 public void testIsEmpty() {
474 VtnFlowConditions root = null;
475 assertEquals(true, FlowCondUtils.isEmpty(root));
477 root = new VtnFlowConditionsBuilder().build();
478 assertEquals(true, FlowCondUtils.isEmpty(root));
480 List<VtnFlowCondition> vlist = new ArrayList<>();
481 root = new VtnFlowConditionsBuilder().
482 setVtnFlowCondition(vlist).build();
483 assertEquals(true, FlowCondUtils.isEmpty(root));
485 vlist.add(new VtnFlowConditionBuilder().build());
486 assertEquals(false, FlowCondUtils.isEmpty(root));
491 * {@link FlowCondUtils#checkPresent(ReadTransaction, VnodeName)}.
493 * @throws Exception An error occurred.
496 public void testCheckPresent() throws Exception {
497 // Set up mock-up of MD-SAL read transaction.
498 ReadTransaction rtx = Mockito.mock(ReadTransaction.class);
499 LogicalDatastoreType oper = LogicalDatastoreType.OPERATIONAL;
500 List<VnodeName> names = new ArrayList<>();
501 List<InstanceIdentifier<VtnFlowCondition>> paths = new ArrayList<>();
502 Set<VnodeName> present = new HashSet<>();
504 for (int i = 0; i < 10; i++) {
505 String name = "present" + i;
506 VnodeName vname = new VnodeName(name);
508 assertEquals(true, present.add(vname));
509 VtnFlowCondition vfc = new VtnFlowConditionBuilder().
510 setName(vname).build();
511 InstanceIdentifier<VtnFlowCondition> path =
512 FlowCondUtils.getIdentifier(vname);
514 Mockito.when(rtx.read(oper, path)).thenReturn(getReadResult(vfc));
517 VtnFlowCondition vfcNull = null;
518 for (int i = 0; i < 10; i++) {
519 String name = "notfound" + i;
520 VnodeName vname = new VnodeName(name);
522 assertEquals(false, present.contains(vname));
523 InstanceIdentifier<VtnFlowCondition> path =
524 FlowCondUtils.getIdentifier(vname);
526 Mockito.when(rtx.read(oper, path)).
527 thenReturn(getReadResult(vfcNull));
530 for (VnodeName vname: names) {
532 FlowCondUtils.checkPresent(rtx, vname);
533 assertEquals(true, present.contains(vname));
534 } catch (RpcException e) {
535 assertEquals(false, present.contains(vname));
536 assertEquals(null, e.getCause());
537 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
538 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
539 String msg = vname.getValue() +
540 ": Flow condition does not exist.";
541 assertEquals(msg, e.getMessage());
545 for (InstanceIdentifier<VtnFlowCondition> path: paths) {
546 Mockito.verify(rtx).read(oper, path);
551 * Test case for {@link FlowCondUtils#readFlowConditions(ReadTransaction)}.
553 * @throws Exception An error occurred.
556 public void testReadConditions() throws Exception {
557 // Set up mock-up of MD-SAL read transaction.
558 ReadTransaction rtx = Mockito.mock(ReadTransaction.class);
559 LogicalDatastoreType oper = LogicalDatastoreType.OPERATIONAL;
560 InstanceIdentifier<VtnFlowConditions> path =
561 InstanceIdentifier.create(VtnFlowConditions.class);
563 // Root container is not present.
564 VtnFlowConditions root = null;
565 List<VTNFlowCondition> expected = new ArrayList<>();
566 Mockito.when(rtx.read(oper, path)).thenReturn(getReadResult(root));
567 assertEquals(expected, FlowCondUtils.readFlowConditions(rtx));
568 Mockito.verify(rtx).read(oper, path);
571 // Flow condition list is null.
572 root = new VtnFlowConditionsBuilder().build();
573 Mockito.when(rtx.read(oper, path)).thenReturn(getReadResult(root));
574 assertEquals(expected, FlowCondUtils.readFlowConditions(rtx));
575 Mockito.verify(rtx).read(oper, path);
578 // Flow condition list is empty.
579 List<VtnFlowCondition> vlist = new ArrayList<>();
580 root = new VtnFlowConditionsBuilder().
581 setVtnFlowCondition(vlist).build();
582 Mockito.when(rtx.read(oper, path)).thenReturn(getReadResult(root));
583 assertEquals(expected, FlowCondUtils.readFlowConditions(rtx));
584 Mockito.verify(rtx).read(oper, path);
587 // Flow conditions are present.
588 Map<FlowCondParams, FlowCondParams> cases =
589 FlowCondParams.createFlowConditions();
590 for (FlowCondParams params: cases.values()) {
591 vlist.add(params.toVtnFlowConditionBuilder().build());
592 expected.add(params.toVTNFlowCondition());
594 root = new VtnFlowConditionsBuilder().
595 setVtnFlowCondition(vlist).build();
596 Mockito.when(rtx.read(oper, path)).thenReturn(getReadResult(root));
597 assertEquals(expected, FlowCondUtils.readFlowConditions(rtx));
598 Mockito.verify(rtx).read(oper, path);
604 * {@link FlowCondUtils#readFlowCondition(ReadTransaction, String)}.
606 * @throws Exception An error occurred.
609 public void testReadCondition() throws Exception {
610 // Set up mock-up of MD-SAL read transaction.
611 ReadTransaction rtx = Mockito.mock(ReadTransaction.class);
612 LogicalDatastoreType oper = LogicalDatastoreType.OPERATIONAL;
616 FlowCondUtils.readFlowCondition(rtx, (String)null);
618 } catch (RpcException e) {
619 assertEquals(RpcErrorTag.MISSING_ELEMENT, e.getErrorTag());
620 assertEquals(VtnErrorTag.BADREQUEST, e.getVtnErrorTag());
621 assertEquals("Flow condition name cannot be null", e.getMessage());
624 // Invalid name should be treated as if the specified flow condition
626 String[] invalidNames = {
628 "01234567890123456789012345678901",
629 "abcABC_0123_XXXXXXXXXXXXXXXXXXXX",
637 for (String name: invalidNames) {
639 FlowCondUtils.readFlowCondition(rtx, name);
641 } catch (RpcException e) {
642 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
643 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
644 String msg = name + ": Flow condition does not exist.";
645 assertEquals(msg, e.getMessage());
648 Mockito.verify(rtx, Mockito.never()).
649 read(Mockito.any(LogicalDatastoreType.class),
650 (InstanceIdentifier<?>)Mockito.any(InstanceIdentifier.class));
652 // Flow condition is not present.
653 VtnFlowCondition vfc = null;
654 Map<FlowCondParams, FlowCondParams> cases =
655 FlowCondParams.createFlowConditions();
656 for (FlowCondParams params: cases.values()) {
657 String name = params.getName();
658 InstanceIdentifier<VtnFlowCondition> path =
659 FlowCondUtils.getIdentifier(name);
660 Mockito.when(rtx.read(oper, path)).thenReturn(getReadResult(vfc));
662 FlowCondUtils.readFlowCondition(rtx, name);
664 } catch (RpcException e) {
665 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
666 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
667 String msg = name + ": Flow condition does not exist.";
668 assertEquals(msg, e.getMessage());
670 Mockito.verify(rtx).read(oper, path);
673 // Flow condition is present.
674 for (FlowCondParams params: cases.values()) {
675 String name = params.getName();
676 InstanceIdentifier<VtnFlowCondition> path =
677 FlowCondUtils.getIdentifier(name);
678 vfc = params.toVtnFlowConditionBuilder().build();
679 Mockito.when(rtx.read(oper, path)).thenReturn(getReadResult(vfc));
680 assertEquals(params.toVTNFlowCondition(),
681 FlowCondUtils.readFlowCondition(rtx, name));
682 Mockito.verify(rtx, Mockito.times(2)).read(oper, path);
688 * {@link FlowCondUtils#readFlowMatch(ReadTransaction, String, int)}.
690 * @throws Exception An error occurred.
693 public void testReadMatch() throws Exception {
694 // Set up mock-up of MD-SAL read transaction.
695 ReadTransaction rtx = Mockito.mock(ReadTransaction.class);
696 LogicalDatastoreType oper = LogicalDatastoreType.OPERATIONAL;
700 FlowCondUtils.readFlowMatch(rtx, (String)null, 1);
702 } catch (RpcException e) {
703 assertEquals(RpcErrorTag.MISSING_ELEMENT, e.getErrorTag());
704 assertEquals(VtnErrorTag.BADREQUEST, e.getVtnErrorTag());
705 assertEquals("Flow condition name cannot be null", e.getMessage());
708 // Invalid name should be treated as if the specified flow condition
710 String[] invalidNames = {
712 "01234567890123456789012345678901",
713 "abcABC_0123_XXXXXXXXXXXXXXXXXXXX",
721 for (String name: invalidNames) {
723 FlowCondUtils.readFlowMatch(rtx, name, 1);
725 } catch (RpcException e) {
726 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
727 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
728 String msg = name + ": Flow condition does not exist.";
729 assertEquals(msg, e.getMessage());
732 Mockito.verify(rtx, Mockito.never()).
733 read(Mockito.any(LogicalDatastoreType.class),
734 (InstanceIdentifier<?>)Mockito.any(InstanceIdentifier.class));
736 VtnFlowCondition vfc = new VtnFlowConditionBuilder().build();
737 VtnFlowCondition vfcNull = null;
738 VtnFlowMatch vfm = null;
739 Map<FlowMatchParams, FlowMatchParams> cases =
740 FlowMatchParams.createFlowMatches();
741 String name = "fcond";
742 InstanceIdentifier<VtnFlowCondition> fcPath =
743 FlowCondUtils.getIdentifier(name);
744 String msg = name + ": Flow condition does not exist.";
746 for (FlowMatchParams params: cases.values()) {
747 // Flow match is not present.
748 Integer index = params.getIndex();
749 InstanceIdentifier<VtnFlowMatch> path =
750 FlowCondUtils.getIdentifier(name, index);
751 Mockito.when(rtx.read(oper, fcPath)).thenReturn(getReadResult(vfc));
752 Mockito.when(rtx.read(oper, path)).thenReturn(getReadResult(vfm));
753 assertEquals(null, FlowCondUtils.readFlowMatch(rtx, name, index));
755 Mockito.verify(rtx, Mockito.times(fcCount)).read(oper, fcPath);
756 Mockito.verify(rtx).read(oper, path);
758 // Flow condition is not present.
759 Mockito.when(rtx.read(oper, fcPath)).
760 thenReturn(getReadResult(vfcNull));
762 FlowCondUtils.readFlowMatch(rtx, name, index);
764 } catch (RpcException e) {
765 assertEquals(RpcErrorTag.DATA_MISSING, e.getErrorTag());
766 assertEquals(VtnErrorTag.NOTFOUND, e.getVtnErrorTag());
767 assertEquals(msg, e.getMessage());
771 Mockito.verify(rtx, Mockito.times(fcCount)).read(oper, fcPath);
772 Mockito.verify(rtx, Mockito.times(2)).read(oper, path);
775 // Flow match is present.
776 for (FlowMatchParams params: cases.values()) {
777 Integer index = params.getIndex();
778 InstanceIdentifier<VtnFlowMatch> path =
779 FlowCondUtils.getIdentifier(name, index);
780 vfm = params.toVtnFlowMatchBuilder().build();
781 Mockito.when(rtx.read(oper, path)).thenReturn(getReadResult(vfm));
782 assertEquals(params.toVTNFlowMatch(),
783 FlowCondUtils.readFlowMatch(rtx, name, index));
784 Mockito.verify(rtx, Mockito.times(fcCount)).read(oper, fcPath);
785 Mockito.verify(rtx, Mockito.times(3)).read(oper, path);