Enable checkstyle for neutronvpn
[netvirt.git] / vpnservice / neutronvpn / neutronvpn-impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronRouterChangeListener.java
index 1b1a93f6d53c6596ae0c1a65e5591e80410c19d6..8163ae83c86b2c4f2caca55b7772e7fd0d3a0edb 100644 (file)
@@ -8,12 +8,13 @@
 package org.opendaylight.netvirt.neutronvpn;
 
 import com.google.common.base.Optional;
-
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
+import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
 import org.opendaylight.genius.mdsalutil.NwConstants;
 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.neutron.l3.rev150712.l3.attributes.Routes;
@@ -22,94 +23,78 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.router
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.link.states.InterVpnLinkState;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netvirt.inter.vpn.link.rev160311.inter.vpn.links.InterVpnLink;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-
 
-public class NeutronRouterChangeListener extends AbstractDataChangeListener<Router> implements AutoCloseable {
+public class NeutronRouterChangeListener extends AsyncDataTreeChangeListenerBase<Router, NeutronRouterChangeListener>
+        implements AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(NeutronRouterChangeListener.class);
+    private final DataBroker dataBroker;
+    private final NeutronvpnManager nvpnManager;
+    private final NeutronvpnNatManager nvpnNatManager;
+    private final NeutronSubnetGwMacResolver gwMacResolver;
+
+    public NeutronRouterChangeListener(final DataBroker dataBroker, final NeutronvpnManager neutronvpnManager,
+                                       final NeutronvpnNatManager neutronvpnNatManager,
+                                       NeutronSubnetGwMacResolver gwMacResolver) {
+        super(Router.class, NeutronRouterChangeListener.class);
+        this.dataBroker = dataBroker;
+        nvpnManager = neutronvpnManager;
+        nvpnNatManager = neutronvpnNatManager;
+        this.gwMacResolver = gwMacResolver;
+    }
 
-    private ListenerRegistration<DataChangeListener> listenerRegistration;
-    private final DataBroker broker;
-    private NeutronvpnManager nvpnManager;
-    private NeutronvpnNatManager nvpnNatManager;
-
-
-    public NeutronRouterChangeListener(final DataBroker db, NeutronvpnManager nVpnMgr,
-                                       NeutronvpnNatManager nVpnNatMgr) {
-        super(Router.class);
-        broker = db;
-        nvpnManager = nVpnMgr;
-        nvpnNatManager = nVpnNatMgr;
-        registerListener(db);
+    public void start() {
+        LOG.info("{} start", getClass().getSimpleName());
+        registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
     }
 
     @Override
-    public void close() throws Exception {
-        if (listenerRegistration != null) {
-            try {
-                listenerRegistration.close();
-            } catch (final Exception e) {
-                LOG.error("Error when cleaning up DataChangeListener.", e);
-            }
-            listenerRegistration = null;
-        }
-        LOG.info("N_Router listener Closed");
+    protected InstanceIdentifier<Router> getWildCardPath() {
+        return InstanceIdentifier.create(Neutron.class).child(Routers.class).child(Router.class);
     }
 
-
-    private void registerListener(final DataBroker db) {
-        try {
-            listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
-                    InstanceIdentifier.create(Neutron.class).child(Routers.class).child(Router.class),
-                    NeutronRouterChangeListener.this, DataChangeScope.SUBTREE);
-        } catch (final Exception e) {
-            LOG.error("Neutron Manager Router DataChange listener registration fail!", e);
-            throw new IllegalStateException("Neutron Manager Router DataChange listener registration failed.", e);
-        }
+    @Override
+    protected NeutronRouterChangeListener getDataTreeChangeListener() {
+        return NeutronRouterChangeListener.this;
     }
 
+
     @Override
     protected void add(InstanceIdentifier<Router> identifier, Router input) {
-        if (LOG.isTraceEnabled()) {
-            LOG.trace("Adding Router : key: " + identifier + ", value=" + input);
-        }
+        LOG.trace("Adding Router : key: {}, value={}", identifier, input);
+        NeutronvpnUtils.addToRouterCache(input);
         // Create internal VPN
         nvpnManager.createL3InternalVpn(input.getUuid(), null, null, null, null, null, input.getUuid(), null);
-        NeutronvpnUtils.addToRouterCache(input);
+        nvpnNatManager.handleExternalNetworkForRouter(null, input);
+        gwMacResolver.sendArpRequestsToExtGateways(input);
     }
 
     @Override
     protected void remove(InstanceIdentifier<Router> identifier, Router input) {
-        if (LOG.isTraceEnabled()) {
-            LOG.trace("Removing router : key: " + identifier + ", value=" + input);
-        }
+        LOG.trace("Removing router : key: {}, value={}", identifier, input);
         Uuid routerId = input.getUuid();
         //NOTE: Pass an empty routerSubnetIds list, as router interfaces
         //will be removed from VPN by invocations from NeutronPortChangeListener
         List<Uuid> routerSubnetIds = new ArrayList<>();
         nvpnManager.handleNeutronRouterDeleted(routerId, routerSubnetIds);
-        NeutronvpnUtils.removeFromRouterCache(input);
 
         // Handle router deletion for the NAT service
         if (input.getExternalGatewayInfo() != null) {
             Uuid extNetId = input.getExternalGatewayInfo().getExternalNetworkId();
             nvpnNatManager.removeExternalNetworkFromRouter(extNetId, input);
         }
-
+        NeutronvpnUtils.removeFromRouterCache(input);
     }
 
     @Override
     protected void update(InstanceIdentifier<Router> identifier, Router original, Router update) {
         LOG.trace("Updating Router : key: {}, original value={}, update value={}", identifier, original, update);
+        NeutronvpnUtils.addToRouterCache(update);
         Uuid routerId = update.getUuid();
-        Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
+        NeutronvpnUtils.addToRouterCache(update);
+        Uuid vpnId = NeutronvpnUtils.getVpnForRouter(dataBroker, routerId, true);
         // internal vpn always present in case external vpn not found
         if (vpnId == null) {
             vpnId = routerId;
@@ -133,6 +118,7 @@ public class NeutronRouterChangeListener extends AbstractDataChangeListener<Rout
         }
 
         nvpnNatManager.handleExternalNetworkForRouter(original, update);
+        gwMacResolver.sendArpRequestsToExtGateways(update);
     }
 
     private void handleChangedRoutes(Uuid vpnName, List<Routes> routes, int addedOrRemoved) {
@@ -143,11 +129,12 @@ public class NeutronRouterChangeListener extends AbstractDataChangeListener<Rout
         for ( Routes route : routes ) {
             String nextHop = String.valueOf(route.getNexthop().getValue());
             // Nexthop is another VPN?
-            Optional<InterVpnLink> interVpnLink = NeutronvpnUtils.getInterVpnLinkByEndpointIp(broker, nextHop);
+            Optional<InterVpnLink> interVpnLink = NeutronvpnUtils.getInterVpnLinkByEndpointIp(dataBroker, nextHop);
             if ( interVpnLink.isPresent() ) {
                 Optional<InterVpnLinkState> interVpnLinkState =
-                        NeutronvpnUtils.getInterVpnLinkState(broker, interVpnLink.get().getName());
-                if ( interVpnLinkState.isPresent() && interVpnLinkState.get().getState() == InterVpnLinkState.State.Active) {
+                        NeutronvpnUtils.getInterVpnLinkState(dataBroker, interVpnLink.get().getName());
+                if ( interVpnLinkState.isPresent()
+                    && interVpnLinkState.get().getState() == InterVpnLinkState.State.Active) {
                     interVpnLinkRoutes.add(route);
                     nexthopsXinterVpnLinks.put(nextHop, interVpnLink.get());
                 } else {
@@ -161,7 +148,7 @@ public class NeutronRouterChangeListener extends AbstractDataChangeListener<Rout
 
         if ( addedOrRemoved == NwConstants.ADD_FLOW ) {
             nvpnManager.addInterVpnRoutes(vpnName, interVpnLinkRoutes, nexthopsXinterVpnLinks);
-            nvpnManager.addAdjacencyforExtraRoute(vpnName, otherRoutes);
+            nvpnManager.updateVpnInterfaceWithExtraRouteAdjacency(vpnName, otherRoutes);
         } else {
             nvpnManager.removeAdjacencyforExtraRoute(vpnName, otherRoutes);
             nvpnManager.removeInterVpnRoutes(vpnName, interVpnLinkRoutes, nexthopsXinterVpnLinks);