import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.when;
import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.applyActionIns;
import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.instructions;
import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxOutputRegAction;
import java.util.ArrayList;
import java.util.Collections;
-import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.opendaylight.groupbasedpolicy.api.sf.ChainActionDefinition;
import org.opendaylight.groupbasedpolicy.dto.ConditionGroup;
import org.opendaylight.groupbasedpolicy.dto.EgKey;
import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory;
import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.MapperUtilsTest;
import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.MockSwitchManager;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.AllowAction;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf.ChainAction;
+import org.opendaylight.sfc.provider.api.SfcProviderServicePathAPI;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfcName;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfpName;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPaths;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.ServiceFunctionPathsBuilder;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
+import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPathBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionMatcherName;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ParameterName;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfigBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.nodes.node.TunnelBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.condition.matchers.ConditionMatcherBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.Condition;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.ConditionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValue;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.subject.feature.instance.ParameterValueBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.PolicyBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.ContractBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.clause.ProviderMatchersBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.Rule;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.subject.RuleBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ActionInstance;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ActionInstanceBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-
@RunWith(PowerMockRunner.class)
-@PrepareForTest({PolicyManager.class})
+@PrepareForTest({PolicyManager.class, SfcProviderServicePathAPI.class})
public class PolicyEnforcerTest extends MapperUtilsTest {
- //TODO (att: kblagov) XXX needs redesign
- private final int sameEpgFlows = 1;
- private final int allowTunnelFlows = 1;
- private final int layer4flowsIPv4 = 1;
- private final int layer4flowsIPv6 = 1;
- private final int dropAllFlow = 1;
- private final int arpFlows = 1;
+ private static final String SFC_CHAIN = "sfc-chain";
+ private static final String TCP_DST = "tcp_dst_80";
+ private static final int sameEpgFlows = 1;
+ private static final int allowTunnelFlows = 1;
+ private static final int layer4flowsIPv4 = 1;
+ private static final int layer4flowsIPv6 = 1;
+
+ private NodeConnectorId tunnelId = new NodeConnectorId(NODE_ID.getValue() + ":42");
+ private NodeConnectorId nodeConnector = new NodeConnectorId(NODE_ID.getValue() + CONNECTOR_0);
+
+ // custom mock instances to avoid downcasting of parent's fields
private MockEndpointManager endpointManagerMock;
private MockPolicyManager policyManagerMock;
private MockSwitchManager switchManagerMock;
private MockOfContext ctxMock;
- private NodeConnectorId tunnelId =
- new NodeConnectorId(nodeId.getValue() + ":42");
+ private ActionInstance allowActionInstance;
+ private ActionInstance chainActionInstance;
+
@Before
- public void setup() throws Exception {
+ public void init() {
PowerMockito.stub(PowerMockito.method(PolicyManager.class, "setSfcTableOffset")).toReturn(true);
endpointManagerMock = new MockEndpointManager();
ctxMock = new MockOfContext(null, policyManagerMock, switchManagerMock, endpointManagerMock, null);
table = new PolicyEnforcer(ctxMock, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER());
- switchManagerMock.addSwitch(
- nodeId,
- tunnelId,
- Collections.<NodeConnectorId>emptySet(),
- new OfOverlayNodeConfigBuilder().setTunnel(
- ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.4")))
+ ServiceFunctionPath path = new ServiceFunctionPathBuilder().setName(new SfpName("sfp-name"))
+ .setServiceChainName(new SfcName(SFC_CHAIN))
+ .setSymmetric(true)
+ .build();
+ ServiceFunctionPaths paths =
+ new ServiceFunctionPathsBuilder().setServiceFunctionPath(ImmutableList.of(path)).build();
+
+ PowerMockito.mockStatic(SfcProviderServicePathAPI.class);
+ when(SfcProviderServicePathAPI.readAllServiceFunctionPaths()).thenReturn(paths);
+
+ allowActionInstance = new ActionInstanceBuilder().setName(new ActionName("allow"))
+ .setActionDefinitionId(new AllowAction().getId())
+ .build();
+
+ ParameterValue pv = new ParameterValueBuilder().setName(new ParameterName(ChainActionDefinition.SFC_CHAIN_NAME))
+ .setStringValue(SFC_CHAIN)
+ .build();
+ chainActionInstance = new ActionInstanceBuilder().setName(new ActionName("chain"))
+ .setActionDefinitionId(new ChainAction().getId())
+ .setParameterValue(ImmutableList.of(pv))
+ .build();
+
+ switchManagerMock
+ .addSwitch(NODE_ID, tunnelId,
+ Collections
+ .emptySet(),
+ new OfOverlayNodeConfigBuilder()
+ .setTunnel(ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.4")))
.setTunnelType(TunnelTypeVxlan.class)
.setNodeConnectorId(tunnelId)
- .build())).build());
+ .build()))
+ .build());
}
@Test
- public void testSameEg() throws Exception {
- Endpoint ep1 = endpointBuilder(new IpAddress(IPV4_1.toCharArray()), new MacAddress(MAC_0), nodeConnectorId, eg, bd)
- .build();
+ public void test_SameEg() throws Exception {
+ EndpointBuilder ep1Builder = buildEndpoint(IPV4_0, MAC_0, nodeConnector);
+ ep1Builder.setEndpointGroup(ENDPOINT_GROUP_0);
+ ep1Builder.setL2Context(L2BD_ID);
+ Endpoint ep1 = ep1Builder.build();
endpointManagerMock.addEndpoint(ep1);
- Endpoint ep2 = endpointBuilder(new IpAddress(IPV4_2.toCharArray()), new MacAddress(MAC_1), nodeConnectorId, eg, bd)
- .build();
+ EndpointBuilder ep2Builder = buildEndpoint(IPV4_1, MAC_1, nodeConnector);
+ ep2Builder.setEndpointGroup(ENDPOINT_GROUP_1);
+ ep2Builder.setL2Context(L2BD_ID);
+ Endpoint ep2 = ep2Builder.build();
endpointManagerMock.addEndpoint(ep2);
- ctxMock.addTenant(baseTenantBuilder().setPolicy(new PolicyBuilder(baseTenantBuilder().getPolicy())
+ ctxMock.addTenant(buildTenant().setPolicy(new PolicyBuilder(buildTenant().getPolicy())
.setContract(ImmutableList.of(baseContract(null).build())).build()).build());
ofWriter = new OfWriter();
table.sync(ep1, ofWriter);
- assertTrue(!ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+
+ assertTrue(!ofWriter.getTableForNode(NODE_ID, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
.getFlow()
.isEmpty());
+
int count = 0;
- HashMap<String, Flow> flowMap = new HashMap<>();
- for (Flow f : ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
- flowMap.put(f.getId().getValue(), f);
+ for (Flow f : ofWriter.getTableForNode(NODE_ID, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+ .getFlow()) {
if (isAllowSameEpg(f)) {
- count += 1;
+ count++;
}
}
assertEquals(sameEpgFlows, count);
- int totalFlows = sameEpgFlows + allowTunnelFlows + dropAllFlow;
- assertEquals(totalFlows, ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
- .getFlow()
- .size());
+
+ int totalFlows = sameEpgFlows + allowTunnelFlows + layer4flowsIPv4 + layer4flowsIPv6;
+ assertEquals(totalFlows, ofWriter
+ .getTableForNode(NODE_ID, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow().size());
}
@Test
- public void testDifferentEg() throws Exception {
+ public void test_DifferentEg() throws Exception {
int totalFlows = sameEpgFlows + allowTunnelFlows;
- assertEquals(totalFlows, doTestDifferentEg(ImmutableList.of(baseSubject(null).build())));
+ assertEquals(totalFlows, doTestDifferentEg(ImmutableList.of(baseSubject(null).build()), allowActionInstance));
// one layer4 flow for each direction
totalFlows = sameEpgFlows + allowTunnelFlows + (2 * layer4flowsIPv4) + (2 * layer4flowsIPv6);
- assertEquals(totalFlows, doTestDifferentEg(ImmutableList.of(baseSubject(Direction.Bidirectional).build())));
+ assertEquals(totalFlows,
+ doTestDifferentEg(ImmutableList.of(baseSubject(Direction.Bidirectional).build()), allowActionInstance));
totalFlows = sameEpgFlows + allowTunnelFlows + layer4flowsIPv4 + layer4flowsIPv6;
- assertEquals(totalFlows, doTestDifferentEg(ImmutableList.of(baseSubject(Direction.In).build())));
- assertEquals(totalFlows, doTestDifferentEg(ImmutableList.of(baseSubject(Direction.Out).build())));
+ assertEquals(totalFlows,
+ doTestDifferentEg(ImmutableList.of(baseSubject(Direction.In).build()), allowActionInstance));
+ assertEquals(totalFlows,
+ doTestDifferentEg(ImmutableList.of(baseSubject(Direction.Out).build()), allowActionInstance));
}
@Test
- public void doTestRule() throws Exception {
- Rule rule1 = new RuleBuilder().setActionRef(
- ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
- .setClassifierRef(
- createClassifierRefs(ImmutableMap.of("tcp_dst_80", Direction.In, "tcp_src_80",
- Direction.In)))
+ public void test_Rules() throws Exception {
+ Rule rule1 = new RuleBuilder()
+ .setActionRef(ImmutableList.of(new ActionRefBuilder().setName(new ActionName(ALLOW)).build()))
+ .setClassifierRef(createClassifierRefs(ImmutableMap.of(TCP_DST, Direction.In, TCP_SRC, Direction.In)))
.build();
- Rule rule2 = new RuleBuilder().setActionRef(
- ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
- .setClassifierRef(
- createClassifierRefs(ImmutableMap.of("tcp_dst_80", Direction.In, "tcp_src_80",
- Direction.Out)))
+ Rule rule2 = new RuleBuilder()
+ .setActionRef(ImmutableList.of(new ActionRefBuilder().setName(new ActionName(ALLOW)).build()))
+ .setClassifierRef(createClassifierRefs(ImmutableMap.of(TCP_DST, Direction.In, TCP_SRC, Direction.Out)))
.build();
- Rule rule3 = new RuleBuilder().setActionRef(
- ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
- .setClassifierRef(
- createClassifierRefs(ImmutableMap.of("tcp_dst_80", Direction.In, "tcp_src_80",
- Direction.Out, "ether_type", Direction.In)))
+ Rule rule3 = new RuleBuilder()
+ .setActionRef(ImmutableList.of(new ActionRefBuilder().setName(new ActionName(ALLOW)).build()))
+ .setClassifierRef(createClassifierRefs(
+ ImmutableMap.of(TCP_DST, Direction.In, TCP_SRC, Direction.Out, "ether_type", Direction.In)))
.build();
- Rule rule4 = new RuleBuilder().setActionRef(
- ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
- .setClassifierRef(
- createClassifierRefs(ImmutableMap.of("tcp_dst_80", Direction.In, "tcp_dst_90",
- Direction.In)))
+ Rule rule4 = new RuleBuilder()
+ .setActionRef(ImmutableList.of(new ActionRefBuilder().setName(new ActionName(ALLOW)).build()))
+ .setClassifierRef(createClassifierRefs(ImmutableMap.of(TCP_DST, Direction.In, "tcp_dst_90", Direction.In)))
.build();
int totalFlows = sameEpgFlows + allowTunnelFlows + layer4flowsIPv4 + layer4flowsIPv6;
assertEquals(totalFlows,
- doTestDifferentEg(ImmutableList.of(createSubject("s1", ImmutableList.of(rule1)))));
+ doTestDifferentEg(ImmutableList.of(createSubject("s1", ImmutableList.of(rule1))), allowActionInstance));
// one layer4 flow for each direction
totalFlows = sameEpgFlows + allowTunnelFlows + (2 * layer4flowsIPv4) + (2 * layer4flowsIPv6);
assertEquals(totalFlows,
- doTestDifferentEg(ImmutableList.of(createSubject("s2", ImmutableList.of(rule2)))));
- // only one ether_type for out direction
+ doTestDifferentEg(ImmutableList.of(createSubject("s2", ImmutableList.of(rule2))), allowActionInstance));
+ // only one ether_type for out direction
totalFlows = sameEpgFlows + allowTunnelFlows + (2 * layer4flowsIPv4) + layer4flowsIPv6;
assertEquals(totalFlows,
- doTestDifferentEg(ImmutableList.of(createSubject("s3", ImmutableList.of(rule3)))));
+ doTestDifferentEg(ImmutableList.of(createSubject("s3", ImmutableList.of(rule3))), allowActionInstance));
totalFlows = sameEpgFlows + allowTunnelFlows;
assertEquals(totalFlows,
- doTestDifferentEg(ImmutableList.of(createSubject("s4", ImmutableList.of(rule4)))));
+ doTestDifferentEg(ImmutableList.of(createSubject("s4", ImmutableList.of(rule4))), allowActionInstance));
+ }
+
+ @Test
+ public void test_Rules_ChainAction() throws Exception {
+ Rule rule1 = new RuleBuilder()
+ .setActionRef(ImmutableList.of(new ActionRefBuilder().setName(new ActionName(CHAIN)).build()))
+ .setClassifierRef(createClassifierRefs(ImmutableMap.of(TCP_DST, Direction.In, TCP_SRC, Direction.In)))
+ .build();
+ Rule rule2 = new RuleBuilder()
+ .setActionRef(ImmutableList.of(new ActionRefBuilder().setName(new ActionName(CHAIN)).build()))
+ .setClassifierRef(createClassifierRefs(ImmutableMap.of(TCP_DST, Direction.In, TCP_SRC, Direction.Out)))
+ .build();
+ Rule rule3 = new RuleBuilder()
+ .setActionRef(ImmutableList.of(new ActionRefBuilder().setName(new ActionName(CHAIN)).build()))
+ .setClassifierRef(createClassifierRefs(
+ ImmutableMap.of(TCP_DST, Direction.In, TCP_SRC, Direction.Out, "ether_type", Direction.In)))
+ .build();
+ Rule rule4 = new RuleBuilder()
+ .setActionRef(ImmutableList.of(new ActionRefBuilder().setName(new ActionName(CHAIN)).build()))
+ .setClassifierRef(createClassifierRefs(ImmutableMap.of(TCP_DST, Direction.In, "tcp_dst_90", Direction.In)))
+ .build();
+
+ assertEquals(2,
+ doTestDifferentEg(ImmutableList.of(createSubject("s1", ImmutableList.of(rule1))), chainActionInstance));
+ assertEquals(2,
+ doTestDifferentEg(ImmutableList.of(createSubject("s2", ImmutableList.of(rule2))), chainActionInstance));
+ assertEquals(2,
+ doTestDifferentEg(ImmutableList.of(createSubject("s3", ImmutableList.of(rule3))), chainActionInstance));
+ assertEquals(2,
+ doTestDifferentEg(ImmutableList.of(createSubject("s4", ImmutableList.of(rule4))), chainActionInstance));
}
- private int doTestDifferentEg(List<Subject> subjects) throws Exception {
- Endpoint ep1 = endpointBuilder(new IpAddress(IPV4_1.toCharArray()), new MacAddress(MAC_0), nodeConnectorId, eg, bd)
- .build();
+ private int doTestDifferentEg(List<Subject> subjects, ActionInstance actionInstance) throws Exception {
+ EndpointBuilder ep1Builder = buildEndpoint(IPV4_0, MAC_0, nodeConnector);
+ ep1Builder.setEndpointGroup(ENDPOINT_GROUP_0);
+ ep1Builder.setL2Context(L2BD_ID);
+ Endpoint ep1 = ep1Builder.build();
endpointManagerMock.addEndpoint(ep1);
- Endpoint ep2 = endpointBuilder(new IpAddress(IPV4_2.toCharArray()), new MacAddress(MAC_1), nodeConnectorId, eg2, bd)
- .build();
+ EndpointBuilder ep2Builder = buildEndpoint(IPV4_1, MAC_1, nodeConnector);
+ ep2Builder.setEndpointGroup(ENDPOINT_GROUP_1);
+ ep2Builder.setL2Context(L2BD_ID);
+ Endpoint ep2 = ep2Builder.build();
endpointManagerMock.addEndpoint(ep2);
- ctxMock.addTenant(baseTenantBuilder().setPolicy(new PolicyBuilder(baseTenantBuilder().getPolicy())
- .setContract(ImmutableList.of(baseContract(subjects).build())).build()).build());
+
+ TenantBuilder tb = buildTenant(actionInstance);
+ ctxMock.addTenant(tb.setPolicy(
+ new PolicyBuilder(tb.getPolicy()).setContract(ImmutableList.of(baseContract(subjects).build())).build())
+ .build());
ofWriter = new OfWriter();
table.sync(ep1, ofWriter);
- assertTrue(!ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+
+ assertTrue(!ofWriter.getTableForNode(NODE_ID, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
.getFlow()
.isEmpty());
+
int count = 0;
- for (Flow f : ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
+ for (Flow f : ofWriter.getTableForNode(NODE_ID, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+ .getFlow()) {
if (isAllowSameEpg(f)) {
- count += 1;
+ count++;
} else if (f.getMatch() != null && Objects.equals(tunnelId, f.getMatch().getInPort())) {
assertEquals(instructions(applyActionIns(nxOutputRegAction(NxmNxReg7.class))), f.getInstructions());
- count += 1;
- } else if (f.getMatch() != null
- && f.getMatch().getEthernetMatch() != null
- && Objects.equals(FlowUtils.IPv4, f.getMatch()
- .getEthernetMatch()
- .getEthernetType()
- .getType()
- .getValue())
+ count++;
+ } else if (f.getMatch() != null && f.getMatch().getEthernetMatch() != null
+ && Objects.equals(FlowUtils.IPv4,
+ f.getMatch().getEthernetMatch().getEthernetType().getType().getValue())
&& f.getMatch().getIpMatch() != null
&& Objects.equals((short) 6, f.getMatch().getIpMatch().getIpProtocol())
&& f.getMatch().getLayer4Match() != null
&& (Objects.equals(new PortNumber(80),
- ((TcpMatch) f.getMatch().getLayer4Match()).getTcpSourcePort()) || Objects.equals(
- new PortNumber(80),
- ((TcpMatch) f.getMatch().getLayer4Match()).getTcpDestinationPort()))) {
- count += 1;
- } else if (f.getMatch() != null
- && f.getMatch().getEthernetMatch() != null
- && Objects.equals(FlowUtils.IPv6, f.getMatch()
- .getEthernetMatch()
- .getEthernetType()
- .getType()
- .getValue())
+ ((TcpMatch) f.getMatch().getLayer4Match()).getTcpSourcePort())
+ || Objects.equals(new PortNumber(80),
+ ((TcpMatch) f.getMatch().getLayer4Match()).getTcpDestinationPort()))) {
+ count++;
+ } else if (f.getMatch() != null && f.getMatch().getEthernetMatch() != null
+ && Objects.equals(FlowUtils.IPv6,
+ f.getMatch().getEthernetMatch().getEthernetType().getType().getValue())
&& f.getMatch().getIpMatch() != null
&& Objects.equals((short) 6, f.getMatch().getIpMatch().getIpProtocol())
&& f.getMatch().getLayer4Match() != null
&& (Objects.equals(new PortNumber(80),
- ((TcpMatch) f.getMatch().getLayer4Match()).getTcpSourcePort()) || Objects.equals(
- new PortNumber(80),
- ((TcpMatch) f.getMatch().getLayer4Match()).getTcpDestinationPort()))) {
- count += 1;
+ ((TcpMatch) f.getMatch().getLayer4Match()).getTcpSourcePort())
+ || Objects.equals(new PortNumber(80),
+ ((TcpMatch) f.getMatch().getLayer4Match()).getTcpDestinationPort()))) {
+ count++;
}
}
return count;
}
@Test
- public void testConditions() throws Exception {
+ public void test_Conditions() throws Exception {
Condition cond1 = new ConditionBuilder().setName(new ConditionName("cond1")).build();
Condition cond2 = new ConditionBuilder().setName(new ConditionName("cond2")).build();
- Endpoint ep1 = endpointBuilder(new IpAddress(IPV4_1.toCharArray()), new MacAddress(MAC_0), nodeConnectorId, eg, bd)
- .setCondition(ImmutableList.of(cond1.getName())).build();
+ EndpointBuilder ep1Builder = buildEndpoint(IPV4_0, MAC_0, nodeConnector);
+ ep1Builder.setEndpointGroup(ENDPOINT_GROUP_0);
+ ep1Builder.setL2Context(L2BD_ID);
+ ep1Builder.setCondition(ImmutableList.of(cond1.getName())).build();
+ Endpoint ep1 = ep1Builder.build();
endpointManagerMock.addEndpoint(ep1);
- Endpoint ep2 = endpointBuilder(new IpAddress(IPV4_2.toCharArray()), new MacAddress(MAC_1), nodeConnectorId, eg2, bd)
- .setCondition(ImmutableList.of(cond1.getName(), cond2.getName()))
- .build();
+ EndpointBuilder ep2Builder = buildEndpoint(IPV4_1, MAC_1, nodeConnector);
+ ep2Builder.setEndpointGroup(ENDPOINT_GROUP_1);
+ ep2Builder.setL2Context(L2BD_ID);
+ ep2Builder.setCondition(ImmutableList.of(cond1.getName(), cond2.getName())).build();
+ Endpoint ep2 = ep2Builder.build();
endpointManagerMock.addEndpoint(ep2);
- TenantBuilder tb = baseTenantBuilder().setPolicy(new PolicyBuilder(baseTenantBuilder().getPolicy()).setContract(
- ImmutableList.of(new ContractBuilder().setId(cid)
- .setSubject(ImmutableList.of(baseSubject(Direction.Out).build()))
- .setClause(
- ImmutableList.of(new ClauseBuilder().setName(new ClauseName("test"))
- .setSubjectRefs(ImmutableList.of(new SubjectName("s1")))
- .setConsumerMatchers(
- new ConsumerMatchersBuilder().setConditionMatcher(
- ImmutableList.of(new ConditionMatcherBuilder().setName(
- new ConditionMatcherName("m1"))
- .setCondition(ImmutableList.of(cond1, cond2))
- .setMatchType(MatchType.Any)
- .build())).build())
- .setProviderMatchers(
- new ProviderMatchersBuilder().setConditionMatcher(
- ImmutableList.of(new ConditionMatcherBuilder().setName(
- new ConditionMatcherName("m2"))
- .setCondition(ImmutableList.of(cond1, cond2))
- .setMatchType(MatchType.All)
- .build())).build())
+ TenantBuilder tb = buildTenant().setPolicy(new PolicyBuilder(buildTenant().getPolicy())
+ .setContract(ImmutableList.of(new ContractBuilder().setId(CONTRACT_ID)
+ .setSubject(ImmutableList.of(baseSubject(Direction.Out).build()))
+ .setClause(ImmutableList.of(new ClauseBuilder().setName(new ClauseName("test"))
+ .setSubjectRefs(ImmutableList.of(new SubjectName("s1")))
+ .setConsumerMatchers(new ConsumerMatchersBuilder().setConditionMatcher(
+ ImmutableList.of(new ConditionMatcherBuilder().setName(new ConditionMatcherName("m1"))
+ .setCondition(ImmutableList.of(cond1, cond2))
+ .setMatchType(MatchType.Any)
.build()))
- .build())).build());
+ .build())
+ .setProviderMatchers(new ProviderMatchersBuilder()
+ .setConditionMatcher(
+ ImmutableList.of(new ConditionMatcherBuilder().setName(new ConditionMatcherName("m2"))
+ .setCondition(ImmutableList.of(cond1, cond2))
+ .setMatchType(MatchType.All)
+ .build()))
+ .build())
+ .build()))
+ .build()))
+ .build());
ctxMock.addTenant(tb.build());
PolicyInfo policy = ctxMock.getCurrentPolicy();
ofWriter = new OfWriter();
table.sync(ep1, ofWriter);
// one layer4 flow for each direction
- int totalFlows = sameEpgFlows + allowTunnelFlows + layer4flowsIPv4 + layer4flowsIPv6 + arpFlows + dropAllFlow;;
- assertEquals(totalFlows, ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
- .getFlow()
- .size());
- HashMap<String, Flow> flowMap = new HashMap<>();
- for (Flow f : ofWriter.getTableForNode(nodeId, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
- flowMap.put(f.getId().getValue(), f);
+ int dropAllFlow = 1;
+ int arpFlows = 1;
+ int totalFlows = sameEpgFlows + allowTunnelFlows + layer4flowsIPv4 + layer4flowsIPv6 + arpFlows + dropAllFlow;
+ assertEquals(totalFlows, ofWriter
+ .getTableForNode(NODE_ID, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow().size());
+ for (Flow f : ofWriter.getTableForNode(NODE_ID, ctxMock.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+ .getFlow()) {
if (f.getMatch() != null && f.getMatch().getEthernetMatch() != null) {
count++;
}
}
- //flows with ether_type match
+ // flows with ether_type match
totalFlows = layer4flowsIPv4 + layer4flowsIPv6 + arpFlows;
assertEquals(totalFlows, count);
}
// (these register values don't have to be equal)
boolean res = false;
if (flow != null && flow.getMatch() != null) {
- GeneralAugMatchNodesNodeTableFlow genAug = flow.getMatch().getAugmentation(
- GeneralAugMatchNodesNodeTableFlow.class);
+ GeneralAugMatchNodesNodeTableFlow genAug =
+ flow.getMatch().getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
if (genAug != null) {
List<ExtensionList> extensions = genAug.getExtensionList();
if (extensions != null && extensions.size() == 2) {
Class<? extends ExtensionKey> extensionKey = extensionList.getExtensionKey();
Extension extension = extensionList.getExtension();
if (extensionKey != null && extension != null) {
- NxAugMatchNodesNodeTableFlow nxAugMatch = extension.getAugmentation(NxAugMatchNodesNodeTableFlow.class);
+ NxAugMatchNodesNodeTableFlow nxAugMatch =
+ extension.getAugmentation(NxAugMatchNodesNodeTableFlow.class);
if (nxAugMatch != null && nxAugMatch.getNxmNxReg() != null) {
if (extensionKey.equals(NxmNxReg0Key.class)) {
reg0 = nxAugMatch.getNxmNxReg().getValue();
return res;
}
- protected ContractBuilder baseContract(List<Subject> subjects) {
- ContractBuilder contractBuilder = new ContractBuilder().setId(cid).setSubject(subjects);
- // TODO refactor
+ private ContractBuilder baseContract(List<Subject> subjects) {
+ ContractBuilder contractBuilder = new ContractBuilder().setId(CONTRACT_ID).setSubject(subjects);
if (subjects == null) {
return contractBuilder.setClause(ImmutableList.of(new ClauseBuilder().setName(new ClauseName("test"))
- .setSubjectRefs(ImmutableList.<SubjectName>of(new SubjectName("s1")))
+ .setSubjectRefs(ImmutableList.of(new SubjectName("s1")))
.build()));
}
List<SubjectName> subjectNames = new ArrayList<>();
for (Subject subject : subjects) {
subjectNames.add(subject.getName());
}
- return contractBuilder.setClause(ImmutableList.of(new ClauseBuilder().setName(new ClauseName("test"))
- .setSubjectRefs(subjectNames)
- .build()));
+ return contractBuilder.setClause(ImmutableList
+ .of(new ClauseBuilder().setName(new ClauseName("test")).setSubjectRefs(subjectNames).build()));
}
- protected SubjectBuilder baseSubject(Direction direction) {
- return new SubjectBuilder()
- .setName(new SubjectName("s1"))
+ private SubjectBuilder baseSubject(Direction direction) {
+ return new SubjectBuilder().setName(new SubjectName("s1"))
.setRule(ImmutableList.of(new RuleBuilder()
- .setActionRef(ImmutableList.of(new ActionRefBuilder()
- .setName(new ActionName("allow"))
- .build()))
- .setClassifierRef(ImmutableList.of(new ClassifierRefBuilder()
- .setName(new ClassifierName("tcp_dst_80"))
+ .setActionRef(ImmutableList.of(new ActionRefBuilder().setName(new ActionName(ALLOW)).build()))
+ .setClassifierRef(ImmutableList.of(new ClassifierRefBuilder().setName(new ClassifierName(TCP_DST))
.setDirection(direction)
- .setInstanceName(new ClassifierName("tcp_dst_80"))
+ .setInstanceName(new ClassifierName(TCP_DST))
.build()))
.build()));
}
- protected Subject createSubject(String name, List<Rule> rules){
+ private Subject createSubject(String name, List<Rule> rules) {
return new SubjectBuilder().setName(new SubjectName(name)).setRule(rules).build();
}
- protected List<ClassifierRef> createClassifierRefs(Map<String, Direction> refNamesAndDirections) {
+ private List<ClassifierRef> createClassifierRefs(Map<String, Direction> refNamesAndDirections) {
List<ClassifierRef> refs = new ArrayList<>();
for (String refName : refNamesAndDirections.keySet()) {
refs.add(new ClassifierRefBuilder().setName(new ClassifierName(refName))