b2a9071e169feaf0ed70dedc193f1d26b1a88882
[genius.git] / mdsalutil / mdsalutil-api / src / test / java / org / opendaylight / genius / mdsalutil / interfaces / testutils / FlowAssertTestUtils.java
1 /*
2  * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.genius.mdsalutil.interfaces.testutils;
9
10 import static org.opendaylight.mdsal.binding.testutils.AssertDataObjects.assertEqualBeans;
11
12 import java.util.ArrayList;
13 import java.util.Comparator;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
18
19 /**
20  * This class is a place holder for various openflow flow/group related test utilities.
21  *
22  * @author Faseela K
23  */
24 public class FlowAssertTestUtils {
25
26     // The problem we're fixing here is that, in MD-SAL binding v1, YANG lists are represented
27     // as Java lists but they don't preserve order (unless they specify “ordered-by user”).
28     // YANG keyed lists in particular are backed by maps, so you can store such a list in the
29     // MD-SAL and get it back in a different order.
30     // When comparing beans involving such lists, we need to sort the lists before comparing
31     // them.
32     // Hence, we rebuild instances of Flow, and sort the affected lists
33     // in the augmentations.
34     public void assertFlowsInAnyOrder(Flow expected, Flow actual) {
35         // Re-create the flows to avoid re-ordering problems
36         assertEqualBeans(rebuildFlow(expected), rebuildFlow(actual));
37     }
38
39     private Flow rebuildFlow(Flow flow) {
40         FlowBuilder flowBuilder = new FlowBuilder(flow);
41         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
42         if (flow.getInstructions() != null) {
43             ArrayList<Instruction> instructionList =
44                 new ArrayList<>(flow.getInstructions().nonnullInstruction());
45             instructionList.sort(Comparator.comparing(o -> o.key().toString()));
46             instructionsBuilder.setInstruction(instructionList);
47             flowBuilder.setInstructions(instructionsBuilder.build());
48         }
49         return flowBuilder.build();
50     }
51 }