Add some unit tests for conditions 43/10043/1
authorRob Adams <readams@readams.net>
Mon, 18 Aug 2014 20:04:05 +0000 (13:04 -0700)
committerRob Adams <readams@readams.net>
Mon, 18 Aug 2014 20:04:05 +0000 (13:04 -0700)
Change-Id: I42d17f849a40bdffcbb9071a7d6724e6acf800b4
Signed-off-by: Rob Adams <readams@readams.net>
groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/flow/PolicyEnforcer.java
groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/resolver/InheritanceUtils.java
groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/flow/OfTableTest.java
groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/flow/PolicyEnforcerTest.java

index 2abca0faa71906067def7c696a7bad07a90e54c8..b5b6844c9651eaca5d8d8429d60a7e6bf169ce5e 100644 (file)
@@ -118,7 +118,7 @@ public class PolicyEnforcer extends FlowTable {
                 
                     for (Endpoint dst : ctx.epManager.getEndpointsForGroup(depg)) {
                 
-                        conds = ctx.epManager.getCondsForEndpoint(src);
+                        conds = ctx.epManager.getCondsForEndpoint(dst);
                         ConditionGroup dcg = 
                                 policyInfo.getEgCondGroup(new EgKey(dst.getTenant(), 
                                                                     dst.getEndpointGroup()),
index 486a67cf8c59fbe06d555c830a849bea97f1ed3a..2992edae7080592293590fa076ad181fbf24631e 100644 (file)
@@ -539,6 +539,7 @@ public class InheritanceUtils {
         resolveQualityMatcher(Collection<QualityMatcher> toResolve,
                               HashMap<QualityMatcherName,
                                       QualityMatcher> matchers) {
+        if (toResolve == null) return;
         for (QualityMatcher qm : toResolve) {
             if (matchers.containsKey(qm.getName())) {
                 QualityMatcher oqm = matchers.get(qm.getName());
@@ -567,6 +568,7 @@ public class InheritanceUtils {
         resolveCapabilityMatcher(Collection<CapabilityMatcher> toResolve,
                                  HashMap<CapabilityMatcherName,
                                          CapabilityMatcher> matchers) {
+        if (toResolve == null) return;
         for (CapabilityMatcher m : toResolve) {
             if (matchers.containsKey(m.getName())) {
                 CapabilityMatcher om = matchers.get(m.getName());
@@ -595,6 +597,7 @@ public class InheritanceUtils {
         resolveRequirementMatcher(Collection<RequirementMatcher> toResolve,
                                   HashMap<RequirementMatcherName,
                                           RequirementMatcher> matchers) {
+        if (toResolve == null) return;
         for (RequirementMatcher m : toResolve) {
             if (matchers.containsKey(m.getName())) {
                 RequirementMatcher om = matchers.get(m.getName());
@@ -623,6 +626,7 @@ public class InheritanceUtils {
         resolveConditionMatcher(Collection<ConditionMatcher> toResolve,
                                  HashMap<ConditionMatcherName,
                                          ConditionMatcher> matchers) {
+        if (toResolve == null) return;
         for (ConditionMatcher m : toResolve) {
             if (matchers.containsKey(m.getName())) {
                 ConditionMatcher om = matchers.get(m.getName());
index aa441aef7623d10f8bb23872cb9574dc9b874243..2c228d60394777c4550699be0354f4addab40c47 100644 (file)
@@ -174,18 +174,7 @@ public class OfTableTest {
                 .build())
             .setContract(ImmutableList.of(new ContractBuilder()
                 .setId(cid)
-                .setSubject(ImmutableList.of(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_80"))
-                             .setDirection(direction)
-                             .build()))
-                         .build()))
-                    .build()))
+                .setSubject(ImmutableList.of(baseSubject(direction).build()))
                 .setClause(ImmutableList.of(new ClauseBuilder()
                      .setName(new ClauseName("test"))
                      .setSubjectRefs(ImmutableList.of(new SubjectName("s1")))
@@ -193,6 +182,20 @@ public class OfTableTest {
                 .build()));
     }
     
+    protected 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_80"))
+                    .setDirection(direction)
+                    .build()))
+                .build()));
+    }
+    
     protected EndpointBuilder baseEP() {
         return new EndpointBuilder()
             .setL2Context(bd)
index cb4e264346864ec3c9431e3edb4d2af57bce434b..d85ecc76a7d0116b0637dec20d410d54dd48a759 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
 
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Objects;
 
 import org.junit.Before;
@@ -19,20 +20,45 @@ import org.mockito.Matchers;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowTable.FlowCtx;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.RegMatch;
+import org.opendaylight.groupbasedpolicy.resolver.ConditionGroup;
+import org.opendaylight.groupbasedpolicy.resolver.EgKey;
+import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
 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.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.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.SubjectName;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfigBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Matcher.MatchType;
+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.tenants.TenantBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ContractBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.ClauseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ConsumerMatchersBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ProviderMatchersBuilder;
 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.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg7;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.collect.ImmutableList;
+
 import static org.junit.Assert.*;
 
 import static org.mockito.Matchers.*;
@@ -57,7 +83,6 @@ public class PolicyEnforcerTest extends FlowTableTest {
                                     .setTunnelIp(new IpAddress(new Ipv4Address("1.2.3.4")))
                                     .build());
     }
-    
 
     @Test
     public void testNoEps() throws Exception {
@@ -170,6 +195,108 @@ public class PolicyEnforcerTest extends FlowTableTest {
 
     @Test
     public void testConditions() throws Exception {
-        // XXX TODO
+        Condition cond1 = new ConditionBuilder()
+            .setName(new ConditionName("cond1"))
+            .build();
+        Condition cond2 = new ConditionBuilder()
+            .setName(new ConditionName("cond2"))
+            .build();
+
+        Endpoint ep1 = localEP()
+            .setCondition(ImmutableList.of(cond1.getName()))
+            .build();
+        endpointManager.addEndpoint(ep1);
+        Endpoint ep2 = localEP()
+            .setMacAddress(new MacAddress("00:00:00:00:00:02"))
+            .setCondition(ImmutableList.of(cond1.getName(), cond2.getName()))
+            .setEndpointGroup(eg2)
+            .build();
+        endpointManager.addEndpoint(ep2);        
+        
+        TenantBuilder tb = baseTenant()
+            .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())
+                    .build()))
+                .build()));
+        policyResolver.addTenant(tb.build());
+
+        PolicyInfo policy = policyResolver.getCurrentPolicy();
+        List<ConditionName> ep1c = endpointManager.getCondsForEndpoint(ep1);
+        ConditionGroup cg1 = 
+                policy.getEgCondGroup(new EgKey(tb.getId(), 
+                                                ep1.getEndpointGroup()),
+                                      ep1c);
+        List<ConditionName> ep2c = endpointManager.getCondsForEndpoint(ep2);
+        ConditionGroup cg2 = 
+                policy.getEgCondGroup(new EgKey(tb.getId(), 
+                                                ep2.getEndpointGroup()),
+                                      ep2c);
+        int cg1Id = policyManager.getCondGroupOrdinal(cg1);
+        int cg2Id = policyManager.getCondGroupOrdinal(cg2);
+        int eg1Id = policyManager.getContextOrdinal(ep1.getTenant(),
+                                                    ep1.getEndpointGroup());
+        int eg2Id = policyManager.getContextOrdinal(ep1.getTenant(),
+                                                    ep2.getEndpointGroup());
+
+        assertNotEquals(cg1Id, cg2Id);
+
+        MatchBuilder mb = new MatchBuilder();
+        FlowUtils.addNxRegMatch(mb, 
+                                RegMatch.of(NxmNxReg0.class, Long.valueOf(eg1Id)),
+                                RegMatch.of(NxmNxReg1.class, Long.valueOf(cg1Id)),
+                                RegMatch.of(NxmNxReg2.class, Long.valueOf(eg2Id)),
+                                RegMatch.of(NxmNxReg3.class, Long.valueOf(cg2Id)));
+        GeneralAugMatchNodesNodeTableFlow m1 =
+                mb.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
+        FlowUtils.addNxRegMatch(mb, 
+                                RegMatch.of(NxmNxReg0.class, Long.valueOf(eg2Id)),
+                                RegMatch.of(NxmNxReg1.class, Long.valueOf(cg2Id)),
+                                RegMatch.of(NxmNxReg2.class, Long.valueOf(eg1Id)),
+                                RegMatch.of(NxmNxReg3.class, Long.valueOf(cg1Id)));
+        GeneralAugMatchNodesNodeTableFlow m2 =
+                mb.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
+
+        ReadWriteTransaction t = dosync(null);
+        ArgumentCaptor<Flow> ac = ArgumentCaptor.forClass(Flow.class);
+        verify(t, atLeastOnce()).put(eq(LogicalDatastoreType.CONFIGURATION), 
+                                     Matchers.<InstanceIdentifier<Flow>>any(),
+                                     ac.capture(), anyBoolean());
+        int count = 0;
+        HashMap<String, FlowCtx> flowMap = new HashMap<>();
+        for (Flow f : ac.getAllValues()) {
+            flowMap.put(f.getId().getValue(), new FlowCtx(f));
+            if (f.getMatch() != null &&
+                f.getMatch().getEthernetMatch() != null) {
+                GeneralAugMatchNodesNodeTableFlow fm =
+                        f.getMatch().getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
+                assertTrue(Objects.equals(fm, m1) ||
+                           Objects.equals(fm, m2));
+                count += 1;
+            }
+        }
+        assertEquals(2, count);
+
+        t = dosync(flowMap);
+        verify(t, never()).put(any(LogicalDatastoreType.class), 
+                               Matchers.<InstanceIdentifier<Flow>>any(), 
+                               any(Flow.class), anyBoolean());
     }
 }