Upgrade to the Neon base platform
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronPortChangeListener.java
index 38113ad0ab4f1431c14f364d699907bcf9f8c629..03a7caf313c9e80432d46cc9dfd81e42eea93d9c 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.netvirt.neutronvpn;
 
 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
+import static org.opendaylight.netvirt.neutronvpn.api.utils.NeutronUtils.requireNonNullElse;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Strings;
@@ -25,6 +26,7 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
+import javax.annotation.Nullable;
 import javax.annotation.PostConstruct;
 import javax.inject.Singleton;
 import org.apache.commons.lang3.ObjectUtils;
@@ -45,7 +47,7 @@ import org.opendaylight.netvirt.elanmanager.api.IElanService;
 import org.opendaylight.netvirt.neutronvpn.api.enums.IpVersionChoice;
 import org.opendaylight.netvirt.neutronvpn.api.utils.NeutronConstants;
 import org.opendaylight.netvirt.neutronvpn.api.utils.NeutronUtils;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
 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.Uuid;
@@ -132,6 +134,7 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
 
     @Override
     protected void add(InstanceIdentifier<Port> identifier, Port input) {
+        LOG.trace("Received port add event: port={}", input);
         String portName = input.getUuid().getValue();
         LOG.trace("Adding Port : key: {}, value={}", identifier, input);
         Network network = neutronvpnUtils.getNeutronNetwork(input.getNetworkId());
@@ -151,7 +154,7 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                 return;
             }
             if (NeutronConstants.DEVICE_OWNER_GATEWAY_INF.equals(input.getDeviceOwner())) {
-                handleRouterGatewayUpdated(input);
+                handleRouterGatewayUpdated(input, false);
                 portStatus = NeutronUtils.PORT_STATUS_ACTIVE;
             } else if (NeutronConstants.DEVICE_OWNER_FLOATING_IP.equals(input.getDeviceOwner())) {
                 handleFloatingIpPortUpdated(null, input);
@@ -191,6 +194,7 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                 return;
             } else if (NeutronConstants.DEVICE_OWNER_GATEWAY_INF.equals(input.getDeviceOwner())
                     || NeutronConstants.DEVICE_OWNER_FLOATING_IP.equals(input.getDeviceOwner())) {
+                handleRouterGatewayUpdated(input, true);
                 elanService.removeKnownL3DmacAddress(input.getMacAddress().getValue(), input.getNetworkId().getValue());
             }
         }
@@ -201,6 +205,7 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
 
     @Override
     protected void update(InstanceIdentifier<Port> identifier, Port original, Port update) {
+        LOG.trace("Received port update event: original={}, update={}", original, update);
         // Switchdev ports need to be bounded to a host before creation
         // in order to validate the supported vnic types from the hostconfig
         if (isPortTypeSwitchdev(original)
@@ -227,7 +232,7 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                 return;
             }
             if (NeutronConstants.DEVICE_OWNER_GATEWAY_INF.equals(update.getDeviceOwner())) {
-                handleRouterGatewayUpdated(update);
+                handleRouterGatewayUpdated(update, false);
             } else if (NeutronConstants.DEVICE_OWNER_FLOATING_IP.equals(update.getDeviceOwner())) {
                 handleFloatingIpPortUpdated(original, update);
             }
@@ -265,9 +270,9 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
         }
     }
 
-    private void handleFloatingIpPortUpdated(Port original, Port update) {
-        if ((original == null || original.getDeviceId().equals(NeutronConstants.FLOATING_IP_DEVICE_ID_PENDING))
-            && !update.getDeviceId().equals(NeutronConstants.FLOATING_IP_DEVICE_ID_PENDING)) {
+    private void handleFloatingIpPortUpdated(@Nullable Port original, Port update) {
+        if ((original == null || NeutronConstants.FLOATING_IP_DEVICE_ID_PENDING.equals(original.getDeviceId()))
+            && !NeutronConstants.FLOATING_IP_DEVICE_ID_PENDING.equals(update.getDeviceId())) {
             // populate floating-ip uuid and floating-ip port attributes (uuid, mac and subnet id for the ONLY
             // fixed IP) to be used by NAT, depopulated in NATService once mac is retrieved in the removal path
             addToFloatingIpPortInfo(new Uuid(update.getDeviceId()), update.getUuid(), update.getFixedIps().get(0)
@@ -292,9 +297,9 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                 listVpnIds.add(vpnId);
                 Uuid internetVpnId = neutronvpnUtils.getInternetvpnUuidBoundToRouterId(routerId);
                 List<Subnetmap> subnetMapList = new ArrayList<>();
-                List<FixedIps> portIps = routerPort.getFixedIps();
                 boolean portIsIpv6 = false;
-                for (FixedIps portIP : portIps) {
+                for (FixedIps portIP : requireNonNullElse(routerPort.getFixedIps(),
+                        Collections.<FixedIps>emptyList())) {
                     // NOTE:  Please donot change the order of calls to updateSubnetNodeWithFixedIP
                     // and addSubnetToVpn here
                     if (internetVpnId != null
@@ -310,24 +315,22 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                 }
                 if (portIsIpv6) {
                     listVpnIds.add(internetVpnId);
-                    if (neutronvpnUtils.shouldVpnHandleIpVersionChoiceChangeToAdd(
-                                     IpVersionChoice.IPV6, internetVpnId)) {
-                        neutronvpnUtils.updateVpnInstanceWithIpFamily(internetVpnId.getValue(),
-                                                                     IpVersionChoice.IPV6, true);
-                        neutronvpnUtils.updateVpnInstanceWithFallback(internetVpnId.getValue(), true);
+                    if (neutronvpnUtils.shouldVpnHandleIpVersionChoiceChange(
+                                     IpVersionChoice.IPV6, routerId, true)) {
+                        neutronvpnUtils.updateVpnInstanceWithIpFamily(internetVpnId.getValue(), IpVersionChoice.IPV6,
+                                true);
+                        neutronvpnUtils.updateVpnInstanceWithFallback(routerId, internetVpnId, true);
                     }
                 }
                 if (! subnetMapList.isEmpty()) {
                     nvpnManager.createVpnInterface(listVpnIds, routerPort, null);
                 }
-                for (FixedIps portIP : routerPort.getFixedIps()) {
+                IpVersionChoice ipVersion = IpVersionChoice.UNDEFINED;
+                for (FixedIps portIP : requireNonNullElse(routerPort.getFixedIps(),
+                        Collections.<FixedIps>emptyList())) {
                     String ipValue = portIP.getIpAddress().stringValue();
-                    IpVersionChoice version = NeutronvpnUtils.getIpVersionFromString(ipValue);
-                    if (neutronvpnUtils.shouldVpnHandleIpVersionChoiceChangeToAdd(version, vpnId)) {
-                        neutronvpnUtils.updateVpnInstanceWithIpFamily(vpnId.getValue(),
-                               version, true);
-                    }
-                    if (version.isIpVersionChosen(IpVersionChoice.IPV4)) {
+                    ipVersion = NeutronvpnUtils.getIpVersionFromString(ipValue);
+                    if (ipVersion.isIpVersionChosen(IpVersionChoice.IPV4)) {
                         nvpnManager.addSubnetToVpn(vpnId, portIP.getSubnetId(),
                                                         null /* internet-vpn-id */);
                     } else {
@@ -337,6 +340,11 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                             ipValue, routerPort.getMacAddress(),
                             routerPort.getUuid().getValue(), vpnId.getValue());
                 }
+                if (neutronvpnUtils.shouldVpnHandleIpVersionChoiceChange(ipVersion, routerId, true)) {
+                    LOG.debug("vpnInstanceOpDataEntry is getting update with ip address family {} for VPN {}",
+                            ipVersion, vpnId.getValue());
+                    neutronvpnUtils.updateVpnInstanceWithIpFamily(vpnId.getValue(), ipVersion, true);
+                }
                 nvpnManager.addToNeutronRouterInterfacesMap(routerId, routerPort.getUuid().getValue());
                 jobCoordinator.enqueueJob(routerId.toString(), () -> {
                     nvpnNatManager.handleSubnetsForExternalRouter(routerId);
@@ -360,8 +368,9 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
             Uuid routerId = new Uuid(routerPort.getDeviceId());
             Uuid infNetworkId = routerPort.getNetworkId();
             elanService.removeKnownL3DmacAddress(routerPort.getMacAddress().getValue(), infNetworkId.getValue());
-            Uuid vpnId = ObjectUtils.defaultIfNull(neutronvpnUtils.getVpnForRouter(routerId, true), routerId);
-            List<FixedIps> portIps = routerPort.getFixedIps();
+            Uuid vpnId = ObjectUtils.defaultIfNull(neutronvpnUtils.getVpnForRouter(routerId, true),
+                    routerId);
+            List<FixedIps> portIps = requireNonNullElse(routerPort.getFixedIps(), Collections.emptyList());
             boolean vpnInstanceInternetIpVersionRemoved = false;
             Uuid vpnInstanceInternetUuid = null;
             for (FixedIps portIP : portIps) {
@@ -385,24 +394,20 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
              *  cleanup of router interface flows*/
             nvpnManager.deleteVpnInterface(routerPort.getUuid().getValue(),
                                            null /* vpn-id */, null /* wrtConfigTxn*/);
+            final Uuid internetVpnId = vpnInstanceInternetUuid;
             // update RouterInterfaces map
             ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
                 confTx -> {
-                    boolean vpnInstanceIpVersionRemoved = false;
-                    IpVersionChoice vpnInstanceIpVersionToRemove = IpVersionChoice.UNDEFINED;
+                    IpVersionChoice ipVersion = IpVersionChoice.UNDEFINED;
                     for (FixedIps portIP : portIps) {
                         Subnetmap sn = neutronvpnUtils.getSubnetmap(portIP.getSubnetId());
                         // router Port have either IPv4 or IPv6, never both
-                        if (neutronvpnUtils.shouldVpnHandleIpVersionChangeToRemove(sn, vpnId)) {
-                            vpnInstanceIpVersionRemoved = true;
-                            vpnInstanceIpVersionToRemove = NeutronvpnUtils.getIpVersionFromString(sn.getSubnetIp());
-                        }
+                        ipVersion = neutronvpnUtils.getIpVersionFromString(sn.getSubnetIp());
                         String ipValue = portIP.getIpAddress().stringValue();
                         neutronvpnUtils.removeVpnPortFixedIpToPort(vpnId.getValue(), ipValue, confTx);
                         // NOTE:  Please donot change the order of calls to removeSubnetFromVpn and
                         // and updateSubnetNodeWithFixedIP
-                        nvpnManager.removeSubnetFromVpn(vpnId, portIP.getSubnetId(),
-                                sn != null ? sn.getInternetVpnId() : null);
+                        nvpnManager.removeSubnetFromVpn(vpnId, portIP.getSubnetId(), internetVpnId);
                         nvpnManager.updateSubnetNodeWithFixedIp(portIP.getSubnetId(), null, null,
                             null, null, null);
                     }
@@ -413,20 +418,21 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                         nvpnNatManager.handleSubnetsForExternalRouter(routerId);
                         return Collections.emptyList();
                     });
-                    if (vpnInstanceIpVersionRemoved) {
-                        neutronvpnUtils.updateVpnInstanceWithIpFamily(vpnId.getValue(), vpnInstanceIpVersionToRemove,
-                                false);
+                    if (neutronvpnUtils.shouldVpnHandleIpVersionChoiceChange(ipVersion, routerId, false)) {
+                        LOG.debug("vpnInstanceOpDataEntry is getting update with ip address family {} for VPN {}",
+                                ipVersion, vpnId.getValue());
+                        neutronvpnUtils.updateVpnInstanceWithIpFamily(vpnId.getValue(), ipVersion, false);
                     }
                 }), LOG, "Error handling interface removal");
             if (vpnInstanceInternetIpVersionRemoved) {
                 neutronvpnUtils.updateVpnInstanceWithIpFamily(vpnInstanceInternetUuid.getValue(),
                         IpVersionChoice.IPV6, false);
-                neutronvpnUtils.updateVpnInstanceWithFallback(vpnInstanceInternetUuid.getValue(), false);
+                neutronvpnUtils.updateVpnInstanceWithFallback(routerId, vpnInstanceInternetUuid, false);
             }
         }
     }
 
-    private void handleRouterGatewayUpdated(Port routerGwPort) {
+    private void handleRouterGatewayUpdated(Port routerGwPort, boolean isRtrGwRemoved) {
         Uuid routerId = new Uuid(routerGwPort.getDeviceId());
         Uuid networkId = routerGwPort.getNetworkId();
         Network network = neutronvpnUtils.getNeutronNetwork(networkId);
@@ -437,6 +443,9 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
         if (isExternal) {
             Uuid vpnInternetId = neutronvpnUtils.getVpnForNetwork(networkId);
             if (vpnInternetId != null) {
+                if (!isRtrGwRemoved) {
+                    nvpnManager.updateVpnMaps(vpnInternetId, null, routerId, null, null);
+                }
                 List<Subnetmap> snList = neutronvpnUtils.getNeutronRouterSubnetMaps(routerId);
                 for (Subnetmap sn : snList) {
                     if (sn.getNetworkId() == networkId) {
@@ -445,7 +454,15 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                     if (NeutronvpnUtils.getIpVersionFromString(sn.getSubnetIp()) != IpVersionChoice.IPV6) {
                         continue;
                     }
-                    nvpnManager.addSubnetToVpn(null, sn.getId(), vpnInternetId);
+                    if (isRtrGwRemoved) {
+                        nvpnManager.removeV6PrivateSubnetToExtNetwork(routerId, vpnInternetId, sn);
+                    } else {
+                        nvpnManager.addV6PrivateSubnetToExtNetwork(routerId, vpnInternetId, sn);
+                    }
+                }
+                //Update Internet BGP-VPN
+                if (isRtrGwRemoved) {
+                    nvpnManager.updateVpnMaps(vpnInternetId, null, null, null, null);
                 }
             }
         }
@@ -496,6 +513,7 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
         MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, routersId, builder.build());
     }
 
+    @Nullable
     private String getPortHostId(final Port port) {
         if (port != null) {
             PortBindingExtension portBinding = port.augmentation(PortBindingExtension.class);
@@ -506,6 +524,7 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
         return null;
     }
 
+    @Nullable
     private Hostconfig getHostConfig(final Port port) {
         String hostId = getPortHostId(port);
         if (hostId == null) {
@@ -518,7 +537,7 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
             LOG.error("failed to read host config from host {}", hostId, e);
             return null;
         }
-        return hostConfig.isPresent() ? hostConfig.get() : null;
+        return hostConfig.orNull();
     }
 
     private boolean isPortBound(final Port port) {
@@ -545,13 +564,14 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
         return false;
     }
 
+    @Nullable
     private Map<String, JsonElement> unmarshal(final String profile) {
         if (null == profile) {
             return null;
         }
         Gson gson = new Gson();
         JsonObject jsonObject = gson.fromJson(profile, JsonObject.class);
-        Map<String, JsonElement> map = new HashMap();
+        Map<String, JsonElement> map = new HashMap<>();
         for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
             map.put(entry.getKey(), entry.getValue());
         }
@@ -588,7 +608,7 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
     private void handleNeutronPortCreated(final Port port) {
         final String portName = port.getUuid().getValue();
         final Uuid portId = port.getUuid();
-        final List<FixedIps> portIpAddrsList = port.getFixedIps();
+        final List<FixedIps> portIpAddrsList = requireNonNullElse(port.getFixedIps(), Collections.emptyList());
         if (NeutronConstants.IS_ODL_DHCP_PORT.test(port)) {
             return;
         }
@@ -649,7 +669,7 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
     private void handleNeutronPortDeleted(final Port port) {
         final String portName = port.getUuid().getValue();
         final Uuid portId = port.getUuid();
-        final List<FixedIps> portIpsList = port.getFixedIps();
+        final List<FixedIps> portIpsList = requireNonNullElse(port.getFixedIps(), Collections.emptyList());
         jobCoordinator.enqueueJob("PORT- " + portName,
             () -> Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, confTx -> {
                 if (!(NeutronUtils.isPortVnicTypeNormal(port) || isPortTypeSwitchdev(port))) {
@@ -724,6 +744,11 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
                       + "during subnet deletion event.", portupdate.getUuid().getValue());
             return;
         }
+
+        if (NeutronConstants.IS_ODL_DHCP_PORT.test(portupdate)) {
+            return;
+        }
+
         jobCoordinator.enqueueJob("PORT- " + portupdate.getUuid().getValue(),
             () -> Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, confTx -> {
                 final List<Uuid> originalSnMapsIds = portoriginalIps.stream().map(FixedIps::getSubnetId)
@@ -787,6 +812,7 @@ public class NeutronPortChangeListener extends AsyncDataTreeChangeListenerBase<P
             })));
     }
 
+    @Nullable
     private InterfaceAclBuilder handlePortSecurityUpdated(Port portOriginal,
             Port portUpdated, boolean origSecurityEnabled, boolean updatedSecurityEnabled,
             InterfaceBuilder interfaceBuilder) {