use of org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rpc.rev170209.GetNatTranslationsForNetworkAndIpaddressOutput in project netvirt by opendaylight.
the class NatRpcServiceImpl method getNatTranslationsForNetworkAndIpaddress.
public Future<RpcResult<GetNatTranslationsForNetworkAndIpaddressOutput>> getNatTranslationsForNetworkAndIpaddress(GetNatTranslationsForNetworkAndIpaddressInput input) {
String ipAddress = String.valueOf(input.getIpAddress().getValue());
RpcResultBuilder<GetNatTranslationsForNetworkAndIpaddressOutput> rpcResultBuilder = null;
GetNatTranslationsForNetworkAndIpaddressOutputBuilder output = null;
List<Uuid> subnetUuidList = NatUtil.getSubnetIdsFromNetworkId(dataBroker, input.getNetworkUuid());
if (subnetUuidList.isEmpty()) {
String errMsg = String.format("404 Not Found - Invalid Network UUID {%s} provided as no Subnetworks found", input.getNetworkUuid().getValue());
rpcResultBuilder = RpcResultBuilder.<GetNatTranslationsForNetworkAndIpaddressOutput>failed().withError(RpcError.ErrorType.APPLICATION, errMsg);
return Futures.immediateFuture(rpcResultBuilder.build());
}
Subnet subNet = null;
Boolean isIpInSubnet = Boolean.FALSE;
outerloop: for (Uuid subnetUuid : subnetUuidList) {
subNet = nvpnManager.getNeutronSubnet(subnetUuid);
for (AllocationPools allocationPool : subNet.getAllocationPools()) {
if (NatUtil.isIpInSubnet(ipAddress, String.valueOf(allocationPool.getStart().getValue()), String.valueOf(allocationPool.getEnd().getValue()))) {
LOG.debug("getNatTranslationsForNetworkAndIpaddress : IP Adderess {} falls within the Subnet {}", ipAddress, subNet.getUuid().getValue());
isIpInSubnet = Boolean.TRUE;
break outerloop;
}
}
}
if (!isIpInSubnet) {
String errMsg = String.format("404 Not Found - IP Adress {%s} does not fall within the Subnet IP range" + " of Network {%s}", ipAddress, input.getNetworkUuid().getValue());
rpcResultBuilder = RpcResultBuilder.<GetNatTranslationsForNetworkAndIpaddressOutput>failed().withError(RpcError.ErrorType.APPLICATION, errMsg);
return Futures.immediateFuture(rpcResultBuilder.build());
}
Subnetmap subnetMap = NatUtil.getSubnetMap(dataBroker, subNet.getUuid());
long routerId = NatUtil.getVpnId(dataBroker, subnetMap.getRouterId().getValue());
List<Ports> fipPorts = NatUtil.getFloatingIpPortsForRouter(dataBroker, subnetMap.getRouterId());
if (fipPorts.isEmpty()) {
LOG.warn("getNatTranslationsForNetworkAndIpaddress : No DNAT IP Mapping found for IP {}", ipAddress);
} else {
for (Ports fipPort : fipPorts) {
List<InternalToExternalPortMap> ipMapping = fipPort.getInternalToExternalPortMap();
for (InternalToExternalPortMap fipMap : ipMapping) {
if (fipMap.getInternalIp().equals(ipAddress)) {
output = new GetNatTranslationsForNetworkAndIpaddressOutputBuilder().setExternalIp(fipMap.getExternalIp()).setNatTranslation("DNAT");
rpcResultBuilder = RpcResultBuilder.success();
rpcResultBuilder.withResult(output.build());
return Futures.immediateFuture(rpcResultBuilder.build());
}
}
}
}
IpPortMapping ipPortMapping = NatUtil.getIportMapping(dataBroker, routerId);
if (ipPortMapping == null) {
LOG.warn("getNatTranslationsForNetworkAndIpaddress : No SNAT IP Mapping found for IP {}", ipAddress);
} else {
for (IntextIpProtocolType protocolType : ipPortMapping.getIntextIpProtocolType()) {
for (IpPortMap ipPortMap : protocolType.getIpPortMap()) {
String[] internalIpPort = ipPortMap.getIpPortInternal().split(NwConstants.MACADDR_SEP);
if (ipAddress.equals(internalIpPort[0])) {
output = new GetNatTranslationsForNetworkAndIpaddressOutputBuilder().setExternalIp(ipPortMap.getIpPortExternal().getIpAddress()).setInternalIp(internalIpPort[0]).setNatTranslation("SNAT").setInternalPort(internalIpPort[1]).setExternalPort(ipPortMap.getIpPortExternal().getPortNum().toString()).setProtocol(protocolType.getProtocol().getName());
rpcResultBuilder = RpcResultBuilder.success();
rpcResultBuilder.withResult(output.build());
return Futures.immediateFuture(rpcResultBuilder.build());
}
}
}
}
String errMsg = String.format("404 Not Found - No NAT Translation found for IP {%s}", ipAddress);
rpcResultBuilder = RpcResultBuilder.<GetNatTranslationsForNetworkAndIpaddressOutput>failed().withError(RpcError.ErrorType.APPLICATION, errMsg);
return Futures.immediateFuture(rpcResultBuilder.build());
}
Aggregations