Search in sources :

Example 1 with IntIpProtoType

use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoType in project netvirt by opendaylight.

the class InterfaceStateEventListener method removeSnatEntriesForPort.

// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
private void removeSnatEntriesForPort(String interfaceName, String routerName) {
    Long routerId = NatUtil.getVpnId(dataBroker, routerName);
    if (routerId == NatConstants.INVALID_ID) {
        LOG.error("removeSnatEntriesForPort : routerId not found for routername {}", routerName);
        return;
    }
    BigInteger naptSwitch = getNaptSwitchforRouter(dataBroker, routerName);
    if (naptSwitch == null || naptSwitch.equals(BigInteger.ZERO)) {
        LOG.error("removeSnatEntriesForPort : NaptSwitch is not elected for router {} with Id {}", routerName, routerId);
        return;
    }
    // getInternalIp for port
    List<String> fixedIps = getFixedIpsForPort(interfaceName);
    if (fixedIps == null) {
        LOG.warn("removeSnatEntriesForPort : Internal Ips not found for InterfaceName {} in router {} with id {}", interfaceName, routerName, routerId);
        return;
    }
    for (String internalIp : fixedIps) {
        LOG.debug("removeSnatEntriesForPort : Internal Ip retrieved for interface {} is {} in router with Id {}", interfaceName, internalIp, routerId);
        IpPort ipPort = NatUtil.getInternalIpPortInfo(dataBroker, routerId, internalIp);
        if (ipPort == null) {
            LOG.debug("removeSnatEntriesForPort : no snatint-ip-port-map found for ip:{}", internalIp);
            continue;
        }
        for (IntIpProtoType protoType : ipPort.getIntIpProtoType()) {
            ProtocolTypes protocol = protoType.getProtocol();
            for (Integer portnum : protoType.getPorts()) {
                // build and remove the flow in outbound table
                try {
                    removeNatFlow(naptSwitch, NwConstants.OUTBOUND_NAPT_TABLE, routerId, internalIp, portnum);
                } catch (Exception ex) {
                    LOG.error("removeSnatEntriesForPort : Failed to remove snat flow for internalIP {} with " + "Port {} protocol {} for routerId {} in OUTBOUNDTABLE of NaptSwitch {}", internalIp, portnum, protocol, routerId, naptSwitch, ex);
                }
                // 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), 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();
                Integer portNumber = ipPortExternal.getPortNum();
                // build and remove the flow in inboundtable
                try {
                    removeNatFlow(naptSwitch, NwConstants.INBOUND_NAPT_TABLE, routerId, externalIpAddress, portNumber);
                } catch (Exception ex) {
                    LOG.error("removeSnatEntriesForPort : Failed to remove snat flow internalIP {} with " + "Port {} protocol {} for routerId {} in INBOUNDTABLE of naptSwitch {}", externalIpAddress, portNumber, protocol, routerId, naptSwitch, ex);
                }
                String internalIpPort = internalIp + ":" + portnum;
                // delete the entry from IntExtIpPortMap DS
                try {
                    naptManager.removeFromIpPortMapDS(routerId, internalIpPort, proto);
                    naptManager.removePortFromPool(internalIpPort, externalIpAddress);
                } catch (Exception ex) {
                    LOG.error("removeSnatEntriesForPort : releaseIpExtPortMapping failed, Removal of " + "ipportmap {} for router {} failed", internalIpPort, routerId, ex);
                }
            }
        }
        // delete the entry from SnatIntIpPortMap DS
        LOG.debug("removeSnatEntriesForPort : Removing InternalIp:{} on router {}", internalIp, routerId);
        naptManager.removeFromSnatIpPortDS(routerId, internalIp);
    }
}
Also used : BigInteger(java.math.BigInteger) ProtocolTypes(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProtocolTypes) BigInteger(java.math.BigInteger) IntIpProtoType(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoType) IpPort(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.IpPort) 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) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with IntIpProtoType

use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoType in project netvirt by opendaylight.

the class NaptManager method removeSnatIntIpPortDS.

// TODO Clean up the exception handling
@SuppressWarnings("checkstyle:IllegalCatch")
protected void removeSnatIntIpPortDS(long segmentId, SessionAddress address, NAPTEntryEvent.Protocol protocol) {
    LOG.trace("removeSnatIntIpPortDS : method called for IntIpport {} of router {} ", address, segmentId);
    ProtocolTypes protocolType = NatUtil.getProtocolType(protocol);
    List<Integer> portList = NatUtil.getInternalIpPortListInfo(dataBroker, segmentId, address.getIpAddress(), protocolType);
    if (portList.isEmpty() || !portList.contains(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(port);
    IntIpProtoTypeBuilder builder = new IntIpProtoTypeBuilder();
    IntIpProtoType intIpProtocolType = builder.setKey(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) ProtocolTypes(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProtocolTypes) 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)

Example 3 with IntIpProtoType

use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoType 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")
public SessionAddress getExternalAddressMapping(long 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;
    } else {
        // 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;
        } else {
            /* 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);
                }
                AllocateIdInput getIdInput = new AllocateIdInputBuilder().setPoolName(extIp).setIdKey(internalIpPort).build();
                try {
                    Future<RpcResult<AllocateIdOutput>> result = idManager.allocateId(getIdInput);
                    RpcResult<AllocateIdOutput> rpcResult;
                    if (result != null && result.get().isSuccessful()) {
                        LOG.debug("getExternalAddressMapping : Got id from idManager");
                        rpcResult = result.get();
                    } else {
                        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;
                    }
                    int extPort = rpcResult.getResult().getIdValue().intValue();
                    // Write to ip-port-map before returning
                    IpPortExternalBuilder ipExt = new IpPortExternalBuilder();
                    IpPortExternal ipPortExt = ipExt.setIpAddress(extIp).setPortNum(extPort).build();
                    IpPortMap ipm = new IpPortMapBuilder().setKey(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);
                    List<Integer> portList = new ArrayList<>(NatUtil.getInternalIpPortListInfo(dataBroker, segmentId, internalIpAddress, protocolType));
                    portList.add(ipPort);
                    IntIpProtoTypeBuilder builder = new IntIpProtoTypeBuilder();
                    IntIpProtoType intIpProtocolType = builder.setKey(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);
                    }
                    SessionAddress externalIpPort = new SessionAddress(extIp, extPort);
                    LOG.debug("getExternalAddressMapping : successfully returning externalIP {} " + "and port {}", externalIpPort.getIpAddress(), externalIpPort.getPortNumber());
                    return externalIpPort;
                } catch (InterruptedException | ExecutionException e) {
                    LOG.error("getExternalAddressMapping : Exception caught", e);
                    return null;
                }
            }
        // end of for loop
        }
    // end of else ipmap present
    }
    // end of else check ipmap
    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 : 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) AllocateIdOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput) 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) ProtocolTypes(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProtocolTypes) 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) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) 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) AllocateIdInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) AllocateIdInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder) 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)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)3 ProtocolTypes (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProtocolTypes)3 IntIpProtoType (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.ip.port.IntIpProtoType)3 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)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 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 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 SubnetUtils (org.apache.commons.net.util.SubnetUtils)1 AllocateIdInput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput)1 AllocateIdInputBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder)1 AllocateIdOutput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput)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 IpPort (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.snatint.ip.port.map.intip.port.map.IpPort)1