2 * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. and others. 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
8 package org.opendaylight.genius.mdsalutil.interfaces.testutils;
10 import static org.opendaylight.mdsal.binding.testutils.AssertDataObjects.assertEqualBeans;
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;
20 * This class is a place holder for various openflow flow/group related test utilities.
24 public class FlowAssertTestUtils {
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
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));
39 private static 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().values());
45 instructionList.sort(Comparator.comparing(o -> o.key().toString()));
46 instructionsBuilder.setInstruction(instructionList);
47 flowBuilder.setInstructions(instructionsBuilder.build());
49 return flowBuilder.build();