Search in sources :

Example 1 with Uint16

use of org.opendaylight.yangtools.yang.common.Uint16 in project netvirt by opendaylight.

the class NaptManager method getExternalAddressMapping.

/**
 * method to get external ip/port mapping when provided with internal ip/port pair
 * If already a mapping exist for the given input, then the existing mapping is returned
 * instead of overwriting with new ip/port pair.
 *
 * @param segmentId     - Router ID
 * @param sourceAddress - internal ip address/port pair
 * @param protocol      - TCP/UDP
 * @return external ip address/port
 */
// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
@Nullable
public SessionAddress getExternalAddressMapping(Uint32 segmentId, SessionAddress sourceAddress, NAPTEntryEvent.Protocol protocol) {
    LOG.debug("getExternalAddressMapping : called with segmentId {}, internalIp {} and port {}", segmentId, sourceAddress.getIpAddress(), sourceAddress.getPortNumber());
    /*
         1. Get Internal IP, Port in IP:Port format
         2. Inside DB with routerId get the list of entries and check if it matches with existing IP:Port
         3. If True return SessionAddress of ExternalIp and Port
         4. Else check ip Map and Form the ExternalIp and Port and update DB and then return ExternalIp and Port
         */
    // SessionAddress externalIpPort = new SessionAddress();
    String internalIpPort = sourceAddress.getIpAddress() + ":" + sourceAddress.getPortNumber();
    // First check existing Port Map.
    SessionAddress existingIpPort = checkIpPortMap(segmentId, internalIpPort, protocol);
    if (existingIpPort != null) {
        // populate externalIpPort from IpPortMap and return
        LOG.debug("getExternalAddressMapping : successfully returning existingIpPort as {} and {}", existingIpPort.getIpAddress(), existingIpPort.getPortNumber());
        return existingIpPort;
    }
    // Now check in ip-map
    String externalIp = checkIpMap(segmentId, sourceAddress.getIpAddress());
    if (externalIp == null) {
        LOG.error("getExternalAddressMapping : Unexpected error, internal to external " + "ip map does not exist");
        return null;
    }
    /* Logic assuming internalIp is always ip and not subnet
         * case 1: externalIp is ip
         *        a) goto externalIp pool and getPort and return
         *        b) else return error
         * case 2: externalIp is subnet
         *        a) Take first externalIp and goto that Pool and getPort
         *             if port -> return
         *             else Take second externalIp and create that Pool and getPort
         *             if port ->return
         *             else
         *             Continue same with third externalIp till we exhaust subnet
         *        b) Nothing worked return error
         */
    SubnetUtils externalIpSubnet;
    List<String> allIps = new ArrayList<>();
    String subnetPrefix = "/" + String.valueOf(NatConstants.DEFAULT_PREFIX);
    boolean extSubnetFlag = false;
    if (!externalIp.contains(subnetPrefix)) {
        extSubnetFlag = true;
        externalIpSubnet = new SubnetUtils(externalIp);
        allIps = Arrays.asList(externalIpSubnet.getInfo().getAllAddresses());
        LOG.debug("getExternalAddressMapping : total count of externalIps available {}", externalIpSubnet.getInfo().getAddressCount());
    } else {
        LOG.debug("getExternalAddressMapping : getExternalAddress single ip case");
        if (externalIp.contains(subnetPrefix)) {
            // remove /32 what we got from checkIpMap
            externalIp = externalIp.substring(0, externalIp.indexOf(subnetPrefix));
        }
        allIps.add(externalIp);
    }
    boolean nextExtIpFlag = false;
    for (String extIp : allIps) {
        LOG.info("getExternalAddressMapping : Looping externalIPs with externalIP now as {}", extIp);
        if (nextExtIpFlag) {
            createNaptPortPool(extIp);
            LOG.debug("getExternalAddressMapping : Created Pool for next Ext IP {}", extIp);
        }
        Uint32 extPort = NatUtil.getUniqueId(idManager, extIp, internalIpPort);
        if (extPort == NatConstants.INVALID_ID) {
            LOG.error("getExternalAddressMapping : getExternalAddressMapping, idManager could not " + "allocate id retry if subnet");
            if (!extSubnetFlag) {
                LOG.error("getExternalAddressMapping : getExternalAddressMapping returning null " + "for single IP case, may be ports exhausted");
                return null;
            }
            LOG.debug("getExternalAddressMapping : Could be ports exhausted case, " + "try with another externalIP if possible");
            nextExtIpFlag = true;
            continue;
        }
        // Write to ip-port-map before returning
        IpPortExternalBuilder ipExt = new IpPortExternalBuilder();
        IpPortExternal ipPortExt = ipExt.setIpAddress(extIp).setPortNum(extPort.intValue()).build();
        IpPortMap ipm = new IpPortMapBuilder().withKey(new IpPortMapKey(internalIpPort)).setIpPortInternal(internalIpPort).setIpPortExternal(ipPortExt).build();
        LOG.debug("getExternalAddressMapping : writing into ip-port-map with " + "externalIP {} and port {}", ipPortExt.getIpAddress(), ipPortExt.getPortNum());
        try {
            MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, getIpPortMapIdentifier(segmentId, internalIpPort, protocol), ipm);
        } catch (UncheckedExecutionException uee) {
            LOG.error("getExternalAddressMapping : Failed to write into ip-port-map with exception", uee);
        }
        // Write to snat-internal-ip-port-info
        String internalIpAddress = sourceAddress.getIpAddress();
        int ipPort = sourceAddress.getPortNumber();
        ProtocolTypes protocolType = NatUtil.getProtocolType(protocol);
        final ReentrantLock lock = lockFor(segmentId, internalIpAddress, protocolType);
        lock.lock();
        try {
            List<Uint16> portList = new ArrayList<>(NatUtil.getInternalIpPortListInfo(dataBroker, segmentId, internalIpAddress, protocolType));
            portList.add(Uint16.valueOf(ipPort));
            IntIpProtoTypeBuilder builder = new IntIpProtoTypeBuilder();
            IntIpProtoType intIpProtocolType = builder.withKey(new IntIpProtoTypeKey(protocolType)).setPorts(portList).build();
            try {
                MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, NatUtil.buildSnatIntIpPortIdentifier(segmentId, internalIpAddress, protocolType), intIpProtocolType);
            } catch (Exception ex) {
                LOG.error("getExternalAddressMapping : Failed to write into snat-internal-ip-port-info " + "with exception", ex);
            }
        } finally {
            lock.unlock();
        }
        SessionAddress externalIpPort = new SessionAddress(extIp, extPort.intValue());
        LOG.debug("getExternalAddressMapping : successfully returning externalIP {} " + "and port {}", externalIpPort.getIpAddress(), externalIpPort.getPortNumber());
        return externalIpPort;
    }
    // end of for loop
    LOG.error("getExternalAddressMapping : Unable to handle external IP address and port mapping with segmentId {}," + "internalIp {} and internalPort {}", segmentId, sourceAddress.getIpAddress(), sourceAddress.getPortNumber());
    return null;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) IntIpProtoTypeKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoTypeKey) SubnetUtils(org.apache.commons.net.util.SubnetUtils) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ArrayList(java.util.ArrayList) IntIpProtoTypeBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoTypeBuilder) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) IpPortExternalBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.ip.port.map.IpPortExternalBuilder) IpPortMapBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.IpPortMapBuilder) SnatintIpPortMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.SnatintIpPortMap) 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) IpPortMapKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.IpPortMapKey) ProtocolTypes(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProtocolTypes) Uint16(org.opendaylight.yangtools.yang.common.Uint16) IntIpProtoType(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoType) 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) Uint32(org.opendaylight.yangtools.yang.common.Uint32) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with Uint16

use of org.opendaylight.yangtools.yang.common.Uint16 in project netvirt by opendaylight.

the class NatUtil method removeSnatEntriesForPort.

public static void removeSnatEntriesForPort(DataBroker dataBroker, NaptManager naptManager, IMdsalApiManager mdsalManager, NeutronvpnService neutronVpnService, String interfaceName, String routerName) {
    Uint32 routerId = NatUtil.getVpnId(dataBroker, routerName);
    if (routerId == NatConstants.INVALID_ID) {
        LOG.error("removeSnatEntriesForPort: routerId not found for routername {}", routerName);
        return;
    }
    Uint64 naptSwitch = getPrimaryNaptfromRouterName(dataBroker, routerName);
    if (naptSwitch == null || naptSwitch.equals(Uint64.ZERO)) {
        LOG.error("removeSnatEntriesForPort: NaptSwitch is not elected for router {}" + "with Id {}", routerName, routerId);
        return;
    }
    // getInternalIp for port
    List<String> fixedIps = getFixedIpsForPort(neutronVpnService, interfaceName);
    if (fixedIps == null) {
        LOG.error("removeSnatEntriesForPort: Internal Ips not found for InterfaceName {} in router {} with id {}", interfaceName, routerName, routerId);
        return;
    }
    List<ProtocolTypes> protocolTypesList = getPortocolList();
    for (String internalIp : fixedIps) {
        LOG.debug("removeSnatEntriesForPort: Internal Ip retrieved for interface {} is {} in router with Id {}", interfaceName, internalIp, routerId);
        for (ProtocolTypes protocol : protocolTypesList) {
            List<Uint16> portList = NatUtil.getInternalIpPortListInfo(dataBroker, routerId, internalIp, protocol);
            if (portList != null) {
                for (Uint16 portnum : portList) {
                    // build and remove the flow in outbound table
                    removeNatFlow(mdsalManager, naptSwitch, NwConstants.OUTBOUND_NAPT_TABLE, routerId, internalIp, portnum.toJava(), protocol.getName());
                    // build and remove the flow in inboundtable
                    removeNatFlow(mdsalManager, naptSwitch, NwConstants.INBOUND_NAPT_TABLE, routerId, internalIp, portnum.toJava(), protocol.getName());
                    // Get the external IP address and the port from the model
                    NAPTEntryEvent.Protocol proto = protocol.toString().equals(ProtocolTypes.TCP.toString()) ? NAPTEntryEvent.Protocol.TCP : NAPTEntryEvent.Protocol.UDP;
                    IpPortExternal ipPortExternal = NatUtil.getExternalIpPortMap(dataBroker, routerId, internalIp, String.valueOf(portnum.toJava()), proto);
                    if (ipPortExternal == null) {
                        LOG.error("removeSnatEntriesForPort: Mapping for internalIp {} " + "with port {} is not found in " + "router with Id {}", internalIp, portnum, routerId);
                        return;
                    }
                    String externalIpAddress = ipPortExternal.getIpAddress();
                    String internalIpPort = internalIp + ":" + portnum.toJava();
                    // delete the entry from IntExtIpPortMap DS
                    naptManager.removeFromIpPortMapDS(routerId, internalIpPort, proto);
                    naptManager.removePortFromPool(internalIpPort, externalIpAddress);
                }
            } else {
                LOG.debug("removeSnatEntriesForPort: No {} session for interface {} with internalIP {} " + "in router with id {}", protocol, interfaceName, internalIp, routerId);
            }
        }
        // delete the entry from SnatIntIpPortMap DS
        LOG.debug("removeSnatEntriesForPort: Removing InternalIp :{} of router {} from snatint-ip-port-map", internalIp, routerId);
        naptManager.removeFromSnatIpPortDS(routerId, internalIp);
    }
}
Also used : ProtocolTypes(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProtocolTypes) Uint16(org.opendaylight.yangtools.yang.common.Uint16) 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) Uint32(org.opendaylight.yangtools.yang.common.Uint32) Uint64(org.opendaylight.yangtools.yang.common.Uint64)

Example 3 with Uint16

use of org.opendaylight.yangtools.yang.common.Uint16 in project netvirt by opendaylight.

the class NaptManager method removeSnatIntIpPortDS.

// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
protected void removeSnatIntIpPortDS(Uint32 segmentId, SessionAddress address, ProtocolTypes protocolType) {
    LOG.trace("removeSnatIntIpPortDS : method called for IntIpport {} of router {} ", address, segmentId);
    List<Uint16> portList = NatUtil.getInternalIpPortListInfo(dataBroker, segmentId, address.getIpAddress(), protocolType);
    if (portList.isEmpty() || !portList.contains(Uint16.valueOf(address.getPortNumber()))) {
        LOG.error("removeSnatIntIpPortDS : Internal IP {} for port {} entry not found in SnatIntIpPort DS", address.getIpAddress(), address.getPortNumber());
        return;
    }
    LOG.trace("removeSnatIntIpPortDS : PortList {} retrieved for InternalIp {} of router {}", portList, address.getIpAddress(), segmentId);
    Integer port = address.getPortNumber();
    portList.remove(Uint16.valueOf(port));
    IntIpProtoTypeBuilder builder = new IntIpProtoTypeBuilder();
    IntIpProtoType intIpProtocolType = builder.withKey(new IntIpProtoTypeKey(protocolType)).setPorts(portList).build();
    try {
        MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, NatUtil.buildSnatIntIpPortIdentifier(segmentId, address.getIpAddress(), protocolType), intIpProtocolType);
    } catch (Exception ex) {
        LOG.error("removeSnatIntIpPortDS : Failed to write into snat-internal-ip-port-info with exception", ex);
    }
    LOG.debug("removeSnatIntIpPortDS : Removing SnatIp {} Port {} of router {} from SNATIntIpport datastore", address.getIpAddress(), address.getPortNumber(), segmentId);
}
Also used : IntIpProtoTypeKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoTypeKey) Uint16(org.opendaylight.yangtools.yang.common.Uint16) IntIpProtoTypeBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoTypeBuilder) IntIpProtoType(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoType) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Uint16 (org.opendaylight.yangtools.yang.common.Uint16)3 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)2 ExecutionException (java.util.concurrent.ExecutionException)2 ProtocolTypes (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProtocolTypes)2 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)2 IntIpProtoType (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoType)2 IntIpProtoTypeBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoTypeBuilder)2 IntIpProtoTypeKey (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoTypeKey)2 Uint32 (org.opendaylight.yangtools.yang.common.Uint32)2 ArrayList (java.util.ArrayList)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 SubnetUtils (org.apache.commons.net.util.SubnetUtils)1 Nullable (org.eclipse.jdt.annotation.Nullable)1 IntextIpPortMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpPortMap)1 SnatintIpPortMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.SnatintIpPortMap)1 IpPortMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.IpPortMap)1 IpPortMapBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.IpPortMapBuilder)1 IpPortMapKey (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.IpPortMapKey)1 IpPortExternalBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.ip.port.map.IpPortExternalBuilder)1 Uint64 (org.opendaylight.yangtools.yang.common.Uint64)1