@Override public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) { final IpAssocCommand command = (IpAssocCommand) cmd; final List<IpAddress> ips = new LinkedList<IpAddress>(); for (final IpAddressTO ip : command.getIpAddresses()) { final IpAddress ipAddress = new IpAddress(ip.getPublicIp(), ip.isSourceNat(), ip.isAdd(), ip.isOneToOneNat(), ip.isFirstIP(), ip.getVlanGateway(), ip.getVlanNetmask(), ip.getVifMacAddress(), ip.getNicDevId(), ip.isNewNic(), ip.getTrafficType().toString()); ips.add(ipAddress); } final IpAssociation ipAssociation = new IpAssociation(ips.toArray(new IpAddress[ips.size()])); return generateConfigItems(ipAssociation); }
protected Answer execute(final IpAssocCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource IPAssocCommand: " + s_gson.toJson(cmd)); } int i = 0; final String[] results = new String[cmd.getIpAddresses().length]; try { final IpAddressTO[] ips = cmd.getIpAddresses(); final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); final String controlIp = getRouterSshControlIp(cmd); for (final IpAddressTO ip : ips) { assignPublicIpAddress(routerName, controlIp, ip.getPublicIp(), ip.isAdd(), ip.isFirstIP(), ip.isSourceNat(), ip.getBroadcastUri(), ip.getVlanGateway(), ip.getVlanNetmask(), ip.getVifMacAddress()); results[i++] = ip.getPublicIp() + " - success"; } for (; i < cmd.getIpAddresses().length; i++) { results[i++] = IpAssocAnswer.errorResult; } } catch (final Throwable e) { s_logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e); for (; i < cmd.getIpAddresses().length; i++) { results[i++] = IpAssocAnswer.errorResult; } } return new IpAssocAnswer(cmd, results); }
protected void setNicDevIdIfCorrectVifIsNotNull(final Connection conn, final IpAddressTO ip, final VIF correctVif) throws InternalErrorException, BadServerResponse, XenAPIException, XmlRpcException { if (correctVif == null) { if (ip.isAdd()) { throw new InternalErrorException("Failed to find DomR VIF to associate IP with."); } else { s_logger.debug("VIF to deassociate IP with does not exist, return success"); } } else { ip.setNicDevId(Integer.valueOf(correctVif.getDevice(conn))); } }
nic.setMac(ip.getVifMacAddress()); nic.setType(ip.getTrafficType()); if (ip.getBroadcastUri() == null) { nic.setBroadcastType(BroadcastDomainType.Native); } else { final URI uri = BroadcastDomainType.fromString(ip.getBroadcastUri()); nic.setBroadcastType(BroadcastDomainType.getSchemeValue(uri)); nic.setBroadcastUri(uri); nic.setNetworkRateMbps(ip.getNetworkRate()); nic.setName(ip.getNetworkName()); if (ip.isAdd() && correctVif == null) { addVif = true; if (ip.isAdd() && correctVif == null) { throw new InternalErrorException("Failed to find DomR VIF to associate/disassociate IP with."); ip.setNicDevId(Integer.valueOf(correctVif.getDevice(conn))); ip.setNewNic(addVif);
final URI broadcastUri = BroadcastDomainType.fromString(ip.getBroadcastUri()); if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) { throw new InternalErrorException("Unable to assign a public IP to a VIF on network " + ip.getBroadcastUri()); if (ip.isAdd() && publicNicInfo == -1) { if (s_logger.isDebugEnabled()) { s_logger.debug("Plug new NIC to associate" + controlIp + " to " + ip.getPublicIp()); throw new InternalErrorException(msg); ip.setNicDevId(publicNicInfo); ip.setNewNic(addVif); } else { ip.setNicDevId(publicNicInfo);
final IpAddressTO[] ips = cmd.getIpAddresses(); for (final IpAddressTO ip : ips) { final long guestVlanTag = Long.parseLong(ip.getBroadcastUri()); final String vlanSelfIp = ip.getVlanGateway(); final String vlanNetmask = ip.getVlanNetmask(); if (ip.isAdd()) { results[i++] = ip.getPublicIp() + " - success"; final String action = ip.isAdd() ? "associate" : "remove"; if (s_logger.isDebugEnabled()) { s_logger.debug("Netscaler load balancer " + _ip + " successfully executed IPAssocCommand to " + action + " IP " + ip);
nic.setMac(ip.getVifMacAddress()); nic.setType(ip.getTrafficType()); if (ip.getBroadcastUri() == null) { nic.setBroadcastType(BroadcastDomainType.Native); } else { final URI uri = BroadcastDomainType.fromString(ip.getBroadcastUri()); nic.setBroadcastType(BroadcastDomainType.getSchemeValue(uri)); nic.setBroadcastUri(uri); nic.setNetworkRateMbps(ip.getNetworkRate()); nic.setName(ip.getNetworkName()); if (org.apache.commons.lang.StringUtils.equalsIgnoreCase(lastIp, "true") && !ip.isAdd()) { final VIF correctVif = getCorrectVif(conn, router, network);
GuestNetworkType type = GuestNetworkType.INTERFACE_NAT; if (ip.isSourceNat()) { type = GuestNetworkType.SOURCE_NAT; if (ip.getPublicIp() == null) { throw new ExecutionException("Source NAT IP address must not be null."); } else { sourceNatIpAddress = ip.getPublicIp(); if (ip.getBroadcastUri() != null) { String parsedVlanTag = parsePublicVlanTag(ip.getBroadcastUri()); if (!parsedVlanTag.equals("untagged")) { try { if (ip.isAdd()) { results[i++] = ip.getPublicIp() + " - success"; } catch (ExecutionException e) { s_logger.error(e);
for (final IpAddressTO ip : ips) { boolean newNic = false; if (!broadcastUriAllocatedToVM.containsKey(ip.getBroadcastUri())) { VifHotPlug(conn, routerName, ip.getBroadcastUri(), ip.getVifMacAddress()); broadcastUriAllocatedToVM.put(ip.getBroadcastUri(), nicPos++); newNic = true; nicNum = broadcastUriAllocatedToVM.get(ip.getBroadcastUri()); networkUsage(routerIp, "addVif", "eth" + nicNum); ip.setNicDevId(nicNum); ip.setNewNic(newNic);
for (final IpAddressTO ip : ips) { if (!broadcastUriAllocatedToVM.containsKey(ip.getBroadcastUri())) { VifHotPlug(conn, routerName, ip.getBroadcastUri(), ip.getVifMacAddress()); broadcastUriAllocatedToVM.put(ip.getBroadcastUri(), nicPos++); nicNum = broadcastUriAllocatedToVM.get(ip.getBroadcastUri()); if (org.apache.commons.lang.StringUtils.equalsIgnoreCase(lastIp, "true") && !ip.isAdd()) { vifHotUnPlug(conn, routerName, ip.getVifMacAddress()); networkUsage(routerIp, "deleteVif", "eth" + nicNum);
private ExecutionResult prepareNetworkElementCommand(final IpAssocVpcCommand cmd) { final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); try { final IpAddressTO[] ips = cmd.getIpAddresses(); for (final IpAddressTO ip : ips) { final URI broadcastUri = BroadcastDomainType.fromString(ip.getBroadcastUri()); if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) { throw new InternalErrorException("Invalid Broadcast URI " + ip.getBroadcastUri()); } final String vlanId = BroadcastDomainType.getValue(broadcastUri); int publicNicInfo = -1; publicNicInfo = getVmNics(routerName, vlanId); if (publicNicInfo < 0) { if (ip.isAdd()) { throw new InternalErrorException("Failed to find DomR VIF to associate/disassociate IP with."); } else { s_logger.debug("VIF to deassociate IP with does not exist, return success"); continue; } } ip.setNicDevId(publicNicInfo); } } catch (final Exception e) { s_logger.error("Prepare Ip Assoc failure on applying one ip due to exception: ", e); return new ExecutionResult(false, e.toString()); } return new ExecutionResult(true, null); }
protected ExecutionResult prepareNetworkElementCommand(final SetSourceNatCommand cmd) { final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); final IpAddressTO pubIp = cmd.getIpAddress(); try { final String broadcastUri = pubIp.getBroadcastUri(); final String vlanId = BroadcastDomainType.getValue(broadcastUri); final int ethDeviceNum = getVmNics(routerName, vlanId); if (ethDeviceNum > 0) { pubIp.setNicDevId(ethDeviceNum); } else { return new ExecutionResult(false, "Prepare Ip SNAT failed due to unable to find the nic"); } } catch (final Exception e) { final String msg = "Prepare Ip SNAT failure due to " + e.toString(); s_logger.error(msg, e); return new ExecutionResult(false, e.toString()); } return new ExecutionResult(true, null); }
private ExecutionResult prepNetBoth(String routerName, IpAddressTO[] ips, String type) { Xen xen = new Xen(c); try { Xen.Vm vm = xen.getVmConfig(routerName); for (IpAddressTO ip : ips) { Integer devId = vm.getVifIdByMac(ip.getVifMacAddress()); if (devId < 0 && "IpAssocVpcCommand".equals(type)) { String msg = "No valid Nic devId found for " + vm.getVmName() + " with " + ip.getVifMacAddress(); logger.error(msg); return new ExecutionResult(false, msg); } else if (devId < 0 && "IpAssocCommand".equals(type)) { // vm.get String msg = "No valid Nic devId found for " + vm.getVmName() + " with " + ip.getVifMacAddress() + " " + " Ignoring for now (routervm)"; logger.debug(msg); devId=2; } ip.setNicDevId(devId); } } catch (Exception e) { String msg = type + " failure on applying one ip due to exception: " + e; logger.error(msg); return new ExecutionResult(false, msg); } return new ExecutionResult(true, null); } }
protected SetSourceNatAnswer execute(final SetSourceNatCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource SetSourceNatCommand " + s_gson.toJson(cmd)); } final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); final String routerIp = getRouterSshControlIp(cmd); final IpAddressTO pubIp = cmd.getIpAddress(); try { final int ethDeviceNum = findRouterEthDeviceIndex(routerName, routerIp, pubIp.getVifMacAddress()); String args = ""; args += " -A "; args += " -l "; args += pubIp.getPublicIp(); args += " -c "; args += "eth" + ethDeviceNum; final String command = String.format("%s%s %s", "/opt/cloud/bin/", VRScripts.VPC_SOURCE_NAT, args); final Pair<Boolean, String> result = SshHelper.sshExecute(routerIp, DEFAULT_DOMR_SSHPORT, "root", getSystemVMKeyFile(), null, command); if (!result.first()) { final String msg = "SetupGuestNetworkCommand on domain router " + routerIp + " failed. message: " + result.second(); s_logger.error(msg); return new SetSourceNatAnswer(cmd, false, msg); } return new SetSourceNatAnswer(cmd, true, "success"); } catch (final Exception e) { final String msg = "Ip SNAT failure due to " + e.toString(); s_logger.error(msg, e); return new SetSourceNatAnswer(cmd, false, msg); } }
protected VIF getCorrectVif(final Connection conn, final VM router, final IpAddressTO ip) throws XmlRpcException, XenAPIException { final NicTO nic = new NicTO(); nic.setType(ip.getTrafficType()); nic.setName(ip.getNetworkName()); if (ip.getBroadcastUri() == null) { nic.setBroadcastType(BroadcastDomainType.Native); } else { final URI uri = BroadcastDomainType.fromString(ip.getBroadcastUri()); nic.setBroadcastType(BroadcastDomainType.getSchemeValue(uri)); nic.setBroadcastUri(uri); } final Network network = getNetwork(conn, nic); // Determine the correct VIF on DomR to associate/disassociate the // IP address with final Set<VIF> routerVIFs = router.getVIFs(conn); for (final VIF vif : routerVIFs) { final Network vifNetwork = vif.getNetwork(conn); if (vifNetwork.getUuid(conn).equals(network.getUuid(conn))) { return vif; } } return null; }
protected ExecutionResult prepareNetworkElementCommand(final IpAssocVpcCommand cmd) { final Connection conn = getConnection(); final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); try { final IpAddressTO[] ips = cmd.getIpAddresses(); for (final IpAddressTO ip : ips) { final VM router = getVM(conn, routerName); final VIF correctVif = getVifByMac(conn, router, ip.getVifMacAddress()); setNicDevIdIfCorrectVifIsNotNull(conn, ip, correctVif); } } catch (final Exception e) { s_logger.error("Ip Assoc failure on applying one ip due to exception: ", e); return new ExecutionResult(false, e.getMessage()); } return new ExecutionResult(true, null); }
private synchronized Answer execute(IpAssocCommand cmd, int numRetries) { String[] results = new String[cmd.getIpAddresses().length]; int i = 0; IpAddressTO[] ips = cmd.getIpAddresses(); for (IpAddressTO ip : ips) { results[i++] = ip.getPublicIp() + " - success"; } return new IpAssocAnswer(cmd, results); }
protected ExecutionResult prepareNetworkElementCommand(final SetSourceNatCommand cmd) { final Connection conn = getConnection(); final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); final IpAddressTO pubIp = cmd.getIpAddress(); try { final VM router = getVM(conn, routerName); final VIF correctVif = getCorrectVif(conn, router, pubIp); pubIp.setNicDevId(Integer.valueOf(correctVif.getDevice(conn))); } catch (final Exception e) { final String msg = "Ip SNAT failure due to " + e.toString(); s_logger.error(msg, e); return new ExecutionResult(false, msg); } return new ExecutionResult(true, null); }
conn = LibvirtConnection.getConnectionByVmName(routerName); Integer devNum = 0; final String pubVlan = pubIP.getBroadcastUri(); final List<InterfaceDef> pluggedNics = getInterfaces(conn, routerName); pubIP.setNicDevId(devNum);
protected ExecutionResult prepareNetworkElementCommand(final IpAssocVpcCommand cmd) { Connect conn; final String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME); try { conn = getLibvirtUtilitiesHelper().getConnectionByVmName(routerName); final IpAddressTO[] ips = cmd.getIpAddresses(); Integer devNum = 0; final List<InterfaceDef> pluggedNics = getInterfaces(conn, routerName); final Map<String, Integer> macAddressToNicNum = new HashMap<>(pluggedNics.size()); for (final InterfaceDef pluggedNic : pluggedNics) { final String pluggedVlan = pluggedNic.getBrName(); macAddressToNicNum.put(pluggedNic.getMacAddress(), devNum); devNum++; } for (final IpAddressTO ip : ips) { ip.setNicDevId(macAddressToNicNum.get(ip.getVifMacAddress())); } return new ExecutionResult(true, null); } catch (final LibvirtException e) { s_logger.error("Ip Assoc failure on applying one ip due to exception: ", e); return new ExecutionResult(false, e.getMessage()); } }