Fix Neutron VPN to handle Routers. 55/38755/2
authorVivekanandan Narasimhan <n.vivekanandan@ericsson.com>
Thu, 12 May 2016 10:57:03 +0000 (16:27 +0530)
committerVishal Thapar <vishal.thapar@ericsson.com>
Sat, 14 May 2016 04:40:54 +0000 (10:10 +0530)
This fix enables Neutron VPN module inside netvirt to
handle routers and router interfaces, thereby enabling
internal VPN to become functional.

Change-Id: I527e182c5ad38b0cabf457dee465763dcd30dd0c
Signed-off-by: Vivekanandan Narasimhan <n.vivekanandan@ericsson.com>
vpnservice/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/netvirt/neutronvpn/NeutronPortChangeListener.java
vpnservice/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/netvirt/neutronvpn/NeutronvpnUtils.java

index 14dbc5023d95db4ff12e10ef11b30de8c7b56754..9abc846d2638d2082aa7be5d29922c186ae12113 100644 (file)
@@ -24,6 +24,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
 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.networks.rev150712.networks.attributes.networks.Network;
 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.ports.attributes.Ports;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
@@ -106,6 +107,15 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener<Port>
         if (LOG.isTraceEnabled()) {
             LOG.trace("Adding Port : key: " + identifier + ", value=" + input);
         }
+
+        /* check if router interface has been created */
+        if ((input.getDeviceOwner() != null) && (input.getDeviceId() != null)) {
+            if (input.getDeviceOwner().equals(NeutronvpnUtils.DEVICE_OWNER_ROUTER_INF)) {
+                handleRouterInterfaceAdded(input);
+                /* nothing else to do here */
+                return;
+            }
+        }
         handleNeutronPortCreated(input);
 
     }
@@ -115,6 +125,14 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener<Port>
         if (LOG.isTraceEnabled()) {
             LOG.trace("Removing Port : key: " + identifier + ", value=" + input);
         }
+
+        if ((input.getDeviceOwner() != null) && (input.getDeviceId() != null)) {
+            if (input.getDeviceOwner().equals(NeutronvpnUtils.DEVICE_OWNER_ROUTER_INF)) {
+                handleRouterInterfaceRemoved(input);
+                /* nothing else to do here */
+                return;
+            }
+        }
         handleNeutronPortDeleted(input);
 
     }
@@ -125,9 +143,19 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener<Port>
             LOG.trace("Updating Port : key: " + identifier + ", original value=" + original + ", update value=" +
                     update);
         }
+
         List<FixedIps> oldIPs = (original.getFixedIps() != null) ? original.getFixedIps() : new ArrayList<FixedIps>();
         List<FixedIps> newIPs = (update.getFixedIps() != null) ? update.getFixedIps() : new ArrayList<FixedIps>();
 
+        /* check if router interface has been updated */
+        if ((update.getDeviceOwner() != null) && (update.getDeviceId() != null)) {
+            if (update.getDeviceOwner().equals(NeutronvpnUtils.DEVICE_OWNER_ROUTER_INF)) {
+                handleRouterInterfaceAdded(update);
+                /* nothing else to do here */
+                return;
+            }
+        }
+
         if (!oldIPs.equals(newIPs)) {
             Iterator<FixedIps> iterator = newIPs.iterator();
             while (iterator.hasNext()) {
@@ -140,6 +168,46 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener<Port>
         }
     }
 
+    private void handleRouterInterfaceAdded(Port routerPort) {
+        if (routerPort.getDeviceId() != null) {
+            Uuid routerId = new Uuid(routerPort.getDeviceId());
+            Uuid infNetworkId = routerPort.getNetworkId();
+            Uuid existingVpnId = NeutronvpnUtils.getVpnForNetwork(broker, infNetworkId);
+            if (existingVpnId == null) {
+                for (FixedIps portIP : routerPort.getFixedIps()) {
+                    if (portIP.getIpAddress().getIpv4Address() != null) {
+                        Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
+                        if (vpnId == null) {
+                            vpnId = routerId;
+                        }
+                        nvpnManager.addSubnetToVpn(vpnId, portIP.getSubnetId());
+                        //nvpnNatManager.handleSubnetsForExternalRouter(routerId, broker);
+                    }
+                }
+            } else {
+                LOG.error("Neutron network {} corresponding to router interface port {} for neutron router {} already" +
+                        " associated to VPN {}", infNetworkId.getValue(), routerPort.getUuid().getValue(), routerId
+                        .getValue(), existingVpnId.getValue());
+            }
+        }
+    }
+
+    private void handleRouterInterfaceRemoved(Port routerPort) {
+        if (routerPort.getDeviceId() != null) {
+            Uuid routerId = new Uuid(routerPort.getDeviceId());
+            for (FixedIps portIP : routerPort.getFixedIps()) {
+                if (portIP.getIpAddress().getIpv4Address() != null) {
+                    Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
+                    if(vpnId == null) {
+                        vpnId = routerId;
+                    }
+                    nvpnManager.removeSubnetFromVpn(vpnId, portIP.getSubnetId());
+                    //nvpnNatManager.handleSubnetsForExternalRouter(routerId, broker);
+                }
+            }
+        }
+    }
+
     private void handleNeutronPortCreated(Port port) {
         if (!NeutronUtils.isPortVnicTypeNormal(port)) {
             LOG.info("Port {} is not a NORMAL VNIC Type port; OF Port interfaces are not created",
index c65c9e32826ed38a79ee897988a535cc54e0d562..d744f1ba436570fc88971ea81352c15d5457a5cd 100644 (file)
@@ -67,6 +67,7 @@ import java.util.concurrent.Future;
 public class NeutronvpnUtils {
 
     private static final Logger logger = LoggerFactory.getLogger(NeutronvpnUtils.class);
+    public static final String DEVICE_OWNER_ROUTER_INF = "network:router_interface";
     public static final String VNIC_TYPE_NORMAL = "normal";
 
     protected static Subnetmap getSubnetmap(DataBroker broker, Uuid subnetId) {