2 * Copyright (c) 2014 Cisco Systems, Inc. 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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
11 import java.math.BigInteger;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Objects;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.Matchers;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowTable.FlowCtx;
23 import org.opendaylight.groupbasedpolicy.resolver.ConditionGroup;
24 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
25 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import static org.junit.Assert.*;
43 import static org.mockito.Matchers.*;
45 import static org.mockito.Mockito.*;
47 public class SourceMapperTest extends FlowTableTest {
48 protected static final Logger LOG =
49 LoggerFactory.getLogger(SourceMapperTest.class);
51 public void setup() throws Exception {
53 table = new SourceMapper(ctx);
58 public void testNoPolicy() throws Exception {
59 endpointManager.addEndpoint(localEP().build());
60 ReadWriteTransaction t = dosync(null);
61 verify(t, times(1)).put(any(LogicalDatastoreType.class),
62 Matchers.<InstanceIdentifier<Flow>>any(),
63 any(Flow.class), anyBoolean());
67 public void testMap() throws Exception {
68 Endpoint ep = localEP().build();
69 endpointManager.addEndpoint(ep);
70 policyResolver.addTenant(baseTenant().build());
72 ReadWriteTransaction t = dosync(null);
73 ArgumentCaptor<Flow> ac = ArgumentCaptor.forClass(Flow.class);
74 verify(t, times(2)).put(eq(LogicalDatastoreType.CONFIGURATION),
75 Matchers.<InstanceIdentifier<Flow>>any(),
76 ac.capture(), anyBoolean());
79 HashMap<String, FlowCtx> flowMap = new HashMap<>();
80 for (Flow f : ac.getAllValues()) {
81 flowMap.put(f.getId().getValue(), new FlowCtx(f));
82 if (f.getMatch() == null) {
83 assertEquals(FlowUtils.dropInstructions(),
86 } else if (Objects.equals(ep.getMacAddress(),
87 f.getMatch().getEthernetMatch()
88 .getEthernetSource().getAddress())) {
89 PolicyInfo pi = policyResolver.getCurrentPolicy();
90 List<ConditionName> cset = endpointManager.getCondsForEndpoint(ep);
91 ConditionGroup cg = pi.getEgCondGroup(new EgKey(tid, eg), cset);
93 Instruction ins = f.getInstructions().getInstruction().get(0);
94 assertTrue(ins.getInstruction() instanceof ApplyActionsCase);
95 List<Action> actions = ((ApplyActionsCase)ins.getInstruction()).getApplyActions().getAction();
96 int v = policyManager.getContextOrdinal(tid, eg);
97 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg0.class,
98 BigInteger.valueOf(v)),
99 actions.get(0).getAction());
100 v = policyManager.getCondGroupOrdinal(cg);
101 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg1.class,
102 BigInteger.valueOf(v)),
103 actions.get(1).getAction());
104 v = policyManager.getContextOrdinal(tid, bd);
105 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg4.class,
106 BigInteger.valueOf(v)),
107 actions.get(2).getAction());
108 v = policyManager.getContextOrdinal(tid, fd);
109 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg5.class,
110 BigInteger.valueOf(v)),
111 actions.get(3).getAction());
112 v = policyManager.getContextOrdinal(tid, l3c);
113 assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg6.class,
114 BigInteger.valueOf(v)),
115 actions.get(4).getAction());
119 assertEquals(2, count);
122 verify(t, never()).put(any(LogicalDatastoreType.class),
123 Matchers.<InstanceIdentifier<Flow>>any(),
124 any(Flow.class), anyBoolean());