forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathNetworkMigrationManagerImpl.java
More file actions
693 lines (607 loc) · 31.5 KB
/
NetworkMigrationManagerImpl.java
File metadata and controls
693 lines (607 loc) · 31.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.network;
import java.util.List;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.cloud.entity.api.db.VMNetworkMapVO;
import org.apache.cloudstack.engine.cloud.entity.api.db.dao.VMNetworkMapDao;
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
import org.apache.cloudstack.resourcedetail.FirewallRuleDetailVO;
import org.apache.cloudstack.resourcedetail.VpcDetailVO;
import org.apache.cloudstack.resourcedetail.dao.FirewallRuleDetailsDao;
import org.apache.cloudstack.resourcedetail.dao.VpcDetailsDao;
import com.cloud.configuration.Resource;
import com.cloud.dc.DataCenter;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.deploy.DataCenterDeployment;
import com.cloud.deploy.DeployDestination;
import com.cloud.exception.InsufficientAddressCapacityException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.InsufficientVirtualNetworkCapacityException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.host.Host;
import com.cloud.host.dao.HostDao;
import com.cloud.network.dao.FirewallRulesCidrsDao;
import com.cloud.network.dao.FirewallRulesCidrsVO;
import com.cloud.network.dao.FirewallRulesDao;
import com.cloud.network.dao.IPAddressDao;
import com.cloud.network.dao.IPAddressVO;
import com.cloud.network.dao.NetworkDao;
import com.cloud.network.dao.NetworkDetailVO;
import com.cloud.network.dao.NetworkDetailsDao;
import com.cloud.network.dao.NetworkDomainDao;
import com.cloud.network.dao.NetworkDomainVO;
import com.cloud.network.dao.NetworkVO;
import com.cloud.network.dao.RouterNetworkDao;
import com.cloud.network.dao.RouterNetworkVO;
import com.cloud.network.guru.NetworkGuru;
import com.cloud.network.router.VirtualRouter;
import com.cloud.network.rules.FirewallRule;
import com.cloud.network.rules.FirewallRuleVO;
import com.cloud.network.rules.RulesManager;
import com.cloud.network.vpc.NetworkACLVO;
import com.cloud.network.vpc.Vpc;
import com.cloud.network.vpc.VpcGatewayVO;
import com.cloud.network.vpc.VpcManager;
import com.cloud.network.vpc.VpcService;
import com.cloud.network.vpc.VpcVO;
import com.cloud.network.vpc.dao.NetworkACLDao;
import com.cloud.network.vpc.dao.VpcDao;
import com.cloud.network.vpc.dao.VpcGatewayDao;
import com.cloud.offering.NetworkOffering;
import com.cloud.offerings.dao.NetworkOfferingDao;
import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
import com.cloud.server.ResourceTag;
import com.cloud.tags.ResourceTagVO;
import com.cloud.tags.dao.ResourceTagDao;
import com.cloud.user.Account;
import com.cloud.user.AccountVO;
import com.cloud.user.ResourceLimitService;
import com.cloud.user.dao.AccountDao;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.EntityManager;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.TransactionCallback;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.DomainRouterVO;
import com.cloud.vm.Nic;
import com.cloud.vm.NicDetailVO;
import com.cloud.vm.NicProfile;
import com.cloud.vm.NicVO;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.ReservationContextImpl;
import com.cloud.vm.UserVmVO;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineManager;
import com.cloud.vm.VirtualMachineProfile;
import com.cloud.vm.VirtualMachineProfileImpl;
import com.cloud.vm.dao.DomainRouterDao;
import com.cloud.vm.dao.NicDao;
import com.cloud.vm.dao.NicDetailsDao;
import com.cloud.vm.dao.NicIpAliasDao;
import com.cloud.vm.dao.NicSecondaryIpDao;
import com.cloud.vm.dao.UserVmDao;
public class NetworkMigrationManagerImpl implements NetworkMigrationManager {
public static final Logger s_logger = Logger.getLogger(NetworkMigrationManagerImpl.class.getName());
@Inject
private DataCenterDao _dcDao = null;
@Inject
private NetworkDetailsDao _networkDetailsDao = null;
@Inject
private AccountDao _accountDao = null;
@Inject
private NetworkDomainDao _networkDomainDao = null;
@Inject
private NetworkOrchestrationService _networkMgr = null;
@Inject
private ResourceLimitService _resourceLimitMgr = null;
@Inject
private NetworkDao _networksDao = null;
@Inject
private NetworkOfferingDao _networkOfferingDao = null;
@Inject
private NetworkOfferingServiceMapDao _networkOfferingServiceDao = null;
@Inject
private NicDao _nicDao = null;
@Inject
private NicSecondaryIpDao _nicSecondaryIpDao = null;
@Inject
private NicIpAliasDao _nicIpAliasDao = null;
@Inject
private NicDetailsDao _nicDetailsDao = null;
@Inject
private FirewallRulesDao _firewallDao = null;
@Inject
private FirewallRulesCidrsDao _firewallRulesCidrDao = null;
@Inject
private FirewallRuleDetailsDao _firewallRuleDetailsDao = null;
@Inject
private EntityManager _entityMgr = null;
@Inject
private RouterNetworkDao _routerNetworkDao = null;
@Inject
private DomainRouterDao _routerDao = null;
@Inject
private NetworkService _networkService = null;
@Inject
private UserVmDao _vmDao = null;
@Inject
private NetworkModel _networkModel= null;
@Inject
private VMNetworkMapDao _vmNetworkMapDao = null;
@Inject
private HostDao _hostDao = null;
@Inject
private VirtualMachineManager _itMgr = null;
@Inject
private IPAddressDao _ipAddressDao = null;
@Inject
private RulesManager _rulesMgr = null;
@Inject
private VpcDao _vpcDao = null;
@Inject
private VpcDetailsDao _vpcDetailsDao = null;
@Inject
private IpAddressManager _ipAddressManager = null;
@Inject
private VpcService _vpcService = null;
@Inject
private NetworkACLDao _networkACLDao = null;
@Inject
private VpcManager _vpcManager = null;
@Inject
private VpcGatewayDao _vpcGatewayDao = null;
@Inject
private ResourceTagDao _resourceTagDao = null;
@Override public long makeCopyOfNetwork(Network network, NetworkOffering networkOffering, Long vpcId) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Making a copy of network with uuid " + network.getUuid() + " and id " + network.getId() + " for migration.");
}
long originalNetworkId = network.getId();
NetworkDomainVO domainNetworkMapByNetworkId = _networkDomainDao.getDomainNetworkMapByNetworkId(originalNetworkId);
AccountVO networkAccount = _accountDao.findById(network.getAccountId());
boolean subdomainAccess = true;
if (domainNetworkMapByNetworkId != null) {
subdomainAccess = domainNetworkMapByNetworkId.isSubdomainAccess();
}
DataCenterDeployment plan = new DataCenterDeployment(network.getDataCenterId(), null, null, null, null, network.getPhysicalNetworkId());
List<? extends Network> networks = _networkMgr.setupNetwork(networkAccount, networkOffering, network, plan, network.getName(), network.getDisplayText(), true,
network.getDomainId(), network.getAclType(), subdomainAccess,
vpcId, true);
_resourceLimitMgr.incrementResourceCount(network.getAccountId(), Resource.ResourceType.network, network.isDisplay());
long networkCopyId;
if (networks == null || networks.isEmpty()) {
throw new CloudRuntimeException("Fail to create a network");
} else {
DataCenter zone = _dcDao.findById(network.getDataCenterId());
String guestNetworkCidr = zone.getGuestNetworkCidr();
if (networks.get(0).getGuestType() == Network.GuestType.Isolated
&& networks.get(0).getTrafficType() == Networks.TrafficType.Guest) {
Network networkCopy = networks.get(0);
for (final Network nw : networks) {
if (nw.getCidr() != null && nw.getCidr().equals(guestNetworkCidr)) {
networkCopy = nw;
}
}
networkCopyId = networkCopy.getId();
} else {
// For shared network
networkCopyId = networks.get(0).getId();
}
}
//Update the related network
NetworkVO originalNetwork = _networksDao.findById(originalNetworkId);
originalNetwork.setRelated(networkCopyId);
_networksDao.update(originalNetworkId, originalNetwork);
NetworkVO copiedNetwork = _networksDao.findById(networkCopyId);
copiedNetwork.setRelated(originalNetworkId);
copiedNetwork.setDisplayNetwork(false);
copiedNetwork.setBroadcastUri(network.getBroadcastUri());
copiedNetwork.setState(network.getState());
_networksDao.update(networkCopyId, copiedNetwork);
copyNetworkDetails(originalNetworkId, networkCopyId);
copyFirewallRulesToNewNetwork(network, networkCopyId);
assignUserNicsToNewNetwork(originalNetworkId, networkCopyId);
assignRouterNicsToNewNetwork(network.getId(), networkCopyId);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Succesfully created a copy of network " + originalNetwork.getName() + "(" + originalNetwork.getUuid() + ") id is " + originalNetwork.getId() + " for migration. The network copy has uuid " + network.getUuid() + " and id " + network.getId());
}
return networkCopyId;
}
@DB
private void copyNetworkDetails(long srcNetworkId, long dstNetworkId) {
List<NetworkDetailVO> networkDetails = _networkDetailsDao.listDetails(srcNetworkId);
for (NetworkDetailVO networkDetail : networkDetails) {
_networkDetailsDao.persist(new NetworkDetailVO(dstNetworkId, networkDetail.getName(), networkDetail.getValue(), networkDetail.isDisplay()));
}
}
/**
* reassigns the nics to the new network from the src network.
* @param srcNetworkId
* @param dstNetworkId
*/
private void assignUserNicsToNewNetwork(long srcNetworkId, long dstNetworkId) {
List<NicVO> nics = _nicDao.listByNetworkId(srcNetworkId);
for (NicVO nic : nics) {
if (nic.getVmType() == VirtualMachine.Type.User) {
nic.setNetworkId(dstNetworkId);
_nicDao.persist(nic);
//update the number of active nics in both networks after migration.
if (nic.getState() == Nic.State.Reserved) {
_networksDao.changeActiveNicsBy(srcNetworkId, -1);
_networksDao.changeActiveNicsBy(dstNetworkId, 1);
}
}
}
List<? extends IPAddressVO> publicIps = _ipAddressDao.listByAssociatedNetwork(srcNetworkId, null);
for (IPAddressVO ipAddress : publicIps) {
ipAddress.setAssociatedWithNetworkId(dstNetworkId);
_ipAddressDao.persist(ipAddress);
}
}
@Override
public Long makeCopyOfVpc(long vpcId, long vpcOfferingId) {
VpcVO vpc = _vpcDao.findById(vpcId);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Making a copy of vpc with uuid " + vpc.getUuid() + " and id " + vpc.getId() + " for migration.");
}
if (vpc == null) {
InvalidParameterValueException ex = new InvalidParameterValueException("Specified vpc id doesn't exist in the system");
ex.addProxyObject(String.valueOf(vpcId), "vpcId");
throw ex;
}
Vpc copyOfVpc;
long copyOfVpcId;
try {
copyOfVpc = _vpcService.createVpc(vpc.getZoneId(), vpcOfferingId, vpc.getAccountId(), vpc.getName(), vpc.getDisplayText(), vpc.getCidr(),
vpc.getNetworkDomain(), vpc.isDisplay());
copyOfVpcId = copyOfVpc.getId();
//on resume of migration the uuid will be swapped already. So the copy will have the value of the original vpcid.
_resourceTagDao.persist(new ResourceTagVO(MIGRATION, Long.toString(vpcId), vpc.getAccountId(), vpc.getDomainId(), copyOfVpcId, ResourceTag.ResourceObjectType.Vpc, null, vpc.getUuid()));
VpcVO copyVpcVO = _vpcDao.findById(copyOfVpcId);
vpc.setDisplay(false);
swapUuids(vpc, copyVpcVO);
reassignACLRulesToNewVpc(vpcId, copyOfVpcId);
reassignPublicIpsToNewVpc(vpcId, copyOfVpc);
copyVpcDetails(vpcId, copyOfVpcId);
reassignGatewayToNewVpc(vpcId, copyOfVpcId);
copyVpcResourceTagsToNewVpc(vpcId, copyOfVpcId);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Succesfully created a copy of network " + vpc.getName() + "(" + vpc.getUuid() + ") id is " + vpc.getId() + " for migration. The network copy has uuid " + copyVpcVO.getUuid() + " and id " + copyOfVpc.getId());
}
} catch (ResourceAllocationException e) {
throw new CloudRuntimeException(e.getMessage());
}
return copyOfVpcId;
}
@Override
public void startVpc(Vpc vpc) {
if (vpc.getState() != Vpc.State.Enabled) {
try {
_vpcService.startVpc(vpc.getId(), true);
} catch (ResourceUnavailableException | InsufficientCapacityException e) {
s_logger.error("Vpc can not be started. Aborting migration process");
throw new CloudRuntimeException("Vpc can not be started.", e);
}
}
}
private void copyVpcDetails(long srcVpcId, long dstVpcId) {
List<VpcDetailVO> vpcDetails = _vpcDetailsDao.listDetails(srcVpcId);
for (VpcDetailVO vpcDetail : vpcDetails) {
_vpcDetailsDao.persist(new VpcDetailVO(dstVpcId, vpcDetail.getName(), vpcDetail.getValue(), vpcDetail.isDisplay()));
}
}
/**
* Reassign the ACL rules from the original vpc to the new VPC
* @param srcVpcId
* @param dstVpcId
*/
private void reassignACLRulesToNewVpc(long srcVpcId, long dstVpcId){
List<NetworkACLVO> networkACL = _networkACLDao.listByVpcId(srcVpcId);
for (NetworkACLVO aclList : networkACL) {
aclList.setVpcId(dstVpcId);
_networkACLDao.persist(aclList);
}
}
private void reassignPublicIpsToNewVpc(long srcVpcId, Vpc dstVpc) {
List<? extends IPAddressVO> publicIps = _ipAddressDao.listByAssociatedVpc(srcVpcId, _vpcManager.isSrcNatIpRequired(dstVpc.getVpcOfferingId()) ? null : false);
for(IPAddressVO publicIp : publicIps) {
publicIp.setVpcId(dstVpc.getId());
_ipAddressDao.persist(publicIp);
}
}
private void reassignGatewayToNewVpc(long srcVpcId, long dstVpcId){
List<VpcGatewayVO> vpcGateways = _vpcGatewayDao.listByVpcId(srcVpcId);
for (VpcGatewayVO vpcGateway : vpcGateways) {
vpcGateway.setVpcId(dstVpcId);
_vpcGatewayDao.persist(vpcGateway);
}
}
private void copyVpcResourceTagsToNewVpc(long srcVpcId, long dstVpcId){
List<? extends ResourceTag> resourceTags = _resourceTagDao.listBy(srcVpcId, ResourceTag.ResourceObjectType.Vpc);
for (ResourceTag resourceTag : resourceTags) {
resourceTag.setResourceId(dstVpcId);
_resourceTagDao.persist(
new ResourceTagVO(
resourceTag.getKey(),
resourceTag.getValue(),
resourceTag.getAccountId(),
resourceTag.getDomainId(),
dstVpcId,
resourceTag.getResourceType(),
resourceTag.getCustomer(),
resourceTag.getResourceUuid()));
}
}
private void copyFirewallRulesToNewNetwork(Network srcNetwork, long dstNetworkId) {
List<FirewallRuleVO> firewallRules = _firewallDao.listByNetworkPurposeTrafficType(srcNetwork.getId(), FirewallRule.Purpose.Firewall, FirewallRule.TrafficType.Egress);
firewallRules.addAll(_firewallDao.listByNetworkPurposeTrafficType(srcNetwork.getId(), FirewallRule.Purpose.Firewall, FirewallRule.TrafficType.Ingress));
if (s_logger.isDebugEnabled()) {
s_logger.debug("Copying firewall rules from network with id " + srcNetwork.getId() + " to network with id " + dstNetworkId);
}
//Loop over all the firewall rules in the original network and copy all values to a new firewall rule
//Copy all objects that are dependant on the firewall rules
for (FirewallRuleVO originalFirewallRule : firewallRules) {
FirewallRuleVO ruleVO = new FirewallRuleVO(originalFirewallRule.getXid(),
originalFirewallRule.getSourceIpAddressId(),
originalFirewallRule.getSourcePortStart(),
originalFirewallRule.getSourcePortEnd(),
originalFirewallRule.getProtocol(),
dstNetworkId,
srcNetwork.getAccountId(),
srcNetwork.getDomainId(),
originalFirewallRule.getPurpose(),
originalFirewallRule.getSourceCidrList(),
originalFirewallRule.getDestinationCidrList(),
originalFirewallRule.getIcmpCode(),
originalFirewallRule.getIcmpType(),
originalFirewallRule.getRelated(),
originalFirewallRule.getTrafficType(),
originalFirewallRule.getType());
ruleVO = _firewallDao.persist(ruleVO);
//Firewall rule cidrs
List<FirewallRulesCidrsVO> firewallRulesCidrsVOS = _firewallRulesCidrDao.listByFirewallRuleId(originalFirewallRule.getId());
for (FirewallRulesCidrsVO firewallRulesCidrVO: firewallRulesCidrsVOS) {
_firewallRulesCidrDao.persist(new FirewallRulesCidrsVO(ruleVO.getId(), firewallRulesCidrVO.getSourceCidrList()));
}
//Firewall rules details
List<FirewallRuleDetailVO> originalFirewallRuleDetails = _firewallRuleDetailsDao.listDetails(originalFirewallRule.getId());
for (FirewallRuleDetailVO originalFirewallRuleDetail : originalFirewallRuleDetails) {
_firewallRuleDetailsDao.persist(new FirewallRuleDetailVO(ruleVO.getId(), originalFirewallRuleDetail.getName(), originalFirewallRuleDetail.getValue(), originalFirewallRuleDetail.isDisplay()));
}
}
}
private void assignRouterNicsToNewNetwork(long srcNetworkId, long dstNetworkId) {
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(srcNetworkId, VirtualRouter.Role.VIRTUAL_ROUTER);
for (DomainRouterVO domainRouter : routers) {
NicVO vrNic = _nicDao.findByNetworkIdAndType(srcNetworkId, VirtualMachine.Type.DomainRouter);
vrNic.setNetworkId(dstNetworkId);
_nicDao.update(vrNic.getId(), vrNic);
RouterNetworkVO routerNetwork = _routerNetworkDao.findByRouterAndNetwork(domainRouter.getId(), srcNetworkId);
routerNetwork.setNetworkId(dstNetworkId);
_routerNetworkDao.persist(routerNetwork);
}
}
@Override
public Network upgradeNetworkToNewNetworkOffering(long networkId, long newPhysicalNetworkId, long networkOfferingId, Long vpcId) {
s_logger.debug("upgrading network to network with new offering.");
NetworkVO network = _networksDao.findById(networkId);
NetworkOffering newOffering = _networkOfferingDao.findByIdIncludingRemoved(networkOfferingId);
long gurusImplementing = 0;
network.setBroadcastUri(null);
AccountVO networkAccount = _accountDao.findById(network.getAccountId());
DataCenterDeployment plan = new DataCenterDeployment(network.getDataCenterId(), null, null, null, null, newPhysicalNetworkId);
for (final NetworkGuru guru : _networkMgr.getNetworkGurus()) {
final Network designedNetwork = guru.design(newOffering, plan, network, networkAccount);
if (designedNetwork == null) {
continue;
}
gurusImplementing++;
if (gurusImplementing > 1) {
throw new CloudRuntimeException("Failed to migrate network to new physical network. Multiple network guru's for the same network are currently not supported.");
}
network.setTrafficType(designedNetwork.getTrafficType());
network.setMode(designedNetwork.getMode());
network.setBroadcastDomainType(designedNetwork.getBroadcastDomainType());
network.setBroadcastUri(designedNetwork.getBroadcastUri());
network.setNetworkOfferingId(designedNetwork.getNetworkOfferingId());
network.setState(designedNetwork.getState());
network.setPhysicalNetworkId(designedNetwork.getPhysicalNetworkId());
network.setRedundant(designedNetwork.isRedundant());
network.setGateway(designedNetwork.getGateway());
network.setCidr(designedNetwork.getCidr());
network.setGuruName(guru.getName());
network.setVpcId(vpcId);
}
_networksDao.update(network.getId(), network, _networkMgr.finalizeServicesAndProvidersForNetwork(_entityMgr.findById(NetworkOffering.class, networkOfferingId), newPhysicalNetworkId));
return network;
}
@Override
public void deleteCopyOfNetwork(long networkCopyId, long originalNetworkId) {
NetworkVO networkCopy = _networksDao.findById(networkCopyId);
NicVO userNic = _nicDao.findByNetworkIdAndType(networkCopyId, VirtualMachine.Type.User);
if (userNic != null) {
s_logger.error("Something went wrong while migrating nics from the old network to the new network. Failed to delete copy of network. There are still user nics present in the network.");
throw new CloudRuntimeException("Failed to delete copy of network. There are still user nics present in the network.");
}
NetworkVO originalNetwork = _networksDao.findById(originalNetworkId);
swapUuids(originalNetwork, networkCopy);
try {
if (!_networkService.deleteNetwork(networkCopy.getId(), true)) {
throw new CloudRuntimeException("Failed to delete network. Clean up not successful.");
}
} finally {
swapUuids(networkCopy, originalNetwork);
}
originalNetwork.setRelated(originalNetworkId);
_networksDao.persist(originalNetwork);
}
@Override
public void deleteCopyOfVpc(long vpcCopyId, long originalVpcId) {
VpcVO copyOfvpc = _vpcDao.findById(vpcCopyId);
VpcVO originalVpc = _vpcDao.findById(originalVpcId);
//Be sure that when we delete the vpc, it has the uuid with what it was created.
swapUuids(copyOfvpc, originalVpc);
try {
if(!_vpcService.deleteVpc(vpcCopyId)) {
throw new CloudRuntimeException("Deletion of VPC failed. Clean up was not successful.");
}
} catch (ResourceUnavailableException e) {
throw new CloudRuntimeException(e.getMessage());
} finally {
swapUuids(originalVpc, copyOfvpc);
}
_resourceTagDao.removeByResourceIdAndKey(originalVpcId, ResourceTag.ResourceObjectType.Vpc, MIGRATION);
}
private Boolean migrateNicsInDB(NicVO originalNic, Network networkInNewPhysicalNet, DataCenter dc, ReservationContext context) {
s_logger.debug("migrating nics in database.");
UserVmVO vmVO = _vmDao.findById(originalNic.getInstanceId());
VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vmVO, null, null, null, null);
NicProfile nicProfile = new NicProfile(originalNic, networkInNewPhysicalNet, null, null, null, _networkModel.isSecurityGroupSupportedInNetwork(networkInNewPhysicalNet), null);
try {
nicProfile = _networkMgr.allocateNic(nicProfile, networkInNewPhysicalNet, originalNic.isDefaultNic(), nicProfile.getDeviceId(), vmProfile).first();
} catch (InsufficientVirtualNetworkCapacityException | InsufficientAddressCapacityException e) {
throw new CloudRuntimeException("Allocation of new nicProfile failed during migration", e);
}
//Update vm_network_map table
if (vmProfile.getType() == VirtualMachine.Type.User) {
final VMNetworkMapVO vno = new VMNetworkMapVO(vmVO.getId(), networkInNewPhysicalNet.getId());
_vmNetworkMapDao.persist(vno);
}
NicVO newNic = _nicDao.findById(nicProfile.getId());
copyNicDetails(originalNic.getId(), newNic.getId());
//Update nic uuid here
moveServices(originalNic, newNic);
if (originalNic.getState() == Nic.State.Reserved) {
final VirtualMachine vm = vmProfile.getVirtualMachine();
final Host host = _hostDao.findById(vm.getHostId());
final DeployDestination dest = new DeployDestination(dc, null, null, host);
try {
nicProfile = _networkMgr.prepareNic(vmProfile, dest, context, nicProfile.getId(), networkInNewPhysicalNet);
_itMgr.replugNic(networkInNewPhysicalNet, _itMgr.toNicTO(nicProfile, host.getHypervisorType()), _itMgr.toVmTO(vmProfile), context, dest);
} catch (ResourceUnavailableException | InsufficientCapacityException e) {
throw new CloudRuntimeException("Migration of Nic failed", e);
}
}
//Mark the old nic as removed
markAsNonDefault(originalNic);
_networkMgr.removeNic(vmProfile, originalNic);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Nic is migrated successfully for vm " + vmVO + " to " + networkInNewPhysicalNet);
}
return true;
}
@Override
public void assignNicsToNewPhysicalNetwork(Network srcNetwork, Network networkInNewPhysicalNet) {
List<NicVO> nics = _nicDao.listByNetworkId(srcNetwork.getId());
final CallContext cctx = CallContext.current();
final ReservationContext context = new ReservationContextImpl(null, null, cctx.getCallingUser(), cctx.getCallingAccount());
final DataCenter dc = _entityMgr.findById(DataCenter.class, networkInNewPhysicalNet.getDataCenterId());
//For each nic in the old network check if the nic belongs to a guest vm and migrate it to the new network.
for (NicVO originalNic : nics) {
if (originalNic.getVmType() != VirtualMachine.Type.User) {
continue;
}
Transaction.execute((TransactionCallback<Boolean>)
(status) -> migrateNicsInDB(originalNic, networkInNewPhysicalNet, dc, context));
}
//Now that nics are migrated we can migrate the static nats on those nics
reapplyPublicIps(srcNetwork, networkInNewPhysicalNet);
}
private void reapplyPublicIps(Network networkInOldPhysicalNetwork, Network networkInNewPhysicalNet) {
CallContext ctx = CallContext.current();
long callerUserId = ctx.getCallingUserId();
Account caller = ctx.getCallingAccount();
AccountVO networkAccount = _accountDao.findById(networkInNewPhysicalNet.getAccountId());
List<? extends IPAddressVO> staticNatIps = _ipAddressDao.listStaticNatPublicIps(networkInOldPhysicalNetwork.getId());
List<String> providers = _networkOfferingServiceDao.listProvidersForServiceForNetworkOffering(networkInNewPhysicalNet.getNetworkOfferingId(), Network.Service.SourceNat);
boolean isSrcNatIpNeeded = providers.stream().anyMatch(provider -> provider.contains(Network.Provider.VirtualRouter.getName()));
for (IPAddressVO ipAddress : staticNatIps) {
if (!ipAddress.isSourceNat() || isSrcNatIpNeeded) {
ipAddress.setAssociatedWithNetworkId(networkInNewPhysicalNet.getId());
_ipAddressDao.persist(ipAddress);
} else {
_ipAddressManager.disassociatePublicIpAddress(ipAddress.getId(), callerUserId, caller);
}
}
_rulesMgr.applyStaticNatsForNetwork(networkInNewPhysicalNet.getId(), false, networkAccount);
}
private void copyNicDetails(long originalNicId, long dstNicId) {
List<NicDetailVO> nicDetails = _nicDetailsDao.listDetails(originalNicId);
for (NicDetailVO nicDetail : nicDetails) {
_nicDetailsDao.persist(new NicDetailVO(dstNicId, nicDetail.getName(), nicDetail.getValue(), nicDetail.isDisplay()));
}
}
private void moveServices(NicVO originalNic, NicVO newNic) {
_nicIpAliasDao.moveIpAliases(originalNic.getId(), newNic.getId());
_nicSecondaryIpDao.moveSecondaryIps(originalNic.getId(), newNic.getId());
swapUuids(originalNic, newNic);
}
private void markAsNonDefault(NicVO nic) {
nic.setDefaultNic(false);
_nicDao.persist(nic);
}
/**
* Swaps the UUID's of the given nics's
* @param oldNic
* @param newNic
*/
private void swapUuids(NicVO oldNic, NicVO newNic) {
final String realUuid = oldNic.getUuid();
final String dummyUuid = newNic.getUuid();
oldNic.setUuid(dummyUuid.replace("-", "+"));
newNic.setUuid(realUuid);
_nicDao.persist(oldNic);
_nicDao.persist(newNic);
oldNic.setUuid(dummyUuid);
_nicDao.persist(oldNic);
}
/**
* Swaps the UUID's of the given networks
* @param oldNetwork
* @param newNetwork
*/
private void swapUuids(NetworkVO oldNetwork, NetworkVO newNetwork) {
String realUuid = oldNetwork.getUuid();
String dummyUuid = newNetwork.getUuid();
oldNetwork.setUuid(dummyUuid.replace("-","+"));
newNetwork.setUuid(realUuid);
_networksDao.update(oldNetwork.getId(), oldNetwork);
_networksDao.update(newNetwork.getId(), newNetwork);
oldNetwork.setUuid(dummyUuid);
_networksDao.update(oldNetwork.getId(), oldNetwork);
}
/**
* Swaps the UUID's of the given vpcs
* @param oldVpc
* @param newVpc
*/
private void swapUuids(VpcVO oldVpc, VpcVO newVpc) {
String realUuid = oldVpc.getUuid();
String dummyUuid = newVpc.getUuid();
oldVpc.setUuid(dummyUuid.replace("-","+"));
newVpc.setUuid(realUuid);
_vpcDao.update(oldVpc.getId(), oldVpc);
_vpcDao.update(newVpc.getId(), newVpc);
oldVpc.setUuid(dummyUuid);
_vpcDao.update(oldVpc.getId(), oldVpc);
}
}