Tests for NeutronRouterAware 47/40047/3
authorKonstantin Blagov <kblagov@cisco.com>
Wed, 8 Jun 2016 15:17:58 +0000 (17:17 +0200)
committerMartin Sunal <msunal@cisco.com>
Wed, 8 Jun 2016 18:32:57 +0000 (18:32 +0000)
ATTENTION: depends on https://git.opendaylight.org/gerrit/39947

Change-Id: I00d83b39bf385c0cf2f960a171fbcd38dcb0d0da
Signed-off-by: Konstantin Blagov <kblagov@cisco.com>
neutron-mapper/src/main/java/org/opendaylight/groupbasedpolicy/neutron/mapper/mapping/NeutronRouterAware.java
neutron-mapper/src/test/java/org/opendaylight/groupbasedpolicy/neutron/mapper/mapping/NeutronRouterAwareDataStoreTest.java [new file with mode: 0644]

index 2227224946b31a4c017a611883b79bb79d3a84a7..96d95bcdc1b88d9c070a485e15003081a360e09e 100644 (file)
@@ -118,7 +118,7 @@ public class NeutronRouterAware implements NeutronAware<Router> {
             l3ContextBuilder.setName(name);
         }
         L3Context l3Context = l3ContextBuilder.setId(l3ContextId).build();
-        wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.l3ContextIid(tenantId, l3ContextId), l3Context);
+        wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.l3ContextIid(tenantId, l3ContextId), l3Context, true);
     }
 
     @Override
@@ -176,7 +176,7 @@ public class NeutronRouterAware implements NeutronAware<Router> {
                     L2L3IidFactory.l2BridgeDomainIid(tenantId, l2BdId), rwTx);
             if (!optBd.isPresent()) {
                 LOG.warn(
-                        "Could not read L2-Bridge-Domain {} Modifiaction of it's parent to L3-Context of router {} aborted.",
+                        "Could not read L2-Bridge-Domain {}. Modification of its parent to L3-Context of router {} aborted.",
                         l2BdId, newRouter.getUuid());
                 rwTx.cancel();
                 return;
@@ -224,7 +224,7 @@ public class NeutronRouterAware implements NeutronAware<Router> {
              l3Context = optL3Context.get();
          } else { // add L3 context if missing
              l3Context = createL3CtxFromRouter(newRouter);
-             rwTx.put(LogicalDatastoreType.CONFIGURATION, l3ContextIid, l3Context);
+             rwTx.put(LogicalDatastoreType.CONFIGURATION, l3ContextIid, l3Context, true);
          }
 
          if (newRouter.getGatewayPortId() != null && oldRouter.getGatewayPortId() == null) {
@@ -257,8 +257,9 @@ public class NeutronRouterAware implements NeutronAware<Router> {
                  return;
              }
              IpAddress gatewayIp =  potentialSubnet.get().getGatewayIp();
+             NetworkDomainId networkContainment = new NetworkDomainId(ipWithSubnetFromGwPort.getSubnetId().getValue());
              boolean registeredExternalGateway = epRegistrator.registerL3EpAsExternalGateway(tenantId, gatewayIp,
-                     l3ContextId, new NetworkDomainId(ipWithSubnetFromGwPort.getSubnetId().getValue()));
+                     l3ContextId, networkContainment);
              if (!registeredExternalGateway) {
                  LOG.warn("Could not add L3Prefix as gateway of default route. Gateway port {}", gwPort);
                  rwTx.cancel();
@@ -285,7 +286,7 @@ public class NeutronRouterAware implements NeutronAware<Router> {
                      IidFactory.l2BridgeDomainIid(tenantId, l2BdId), rwTx);
              if (!optBd.isPresent()) {
                  LOG.warn(
-                         "Could not read L2-Bridge-Domain {} Modifiaction of it's parent to L3-Context of router {} aborted.",
+                         "Could not read L2-Bridge-Domain {}. Modification of its parent to L3-Context of router {} aborted.",
                          l2BdId, newRouter.getUuid());
                  rwTx.cancel();
                  return;
diff --git a/neutron-mapper/src/test/java/org/opendaylight/groupbasedpolicy/neutron/mapper/mapping/NeutronRouterAwareDataStoreTest.java b/neutron-mapper/src/test/java/org/opendaylight/groupbasedpolicy/neutron/mapper/mapping/NeutronRouterAwareDataStoreTest.java
new file mode 100644 (file)
index 0000000..f7fb730
--- /dev/null
@@ -0,0 +1,347 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableList;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.groupbasedpolicy.neutron.mapper.EndpointRegistrator;
+import org.opendaylight.groupbasedpolicy.neutron.mapper.test.NeutronMapperDataBrokerTest;
+import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
+import org.opendaylight.groupbasedpolicy.util.IidFactory;
+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.rev130715.Uuid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.BaseEndpointService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.EndpointService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.RouterBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.NetworkBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIpsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.PortsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.SubnetsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetBuilder;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+
+public class NeutronRouterAwareDataStoreTest extends NeutronMapperDataBrokerTest {
+
+    private final Uuid tenantUuid = new Uuid("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa");
+    private final Uuid routerUuid = new Uuid("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb");
+    private final Uuid newRouterUuid = new Uuid("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbb2");
+    private final Uuid subnetUuid = new Uuid("cccccccc-cccc-cccc-cccc-cccccccccccc");
+    private final Uuid networkUuid = new Uuid("dddddddd-dddd-dddd-dddd-dddddddddddd");
+    private final Uuid gatewayPortUuid = new Uuid("dddddddd-dddd-dddd-dddd-ddddddddddd1");
+    private final IpAddress ipAddress = new IpAddress(new Ipv4Address("10.0.0.2"));
+
+    private DataBroker dataBroker;
+    private NeutronRouterAware routerAware;
+    private EndpointRegistrator epRegistrator;
+    private EndpointService epService;
+    private BaseEndpointService baseEpService;
+    private Neutron neutron;
+    private Future<RpcResult<Void>> futureRpcResult;
+    private Future<RpcResult<Void>> futureRpcFail;
+    private RpcResult<Void> rpcResult;
+    private RpcResult<Void> rpcFail;
+    private NeutronNetworkAware networkAware;
+    private Network network;
+
+    @Before
+    public void init() throws ExecutionException, InterruptedException {
+        futureRpcResult = mock(Future.class);
+        futureRpcFail = mock(Future.class);
+        rpcResult = mock(RpcResult.class);
+        rpcFail = mock(RpcResult.class);
+        when(rpcResult.isSuccessful()).thenReturn(true);
+        when(rpcFail.isSuccessful()).thenReturn(false);
+        dataBroker = getDataBroker();
+        neutron = mock(Neutron.class);
+        epService = mock(EndpointService.class);
+        baseEpService = mock(BaseEndpointService.class);
+        when(futureRpcResult.get()).thenReturn(rpcResult);
+        when(futureRpcFail.get()).thenReturn(rpcFail);
+        when(epService.registerEndpoint(
+                any(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterEndpointInput.class)))
+                    .thenReturn(futureRpcResult);
+        when(epService.registerL3PrefixEndpoint(
+                any(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.RegisterL3PrefixEndpointInput.class)))
+                    .thenReturn(futureRpcResult);
+        when(baseEpService.registerEndpoint(
+                any(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInput.class)))
+                    .thenReturn(futureRpcResult);
+        epRegistrator = new EndpointRegistrator(epService, baseEpService);
+
+        networkAware = new NeutronNetworkAware(dataBroker);
+        network = new NetworkBuilder().setTenantId(tenantUuid).setUuid(networkUuid).setName("networkName").build();
+
+        routerAware = new NeutronRouterAware(dataBroker, epRegistrator);
+    }
+
+    @Rule
+    public ExpectedException thrown = ExpectedException.none();
+
+    @Test
+    public void testConstructor_invalidArgument() throws NullPointerException {
+        thrown.expect(NullPointerException.class);
+        new NeutronRouterAware(null, null);
+    }
+
+    @Test
+    public void testOnCreated() {
+        Router router = new RouterBuilder().setTenantId(tenantUuid).setName("routerName").setUuid(routerUuid).build();
+
+        routerAware.onCreated(router, neutron);
+
+        assertRouterExists(router);
+    }
+
+    @Test
+    public void testOnCreated_incorrectName() {
+        Router router = new RouterBuilder().setTenantId(tenantUuid).setName("123").setUuid(routerUuid).build();
+
+        routerAware.onCreated(router, neutron);
+
+        assertRouterExists(router);
+    }
+
+    @Test
+    public void testOnUpdated() {
+        Subnets subnets = createSubnets();
+        when(neutron.getSubnets()).thenReturn(subnets);
+
+        FixedIps fixedIps = new FixedIpsBuilder().setIpAddress(ipAddress).setSubnetId(subnetUuid).build();
+        Port port = new PortBuilder().setUuid(gatewayPortUuid).setFixedIps(ImmutableList.of(fixedIps)).build();
+        Ports ports = new PortsBuilder().setPort(ImmutableList.of(port)).build();
+        when(neutron.getPorts()).thenReturn(ports);
+
+        Router oldRouter =
+                new RouterBuilder().setTenantId(tenantUuid).setName("oldRouterName").setUuid(routerUuid).build();
+        Router newRouter = new RouterBuilder().setTenantId(tenantUuid)
+            .setName("newRouterName")
+            .setUuid(newRouterUuid)
+            .setGatewayPortId(gatewayPortUuid)
+            .build();
+
+        networkAware.onCreated(network, neutron);
+
+        assertRouterNotExists(oldRouter);
+
+        routerAware.onCreated(oldRouter, neutron);
+
+        assertRouterExists(oldRouter);
+        assertRouterNotExists(newRouter);
+
+        routerAware.onUpdated(oldRouter, newRouter, neutron, neutron);
+
+        assertRouterExists(oldRouter);
+        assertRouterExists(newRouter);
+    }
+
+    @Test
+    public void testOnUpdated_GatewayPortNotFound() {
+        Subnets subnets = createSubnets();
+        when(neutron.getSubnets()).thenReturn(subnets);
+
+        FixedIps fixedIps = new FixedIpsBuilder().setIpAddress(ipAddress).setSubnetId(subnetUuid).build();
+        Port port = new PortBuilder().setUuid(new Uuid("dddddddd-dddd-dddd-dddd-000000000000"))
+            .setFixedIps(ImmutableList.of(fixedIps))
+            .build();
+        Ports ports = new PortsBuilder().setPort(ImmutableList.of(port)).build();
+        when(neutron.getPorts()).thenReturn(ports);
+
+        Router oldRouter =
+                new RouterBuilder().setTenantId(tenantUuid).setName("oldRouterName").setUuid(routerUuid).build();
+        Router newRouter = new RouterBuilder().setTenantId(tenantUuid)
+            .setName("newRouterName")
+            .setUuid(newRouterUuid)
+            .setGatewayPortId(gatewayPortUuid)
+            .build();
+
+        routerAware.onCreated(oldRouter, neutron);
+        routerAware.onUpdated(oldRouter, newRouter, neutron, neutron);
+
+        assertRouterNotExists(newRouter);
+    }
+
+    @Test
+    public void testOnUpdated_noIps() {
+        Subnets subnets = createSubnets();
+        when(neutron.getSubnets()).thenReturn(subnets);
+
+        Port port = new PortBuilder().setUuid(gatewayPortUuid).build();
+        Ports ports = new PortsBuilder().setPort(ImmutableList.of(port)).build();
+        when(neutron.getPorts()).thenReturn(ports);
+
+        Router oldRouter =
+                new RouterBuilder().setTenantId(tenantUuid).setName("oldRouterName").setUuid(routerUuid).build();
+        Router newRouter = new RouterBuilder().setTenantId(tenantUuid)
+            .setName("newRouterName")
+            .setUuid(newRouterUuid)
+            .setGatewayPortId(gatewayPortUuid)
+            .build();
+
+        routerAware.onCreated(oldRouter, neutron);
+        routerAware.onUpdated(oldRouter, newRouter, neutron, neutron);
+
+        assertRouterNotExists(newRouter);
+    }
+
+    @Test
+    public void testOnUpdated_noSubnet() {
+        Subnets subnets = createSubnets();
+        when(neutron.getSubnets()).thenReturn(subnets);
+
+        FixedIps fixedIps = new FixedIpsBuilder().setIpAddress(ipAddress).build();
+        Port port = new PortBuilder().setUuid(gatewayPortUuid).setFixedIps(ImmutableList.of(fixedIps)).build();
+        Ports ports = new PortsBuilder().setPort(ImmutableList.of(port)).build();
+        when(neutron.getPorts()).thenReturn(ports);
+
+        Router oldRouter =
+                new RouterBuilder().setTenantId(tenantUuid).setName("oldRouterName").setUuid(routerUuid).build();
+        Router newRouter = new RouterBuilder().setTenantId(tenantUuid)
+            .setName("newRouterName")
+            .setUuid(newRouterUuid)
+            .setGatewayPortId(gatewayPortUuid)
+            .build();
+
+        routerAware.onCreated(oldRouter, neutron);
+        routerAware.onUpdated(oldRouter, newRouter, neutron, neutron);
+
+        assertRouterNotExists(newRouter);
+    }
+
+    @Test
+    public void testOnUpdated_ExtGatewayNotRegistered() {
+        when(baseEpService.registerEndpoint(
+                any(org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInput.class)))
+                    .thenReturn(futureRpcFail);
+
+        Subnets subnets = createSubnets();
+        when(neutron.getSubnets()).thenReturn(subnets);
+
+        FixedIps fixedIps = new FixedIpsBuilder().setIpAddress(ipAddress).setSubnetId(subnetUuid).build();
+        Port port = new PortBuilder().setUuid(gatewayPortUuid).setFixedIps(ImmutableList.of(fixedIps)).build();
+        Ports ports = new PortsBuilder().setPort(ImmutableList.of(port)).build();
+        when(neutron.getPorts()).thenReturn(ports);
+
+        Router oldRouter =
+                new RouterBuilder().setTenantId(tenantUuid).setName("oldRouterName").setUuid(routerUuid).build();
+        Router newRouter = new RouterBuilder().setTenantId(tenantUuid)
+            .setName("newRouterName")
+            .setUuid(newRouterUuid)
+            .setGatewayPortId(gatewayPortUuid)
+            .build();
+
+        routerAware.onCreated(oldRouter, neutron);
+        routerAware.onUpdated(oldRouter, newRouter, neutron, neutron);
+
+        assertRouterNotExists(newRouter);
+    }
+
+    @Test
+    public void testOnUpdated_noL2BridgeDomain() {
+        Subnets subnets = createSubnets();
+        when(neutron.getSubnets()).thenReturn(subnets);
+
+        FixedIps fixedIps = new FixedIpsBuilder().setIpAddress(ipAddress).setSubnetId(subnetUuid).build();
+        Port port = new PortBuilder().setUuid(gatewayPortUuid).setFixedIps(ImmutableList.of(fixedIps)).build();
+        Ports ports = new PortsBuilder().setPort(ImmutableList.of(port)).build();
+        when(neutron.getPorts()).thenReturn(ports);
+
+        Router oldRouter =
+                new RouterBuilder().setTenantId(tenantUuid).setName("oldRouterName").setUuid(routerUuid).build();
+        Router newRouter = new RouterBuilder().setTenantId(tenantUuid)
+            .setName("newRouterName")
+            .setUuid(newRouterUuid)
+            .setGatewayPortId(gatewayPortUuid)
+            .build();
+
+        routerAware.onCreated(oldRouter, neutron);
+        routerAware.onUpdated(oldRouter, newRouter, neutron, neutron);
+
+        assertRouterNotExists(newRouter);
+    }
+
+    @Test
+    public void testOnDeleted() {
+        Router router =
+                new RouterBuilder().setTenantId(tenantUuid).setName("oldRouterName").setUuid(routerUuid).build();
+
+        routerAware.onCreated(router, neutron);
+        routerAware.onDeleted(router, neutron, neutron);
+        // no op
+    }
+
+    private Subnets createSubnets() {
+        Subnet subnet = new SubnetBuilder().setTenantId(tenantUuid)
+            .setUuid(subnetUuid)
+            .setName("subnetName")
+            .setNetworkId(networkUuid)
+            .setGatewayIp(ipAddress)
+            .setCidr("10.0.0.0/24")
+            .build();
+        return new SubnetsBuilder().setSubnet(ImmutableList.of(subnet)).build();
+    }
+
+    private void assertRouterExists(Router router) {
+        Optional<L3Context> opt = getL3ContextOptional(router);
+        if (opt.isPresent()) {
+            assertEquals(router.getUuid().getValue(), opt.get().getId().getValue());
+        } else {
+            fail("no router in DS, Uuid:" + router.getUuid());
+        }
+    }
+
+    private void assertRouterNotExists(Router router) {
+        Optional<L3Context> opt = getL3ContextOptional(router);
+        if (opt.isPresent()) {
+            assertNotEquals(router.getUuid().getValue(), opt.get().getId().getValue());
+        }
+    }
+
+    private Optional<L3Context> getL3ContextOptional(Router router) {
+        ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
+        TenantId tenantId = new TenantId(router.getTenantId().getValue());
+        ContextId routerL3CtxId = new ContextId(router.getUuid().getValue());
+        L3ContextId l3ContextId = new L3ContextId(routerL3CtxId);
+        InstanceIdentifier<L3Context> l3ContextIid = IidFactory.l3ContextIid(tenantId, l3ContextId);
+        return DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, l3ContextIid, rTx);
+    }
+
+}