Search in sources :

Example 1 with NatMode

use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig.NatMode in project netvirt by opendaylight.

the class ExternalNetworksChangeListener method associateExternalNetworkWithVPN.

private void associateExternalNetworkWithVPN(Networks network, WriteTransaction writeFlowInvTx) {
    List<Uuid> routerIds = network.getRouterIds();
    for (Uuid routerId : routerIds) {
        // long router = NatUtil.getVpnId(dataBroker, routerId.getValue());
        InstanceIdentifier<RouterPorts> routerPortsId = NatUtil.getRouterPortsId(routerId.getValue());
        Optional<RouterPorts> optRouterPorts = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routerPortsId);
        if (!optRouterPorts.isPresent()) {
            LOG.debug("associateExternalNetworkWithVPN : Could not read Router Ports data object with id: {} " + "to handle associate ext nw {}", routerId, network.getId());
            continue;
        }
        RouterPorts routerPorts = optRouterPorts.get();
        List<Ports> interfaces = routerPorts.getPorts();
        for (Ports port : interfaces) {
            String portName = port.getPortName();
            BigInteger dpnId = NatUtil.getDpnForInterface(interfaceManager, portName);
            if (dpnId.equals(BigInteger.ZERO)) {
                LOG.debug("associateExternalNetworkWithVPN : DPN not found for {}, " + "skip handling of ext nw {} association", portName, network.getId());
                continue;
            }
            List<InternalToExternalPortMap> intExtPortMapList = port.getInternalToExternalPortMap();
            for (InternalToExternalPortMap ipMap : intExtPortMapList) {
                // remove all VPN related entries
                floatingIpListener.createNATFlowEntries(dpnId, portName, routerId.getValue(), network.getId(), ipMap, writeFlowInvTx);
            }
        }
    }
    // SNAT
    for (Uuid routerId : routerIds) {
        LOG.debug("associateExternalNetworkWithVPN() : for routerId {}", routerId);
        Uuid networkId = network.getId();
        if (networkId == null) {
            LOG.error("associateExternalNetworkWithVPN : networkId is null for the router ID {}", routerId);
            return;
        }
        final String vpnName = network.getVpnid().getValue();
        if (vpnName == null) {
            LOG.error("associateExternalNetworkWithVPN : No VPN associated with ext nw {} for router {}", networkId, routerId);
            return;
        }
        BigInteger dpnId = new BigInteger("0");
        InstanceIdentifier<RouterToNaptSwitch> routerToNaptSwitch = NatUtil.buildNaptSwitchRouterIdentifier(routerId.getValue());
        Optional<RouterToNaptSwitch> rtrToNapt = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, routerToNaptSwitch);
        if (rtrToNapt.isPresent()) {
            dpnId = rtrToNapt.get().getPrimarySwitchId();
        }
        LOG.debug("associateExternalNetworkWithVPN : got primarySwitch as dpnId{} ", dpnId);
        if (dpnId == null || dpnId.equals(BigInteger.ZERO)) {
            LOG.warn("associateExternalNetworkWithVPN : primary napt Switch not found for router {} on dpn: {}", routerId, dpnId);
            return;
        }
        Long routerIdentifier = NatUtil.getVpnId(dataBroker, routerId.getValue());
        InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping> idBuilder = InstanceIdentifier.builder(IntextIpMap.class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping.class, new org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMappingKey(routerIdentifier));
        InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping> id = idBuilder.build();
        Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping> ipMapping = MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
        if (ipMapping.isPresent()) {
            List<IpMap> ipMaps = ipMapping.get().getIpMap();
            for (IpMap ipMap : ipMaps) {
                String externalIp = ipMap.getExternalIp();
                LOG.debug("associateExternalNetworkWithVPN : Calling advToBgpAndInstallFibAndTsFlows for dpnId {}," + "vpnName {} and externalIp {}", dpnId, vpnName, externalIp);
                if (natMode == NatMode.Controller) {
                    externalRouterListener.advToBgpAndInstallFibAndTsFlows(dpnId, NwConstants.INBOUND_NAPT_TABLE, vpnName, routerIdentifier, routerId.getValue(), externalIp, network.getId(), null, /* external-router */
                    writeFlowInvTx);
                }
            }
        } else {
            LOG.warn("associateExternalNetworkWithVPN : No ipMapping present fot the routerId {}", routerId);
        }
        long vpnId = NatUtil.getVpnId(dataBroker, vpnName);
        // Install 47 entry to point to 21
        if (natMode == NatMode.Controller) {
            externalRouterListener.installNaptPfibEntriesForExternalSubnets(routerId.getValue(), dpnId, writeFlowInvTx);
            if (vpnId != -1) {
                LOG.debug("associateExternalNetworkWithVPN : Calling externalRouterListener installNaptPfibEntry " + "for dpnId {} and vpnId {}", dpnId, vpnId);
                externalRouterListener.installNaptPfibEntry(dpnId, vpnId, writeFlowInvTx);
            }
        }
    }
}
Also used : RouterPorts(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.RouterPorts) IntextIpMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpMap) InternalToExternalPortMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.router.ports.ports.InternalToExternalPortMap) RouterToNaptSwitch(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.napt.switches.RouterToNaptSwitch) RouterPorts(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.RouterPorts) Ports(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.floating.ip.info.router.ports.Ports) IpMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMap) IntextIpMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpMap) Uuid(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid) BigInteger(java.math.BigInteger)

Example 2 with NatMode

use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig.NatMode in project netvirt by opendaylight.

the class ExternalNetworksChangeListener method removeSnatEntries.

private void removeSnatEntries(Networks original, Uuid networkUuid, WriteTransaction writeFlowInvTx) {
    List<Uuid> routerUuids = original.getRouterIds();
    for (Uuid routerUuid : routerUuids) {
        Long routerId = NatUtil.getVpnId(dataBroker, routerUuid.getValue());
        if (routerId == NatConstants.INVALID_ID) {
            LOG.error("removeSnatEntries : Invalid routerId returned for routerName {}", routerUuid.getValue());
            return;
        }
        Collection<String> externalIps = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
        if (natMode == NatMode.Controller) {
            externalRouterListener.handleDisableSnatInternetVpn(routerUuid.getValue(), routerId, networkUuid, externalIps, original.getVpnid().getValue(), writeFlowInvTx);
        }
    }
}
Also used : Uuid(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid)

Example 3 with NatMode

use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig.NatMode in project netvirt by opendaylight.

the class ExternalRoutersListener method update.

@Override
protected void update(InstanceIdentifier<Routers> identifier, Routers original, Routers update) {
    String routerName = original.getRouterName();
    Long routerId = NatUtil.getVpnId(dataBroker, routerName);
    if (routerId == NatConstants.INVALID_ID) {
        LOG.error("update : external router event - Invalid routerId for routerName {}", routerName);
        return;
    }
    // Check if its update on SNAT flag
    boolean originalSNATEnabled = original.isEnableSnat();
    boolean updatedSNATEnabled = update.isEnableSnat();
    LOG.debug("update :called with originalFlag and updatedFlag for SNAT enabled " + "as {} and {}", originalSNATEnabled, updatedSNATEnabled);
    if (natMode == NatMode.Conntrack && !upgradeState.isUpgradeInProgress()) {
        if (originalSNATEnabled != updatedSNATEnabled) {
            BigInteger primarySwitchId;
            if (originalSNATEnabled) {
                // SNAT disabled for the router
                centralizedSwitchScheduler.releaseCentralizedSwitch(update);
            } else {
                centralizedSwitchScheduler.scheduleCentralizedSwitch(update);
            }
        } else if (updatedSNATEnabled) {
            centralizedSwitchScheduler.updateCentralizedSwitch(original, update);
        }
        List<ExternalIps> originalExternalIps = original.getExternalIps();
        List<ExternalIps> updateExternalIps = update.getExternalIps();
        if (!Objects.equals(originalExternalIps, updateExternalIps)) {
            if (originalExternalIps == null || originalExternalIps.isEmpty()) {
                centralizedSwitchScheduler.scheduleCentralizedSwitch(update);
            }
        }
    } else {
        /* Get Primary Napt Switch for existing router from "router-to-napt-switch" DS.
             * if dpnId value is null or zero then go for electing new Napt switch for existing router.
             */
        long bgpVpnId = NatConstants.INVALID_ID;
        Uuid bgpVpnUuid = NatUtil.getVpnForRouter(dataBroker, routerName);
        if (bgpVpnUuid != null) {
            bgpVpnId = NatUtil.getVpnId(dataBroker, bgpVpnUuid.getValue());
        }
        BigInteger dpnId = getPrimaryNaptSwitch(routerName);
        if (dpnId == null || dpnId.equals(BigInteger.ZERO)) {
            // Router has no interface attached
            return;
        }
        final long finalBgpVpnId = bgpVpnId;
        coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + update.getKey(), () -> {
            WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
            WriteTransaction removeFlowInvTx = dataBroker.newWriteOnlyTransaction();
            Uuid networkId = original.getNetworkId();
            if (originalSNATEnabled != updatedSNATEnabled) {
                if (originalSNATEnabled) {
                    // SNAT disabled for the router
                    Uuid networkUuid = original.getNetworkId();
                    LOG.info("update : SNAT disabled for Router {}", routerName);
                    Collection<String> externalIps = NatUtil.getExternalIpsForRouter(dataBroker, routerId);
                    handleDisableSnat(original, networkUuid, externalIps, false, null, dpnId, routerId, removeFlowInvTx);
                } else {
                    LOG.info("update : SNAT enabled for Router {}", original.getRouterName());
                    handleEnableSnat(original, routerId, dpnId, finalBgpVpnId, removeFlowInvTx);
                }
            }
            if (!Objects.equals(original.getExtGwMacAddress(), update.getExtGwMacAddress())) {
                NatUtil.installRouterGwFlows(dataBroker, vpnManager, original, dpnId, NwConstants.DEL_FLOW);
                NatUtil.installRouterGwFlows(dataBroker, vpnManager, update, dpnId, NwConstants.ADD_FLOW);
            }
            // Check if the Update is on External IPs
            LOG.debug("update : Checking if this is update on External IPs");
            List<String> originalExternalIps = NatUtil.getIpsListFromExternalIps(original.getExternalIps());
            List<String> updatedExternalIps = NatUtil.getIpsListFromExternalIps(update.getExternalIps());
            // Check if the External IPs are added during the update.
            Set<String> addedExternalIps = new HashSet<>(updatedExternalIps);
            addedExternalIps.removeAll(originalExternalIps);
            if (addedExternalIps.size() != 0) {
                LOG.debug("update : Start processing of the External IPs addition during the update operation");
                vpnManager.addArpResponderFlowsToExternalNetworkIps(routerName, addedExternalIps, update.getExtGwMacAddress(), dpnId, update.getNetworkId(), null);
                for (String addedExternalIp : addedExternalIps) {
                    /*
                        1) Do nothing in the IntExtIp model.
                        2) Initialise the count of the added external IP to 0 in the ExternalCounter model.
                     */
                    String[] externalIpParts = NatUtil.getExternalIpAndPrefix(addedExternalIp);
                    String externalIp = externalIpParts[0];
                    String externalIpPrefix = externalIpParts[1];
                    String externalpStr = externalIp + "/" + externalIpPrefix;
                    LOG.debug("update : Initialise the count mapping of the external IP {} for the " + "router ID {} in the ExternalIpsCounter model.", externalpStr, routerId);
                    naptManager.initialiseNewExternalIpCounter(routerId, externalpStr);
                }
                LOG.debug("update : End processing of the External IPs addition during the update operation");
            }
            // Check if the External IPs are removed during the update.
            Set<String> removedExternalIps = new HashSet<>(originalExternalIps);
            removedExternalIps.removeAll(updatedExternalIps);
            if (removedExternalIps.size() > 0) {
                LOG.debug("update : Start processing of the External IPs removal during the update operation");
                vpnManager.removeArpResponderFlowsToExternalNetworkIps(routerName, removedExternalIps, original.getExtGwMacAddress(), dpnId, networkId);
                for (String removedExternalIp : removedExternalIps) {
                    /*
                        1) Remove the mappings in the IntExt IP model which has external IP.
                        2) Remove the external IP in the ExternalCounter model.
                        3) For the corresponding subnet IDs whose external IP mapping was removed, allocate one of the
                           least loaded external IP.
                           Store the subnet IP and the reallocated external IP mapping in the IntExtIp model.
                        4) Increase the count of the allocated external IP by one.
                        5) Advertise to the BGP if external IP is allocated for the first time for the router
                         i.e. the route for the external IP is absent.
                        6) Remove the NAPT translation entries from Inbound and Outbound NAPT tables for
                         the removed external IPs and also from the model.
                        7) Advertise to the BGP for removing the route for the removed external IPs.
                     */
                    String[] externalIpParts = NatUtil.getExternalIpAndPrefix(removedExternalIp);
                    String externalIp = externalIpParts[0];
                    String externalIpPrefix = externalIpParts[1];
                    String externalIpAddrStr = externalIp + "/" + externalIpPrefix;
                    LOG.debug("update : Clear the routes from the BGP and remove the FIB and TS " + "entries for removed external IP {}", externalIpAddrStr);
                    Uuid vpnUuId = NatUtil.getVpnIdfromNetworkId(dataBroker, networkId);
                    String vpnName = "";
                    if (vpnUuId != null) {
                        vpnName = vpnUuId.getValue();
                    }
                    clrRtsFromBgpAndDelFibTs(dpnId, routerId, externalIpAddrStr, vpnName, networkId, update.getExtGwMacAddress(), removeFlowInvTx);
                    LOG.debug("update : Remove the mappings in the IntExtIP model which has external IP.");
                    // Get the internal IPs which are associated to the removed external IPs
                    List<IpMap> ipMaps = naptManager.getIpMapList(dataBroker, routerId);
                    List<String> removedInternalIps = new ArrayList<>();
                    for (IpMap ipMap : ipMaps) {
                        if (ipMap.getExternalIp().equals(externalIpAddrStr)) {
                            removedInternalIps.add(ipMap.getInternalIp());
                        }
                    }
                    LOG.debug("update : Remove the mappings of the internal IPs from the IntExtIP model.");
                    for (String removedInternalIp : removedInternalIps) {
                        LOG.debug("update : Remove the IP mapping of the internal IP {} for the " + "router ID {} from the IntExtIP model", removedInternalIp, routerId);
                        naptManager.removeFromIpMapDS(routerId, removedInternalIp);
                    }
                    LOG.debug("update : Remove the count mapping of the external IP {} for the " + "router ID {} from the ExternalIpsCounter model.", externalIpAddrStr, routerId);
                    naptManager.removeExternalIpCounter(routerId, externalIpAddrStr);
                    LOG.debug("update : Allocate the least loaded external IPs to the subnets " + "whose external IPs were removed.");
                    for (String removedInternalIp : removedInternalIps) {
                        allocateExternalIp(dpnId, update, routerId, routerName, networkId, removedInternalIp, writeFlowInvTx);
                    }
                    LOG.debug("update : Remove the NAPT translation entries from " + "Inbound and Outbound NAPT tables for the removed external IPs.");
                    // Get the internalIP and internal Port which were associated to the removed external IP.
                    List<Integer> externalPorts = new ArrayList<>();
                    Map<ProtocolTypes, List<String>> protoTypesIntIpPortsMap = new HashMap<>();
                    InstanceIdentifier<IpPortMapping> ipPortMappingId = InstanceIdentifier.builder(IntextIpPortMap.class).child(IpPortMapping.class, new IpPortMappingKey(routerId)).build();
                    Optional<IpPortMapping> ipPortMapping = MDSALUtil.read(dataBroker, LogicalDatastoreType.CONFIGURATION, ipPortMappingId);
                    if (ipPortMapping.isPresent()) {
                        List<IntextIpProtocolType> intextIpProtocolTypes = ipPortMapping.get().getIntextIpProtocolType();
                        for (IntextIpProtocolType intextIpProtocolType : intextIpProtocolTypes) {
                            ProtocolTypes protoType = intextIpProtocolType.getProtocol();
                            List<IpPortMap> ipPortMaps = intextIpProtocolType.getIpPortMap();
                            for (IpPortMap ipPortMap : ipPortMaps) {
                                IpPortExternal ipPortExternal = ipPortMap.getIpPortExternal();
                                if (ipPortExternal.getIpAddress().equals(externalIp)) {
                                    externalPorts.add(ipPortExternal.getPortNum());
                                    List<String> removedInternalIpPorts = protoTypesIntIpPortsMap.get(protoType);
                                    if (removedInternalIpPorts != null) {
                                        removedInternalIpPorts.add(ipPortMap.getIpPortInternal());
                                        protoTypesIntIpPortsMap.put(protoType, removedInternalIpPorts);
                                    } else {
                                        removedInternalIpPorts = new ArrayList<>();
                                        removedInternalIpPorts.add(ipPortMap.getIpPortInternal());
                                        protoTypesIntIpPortsMap.put(protoType, removedInternalIpPorts);
                                    }
                                }
                            }
                        }
                    }
                    // Remove the IP port map from the intext-ip-port-map model, which were containing
                    // the removed external IP.
                    Set<Map.Entry<ProtocolTypes, List<String>>> protoTypesIntIpPorts = protoTypesIntIpPortsMap.entrySet();
                    Map<String, List<String>> internalIpPortMap = new HashMap<>();
                    for (Map.Entry protoTypesIntIpPort : protoTypesIntIpPorts) {
                        ProtocolTypes protocolType = (ProtocolTypes) protoTypesIntIpPort.getKey();
                        List<String> removedInternalIpPorts = (List<String>) protoTypesIntIpPort.getValue();
                        for (String removedInternalIpPort : removedInternalIpPorts) {
                            // Remove the IP port map from the intext-ip-port-map model,
                            // which were containing the removed external IP
                            naptManager.removeFromIpPortMapDS(routerId, removedInternalIpPort, protocolType);
                            // Remove the IP port incomint packer map.
                            naptPacketInHandler.removeIncomingPacketMap(routerId + NatConstants.COLON_SEPARATOR + removedInternalIpPort);
                            String[] removedInternalIpPortParts = removedInternalIpPort.split(NatConstants.COLON_SEPARATOR);
                            if (removedInternalIpPortParts.length == 2) {
                                String removedInternalIp = removedInternalIpPortParts[0];
                                String removedInternalPort = removedInternalIpPortParts[1];
                                List<String> removedInternalPortsList = internalIpPortMap.get(removedInternalPort);
                                if (removedInternalPortsList != null) {
                                    removedInternalPortsList.add(removedInternalPort);
                                    internalIpPortMap.put(removedInternalIp, removedInternalPortsList);
                                } else {
                                    removedInternalPortsList = new ArrayList<>();
                                    removedInternalPortsList.add(removedInternalPort);
                                    internalIpPortMap.put(removedInternalIp, removedInternalPortsList);
                                }
                            }
                        }
                    }
                    // Delete the entry from SnatIntIpPortMap DS
                    Set<String> internalIps = internalIpPortMap.keySet();
                    for (String internalIp : internalIps) {
                        LOG.debug("update : Removing IpPort having the internal IP {} from the " + "model SnatIntIpPortMap", internalIp);
                        naptManager.removeFromSnatIpPortDS(routerId, internalIp);
                    }
                    naptManager.removeNaptPortPool(externalIp);
                    LOG.debug("update : Remove the NAPT translation entries from Inbound NAPT tables for the " + "removed external IP {}", externalIp);
                    for (Integer externalPort : externalPorts) {
                        // Remove the NAPT translation entries from Inbound NAPT table
                        naptEventHandler.removeNatFlows(dpnId, NwConstants.INBOUND_NAPT_TABLE, routerId, externalIp, externalPort);
                    }
                    Set<Map.Entry<String, List<String>>> internalIpPorts = internalIpPortMap.entrySet();
                    for (Map.Entry<String, List<String>> internalIpPort : internalIpPorts) {
                        String internalIp = internalIpPort.getKey();
                        LOG.debug("update : Remove the NAPT translation entries from Outbound NAPT tables for " + "the removed internal IP {}", internalIp);
                        List<String> internalPorts = internalIpPort.getValue();
                        for (String internalPort : internalPorts) {
                            // Remove the NAPT translation entries from Outbound NAPT table
                            naptPacketInHandler.removeIncomingPacketMap(routerId + NatConstants.COLON_SEPARATOR + internalIp + NatConstants.COLON_SEPARATOR + internalPort);
                            naptEventHandler.removeNatFlows(dpnId, NwConstants.OUTBOUND_NAPT_TABLE, routerId, internalIp, Integer.parseInt(internalPort));
                        }
                    }
                }
                LOG.debug("update : End processing of the External IPs removal during the update operation");
            }
            // Check if its Update on subnets
            LOG.debug("update : Checking if this is update on subnets");
            List<Uuid> originalSubnetIds = original.getSubnetIds();
            List<Uuid> updatedSubnetIds = update.getSubnetIds();
            Set<Uuid> addedSubnetIds = new HashSet<>(updatedSubnetIds);
            addedSubnetIds.removeAll(originalSubnetIds);
            // Check if the Subnet IDs are added during the update.
            if (addedSubnetIds.size() != 0) {
                LOG.debug("update : Start processing of the Subnet IDs addition during the update operation");
                for (Uuid addedSubnetId : addedSubnetIds) {
                    /*
                        1) Select the least loaded external IP for the subnet and store the mapping of the
                        subnet IP and the external IP in the IntExtIp model.
                        2) Increase the count of the selected external IP by one.
                        3) Advertise to the BGP if external IP is allocated for the first time for the
                        router i.e. the route for the external IP is absent.
                     */
                    String subnetIp = NatUtil.getSubnetIp(dataBroker, addedSubnetId);
                    if (subnetIp != null) {
                        allocateExternalIp(dpnId, update, routerId, routerName, networkId, subnetIp, writeFlowInvTx);
                    }
                }
                LOG.debug("update : End processing of the Subnet IDs addition during the update operation");
            }
            // Check if the Subnet IDs are removed during the update.
            Set<Uuid> removedSubnetIds = new HashSet<>(originalSubnetIds);
            removedSubnetIds.removeAll(updatedSubnetIds);
            List<ListenableFuture<Void>> futures = new ArrayList<>();
            if (removedSubnetIds.size() != 0) {
                LOG.debug("update : Start processing of the Subnet IDs removal during the update operation");
                for (Uuid removedSubnetId : removedSubnetIds) {
                    String[] subnetAddr = NatUtil.getSubnetIpAndPrefix(dataBroker, removedSubnetId);
                    if (subnetAddr != null) {
                        /*
                            1) Remove the subnet IP and the external IP in the IntExtIp map
                            2) Decrease the count of the coresponding external IP by one.
                            3) Advertise to the BGP for removing the routes of the corresponding external
                            IP if its not allocated to any other internal IP.
                        */
                        String externalIp = naptManager.getExternalIpAllocatedForSubnet(routerId, subnetAddr[0] + "/" + subnetAddr[1]);
                        if (externalIp == null) {
                            LOG.error("update : No mapping found for router ID {} and internal IP {}", routerId, subnetAddr[0]);
                            futures.add(NatUtil.waitForTransactionToComplete(writeFlowInvTx));
                            futures.add(NatUtil.waitForTransactionToComplete(removeFlowInvTx));
                            return futures;
                        }
                        naptManager.updateCounter(routerId, externalIp, false);
                        // used by any other internal ip in any router
                        if (!isExternalIpAllocated(externalIp)) {
                            LOG.debug("update : external ip is not allocated to any other " + "internal IP so proceeding to remove routes");
                            clrRtsFromBgpAndDelFibTs(dpnId, routerId, networkId, Collections.singleton(externalIp), null, update.getExtGwMacAddress(), removeFlowInvTx);
                            LOG.debug("update : Successfully removed fib entries in switch {} for " + "router {} with networkId {} and externalIp {}", dpnId, routerId, networkId, externalIp);
                        }
                        LOG.debug("update : Remove the IP mapping for the router ID {} and " + "internal IP {} external IP {}", routerId, subnetAddr[0], externalIp);
                        naptManager.removeIntExtIpMapDS(routerId, subnetAddr[0] + "/" + subnetAddr[1]);
                    }
                }
                LOG.debug("update : End processing of the Subnet IDs removal during the update operation");
            }
            futures.add(NatUtil.waitForTransactionToComplete(writeFlowInvTx));
            futures.add(NatUtil.waitForTransactionToComplete(removeFlowInvTx));
            return futures;
        }, NatConstants.NAT_DJC_MAX_RETRIES);
    }
// end of controller based SNAT
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IpPortMapping(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.IpPortMapping) ProtocolTypes(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProtocolTypes) ArrayList(java.util.ArrayList) List(java.util.List) IpPortExternal(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.ip.port.map.IpPortExternal) HashSet(java.util.HashSet) WriteTransaction(org.opendaylight.controller.md.sal.binding.api.WriteTransaction) IpPortMappingKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.IpPortMappingKey) IpMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMap) ExternalIps(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps) BigInteger(java.math.BigInteger) Uuid(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid) IntextIpPortMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpPortMap) IpPortMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.IpPortMap) IntextIpProtocolType(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.IntextIpProtocolType) BigInteger(java.math.BigInteger) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) IpMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMap) Map(java.util.Map) HashMap(java.util.HashMap) IntextIpPortMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpPortMap) IpPortMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.IpPortMap)

Example 4 with NatMode

use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig.NatMode in project netvirt by opendaylight.

the class RouterDpnChangeListener method add.

@Override
protected void add(final InstanceIdentifier<DpnVpninterfacesList> identifier, final DpnVpninterfacesList dpnInfo) {
    LOG.trace("add : key: {}, value: {}", dpnInfo.getKey(), dpnInfo);
    final String routerUuid = identifier.firstKeyOf(RouterDpnList.class).getRouterId();
    BigInteger dpnId = dpnInfo.getDpnId();
    // check router is associated to external network
    InstanceIdentifier<Routers> id = NatUtil.buildRouterIdentifier(routerUuid);
    Optional<Routers> routerData = SingleTransactionDataBroker.syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(dataBroker, LogicalDatastoreType.CONFIGURATION, id);
    if (routerData.isPresent()) {
        Routers router = routerData.get();
        Uuid networkId = router.getNetworkId();
        if (networkId != null) {
            if (natMode == NatMode.Conntrack) {
                BigInteger naptSwitch = NatUtil.getPrimaryNaptfromRouterName(dataBroker, router.getRouterName());
                if (naptSwitch == null || naptSwitch.equals(BigInteger.ZERO)) {
                    LOG.warn("add : NAPT switch is not selected.");
                    return;
                }
                // If it is for NAPT switch skip as the flows would be already programmed.
                if (naptSwitch.equals(dpnId)) {
                    LOG.debug("Skipping the notification recived for NAPT switch {}", routerUuid);
                    return;
                }
                natServiceManager.notify(router, naptSwitch, dpnId, SnatServiceManager.Action.SNAT_ROUTER_ENBL);
            } else {
                coordinator.enqueueJob(NatConstants.NAT_DJC_PREFIX + dpnInfo.getKey(), () -> {
                    WriteTransaction writeFlowInvTx = dataBroker.newWriteOnlyTransaction();
                    WriteTransaction removeFlowInvTx = dataBroker.newWriteOnlyTransaction();
                    LOG.debug("add : Router {} is associated with ext nw {}", routerUuid, networkId);
                    Uuid vpnName = NatUtil.getVpnForRouter(dataBroker, routerUuid);
                    Long routerId = NatUtil.getVpnId(dataBroker, routerUuid);
                    List<ListenableFuture<Void>> futures = new ArrayList<>();
                    if (routerId == NatConstants.INVALID_ID) {
                        LOG.error("add : Invalid routerId returned for routerName {}", routerUuid);
                        writeFlowInvTx.cancel();
                        removeFlowInvTx.cancel();
                        return futures;
                    }
                    extNetGroupInstaller.installExtNetGroupEntries(networkId, dpnId);
                    Long vpnId;
                    if (vpnName == null) {
                        LOG.debug("add : Internal vpn associated to router {}", routerUuid);
                        vpnId = routerId;
                        if (vpnId == NatConstants.INVALID_ID) {
                            LOG.error("add : Invalid vpnId returned for routerName {}", routerUuid);
                            writeFlowInvTx.cancel();
                            removeFlowInvTx.cancel();
                            return futures;
                        }
                        LOG.debug("add : Retrieved vpnId {} for router {}", vpnId, routerUuid);
                        // Install default entry in FIB to SNAT table
                        LOG.info("add : Installing default route in FIB on dpn {} for router {} with vpn {}", dpnId, routerUuid, vpnId);
                        installDefaultNatRouteForRouterExternalSubnets(dpnId, NatUtil.getExternalSubnetIdsFromExternalIps(router.getExternalIps()));
                        snatDefaultRouteProgrammer.installDefNATRouteInDPN(dpnId, vpnId, writeFlowInvTx);
                    } else {
                        LOG.debug("add : External BGP vpn associated to router {}", routerUuid);
                        vpnId = NatUtil.getVpnId(dataBroker, vpnName.getValue());
                        if (vpnId == NatConstants.INVALID_ID) {
                            LOG.error("add : Invalid vpnId returned for routerName {}", routerUuid);
                            writeFlowInvTx.cancel();
                            removeFlowInvTx.cancel();
                            return futures;
                        }
                        LOG.debug("add : Retrieved vpnId {} for router {}", vpnId, routerUuid);
                        // Install default entry in FIB to SNAT table
                        LOG.debug("add : Installing default route in FIB on dpn {} for routerId {} with " + "vpnId {}...", dpnId, routerUuid, vpnId);
                        installDefaultNatRouteForRouterExternalSubnets(dpnId, NatUtil.getExternalSubnetIdsFromExternalIps(router.getExternalIps()));
                        snatDefaultRouteProgrammer.installDefNATRouteInDPN(dpnId, vpnId, routerId, writeFlowInvTx);
                    }
                    if (router.isEnableSnat()) {
                        LOG.info("add : SNAT enabled for router {}", routerUuid);
                        ProviderTypes extNwProvType = NatEvpnUtil.getExtNwProvTypeFromRouterName(dataBroker, routerUuid, networkId);
                        if (extNwProvType == null) {
                            LOG.error("add : External Network Provider Type missing");
                            writeFlowInvTx.cancel();
                            removeFlowInvTx.cancel();
                            return futures;
                        }
                        handleSNATForDPN(dpnId, routerUuid, routerId, vpnId, writeFlowInvTx, removeFlowInvTx, extNwProvType);
                    } else {
                        LOG.info("add : SNAT is not enabled for router {} to handle addDPN event {}", routerUuid, dpnId);
                    }
                    futures.add(NatUtil.waitForTransactionToComplete(writeFlowInvTx));
                    futures.add(NatUtil.waitForTransactionToComplete(removeFlowInvTx));
                    return futures;
                }, NatConstants.NAT_DJC_MAX_RETRIES);
            }
        // end of controller based SNAT
        }
    } else {
        LOG.debug("add : Router {} is not associated with External network", routerUuid);
    }
}
Also used : WriteTransaction(org.opendaylight.controller.md.sal.binding.api.WriteTransaction) Routers(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers) ProviderTypes(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProviderTypes) ArrayList(java.util.ArrayList) Uuid(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid) BigInteger(java.math.BigInteger) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) RouterDpnList(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.RouterDpnList)

Example 5 with NatMode

use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.config.rev170206.NatserviceConfig.NatMode in project netvirt by opendaylight.

the class NaptSwitchHA method isNaptSwitchDown.

// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
public boolean isNaptSwitchDown(String routerName, Long routerId, BigInteger dpnId, BigInteger naptSwitch, Long routerVpnId, Collection<String> externalIpCache, boolean isClearBgpRts, WriteTransaction writeFlowInvTx) {
    externalIpsCache = externalIpCache;
    if (!naptSwitch.equals(dpnId)) {
        LOG.debug("isNaptSwitchDown : DpnId {} is not a naptSwitch {} for Router {}", dpnId, naptSwitch, routerName);
        return false;
    }
    LOG.debug("NaptSwitch {} is down for Router {}", naptSwitch, routerName);
    if (routerId == NatConstants.INVALID_ID) {
        LOG.error("isNaptSwitchDown : Invalid routerId returned for routerName {}", routerName);
        return true;
    }
    Uuid networkId = NatUtil.getNetworkIdFromRouterName(dataBroker, routerName);
    String vpnName = getExtNetworkVpnName(routerName, networkId);
    // elect a new NaptSwitch
    naptSwitch = naptSwitchSelector.selectNewNAPTSwitch(routerName);
    if (natMode == NatMode.Conntrack) {
        Routers extRouters = NatUtil.getRoutersFromConfigDS(dataBroker, routerName);
        natServiceManager.notify(extRouters, dpnId, dpnId, SnatServiceManager.Action.SNAT_ALL_SWITCH_DISBL);
        natServiceManager.notify(extRouters, naptSwitch, naptSwitch, SnatServiceManager.Action.SNAT_ALL_SWITCH_ENBL);
    } else {
        if (naptSwitch.equals(BigInteger.ZERO)) {
            LOG.warn("isNaptSwitchDown : No napt switch is elected since all the switches for router {}" + " are down. SNAT IS NOT SUPPORTED FOR ROUTER {}", routerName, routerName);
            boolean naptUpdatedStatus = updateNaptSwitch(routerName, naptSwitch);
            if (!naptUpdatedStatus) {
                LOG.debug("isNaptSwitchDown : Failed to update naptSwitch {} for router {} in ds", naptSwitch, routerName);
            }
            // clearBgpRoutes
            if (externalIpsCache != null) {
                if (vpnName != null) {
                    // if (externalIps != null) {
                    if (isClearBgpRts) {
                        LOG.debug("isNaptSwitchDown : Clearing both FIB entries and the BGP routes");
                        for (String externalIp : externalIpsCache) {
                            externalRouterListener.clearBgpRoutes(externalIp, vpnName);
                        }
                    } else {
                        LOG.debug("isNaptSwitchDown : Clearing the FIB entries but not the BGP routes");
                        String rd = NatUtil.getVpnRd(dataBroker, vpnName);
                        for (String externalIp : externalIpsCache) {
                            LOG.debug("isNaptSwitchDown : Removing Fib entry rd {} prefix {}", rd, externalIp);
                            fibManager.removeFibEntry(rd, externalIp, null);
                        }
                    }
                } else {
                    LOG.debug("isNaptSwitchDown : vpn is not associated to extn/w for router {}", routerName);
                }
            } else {
                LOG.debug("isNaptSwitchDown : No ExternalIps found for subnets under router {}, " + "no bgp routes need to be cleared", routerName);
            }
            return true;
        }
        // checking elected switch health status
        if (!getSwitchStatus(naptSwitch)) {
            LOG.error("isNaptSwitchDown : Newly elected Napt switch {} for router {} is down", naptSwitch, routerName);
            return true;
        }
        LOG.debug("isNaptSwitchDown : New NaptSwitch {} is up for Router {} and can proceed for flow installation", naptSwitch, routerName);
        // update napt model for new napt switch
        boolean naptUpdated = updateNaptSwitch(routerName, naptSwitch);
        if (naptUpdated) {
            // update group of ordinary switch point to naptSwitch tunnel port
            updateNaptSwitchBucketStatus(routerName, routerId, naptSwitch);
        } else {
            LOG.error("isNaptSwitchDown : Failed to update naptSwitch model for newNaptSwitch {} for router {}", naptSwitch, routerName);
        }
        // update table26 forward packets to table46(outbound napt table)
        FlowEntity flowEntity = buildSnatFlowEntityForNaptSwitch(naptSwitch, routerName, routerVpnId, NatConstants.ADD_FLOW);
        if (flowEntity == null) {
            LOG.error("isNaptSwitchDown : Failed to populate flowentity for router {} in naptSwitch {}", routerName, naptSwitch);
        } else {
            LOG.debug("isNaptSwitchDown : Successfully installed flow in naptSwitch {} for router {}", naptSwitch, routerName);
            mdsalManager.addFlowToTx(flowEntity, writeFlowInvTx);
        }
        installSnatFlows(routerName, routerId, naptSwitch, routerVpnId, writeFlowInvTx);
        boolean flowInstalledStatus = handleNatFlowsInNewNaptSwitch(routerName, routerId, dpnId, naptSwitch, routerVpnId, networkId);
        if (flowInstalledStatus) {
            LOG.debug("isNaptSwitchDown :Installed all active session flows in newNaptSwitch {} for routerName {}", naptSwitch, routerName);
        } else {
            LOG.error("isNaptSwitchDown : Failed to install flows in newNaptSwitch {} for routerId {}", naptSwitch, routerId);
        }
        // remove group in new naptswitch, coz this switch acted previously as ordinary switch
        long groupId = NatUtil.createGroupId(NatUtil.getGroupIdKey(routerName), idManager);
        GroupEntity groupEntity = null;
        try {
            groupEntity = MDSALUtil.buildGroupEntity(naptSwitch, groupId, routerName, GroupTypes.GroupAll, Collections.emptyList());
            LOG.info("isNaptSwitchDown : Removing NAPT Group in new naptSwitch {}", naptSwitch);
            mdsalManager.removeGroup(groupEntity);
        } catch (Exception ex) {
            LOG.error("isNaptSwitchDown : Failed to remove group in new naptSwitch {}", groupEntity, ex);
        }
    }
    return true;
}
Also used : Uuid(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid) Routers(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers) GroupEntity(org.opendaylight.genius.mdsalutil.GroupEntity) ExecutionException(java.util.concurrent.ExecutionException) FlowEntity(org.opendaylight.genius.mdsalutil.FlowEntity)

Aggregations

Uuid (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid)9 BigInteger (java.math.BigInteger)7 Routers (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.Routers)6 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)4 ArrayList (java.util.ArrayList)4 WriteTransaction (org.opendaylight.controller.md.sal.binding.api.WriteTransaction)4 ExternalIps (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ext.routers.routers.ExternalIps)3 ExecutionException (java.util.concurrent.ExecutionException)2 RouterDpnList (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.neutron.router.dpns.RouterDpnList)2 IpMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMap)2 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 ReadFailedException (org.opendaylight.controller.md.sal.common.api.data.ReadFailedException)1 SingleTransactionDataBroker (org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker)1 FlowEntity (org.opendaylight.genius.mdsalutil.FlowEntity)1 GroupEntity (org.opendaylight.genius.mdsalutil.GroupEntity)1 ExtRouters (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ExtRouters)1