Search in sources :

Example 21 with SegmentId

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.SegmentId in project netvirt by opendaylight.

the class NaptManager method removeFromIpMapDS.

protected void removeFromIpMapDS(long segmentId, String internalIp) {
    InstanceIdentifierBuilder<IpMap> idBuilder = InstanceIdentifier.builder(IntextIpMap.class).child(IpMapping.class, new IpMappingKey(segmentId)).child(IpMap.class, new IpMapKey(internalIp));
    InstanceIdentifier<IpMap> id = idBuilder.build();
    // Get externalIp and decrement the counter
    String externalIp = null;
    Optional<IpMap> ipMap = MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
    if (ipMap.isPresent()) {
        externalIp = ipMap.get().getExternalIp();
        LOG.debug("removeFromIpMapDS : externalIP is {}", externalIp);
    } else {
        LOG.warn("removeFromIpMapDS : ipMap not present for the internal IP {}", internalIp);
    }
    if (externalIp != null) {
        updateCounter(segmentId, externalIp, false);
        // remove from ipmap DS
        LOG.debug("removeFromIpMapDS : Removing ipmap from datastore");
        MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
    } else {
        LOG.warn("removeFromIpMapDS : externalIp not present for the internal IP {}", internalIp);
    }
}
Also used : IpMapKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapKey) IpMappingKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMappingKey) IpMapping(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping) 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)

Example 22 with SegmentId

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.SegmentId in project netvirt by opendaylight.

the class ExternalRoutersListener method subnetRegisterMapping.

protected void subnetRegisterMapping(Routers routerEntry, Long segmentId) {
    List<Uuid> subnetList = null;
    List<String> externalIps = null;
    LOG.debug("subnetRegisterMapping : Fetching values from extRouters model");
    subnetList = routerEntry.getSubnetIds();
    externalIps = NatUtil.getIpsListFromExternalIps(routerEntry.getExternalIps());
    int counter = 0;
    int extIpCounter = externalIps.size();
    LOG.debug("subnetRegisterMapping : counter values before looping counter {} and extIpCounter {}", counter, extIpCounter);
    for (Uuid subnet : subnetList) {
        LOG.debug("subnetRegisterMapping : Looping internal subnets for subnet {}", subnet);
        InstanceIdentifier<Subnetmap> subnetmapId = InstanceIdentifier.builder(Subnetmaps.class).child(Subnetmap.class, new SubnetmapKey(subnet)).build();
        Optional<Subnetmap> sn = read(dataBroker, LogicalDatastoreType.CONFIGURATION, subnetmapId);
        if (sn.isPresent()) {
            // subnets
            Subnetmap subnetmapEntry = sn.get();
            String subnetString = subnetmapEntry.getSubnetIp();
            String[] subnetSplit = subnetString.split("/");
            String subnetIp = subnetSplit[0];
            try {
                InetAddress address = InetAddress.getByName(subnetIp);
                if (address instanceof Inet6Address) {
                    LOG.debug("subnetRegisterMapping : Skipping ipv6 subnet {} for the router {} with ipv6 address " + "{} ", subnet, routerEntry.getRouterName(), address);
                    continue;
                }
            } catch (UnknownHostException e) {
                LOG.error("subnetRegisterMapping : Invalid ip address {}", subnetIp, e);
                return;
            }
            String subnetPrefix = "0";
            if (subnetSplit.length == 2) {
                subnetPrefix = subnetSplit[1];
            }
            IPAddress subnetAddr = new IPAddress(subnetIp, Integer.parseInt(subnetPrefix));
            LOG.debug("subnetRegisterMapping : subnetAddr is {} and subnetPrefix is {}", subnetAddr.getIpAddress(), subnetAddr.getPrefixLength());
            // externalIps
            LOG.debug("subnetRegisterMapping : counter values counter {} and extIpCounter {}", counter, extIpCounter);
            if (extIpCounter != 0) {
                if (counter < extIpCounter) {
                    String[] ipSplit = externalIps.get(counter).split("/");
                    String externalIp = ipSplit[0];
                    String extPrefix = Short.toString(NatConstants.DEFAULT_PREFIX);
                    if (ipSplit.length == 2) {
                        extPrefix = ipSplit[1];
                    }
                    IPAddress externalIpAddr = new IPAddress(externalIp, Integer.parseInt(extPrefix));
                    LOG.debug("subnetRegisterMapping : externalIp is {} and extPrefix  is {}", externalIpAddr.getIpAddress(), externalIpAddr.getPrefixLength());
                    naptManager.registerMapping(segmentId, subnetAddr, externalIpAddr);
                    LOG.debug("subnetRegisterMapping : Called registerMapping for subnetIp {}, prefix {}, " + "externalIp {}. prefix {}", subnetIp, subnetPrefix, externalIp, extPrefix);
                } else {
                    // Reset the counter which runs on externalIps for round-robbin effect
                    counter = 0;
                    LOG.debug("subnetRegisterMapping : Counter on externalIps got reset");
                    String[] ipSplit = externalIps.get(counter).split("/");
                    String externalIp = ipSplit[0];
                    String extPrefix = Short.toString(NatConstants.DEFAULT_PREFIX);
                    if (ipSplit.length == 2) {
                        extPrefix = ipSplit[1];
                    }
                    IPAddress externalIpAddr = new IPAddress(externalIp, Integer.parseInt(extPrefix));
                    LOG.debug("subnetRegisterMapping : externalIp is {} and extPrefix  is {}", externalIpAddr.getIpAddress(), externalIpAddr.getPrefixLength());
                    naptManager.registerMapping(segmentId, subnetAddr, externalIpAddr);
                    LOG.debug("subnetRegisterMapping : Called registerMapping for subnetIp {}, prefix {}, " + "externalIp {}. prefix {}", subnetIp, subnetPrefix, externalIp, extPrefix);
                }
            }
            counter++;
            LOG.debug("subnetRegisterMapping : Counter on externalIps incremented to {}", counter);
        } else {
            LOG.warn("subnetRegisterMapping : No internal subnets present in extRouters Model");
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) Subnetmap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap) Inet6Address(java.net.Inet6Address) Uuid(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid) SubnetmapKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.SubnetmapKey) InetAddress(java.net.InetAddress)

Example 23 with SegmentId

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.SegmentId in project netvirt by opendaylight.

the class NaptManager method registerMapping.

// 1. napt service functions
/**
 * This method is used to inform this service of what external IP address to be used
 * as mapping when requested one for the internal IP address given in the input.
 *
 * @param segmentId – segmentation in which the mapping to be used. Eg; routerid
 * @param internal  subnet prefix or ip address
 * @param external  subnet prefix or ip address
 */
public void registerMapping(long segmentId, IPAddress internal, IPAddress external) {
    LOG.debug("registerMapping : called with segmentid {}, internalIp {}, prefix {}, externalIp {} " + "and prefix {} ", segmentId, internal.getIpAddress(), internal.getPrefixLength(), external.getIpAddress(), external.getPrefixLength());
    // Create Pool per ExternalIp and not for all IPs in the subnet.
    // Create new Pools during getExternalAddressMapping if exhausted.
    String externalIpPool;
    // subnet case
    if (external.getPrefixLength() != 0 && external.getPrefixLength() != NatConstants.DEFAULT_PREFIX) {
        String externalSubnet = external.getIpAddress() + "/" + external.getPrefixLength();
        LOG.debug("registerMapping : externalSubnet is : {}", externalSubnet);
        SubnetUtils subnetUtils = new SubnetUtils(externalSubnet);
        SubnetInfo subnetInfo = subnetUtils.getInfo();
        externalIpPool = subnetInfo.getLowAddress();
    } else {
        // ip case
        externalIpPool = external.getIpAddress();
    }
    createNaptPortPool(externalIpPool);
    // Store the ip to ip map in Operational DS
    String internalIp = internal.getIpAddress();
    if (internal.getPrefixLength() != 0) {
        internalIp = internal.getIpAddress() + "/" + internal.getPrefixLength();
    }
    String externalIp = external.getIpAddress();
    if (external.getPrefixLength() != 0) {
        externalIp = external.getIpAddress() + "/" + external.getPrefixLength();
    }
    updateCounter(segmentId, externalIp, true);
    // update the actual ip-map
    IpMap ipm = new IpMapBuilder().setKey(new IpMapKey(internalIp)).setInternalIp(internalIp).setExternalIp(externalIp).build();
    MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL, getIpMapIdentifier(segmentId, internalIp), ipm);
    LOG.debug("registerMapping : registerMapping exit after updating DS with internalIP {}, externalIP {}", internalIp, externalIp);
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) IpMapKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapKey) SubnetInfo(org.apache.commons.net.util.SubnetUtils.SubnetInfo) 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) IpMapBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapBuilder)

Example 24 with SegmentId

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.SegmentId in project netvirt by opendaylight.

the class NaptManager method getExternalIpAllocatedForSubnet.

protected String getExternalIpAllocatedForSubnet(long segmentId, String internalIp) {
    InstanceIdentifierBuilder<IpMap> idBuilder = InstanceIdentifier.builder(IntextIpMap.class).child(IpMapping.class, new IpMappingKey(segmentId)).child(IpMap.class, new IpMapKey(internalIp));
    InstanceIdentifier<IpMap> id = idBuilder.build();
    Optional<IpMap> ipMap = MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
    if (ipMap.isPresent()) {
        return ipMap.get().getExternalIp();
    }
    return null;
}
Also used : IpMapKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapKey) IpMappingKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMappingKey) IpMapping(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping) 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)

Example 25 with SegmentId

use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.SegmentId in project netvirt by opendaylight.

the class NaptManager method removeIpMappingForRouterID.

private void removeIpMappingForRouterID(long segmentId) {
    InstanceIdentifierBuilder<IpMapping> idBuilder = InstanceIdentifier.builder(IntextIpMap.class).child(IpMapping.class, new IpMappingKey(segmentId));
    InstanceIdentifier<IpMapping> id = idBuilder.build();
    // Get all externalIps and decrement their counters before deleting the ipmap
    String externalIp = null;
    Optional<IpMapping> ipMapping = MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
    if (ipMapping.isPresent()) {
        List<IpMap> ipMaps = ipMapping.get().getIpMap();
        for (IpMap ipMap : ipMaps) {
            externalIp = ipMap.getExternalIp();
            LOG.debug("removeIpMappingForRouterID : externalIP is {}", externalIp);
            if (externalIp != null) {
                updateCounter(segmentId, externalIp, false);
            }
        }
        // remove from ipmap DS
        LOG.debug("removeIpMappingForRouterID : Removing Ipmap for router {} from datastore", segmentId);
        MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
    }
}
Also used : IntextIpMap(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpMap) IpMappingKey(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMappingKey) IpMapping(org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping) 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)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)13 IntextIpMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpMap)12 IpMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMap)12 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)11 IpMapping (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMapping)10 IpMappingKey (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.IpMappingKey)10 ProtocolTypes (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProtocolTypes)8 SnatintIpPortMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.SnatintIpPortMap)8 IpMapKey (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.map.ip.mapping.IpMapKey)8 Nullable (org.eclipse.jdt.annotation.Nullable)7 ArrayList (java.util.ArrayList)6 SubnetUtils (org.apache.commons.net.util.SubnetUtils)6 IntextIpPortMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.IntextIpPortMap)6 IpPortMap (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.IpPortMap)6 IpPortMapKey (org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.intext.ip.port.map.ip.port.mapping.intext.ip.protocol.type.IpPortMapKey)6 ActionInfo (org.opendaylight.genius.mdsalutil.ActionInfo)4 InstructionInfo (org.opendaylight.genius.mdsalutil.InstructionInfo)4 ActionSetDestinationIp (org.opendaylight.genius.mdsalutil.actions.ActionSetDestinationIp)4 InstructionApplyActions (org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions)4 InstructionGotoTable (org.opendaylight.genius.mdsalutil.instructions.InstructionGotoTable)4