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 checkIpPortMap.
@Nullable
private SessionAddress checkIpPortMap(Uint32 segmentId, String internalIpPort, NAPTEntryEvent.Protocol protocol) {
LOG.debug("checkIpPortMap : called with segmentId {} and internalIpPort {}", segmentId, internalIpPort);
ProtocolTypes protocolType = NatUtil.getProtocolType(protocol);
// check if ip-port-map node is there
InstanceIdentifierBuilder<IpPortMap> idBuilder = InstanceIdentifier.builder(IntextIpPortMap.class).child(IpPortMapping.class, new IpPortMappingKey(segmentId)).child(IntextIpProtocolType.class, new IntextIpProtocolTypeKey(protocolType)).child(IpPortMap.class, new IpPortMapKey(internalIpPort));
InstanceIdentifier<IpPortMap> id = idBuilder.build();
Optional<IpPortMap> ipPortMapType = Optional.empty();
try {
ipPortMapType = SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.CONFIGURATION, id);
} catch (ExecutionException | InterruptedException e) {
LOG.error("checkIpPortMap: Exception while reading IpMap DS for the segmentId {} " + "internalIpPort {} protocol {}", segmentId, internalIpPort, protocol, e);
}
if (ipPortMapType.isPresent()) {
LOG.debug("checkIpPortMap : {}", ipPortMapType.get());
SessionAddress externalIpPort = new SessionAddress(ipPortMapType.get().getIpPortExternal().getIpAddress(), ipPortMapType.get().getIpPortExternal().getPortNum().toJava());
LOG.debug("checkIpPortMap : returning successfully externalIP {} and port {}", externalIpPort.getIpAddress(), externalIpPort.getPortNumber());
return externalIpPort;
}
// return null if not found
LOG.warn("checkIpPortMap : no-entry in checkIpPortMap, returning NULL [should be OK] for " + "segmentId {} and internalIPPort {}", segmentId, internalIpPort);
return null;
}
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 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;
}
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(Uint32 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
Optional<IpMapping> ipMapping = Optional.empty();
try {
ipMapping = SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
} catch (ExecutionException | InterruptedException e) {
LOG.error("removeIpMappingForRouterID: Exception while reading IpMapping DS for the segmentId {} ", segmentId, e);
}
if (ipMapping.isPresent()) {
for (IpMap ipMap : ipMapping.get().nonnullIpMap().values()) {
String 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);
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.SegmentId in project netvirt by opendaylight.
the class FloatingIPListener method buildPreDNATFlowEntity.
@Nullable
private FlowEntity buildPreDNATFlowEntity(Uint64 dpId, InternalToExternalPortMap mapping, Uint32 routerId, Uint32 associatedVpn) {
String externalIp = mapping.getExternalIp();
Uuid floatingIpId = mapping.getExternalId();
// Get the FIP MAC address for DNAT
String floatingIpPortMacAddress = NatUtil.getFloatingIpPortMacFromFloatingIpId(dataBroker, floatingIpId);
if (floatingIpPortMacAddress == null) {
LOG.error("buildPreDNATFlowEntity : Unable to retrieve floatingIpPortMacAddress from floating IP UUID {} " + "for floating IP {}", floatingIpId, externalIp);
return null;
}
LOG.debug("buildPreDNATFlowEntity : Bulding DNAT Flow entity for ip {} ", externalIp);
Uint32 segmentId = associatedVpn == NatConstants.INVALID_ID ? routerId : associatedVpn;
LOG.debug("buildPreDNATFlowEntity : Segment id {} in build preDNAT Flow", segmentId);
List<MatchInfo> matches = new ArrayList<>();
matches.add(MatchEthernetType.IPV4);
matches.add(new MatchIpv4Destination(externalIp, "32"));
// Match Destination Floating IP MAC Address on table = 25 (PDNAT_TABLE)
matches.add(new MatchEthernetDestination(new MacAddress(floatingIpPortMacAddress)));
// matches.add(new MatchMetadata(
// BigInteger.valueOf(vpnId), MetaDataUtil.METADATA_MASK_VRFID));
List<ActionInfo> actionsInfos = new ArrayList<>();
String internalIp = mapping.getInternalIp();
actionsInfos.add(new ActionSetDestinationIp(internalIp, "32"));
List<InstructionInfo> instructions = new ArrayList<>();
instructions.add(new InstructionWriteMetadata(MetaDataUtil.getVpnIdMetadata(segmentId.longValue()), MetaDataUtil.METADATA_MASK_VRFID));
instructions.add(new InstructionApplyActions(actionsInfos));
instructions.add(new InstructionGotoTable(NwConstants.DNAT_TABLE));
String flowRef = NatUtil.getFlowRef(dpId, NwConstants.PDNAT_TABLE, routerId, externalIp);
FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.PDNAT_TABLE, flowRef, NatConstants.DEFAULT_DNAT_FLOW_PRIORITY, flowRef, 0, 0, NwConstants.COOKIE_DNAT_TABLE, matches, instructions);
return flowEntity;
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.SegmentId in project netvirt by opendaylight.
the class NatUtil method getLeastLoadedExternalIp.
@Nullable
public static String getLeastLoadedExternalIp(DataBroker dataBroker, Uint32 segmentId) {
String leastLoadedExternalIp = null;
InstanceIdentifier<ExternalCounters> id = InstanceIdentifier.builder(ExternalIpsCounter.class).child(ExternalCounters.class, new ExternalCountersKey(segmentId)).build();
Optional<ExternalCounters> externalCountersData;
try {
externalCountersData = SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL, id);
} catch (ExecutionException | InterruptedException e) {
LOG.error("getLeastLoadedExternalIp: Exception while reading ExternalCounters DS for the segmentId {}", segmentId, e);
return leastLoadedExternalIp;
}
if (externalCountersData.isPresent()) {
ExternalCounters externalCounter = externalCountersData.get();
short countOfLstLoadExtIp = 32767;
for (ExternalIpCounter externalIpCounter : externalCounter.nonnullExternalIpCounter().values()) {
String curExternalIp = externalIpCounter.getExternalIp();
short countOfCurExtIp = externalIpCounter.getCounter().toJava();
if (countOfCurExtIp < countOfLstLoadExtIp) {
countOfLstLoadExtIp = countOfCurExtIp;
leastLoadedExternalIp = curExternalIp;
}
}
}
return leastLoadedExternalIp;
}
Aggregations