Increasing coverage on faas-renderer 91/46491/3
authorKonstantin Blagov <kblagov@cisco.com>
Tue, 4 Oct 2016 16:32:43 +0000 (18:32 +0200)
committerKonstantin Blagov <kblagov@cisco.com>
Fri, 7 Oct 2016 13:12:41 +0000 (13:12 +0000)
Change-Id: I631bd5e1317cbf4a037845c4b8941f2da184756e
Signed-off-by: Konstantin Blagov <kblagov@cisco.com>
renderers/faas/src/main/java/org/opendaylight/groupbasedpolicy/renderer/faas/FaasContractManagerListener.java
renderers/faas/src/main/java/org/opendaylight/groupbasedpolicy/renderer/faas/FaasEndpointManagerListener.java
renderers/faas/src/main/java/org/opendaylight/groupbasedpolicy/renderer/faas/FaasPolicyManager.java
renderers/faas/src/test/java/org/opendaylight/groupbasedpolicy/renderer/faas/FaasContractManagerListenerCovrgTest.java
renderers/faas/src/test/java/org/opendaylight/groupbasedpolicy/renderer/faas/FaasEndpointManagerListenerCovrgTest.java
renderers/faas/src/test/java/org/opendaylight/groupbasedpolicy/renderer/faas/FaasPolicyManagerCovrgTest.java

index d44e7c0e6d4975348a7c00ed3f4f71bccc8d284b..f8f12fc98f541d2f078d5b91dfece42595b41601 100644 (file)
@@ -13,6 +13,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ScheduledExecutorService;
 
 import com.google.common.annotations.VisibleForTesting;
@@ -197,7 +198,8 @@ public class FaasContractManagerListener implements DataChangeListener {
         return val;
     }
 
-    private List<SecurityRuleGroup> buildSecurityRuleGroup(Contract contract) {
+    @VisibleForTesting
+    List<SecurityRuleGroup> buildSecurityRuleGroup(Contract contract) {
         LOG.trace("Start buildSecurityRuleGroup for contract {}", contract.getId().getValue());
         List<SecurityRuleGroup> securityRuleGroups = new ArrayList<>();
         if (contract.getClause() == null) {
@@ -241,7 +243,8 @@ public class FaasContractManagerListener implements DataChangeListener {
         return securityRuleGroups;
     }
 
-    private List<SecurityRule> getSecurityRules(Contract contract, SubjectName subjectRef, List<Rule> subRules) {
+    @VisibleForTesting
+    List<SecurityRule> getSecurityRules(Contract contract, SubjectName subjectRef, List<Rule> subRules) {
         List<SecurityRule> securityRules = new ArrayList<>();
         for (Rule rule : subRules) {
             List<ClassifierRef> classifierRefs = rule.getClassifierRef();
@@ -417,7 +420,7 @@ public class FaasContractManagerListener implements DataChangeListener {
                 return data.get();
             }
         } catch (Exception e) {
-            LOG.error("Couldn't read Action instance from datastore. Exception: ", e);
+            LOG.error("Couldn't read Classifier instance from datastore. Exception: ", e);
         }
         return null;
     }
index a8d01c698585ca9ebe49553c846e9cd0e1b177c2..06116c80257743ea4b951c94489169e24aca0f31 100644 (file)
@@ -204,7 +204,8 @@ public class FaasEndpointManagerListener implements DataChangeListener, AutoClos
         }
     }
 
-    private Uuid getFaasSubnetId(Endpoint endpoint) {
+    @VisibleForTesting
+    Uuid getFaasSubnetId(Endpoint endpoint) {
         if (endpoint.getEndpointGroup() == null) {
             LOG.error("Failed Endpoint registration -- No Endpoint-Group Id in endpoint {}", endpoint);
             return null;
@@ -232,7 +233,8 @@ public class FaasEndpointManagerListener implements DataChangeListener, AutoClos
         return null;
     }
 
-    private boolean validate(Endpoint endpoint) {
+    @VisibleForTesting
+    boolean validate(Endpoint endpoint) {
         if (endpoint.getL2Context() == null) {
             LOG.error("Endpoint Failed Validation -- Missing L2 Context. Endpoint {}", endpoint);
             return false;
index 8d1f364d77c1a06d7ec5cb835b8b9a90ed17a4cc..2dc43f4accc798d60763870bd7f1a7aa1bc3c857 100644 (file)
@@ -91,9 +91,9 @@ public class FaasPolicyManager implements DataChangeListener, AutoCloseable {
     private final ListenerRegistration<DataChangeListener> registerListener;
     private final ScheduledExecutorService executor;
     private final DataBroker dataProvider;
-    protected final Map<Pair<EndpointGroupId, TenantId>, List<SubnetId>> epgSubnetsMap = new HashMap<>();
+    final Map<Pair<EndpointGroupId, TenantId>, List<SubnetId>> epgSubnetsMap = new HashMap<>();
     private final ConcurrentHashMap<TenantId, Uuid> mappedTenants = new ConcurrentHashMap<>();
-    protected final ConcurrentHashMap<TenantId, ArrayList<ListenerRegistration<DataChangeListener>>> registeredTenants =
+    final ConcurrentHashMap<TenantId, ArrayList<ListenerRegistration<DataChangeListener>>> registeredTenants =
             new ConcurrentHashMap<>();
 
     public FaasPolicyManager(DataBroker dataBroker, ScheduledExecutorService executor) {
@@ -118,9 +118,7 @@ public class FaasPolicyManager implements DataChangeListener, AutoCloseable {
     public void close() throws Exception {
         synchronized (registeredTenants) {
             for (ArrayList<ListenerRegistration<DataChangeListener>> list : registeredTenants.values()) {
-                for (ListenerRegistration<DataChangeListener> reg : list) {
-                    reg.close();
-                }
+                list.forEach(ListenerRegistration::close);
             }
             registeredTenants.clear();
 
@@ -287,6 +285,7 @@ public class FaasPolicyManager implements DataChangeListener, AutoCloseable {
         if (!resolvedPoliciesOptional.isPresent() || resolvedPoliciesOptional.get().getResolvedPolicy() == null) {
             return;
         }
+        //TODO forEach possible?
         List<ResolvedPolicy> resolvedPolicies = resolvedPoliciesOptional.get().getResolvedPolicy();
         for (ResolvedPolicy policy : resolvedPolicies) {
             if (policy.getConsumerTenantId().equals(gbpTenantId)) {
@@ -565,7 +564,7 @@ public class FaasPolicyManager implements DataChangeListener, AutoCloseable {
         LogicalRouterBuilder provLR = initLogicalRouterBuilder(provEpg, faasTenantId,
                 isProviderPublic(externalImplicitGroup));
 
-        if (!UlnDatastoreApi.attachAndSubmitToDs(consLR, provLR, new Pair<Uuid, Uuid>(null, privateSecRulesId), null)) {
+        if (!UlnDatastoreApi.attachAndSubmitToDs(consLR, provLR, new Pair<>(null, privateSecRulesId), null)) {
             LOG.error("Failed to join Logical Routers in a Logical Network");
             return;
         }
index 0f44b276ac597beb2c6a9152543f9ca5f44d3674..2d4e94460048a4e4bc4eac32fae1119332b2fc33 100644 (file)
@@ -7,19 +7,30 @@
  */
 package org.opendaylight.groupbasedpolicy.renderer.faas;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.util.List;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 
 import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
 import com.google.common.util.concurrent.CheckedFuture;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Mockito;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -29,11 +40,26 @@ import org.opendaylight.faas.uln.datastore.api.UlnDatastoreApi;
 import org.opendaylight.groupbasedpolicy.renderer.faas.test.DataChangeListenerTester;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.security.rules.rev151013.security.rule.groups.attributes.security.rule.groups.container.SecurityRuleGroups;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.security.rules.rev151013.security.rule.groups.attributes.security.rule.groups.container.security.rule.groups.SecurityRuleGroup;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.security.rules.rev151013.security.rule.groups.attributes.security.rule.groups.container.security.rule.groups.security.rule.group.SecurityRule;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedContract;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRefBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.Contract;
 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;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.ClauseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.contract.SubjectBuilder;
+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.ClassifierInstance;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.subject.feature.instances.ClassifierInstanceBuilder;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.powermock.api.mockito.PowerMockito;
@@ -44,6 +70,8 @@ import org.powermock.modules.junit4.PowerMockRunner;
 @PrepareForTest(UlnDatastoreApi.class)
 public class FaasContractManagerListenerCovrgTest {
 
+    private static final ClauseName CLAUSE_NAME = new ClauseName("clause-1");
+    public static final SubjectName SUBJECT_NAME = new SubjectName("subject-name");
     private InstanceIdentifier<Contract> contractIid;
     private ContractId contractId = new ContractId("contractId");
     private FaasContractManagerListener listener;
@@ -64,8 +92,9 @@ public class FaasContractManagerListenerCovrgTest {
         tester.setRemovedPath(contractIid);
     }
 
+    @SuppressWarnings("unchecked")
     @Test
-    public void testT() throws ReadFailedException {
+    public void testExecuteEvent() throws ReadFailedException {
         PowerMockito.mockStatic(UlnDatastoreApi.class);
         PowerMockito.doNothing().when(UlnDatastoreApi.class);
         UlnDatastoreApi.submitSecurityGroupsToDs(any(SecurityRuleGroups.class));
@@ -90,9 +119,115 @@ public class FaasContractManagerListenerCovrgTest {
         listener.executeEvent(tester.getChangeMock());
     }
 
-    private DataObject makeTestContract() {
-        ContractBuilder builder = new ContractBuilder();
-        builder.setId(new ContractId("b4511aac-ae43-11e5-bf7f-feff819cdc9f"));
-        return builder.build();
+    @Test
+    public void testBuildSecurityRuleGroup(){
+        List<SecurityRuleGroup> securityRuleGroups;
+
+        Clause clause = new ClauseBuilder()
+                .setName(CLAUSE_NAME)
+                .setSubjectRefs(ImmutableList.of(SUBJECT_NAME))
+                .build();
+
+        Contract contractNoClause = new ContractBuilder()
+                .setId(contractId)
+                .build();
+
+        securityRuleGroups = listener.buildSecurityRuleGroup(contractNoClause);
+        assertNull(securityRuleGroups);
+
+        Clause clauseNoSubjectRefs = new ClauseBuilder()
+                .setName(CLAUSE_NAME)
+                .build();
+        Contract contractClauseNoSubjectRefs = new ContractBuilder()
+                .setId(contractId)
+                .setClause(ImmutableList.of(clauseNoSubjectRefs))
+                .build();
+
+        securityRuleGroups = listener.buildSecurityRuleGroup(contractClauseNoSubjectRefs);
+        assertNotNull(securityRuleGroups);
+        assertTrue(securityRuleGroups.isEmpty());
+
+        Contract contractNoSubject = new ContractBuilder()
+                .setId(contractId)
+                .setClause(ImmutableList.of(clause))
+                .build();
+
+        securityRuleGroups = listener.buildSecurityRuleGroup(contractNoSubject);
+        assertNotNull(securityRuleGroups);
+        assertTrue(securityRuleGroups.isEmpty());
+
+        Contract contract = new ContractBuilder()
+                .setId(contractId)
+                .setClause(ImmutableList.of(clause))
+                .setSubject(ImmutableList.of(new SubjectBuilder().setName(SUBJECT_NAME).build()))
+                .build();
+
+        securityRuleGroups = listener.buildSecurityRuleGroup(contract);
+        assertNotNull(securityRuleGroups);
+        assertFalse(securityRuleGroups.isEmpty());
+
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testGetSecurityRules() throws ReadFailedException, ExecutionException, InterruptedException {
+        List<Rule> rules;
+        List<SecurityRule> securityRules;
+
+        Clause clause = new ClauseBuilder()
+                .setName(CLAUSE_NAME)
+                .setSubjectRefs(ImmutableList.of(SUBJECT_NAME))
+                .build();
+
+        Contract contract = new ContractBuilder()
+                .setId(contractId)
+                .setClause(ImmutableList.of(clause))
+                .setSubject(ImmutableList.of(new SubjectBuilder().setName(SUBJECT_NAME).build()))
+                .build();
+
+        Rule ruleNoRefs = new RuleBuilder()
+                .setName(new RuleName("rule-no-refs"))
+                .setOrder(1)
+                .build();
+
+        Rule ruleEmptyRefs = new RuleBuilder()
+                .setClassifierRef(ImmutableList.of())
+                .setActionRef(ImmutableList.of())
+                .setName(new RuleName("rule-empty-refs"))
+                .setOrder(2)
+                .build();
+
+        rules = ImmutableList.of(ruleNoRefs, ruleEmptyRefs);
+
+        securityRules = listener.getSecurityRules(contract, SUBJECT_NAME, rules);
+
+        assertEquals(rules.size(), securityRules.size());
+
+        ClassifierName CLASSIFIER_NAME = new ClassifierName("classifier-1");
+        ClassifierRef classifierRef = new ClassifierRefBuilder()
+                .setName(CLASSIFIER_NAME)
+                .build();
+        Rule rule = new RuleBuilder()
+                .setClassifierRef(ImmutableList.of(classifierRef))
+                .setActionRef(ImmutableList.of())
+                .setName(new RuleName("rule-1"))
+                .setOrder(2)
+                .build();
+
+        ReadOnlyTransaction roTx1 = mock(ReadOnlyTransaction.class);
+
+        CheckedFuture<Optional<ClassifierInstance>, ReadFailedException> futureClassifierInstance = mock(CheckedFuture.class);
+        Optional<ClassifierInstance> optClassifierInstance = mock(Optional.class);
+        when(optClassifierInstance.isPresent()).thenReturn(false);
+        when(futureClassifierInstance.checkedGet()).thenReturn(optClassifierInstance);
+        when(roTx1.read(eq(LogicalDatastoreType.CONFIGURATION),
+                Mockito.<InstanceIdentifier>any())).thenReturn(futureClassifierInstance);
+
+        rules = ImmutableList.of(rule);
+        securityRules = listener.getSecurityRules(contract, SUBJECT_NAME, rules);
+
+        assertEquals(rules.size(), securityRules.size());
+
     }
+
 }
index 5139ebb28c71cb93a8c22a3e3ec69742334f198f..5177668021327337665768137d9682705dc4d88e 100644 (file)
@@ -7,6 +7,9 @@
  */
 package org.opendaylight.groupbasedpolicy.renderer.faas;
 
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.mock;
@@ -17,10 +20,12 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 
 import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
 import com.google.common.util.concurrent.CheckedFuture;
 import org.junit.Before;
 import org.junit.Test;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
@@ -28,10 +33,15 @@ import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.groupbasedpolicy.renderer.faas.test.DataChangeListenerTester;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.faas.endpoint.rev151009.FaasEndpointContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.faas.endpoint.rev151009.FaasEndpointContextBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;
 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.endpoint.rev140421.endpoints.EndpointL3;
@@ -40,6 +50,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.r
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3PrefixBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedEndpoint;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedEndpointKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnet;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPolicies;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
@@ -51,9 +63,7 @@ public class FaasEndpointManagerListenerCovrgTest {
     private InstanceIdentifier<Endpoint> epIid;
     private FaasEndpointManagerListener listener;
     private TenantId gbpTenantId = new TenantId("gbpTenantId");
-    private SubnetId subnetId = new SubnetId("subnetId");
     private Uuid faasTenantId = new Uuid("b4511aac-ae43-11e5-bf7f-feff819cdc9f");
-    private Uuid faasSubnetId = new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f");
     private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
     private DataChangeListenerTester tester;
     private DataBroker dataProvider;
@@ -104,6 +114,142 @@ public class FaasEndpointManagerListenerCovrgTest {
         listener.executeEvent(tester.getChangeMock());
     }
 
+    @Test
+    public void testValidate() {
+        EndpointGroupId endpointGroupId = new EndpointGroupId("epg-1");
+        FaasEndpointContext faasEndpointContext1 = new FaasEndpointContextBuilder().build();
+        FaasEndpointContext faasEndpointContext2 = new FaasEndpointContextBuilder()
+                .setFaasPortRefId(new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f")).build();
+
+        Endpoint ep = new EndpointBuilder()
+                .build();
+        assertFalse(listener.validate(ep));
+
+        ep = new EndpointBuilder()
+                .setL2Context(L_2_BRIDGE_DOMAIN_ID)
+                .build();
+        assertFalse(listener.validate(ep));
+
+        ep = new EndpointBuilder()
+                .setL2Context(L_2_BRIDGE_DOMAIN_ID)
+                .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
+                .build();
+        assertFalse(listener.validate(ep));
+
+        ep = new EndpointBuilder()
+                .setL2Context(L_2_BRIDGE_DOMAIN_ID)
+                .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
+                .setMacAddress(MAC_ADDRESS)
+                .build();
+        assertFalse(listener.validate(ep));
+
+        ep = new EndpointBuilder()
+                .setL2Context(L_2_BRIDGE_DOMAIN_ID)
+                .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
+                .setMacAddress(MAC_ADDRESS)
+                .setTenant(gbpTenantId)
+                .build();
+        assertFalse(listener.validate(ep));
+
+        ep = new EndpointBuilder()
+                .setL2Context(L_2_BRIDGE_DOMAIN_ID)
+                .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
+                .setMacAddress(MAC_ADDRESS)
+                .setTenant(gbpTenantId)
+                .setEndpointGroup(endpointGroupId)
+                .build();
+        assertFalse(listener.validate(ep));
+
+        ep = new EndpointBuilder()
+                .setL2Context(L_2_BRIDGE_DOMAIN_ID)
+                .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
+                .setMacAddress(MAC_ADDRESS)
+                .setTenant(gbpTenantId)
+                .setEndpointGroup(endpointGroupId)
+                .addAugmentation(FaasEndpointContext.class, faasEndpointContext1)
+                .build();
+        assertFalse(listener.validate(ep));
+
+        ep = new EndpointBuilder()
+                .setL2Context(L_2_BRIDGE_DOMAIN_ID)
+                .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
+                .setMacAddress(MAC_ADDRESS)
+                .setTenant(gbpTenantId)
+                .setEndpointGroup(endpointGroupId)
+                .addAugmentation(FaasEndpointContext.class, faasEndpointContext2)
+                .build();
+        assertTrue(listener.validate(ep));
+    }
+
+    @Test
+    public void testProcessEndpoint(){
+        EndpointGroupId endpointGroupId = new EndpointGroupId("epg-1");
+        FaasEndpointContext faasEndpointContext2 = new FaasEndpointContextBuilder()
+                .setFaasPortRefId(new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f")).build();
+
+        Endpoint ep = new EndpointBuilder()
+                .setL2Context(L_2_BRIDGE_DOMAIN_ID)
+                .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
+                .setMacAddress(MAC_ADDRESS)
+                .setTenant(gbpTenantId)
+                .setEndpointGroup(endpointGroupId)
+                .addAugmentation(FaasEndpointContext.class, faasEndpointContext2)
+                .build();
+
+        listener.processEndpoint(ep);
+    }
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testGetFaasSubnetId() throws ReadFailedException{
+        EndpointGroupId endpointGroupId = new EndpointGroupId("epg-1");
+        NetworkDomainId networkDomainId = new NetworkDomainId("network-domain-1");
+        FaasEndpointContext faasEndpointContext2 = new FaasEndpointContextBuilder()
+                .setFaasPortRefId(new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f")).build();
+        SubnetId subnetId = new SubnetId(networkDomainId);
+        ReadOnlyTransaction roTx1 = mock(ReadOnlyTransaction.class);
+        ReadWriteTransaction rwTx2 = mock(ReadWriteTransaction.class);
+
+        CheckedFuture<Optional<ResolvedPolicies>, ReadFailedException> futureResolvedPolicies = mock(CheckedFuture.class);
+        Optional<ResolvedPolicies> optResolvedPolicies = mock(Optional.class);
+        when(optResolvedPolicies.isPresent()).thenReturn(false);
+        when(futureResolvedPolicies.checkedGet()).thenReturn(optResolvedPolicies);
+        when(roTx1.read(LogicalDatastoreType.OPERATIONAL,
+                InstanceIdentifier.builder(ResolvedPolicies.class).build())).thenReturn(futureResolvedPolicies);
+
+        CheckedFuture<Optional<MappedSubnet>, ReadFailedException> futureMappedSubnet = mock(CheckedFuture.class);
+        Optional<MappedSubnet> optMappedSubnet = mock(Optional.class);
+        when(optMappedSubnet.isPresent()).thenReturn(false);
+        when(futureMappedSubnet.checkedGet()).thenReturn(optMappedSubnet);
+        when(rwTx2.read(LogicalDatastoreType.OPERATIONAL,
+                FaasIidFactory.mappedSubnetIid(gbpTenantId, subnetId))).thenReturn(futureMappedSubnet);
+
+        when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx1);
+        when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx2);
+
+        Endpoint ep = new EndpointBuilder()
+                .setL2Context(L_2_BRIDGE_DOMAIN_ID)
+                .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
+                .setMacAddress(MAC_ADDRESS)
+                .setTenant(gbpTenantId)
+                .addAugmentation(FaasEndpointContext.class, faasEndpointContext2)
+                .build();
+
+        assertNull(listener.getFaasSubnetId(ep));
+
+        ep = new EndpointBuilder()
+                .setL2Context(L_2_BRIDGE_DOMAIN_ID)
+                .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
+                .setMacAddress(MAC_ADDRESS)
+                .setTenant(gbpTenantId)
+                .setEndpointGroup(endpointGroupId)
+                .setNetworkContainment(networkDomainId)
+                .addAugmentation(FaasEndpointContext.class, faasEndpointContext2)
+                .build();
+
+        assertNull(listener.getFaasSubnetId(ep));
+    }
+
     @Test
     public void testOnDataChanged_EndpointL3() {
         EndpointL3 ep = new EndpointL3Builder().setTenant(gbpTenantId)
index 52decef321ea256801b709525ab9a0ba6953fd15..4254dfcc93fb8ca5e0d1efe2e2fc0fd9c62a3e9e 100644 (file)
@@ -59,15 +59,13 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev15
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.logical.networks.logical.network.ProviderNetworkBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.MappedEntity;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.MappedTenant;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnet;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnetBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomain;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomainBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomainBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3ContextBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.SubnetBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroupBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPolicies;
@@ -85,27 +83,27 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
 import org.powermock.modules.junit4.PowerMockRunner;
 
 @RunWith(PowerMockRunner.class)
-@PrepareForTest({UlnDatastoreApi.class})
+@PrepareForTest(UlnDatastoreApi.class)
 public class FaasPolicyManagerCovrgTest {
 
     private InstanceIdentifier<DataObject> policyId;
     private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change;
     DataBroker dataProvider;
     private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(Runtime.getRuntime()
-        .availableProcessors());
-    EndpointGroupId consumerEpgId = new EndpointGroupId("consumerEpgId");
-    SubnetId consumerSubnet = new SubnetId("consumerSubnet");
-    SubnetId providerSubnet = new SubnetId("providerSubnet");
-    EndpointGroupId providerEpgId = new EndpointGroupId("providerEpgId");
-    ContractId contractId = new ContractId("contractId");
-    TenantId tenantId = new TenantId("tenantId");
-    Uuid faasTenantId = new Uuid("0eb98cf5-086c-4a81-8a4e-0c3b4566108b");
-    Uuid faasSecRulesId = new Uuid("1eb98cf5-086c-4a81-8a4e-0c3b4566108b");
-    L3ContextId l3Context = new L3ContextId("l3ContextId");
-    EndpointGroupId epgId = new EndpointGroupId("epgId");
-    SubnetId subnetId = new SubnetId("subnetId");
-    Uuid dummyUuid1 = new Uuid("2eb98cf5-086c-4a81-8a4e-0c3b4566108b");
-    Uuid dummyUuid2 = new Uuid("3eb98cf5-086c-4a81-8a4e-0c3b4566108b");
+            .availableProcessors());
+    private EndpointGroupId consumerEpgId = new EndpointGroupId("consumerEpgId");
+    private SubnetId consumerSubnet = new SubnetId("consumerSubnet");
+    private SubnetId providerSubnet = new SubnetId("providerSubnet");
+    private EndpointGroupId providerEpgId = new EndpointGroupId("providerEpgId");
+    private ContractId contractId = new ContractId("contractId");
+    private TenantId tenantId = new TenantId("tenantId");
+    private Uuid faasTenantId = new Uuid("0eb98cf5-086c-4a81-8a4e-0c3b4566108b");
+    private Uuid faasSecRulesId = new Uuid("1eb98cf5-086c-4a81-8a4e-0c3b4566108b");
+    private L3ContextId l3Context = new L3ContextId("l3ContextId");
+    private EndpointGroupId epgId = new EndpointGroupId("epgId");
+    private SubnetId subnetId = new SubnetId("subnetId");
+    private Uuid dummyUuid1 = new Uuid("2eb98cf5-086c-4a81-8a4e-0c3b4566108b");
+    private Uuid dummyUuid2 = new Uuid("3eb98cf5-086c-4a81-8a4e-0c3b4566108b");
 
     @SuppressWarnings("unchecked")
     @Before
@@ -181,7 +179,7 @@ public class FaasPolicyManagerCovrgTest {
         when(roTx.read(LogicalDatastoreType.OPERATIONAL,
                 FaasIidFactory.logicalNetworksIid())).thenReturn(futureLogicalNetworks);
         LogicalNetworks logicalNetworks = new LogicalNetworksBuilder()
-                .setLogicalNetwork( new ArrayList<LogicalNetwork>())
+                .setLogicalNetwork(new ArrayList<LogicalNetwork>())
                 .build();
         Optional<LogicalNetworks> optLogicalNetworks = mock(Optional.class);
         when(optLogicalNetworks.isPresent()).thenReturn(true);
@@ -196,7 +194,7 @@ public class FaasPolicyManagerCovrgTest {
     }
 
     @Test
-    public void testRegisterTenant_null(){
+    public void testRegisterTenant_null() {
         FaasPolicyManager policyManager = spy(new FaasPolicyManager(dataProvider, executor));
         doNothing().when(policyManager).registerTenant(tenantId, null);
 
@@ -277,14 +275,14 @@ public class FaasPolicyManagerCovrgTest {
     }
 
     @Test
-    public void testIsUuid(){
+    public void testIsUuid() {
         assertFalse(FaasPolicyManager.isUUid(null));
         assertFalse(FaasPolicyManager.isUUid("non-matching string"));
         assertTrue(FaasPolicyManager.isUUid("12345678-1234-5123-b123-0123456789ab"));
     }
 
     @Test
-    public void testHandledPolicy_notEquals(){
+    public void testHandledPolicy_notEquals() {
         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
         ResolvedPolicy policy = new ResolvedPolicyBuilder()
                 .setConsumerTenantId(new TenantId("t1"))
@@ -295,7 +293,7 @@ public class FaasPolicyManagerCovrgTest {
     }
 
     @Test
-    public void testRegisterFollowedEndpointgroup(){
+    public void testRegisterFollowedEndpointgroup() {
         EndpointGroupId epgId = new EndpointGroupId("epgId");
         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
 
@@ -565,22 +563,23 @@ public class FaasPolicyManagerCovrgTest {
     }
 
     @Test
-    public void testRemoveLogicalNetwork_null(){
+    public void testRemoveLogicalNetwork_null() {
         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
 
         policyManager.removeLogicalNetwork(null);
     }
 
-    /*@SuppressWarnings("unchecked")
+    @SuppressWarnings("unchecked")
     @Test
     public void testCreateLayer3LogicalNetwork() throws ReadFailedException {
-        ReadOnlyTransaction roTx = mock(ReadOnlyTransaction.class);
+        ReadOnlyTransaction roTx1 = mock(ReadOnlyTransaction.class);
+        ReadOnlyTransaction roTx2 = mock(ReadOnlyTransaction.class);
 
         CheckedFuture<Optional<ResolvedPolicies>, ReadFailedException> futureResolvedPolicies =
                 mock(CheckedFuture.class);
         Optional<ResolvedPolicies> optResolvedPolicies = mock(Optional.class);
         when(futureResolvedPolicies.checkedGet()).thenReturn(optResolvedPolicies);
-        when(roTx.read(LogicalDatastoreType.OPERATIONAL,
+        when(roTx1.read(LogicalDatastoreType.OPERATIONAL,
                 InstanceIdentifier.builder(ResolvedPolicies.class).build())).thenReturn(
                 futureResolvedPolicies);
 
@@ -590,84 +589,22 @@ public class FaasPolicyManagerCovrgTest {
         CheckedFuture<Optional<MappedSubnet>, ReadFailedException> futureMappedSubnet =
                 mock(CheckedFuture.class);
         Optional<MappedSubnet> optMappedSubnet = mock(Optional.class);
-        when(optMappedSubnet.isPresent()).thenReturn(true);
+        when(optMappedSubnet.isPresent()).thenReturn(false);
         when(optMappedSubnet.get()).thenReturn(mappedSubnet);
         when(futureMappedSubnet.checkedGet()).thenReturn(optMappedSubnet);
-        when(roTx.read(LogicalDatastoreType.OPERATIONAL,
+        when(roTx2.read(LogicalDatastoreType.OPERATIONAL,
                 FaasIidFactory.mappedSubnetIid(tenantId, subnetId))).thenReturn(
                 futureMappedSubnet);
 
-        when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx);
+        when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx1);
+        when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx2);
 
         EndpointGroup consumerEpg = new EndpointGroupBuilder().setId(consumerEpgId).build();
         EndpointGroup providerEpg = new EndpointGroupBuilder().setId(providerEpgId).build();
         FaasPolicyManager policyManager = new FaasPolicyManager(dataProvider, executor);
 
-        policyManager.registerSubnetWithEpg(consumerEpgId, tenantId, consumerSubnet);
-        policyManager.registerSubnetWithEpg(providerEpgId, tenantId, providerSubnet);
         policyManager.createLayer3LogicalNetwork(consumerEpg, contractId, providerEpg, tenantId,
                 ServiceCommunicationLayer.Layer3, ExternalImplicitGroup.ProviderEpg);
-    }*/
-
-
-
-
-
-    private L3Context makeTestL3Context() {
-        L3ContextBuilder builder = new L3ContextBuilder();
-        builder.setId(l3Context);
-        return builder.build();
-    }
-
-    private L2FloodDomain makeTestL2FloodDomain(String id, L2BridgeDomainId brdgId) {
-        L2FloodDomainBuilder builder = new L2FloodDomainBuilder();
-        builder.setId(new L2FloodDomainId(id));
-        builder.setParent(brdgId);
-        return builder.build();
-    }
-
-    private L2BridgeDomain makeTestBridgeDomain(String id) {
-        L2BridgeDomainBuilder builder = new L2BridgeDomainBuilder();
-        builder.setId(new L2BridgeDomainId(id));
-        builder.setParent(l3Context);
-        return builder.build();
     }
 
-    private EndpointGroup makeTestEndpointGroup(EndpointGroupId epgId) {
-        EndpointGroupBuilder builder = new EndpointGroupBuilder();
-        builder.setId(epgId);
-        return builder.build();
-    }
-
-    private Subnet makeTestSubnet(SubnetId subnetId, L2FloodDomainId l2FloodDomainId) {
-        SubnetBuilder builder = new SubnetBuilder();
-        builder.setId(subnetId);
-        builder.setParent(l2FloodDomainId);
-        return builder.build();
-
-    }
-
-    private DataObject makeTestResolvedPolicy() {
-        ResolvedPolicyBuilder builder = new ResolvedPolicyBuilder();
-        builder.setConsumerEpgId(consumerEpgId);
-        builder.setConsumerTenantId(tenantId);
-        builder.setProviderEpgId(providerEpgId);
-        builder.setProviderTenantId(tenantId);
-        List<PolicyRuleGroupWithEndpointConstraints> pRulesGrpsWEp = new ArrayList<>();
-        PolicyRuleGroupWithEndpointConstraintsBuilder pRulesGrpWEp = new PolicyRuleGroupWithEndpointConstraintsBuilder();
-        List<PolicyRuleGroup> pRulesGrps = new ArrayList<>();
-        PolicyRuleGroupBuilder pRulesGrp = new PolicyRuleGroupBuilder();
-        pRulesGrp.setContractId(contractId);
-        pRulesGrps.add(pRulesGrp.build());
-        pRulesGrpWEp.setPolicyRuleGroup(pRulesGrps);
-        pRulesGrpsWEp.add(pRulesGrpWEp.build());
-        builder.setPolicyRuleGroupWithEndpointConstraints(pRulesGrpsWEp);
-        return builder.build();
-    }
-
-    private DataObject makeTestResolvedPolicyWithImpExternalEpg() {
-        ResolvedPolicyBuilder builder = new ResolvedPolicyBuilder((ResolvedPolicy) makeTestResolvedPolicy());
-        builder.setExternalImplicitGroup(ExternalImplicitGroup.ConsumerEpg);
-        return builder.build();
-    }
 }