|
21 | 21 | import java.util.Collections; |
22 | 22 | import java.util.Date; |
23 | 23 | import java.util.HashMap; |
| 24 | +import java.util.HashSet; |
24 | 25 | import java.util.LinkedHashMap; |
25 | | -import java.util.LinkedHashSet; |
26 | 26 | import java.util.List; |
27 | 27 | import java.util.Map; |
28 | 28 | import java.util.Objects; |
@@ -329,22 +329,26 @@ public Map<String, String> getVmDetailsForBackup(VirtualMachine vm) { |
329 | 329 | ServiceOffering serviceOffering = serviceOfferingDao.findById(vm.getServiceOfferingId()); |
330 | 330 | details.put(ApiConstants.SERVICE_OFFERING_ID, serviceOffering.getUuid()); |
331 | 331 | List<UserVmJoinVO> userVmJoinVOs = userVmJoinDao.searchByIds(vm.getId()); |
| 332 | + |
332 | 333 | if (userVmJoinVOs != null && !userVmJoinVOs.isEmpty()) { |
333 | | - Set<String> networkIds = new LinkedHashSet<>(); |
334 | | - Set<String> ipAddresses = new LinkedHashSet<>(); |
335 | | - Set<String> ip6Addresses = new LinkedHashSet<>(); |
336 | | - Set<String> macAddresses = new LinkedHashSet<>(); |
| 334 | + List<Map<String, String>> nics = new ArrayList<>(); |
| 335 | + Set<String> seen = new HashSet<>(); |
| 336 | + |
337 | 337 | for (UserVmJoinVO userVmJoinVO : userVmJoinVOs) { |
338 | | - networkIds.add(userVmJoinVO.getNetworkUuid()); |
339 | | - ipAddresses.add(userVmJoinVO.getIpAddress()); |
340 | | - ip6Addresses.add(userVmJoinVO.getIp6Address()); |
341 | | - macAddresses.add(userVmJoinVO.getMacAddress()); |
| 338 | + Map<String, String> nicInfo = new HashMap<>(); |
| 339 | + String key = userVmJoinVO.getNetworkUuid(); |
| 340 | + if (seen.add(key)) { |
| 341 | + nicInfo.put(ApiConstants.NETWORK_ID, userVmJoinVO.getNetworkUuid()); |
| 342 | + nicInfo.put(ApiConstants.IP_ADDRESS, userVmJoinVO.getIpAddress()); |
| 343 | + nicInfo.put(ApiConstants.IP6_ADDRESS, userVmJoinVO.getIp6Address()); |
| 344 | + nicInfo.put(ApiConstants.MAC_ADDRESS, userVmJoinVO.getMacAddress()); |
| 345 | + nics.add(nicInfo); |
| 346 | + } |
342 | 347 | } |
343 | | - if (!networkIds.isEmpty()) { |
344 | | - details.put(ApiConstants.NETWORK_IDS, String.join(",", networkIds)); |
345 | | - details.put(ApiConstants.IP_ADDRESSES, String.join(",", ipAddresses)); |
346 | | - details.put(ApiConstants.IP6_ADDRESSES, String.join(",", ip6Addresses)); |
347 | | - details.put(ApiConstants.MAC_ADDRESSES, String.join(",", macAddresses)); |
| 348 | + |
| 349 | + if (!nics.isEmpty()) { |
| 350 | + String nicJson = new Gson().toJson(nics); |
| 351 | + details.put("nics", nicJson); |
348 | 352 | } |
349 | 353 | } |
350 | 354 | return details; |
@@ -1092,47 +1096,39 @@ public Map<Long, Network.IpAddresses> getIpToNetworkMapFromBackup(Backup backup, |
1092 | 1096 | { |
1093 | 1097 | Map<Long, Network.IpAddresses> ipToNetworkMap = new LinkedHashMap<Long, Network.IpAddresses>(); |
1094 | 1098 |
|
1095 | | - String networkIdsString = backup.getDetail(ApiConstants.NETWORK_IDS); |
1096 | | - if (networkIdsString == null) { |
| 1099 | + String nicsJson = backup.getDetail(ApiConstants.NICS); |
| 1100 | + if (nicsJson == null) { |
1097 | 1101 | throw new CloudRuntimeException("Backup doesn't contain network information. " + |
1098 | 1102 | "Please specify at least one valid network while creating instance"); |
1099 | 1103 | } |
1100 | | - List<String> networkUuids = List.of(networkIdsString.split(",")); |
1101 | 1104 |
|
1102 | | - List<String> requestedIp = null; |
1103 | | - List<String> requestedMac = null; |
1104 | | - List<String> requestedIpv6 = null; |
1105 | | - boolean preserveIpAddresses = false; |
1106 | | - if (preserveIps) { |
1107 | | - VMInstanceVO vm = vmInstanceDao.findById(backup.getVmId()); |
1108 | | - if (vm == null) { |
1109 | | - preserveIpAddresses = true; |
| 1105 | + List<Map<String, String>> nics = new Gson().fromJson(nicsJson, List.class); |
| 1106 | + |
| 1107 | + for (Map<String, String> nic : nics) { |
| 1108 | + String networkUuid = nic.get(ApiConstants.NETWORK_ID); |
| 1109 | + if (networkUuid == null) { |
| 1110 | + throw new CloudRuntimeException("Backup doesn't contain network information. " + |
| 1111 | + "Please specify at least one valid network while creating instance"); |
1110 | 1112 | } |
1111 | | - } |
1112 | | - if (preserveIpAddresses) { |
1113 | | - requestedIp = parseAddressString(backup.getDetail(ApiConstants.IP_ADDRESSES)); |
1114 | | - requestedMac = parseAddressString(backup.getDetail(ApiConstants.MAC_ADDRESSES)); |
1115 | | - requestedIpv6 = parseAddressString(backup.getDetail(ApiConstants.IP6_ADDRESSES)); |
1116 | | - } |
1117 | 1113 |
|
1118 | | - int index = 0; |
1119 | | - for (String networkUuid: networkUuids) { |
1120 | 1114 | Network network = networkDao.findByUuid(networkUuid); |
1121 | 1115 | if (network == null) { |
1122 | | - throw new CloudRuntimeException("Unable to find network with the uuid " + networkUuid + "stored in backup. " + |
| 1116 | + throw new CloudRuntimeException("Unable to find network with the uuid " + networkUuid + " stored in backup. " + |
1123 | 1117 | "Please specify a valid network id while creating the instance"); |
1124 | 1118 | } |
| 1119 | + |
1125 | 1120 | Long networkId = network.getId(); |
1126 | 1121 | Network.IpAddresses ipAddresses = null; |
1127 | | - if (preserveIpAddresses) { |
1128 | | - String ip = (requestedIp != null) ? requestedIp.get(index) : null; |
1129 | | - String ipv6 = (requestedIpv6 != null) ? requestedIpv6.get(index) : null; |
1130 | | - String mac = (requestedMac != null) ? requestedMac.get(index) : null; |
| 1122 | + |
| 1123 | + if (preserveIps) { |
| 1124 | + String ip = nic.get(ApiConstants.IP_ADDRESS); |
| 1125 | + String ipv6 = nic.get(ApiConstants.IP6_ADDRESS); |
| 1126 | + String mac = nic.get(ApiConstants.MAC_ADDRESS); |
1131 | 1127 | ipAddresses = networkService.getIpAddressesFromIps(ip, ipv6, mac); |
1132 | 1128 | } |
| 1129 | + |
1133 | 1130 | ipToNetworkMap.put(networkId, ipAddresses); |
1134 | 1131 | networkIds.add(networkId); |
1135 | | - index += 1; |
1136 | 1132 | } |
1137 | 1133 | return ipToNetworkMap; |
1138 | 1134 | } |
|
0 commit comments