List of usage examples for com.google.common.util.concurrent UncheckedExecutionException getMessage
public String getMessage()
From source file:org.opendaylight.netvirt.natservice.internal.NaptManager.java
/** * 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/*from ww w. j a va 2s . c o m*/ * @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("NAPT Service : 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("NAPT Service : 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("NAPT Service : 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); if (!externalIp.contains(subnetPrefix)) { EXTSUBNET_FLAG = true; externalIpSubnet = new SubnetUtils(externalIp); allIps = Arrays.asList(externalIpSubnet.getInfo().getAllAddresses()); LOG.debug("NAPT Service : total count of externalIps available {}", externalIpSubnet.getInfo().getAddressCount()); } else { LOG.debug("NAPT Service : 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); } for (String extIp : allIps) { LOG.info("NAPT Service : Looping externalIPs with externalIP now as {}", extIp); if (NEXT_EXTIP_FLAG) { createNaptPortPool(extIp); LOG.debug("NAPT Service : 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("NAPT Service : Got id from idManager"); rpcResult = result.get(); } else { LOG.error("NAPT Service : getExternalAddressMapping, idManager could not " + "allocate id retry if subnet"); if (!EXTSUBNET_FLAG) { LOG.error( "NAPT Service : getExternalAddressMapping returning null for single IP case," + " may be ports exhausted"); return null; } LOG.debug("NAPT Service : Could be ports exhausted case, try with another externalIP" + " if possible"); NEXT_EXTIP_FLAG = 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( "NAPT Service : 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("NAPT Service : Failed to write into ip-port-map with exception {}", uee.getMessage()); } // Write to snat-internal-ip-port-info String internalIpAddress = sourceAddress.getIpAddress(); int ipPort = sourceAddress.getPortNumber(); ProtocolTypes protocolType = NatUtil.getProtocolType(protocol); List<Integer> portList = NatUtil.getInternalIpPortListInfo(dataBroker, segmentId, internalIpAddress, protocolType); if (portList == null) { portList = new ArrayList<>(); } 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("NAPT Service : Failed to write into snat-internal-ip-port-info with " + "exception {}", ex.getMessage()); } SessionAddress externalIpPort = new SessionAddress(extIp, extPort); LOG.debug( "NAPT Service : getExternalAddressMapping successfully returning externalIP {} " + "and port {}", externalIpPort.getIpAddress(), externalIpPort.getPortNumber()); return externalIpPort; } catch (InterruptedException | ExecutionException e) { LOG.error("NAPT Service : getExternalAddressMapping, Exception caught {}", e); return null; } } // end of for loop } // end of else ipmap present } // end of else check ipmap LOG.error( "NAPT Service: getExternalAddressMapping returning null, nothing worked or externalIPs exhausted"); return null; }