/
VncDB.java
1273 lines (1098 loc) · 48 KB
/
VncDB.java
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
package net.juniper.contrail.vcenter;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.SortedMap;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.apache.commons.net.util.SubnetUtils;
import net.juniper.contrail.api.ApiConnector;
import net.juniper.contrail.api.ApiConnectorFactory;
import net.juniper.contrail.api.ApiPropertyBase;
import net.juniper.contrail.api.ObjectReference;
import net.juniper.contrail.api.types.AddressType;
import net.juniper.contrail.api.types.AllocationPoolType;
import net.juniper.contrail.api.types.InstanceIp;
import net.juniper.contrail.api.types.FloatingIp;
import net.juniper.contrail.api.types.MacAddressesType;
import net.juniper.contrail.api.types.NetworkIpam;
import net.juniper.contrail.api.types.SecurityGroup;
import net.juniper.contrail.api.types.PolicyEntriesType;
import net.juniper.contrail.api.types.PolicyRuleType;
import net.juniper.contrail.api.types.SubnetType;
import net.juniper.contrail.api.types.VirtualMachine;
import net.juniper.contrail.api.types.VirtualMachineInterface;
import net.juniper.contrail.api.types.VirtualNetwork;
import net.juniper.contrail.api.types.VnSubnetsType;
import net.juniper.contrail.api.types.IpamSubnetType;
import net.juniper.contrail.api.types.PortType;
import net.juniper.contrail.api.types.Project;
import net.juniper.contrail.api.types.IdPermsType;
import com.google.common.base.Throwables;
import com.google.common.net.InetAddresses;
public class VncDB {
private static final Logger s_logger =
Logger.getLogger(VncDB.class);
protected static final int vrouterApiPort = 9090;
protected final String apiServerAddress;
protected final int apiServerPort;
protected final String username;
protected final String password;
protected final String tenant;
protected final String authtype;
protected final String authurl;
protected volatile ApiConnector apiConnector;
private boolean alive;
private Project vCenterProject;
private NetworkIpam vCenterIpam;
private SecurityGroup vCenterDefSecGrp;
private IdPermsType vCenterIdPerms;
Mode mode;
public static final String VNC_ROOT_DOMAIN = "default-domain";
public static final String VNC_VCENTER_PROJECT = "vCenter";
public static final String VNC_VCENTER_IPAM = "vCenter-ipam";
public static final String VNC_VCENTER_DEFAULT_SG = "default";
public static final String VNC_VCENTER_PLUGIN = "vcenter-plugin";
public static final String VNC_VCENTER_TEST_PROJECT = "vCenter-test";
public static final String VNC_VCENTER_TEST_IPAM = "vCenter-ipam-test";
public VncDB(String apiServerAddress, int apiServerPort, Mode mode) {
this.apiServerAddress = apiServerAddress;
this.apiServerPort = apiServerPort;
this.mode = mode;
if (mode == Mode.VCENTER_ONLY) {
// Create global id-perms object.
vCenterIdPerms = new IdPermsType();
vCenterIdPerms.setCreator("vcenter-plugin");
vCenterIdPerms.setEnable(true);
}
this.username = null;
this.password = null;
this.tenant = null;
this.authtype = null;
this.authurl = null;
}
public VncDB(String apiServerAddress, int apiServerPort,
String username, String password,
String tenant,
String authtype, String authurl, Mode mode) {
this.apiServerAddress = apiServerAddress;
this.apiServerPort = apiServerPort;
this.mode = mode;
if (mode == Mode.VCENTER_ONLY) {
// Create global id-perms object.
vCenterIdPerms = new IdPermsType();
vCenterIdPerms.setCreator("vcenter-plugin");
vCenterIdPerms.setEnable(true);
}
this.username = username;
this.password = password;
this.tenant = tenant;
this.authtype = authtype;
this.authurl = authurl;
}
public void setApiConnector(ApiConnector _apiConnector) {
apiConnector = _apiConnector;
}
public ApiConnector getApiConnector() {
return apiConnector;
}
public String getApiServerAddress() {
return apiServerAddress;
}
public int getApiServerPort() {
return apiServerPort;
}
public IdPermsType getVCenterIdPerms() {
return vCenterIdPerms;
}
public Project getVCenterProject() {
return vCenterProject;
}
public boolean isServerAlive() {
return alive;
}
public boolean isVncApiServerAlive() {
if (apiConnector == null) {
apiConnector = ApiConnectorFactory.build(apiServerAddress,
apiServerPort);
if (mode == Mode.VCENTER_AS_COMPUTE) {
apiConnector.credentials(username, password)
.tenantName(tenant)
.authServer(authtype, authurl);
}
if (apiConnector == null) {
s_logger.error(" failed to create ApiConnector.. retry later");
alive = false;
return false;
}
}
// Read project list as a life check
s_logger.info(" Checking if api-server is alive and kicking..");
try {
@SuppressWarnings("unchecked")
List<Project> projects = (List<Project>) apiConnector.list(Project.class, null);
if (projects == null) {
s_logger.info(" ApiServer not fully awake yet.. retry again..");
alive = false;
return false;
}
} catch (Exception e) {
alive = false;
return false;
}
alive = true;
s_logger.info(" Api-server alive. Got the pulse..");
return true;
}
public boolean Initialize() {
// Check if api-server is alive
if (isVncApiServerAlive() == false) {
return false;
}
if (mode == Mode.VCENTER_AS_COMPUTE) {
return true;
}
// create objects specific to VCENTER_ONLY mode
// Check if Vmware Project exists on VNC.
try {
vCenterProject = (Project) apiConnector.findByFQN(Project.class,
VNC_ROOT_DOMAIN + ":" + VNC_VCENTER_PROJECT);
} catch (Exception e) {
s_logger.error("Exception in find vcenter project: " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
return false;
}
if (vCenterProject == null) {
s_logger.info(" vCenter project not present, creating ");
vCenterProject = new Project();
vCenterProject.setName("vCenter");
vCenterProject.setIdPerms(vCenterIdPerms);
try {
if (!apiConnector.create(vCenterProject).isSuccess()) {
s_logger.error("Unable to create project: " + vCenterProject.getName());
return false;
}
} catch (Exception e) {
s_logger.error("Exception in creating vcenter object: " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
return false;
}
} else {
s_logger.info(" vCenter project present, continue ");
}
// Check if VMWare vCenter-ipam exists on VNC.
try {
vCenterIpam = (NetworkIpam) apiConnector.findByFQN(NetworkIpam.class,
VNC_ROOT_DOMAIN + ":" + VNC_VCENTER_PROJECT + ":" + VNC_VCENTER_IPAM);
} catch (Exception e) {
s_logger.error("Exception in find network Ipam: " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
return false;
}
if (vCenterIpam == null) {
s_logger.info(" vCenter Ipam not present, creating ...");
vCenterIpam = new NetworkIpam();
vCenterIpam.setParent(vCenterProject);
vCenterIpam.setName("vCenter-ipam");
vCenterIpam.setIdPerms(vCenterIdPerms);
try {
if (!apiConnector.create(vCenterIpam).isSuccess()) {
s_logger.error("Unable to create Ipam: " + vCenterIpam.getName());
}
} catch (Exception e) {
s_logger.error("Exception in network Ipam create: " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
return false;
}
} else {
s_logger.info(" vCenter Ipam present, continue ");
}
// Check if VMWare vCenter default security-group exists on VNC.
try {
vCenterDefSecGrp = (SecurityGroup) apiConnector.findByFQN(SecurityGroup.class,
VNC_ROOT_DOMAIN + ":" + VNC_VCENTER_PROJECT + ":" + VNC_VCENTER_DEFAULT_SG);
} catch (Exception e) {
s_logger.error("Exception in find sec group: " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
return false;
}
if (vCenterDefSecGrp == null) {
s_logger.info(" vCenter default Security-group not present, creating ...");
vCenterDefSecGrp = new SecurityGroup();
vCenterDefSecGrp.setParent(vCenterProject);
vCenterDefSecGrp.setName("default");
vCenterDefSecGrp.setIdPerms(vCenterIdPerms);
PolicyEntriesType sg_rules = new PolicyEntriesType();
PolicyRuleType ingress_rule =
new PolicyRuleType(
null,
UUID.randomUUID().toString(),
">",
"any",
Arrays.asList(new AddressType(null, null, VNC_ROOT_DOMAIN + ":" + VNC_VCENTER_PROJECT + ":" + "default", null, null)),
Arrays.asList(new PortType(0,65535)), //src_ports
null, //application
Arrays.asList(new AddressType(null, null, "local", null, null)),
Arrays.asList(new PortType(0,65535)), //dest_ports
null, // action_list
"IPv4"); // ethertype
sg_rules.addPolicyRule(ingress_rule);
PolicyRuleType egress_rule =
new PolicyRuleType(
null,
UUID.randomUUID().toString(),
">",
"any",
Arrays.asList(new AddressType(null, null, "local", null, null)),
Arrays.asList(new PortType(0,65535)), //src_ports
null, //application
Arrays.asList(new AddressType(new SubnetType("0.0.0.0", 0), null, null, null, null)),
Arrays.asList(new PortType(0,65535)), //dest_ports
null, // action_list
"IPv4"); // ethertype);
sg_rules.addPolicyRule(egress_rule);
vCenterDefSecGrp.setEntries(sg_rules);
try {
if (!apiConnector.create(vCenterDefSecGrp).isSuccess()) {
s_logger.error("Unable to create defSecGrp: " + vCenterDefSecGrp.getName());
}
} catch (Exception e) {
s_logger.error("Exception : " + e);
String stackTrace = Throwables.getStackTraceAsString(e);
s_logger.error(stackTrace);
return false;
}
} else {
s_logger.info(" vCenter default sec-group present, continue ");
}
return true;
}
public boolean TestInitialize() {
// Check if Vmware Test Project exists on VNC. If not, create one.
try {
vCenterProject = (Project) apiConnector.findByFQN(Project.class,
VNC_ROOT_DOMAIN + ":" + VNC_VCENTER_TEST_PROJECT);
} catch (IOException e) {
return false;
}
if (vCenterProject == null) {
s_logger.info(" vCenter-test project not present, creating ");
vCenterProject = new Project();
vCenterProject.setName("vCenter-test");
try {
if (!apiConnector.create(vCenterProject).isSuccess()) {
s_logger.error("Unable to create project: " + vCenterProject.getName());
return false;
}
} catch (IOException e) {
s_logger.error("Exception : " + e);
String stackTrace = Throwables.getStackTraceAsString(e);
s_logger.error(stackTrace);
return false;
}
} else {
s_logger.info(" vCenter-test project present, continue ");
}
// Check if VMWare vCenter-test ipam exists on VNC. If not, create one.
try {
vCenterIpam = (NetworkIpam) apiConnector.findByFQN(NetworkIpam.class,
VNC_ROOT_DOMAIN + ":" + VNC_VCENTER_PROJECT + ":" + VNC_VCENTER_TEST_IPAM);
} catch (IOException e) {
return false;
}
if (vCenterIpam == null) {
s_logger.info(" vCenter test Ipam not present, creating ...");
vCenterIpam = new NetworkIpam();
vCenterIpam.setParent(vCenterProject);
vCenterIpam.setName("vCenter-ipam-test");
try {
if (!apiConnector.create(vCenterIpam).isSuccess()) {
s_logger.error("Unable to create test Ipam: " + vCenterIpam.getName());
}
} catch (IOException e) {
s_logger.error("Exception in vCenterIpam create : " + e );
s_logger.error(Throwables.getStackTraceAsString(e));
return false;
}
} else {
s_logger.info(" vCenter test Ipam present, continue ");
}
// Check if VMWare vCenter default security-group exists on VNC. If not, create one.
try {
vCenterDefSecGrp = (SecurityGroup) apiConnector.findByFQN(SecurityGroup.class,
VNC_ROOT_DOMAIN + ":" + VNC_VCENTER_PROJECT + ":" + VNC_VCENTER_DEFAULT_SG);
} catch (Exception e) {
s_logger.error("Exception in default sec group find: " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
return false;
}
if (vCenterDefSecGrp == null) {
s_logger.info(" vCenter default Security-group not present, creating ...");
vCenterDefSecGrp = new SecurityGroup();
vCenterDefSecGrp.setParent(vCenterProject);
vCenterDefSecGrp.setName("default");
PolicyEntriesType sg_rules = new PolicyEntriesType();
PolicyRuleType ingress_rule =
new PolicyRuleType(
null,
UUID.randomUUID().toString(),
">",
"any",
Arrays.asList(new AddressType(null, null, VNC_ROOT_DOMAIN + ":" + VNC_VCENTER_PROJECT + ":" + "default", null, null)),
Arrays.asList(new PortType(0,65535)), //src_ports
null, //application
Arrays.asList(new AddressType(null, null, "local", null, null)),
Arrays.asList(new PortType(0,65535)), //dest_ports
null, // action_list
"IPv4"); // ethertype
sg_rules.addPolicyRule(ingress_rule);
PolicyRuleType egress_rule =
new PolicyRuleType(
null,
UUID.randomUUID().toString(),
">",
"any",
Arrays.asList(new AddressType(null, null, "local", null, null)),
Arrays.asList(new PortType(0,65535)), //src_ports
null, //application
Arrays.asList(new AddressType(new SubnetType("0.0.0.0", 0), null, null, null, null)),
Arrays.asList(new PortType(0,65535)), //dest_ports
null, // action_list
"IPv4"); // ethertype);
sg_rules.addPolicyRule(egress_rule);
vCenterDefSecGrp.setEntries(sg_rules);
try {
if (!apiConnector.create(vCenterDefSecGrp).isSuccess()) {
s_logger.error("Unable to create def sec grp: " + vCenterDefSecGrp.getName());
}
} catch (Exception e) {
s_logger.error("Exception in default sec group find: " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
return false;
}
} else {
s_logger.info(" vCenter default sec-group present, continue ");
}
return true;
}
protected static boolean doIgnoreVirtualNetwork(String name) {
// Ignore default, fabric, and link-local networks
if (name.equals("__link_local__") ||
name.equals("default-virtual-network") ||
name.equals("ip-fabric")) {
return true;
}
return false;
}
public void createVirtualNetwork(VirtualNetworkInfo vnInfo)
throws IOException
{
updateVirtualNetwork(vnInfo, true);
}
public void updateVirtualNetwork(VirtualNetworkInfo vnInfo)
throws IOException
{
updateVirtualNetwork(vnInfo, false);
}
private void updateVirtualNetwork(VirtualNetworkInfo vnInfo, boolean create)
throws IOException {
if (vnInfo == null) {
s_logger.error("Cannot create API VN: null arguments");
throw new IllegalArgumentException("Null vnInfo argument");
}
if (mode == Mode.VCENTER_AS_COMPUTE) {
VirtualNetwork vn = new VirtualNetwork();
vn.setUuid(vnInfo.getUuid());
apiConnector.read(vn);
vnInfo.apiVn = vn;
return;
}
VirtualNetwork vn = new VirtualNetwork();
vn.setUuid(vnInfo.getUuid());
vn.setName(vnInfo.getName());
vn.setDisplayName(vnInfo.getName());
vn.setIdPerms(vCenterIdPerms);
vn.setParent(vCenterProject);
vn.setExternalIpam(vnInfo.getExternalIpam());
vn.setPortSecurityEnabled(vnInfo.getPortSecurityEnabled());
VnSubnetsType subnet = getSubnet(vnInfo);
if (subnet != null) {
vn.setNetworkIpam(vCenterIpam, subnet);
}
if (create) {
s_logger.info("Creating API " + vnInfo);
apiConnector.create(vn);
} else {
s_logger.info("Updating API " + vnInfo);
apiConnector.update(vn);
}
apiConnector.read(vn);
vnInfo.apiVn = vn;
}
private VnSubnetsType getSubnet(VirtualNetworkInfo vnInfo) {
if (vnInfo.getSubnetAddress() == null || vnInfo.getSubnetMask() == null) {
return null;
}
SubnetUtils subnetUtils = new SubnetUtils(vnInfo.getSubnetAddress(), vnInfo.getSubnetMask());
String cidr = subnetUtils.getInfo().getCidrSignature();
String[] addr_pair = cidr.split("\\/");
List<AllocationPoolType> allocation_pools = null;
if (vnInfo.getIpPoolEnabled() == true && !vnInfo.getRange().isEmpty()) {
String[] pools = vnInfo.getRange().split("\\#");
if (pools.length == 2) {
allocation_pools = new ArrayList<AllocationPoolType>();
String start = (pools[0]).replace(" ","");
String num = (pools[1]).replace(" ","");
int start_ip = InetAddresses.coerceToInteger(InetAddresses.forString(start));
int end_ip = start_ip + Integer.parseInt(num) - 1;
String end = InetAddresses.toAddrString(InetAddresses.fromInteger(end_ip));
s_logger.debug("Subnet IP Range : Start:" + start + " End:" + end);
AllocationPoolType pool1 = new
AllocationPoolType(start, end);
allocation_pools.add(pool1);
}
}
// if gateway address is empty string, don't pass empty string to
// api-server. INstead set it to null so that java binding will
// drop gateway address from json content for virtual-network create
if (vnInfo.getGatewayAddress() != null) {
if (vnInfo.getGatewayAddress().trim().isEmpty())
vnInfo.setGatewayAddress(null);
}
VnSubnetsType subnet = new VnSubnetsType();
subnet.addIpamSubnets(new IpamSubnetType(
new SubnetType(addr_pair[0],
Integer.parseInt(addr_pair[1])),
vnInfo.getGatewayAddress(),
null, // dns_server_address
UUID.randomUUID().toString(), // subnet_uuid
true, // enable_dhcp
null, // dns_nameservers
allocation_pools,
true, // addr_from_start
null, // dhcp_options_list
null, // host_routes
vnInfo.getName() + "-subnet", 1));
return subnet;
}
public void deleteVirtualNetwork(VirtualNetworkInfo vnInfo)
throws IOException {
if (vnInfo == null) {
s_logger.error("Cannot delete API VN: null arguments");
throw new IllegalArgumentException("Null vnInfo argument");
}
if (mode == Mode.VCENTER_AS_COMPUTE) {
return;
}
VirtualNetwork apiVn = (VirtualNetwork) apiConnector.findById(
VirtualNetwork.class, vnInfo.getUuid());
if (apiVn == null) {
s_logger.error("Cannot delete, not found: " + vnInfo);
return;
}
deleteInstanceIps(apiVn);
deleteVirtualMachineInterfaces(apiVn);
apiConnector.delete(apiVn);
vnInfo.apiVn = null;
s_logger.info("Deleted " + vnInfo);
}
public void createVirtualMachine(VirtualMachineInfo vmInfo)
throws IOException {
if (vmInfo == null) {
s_logger.error("Cannot create API VM: null arguments");
throw new IllegalArgumentException("Null vmInfo argument");
}
if (mode == Mode.VCENTER_AS_COMPUTE) {
return;
}
String vmUuid = vmInfo.getUuid();
VirtualMachine vm = new VirtualMachine();
vmInfo.apiVm = vm;
vm.setName(vmUuid);
vm.setUuid(vmUuid);
// Encode VRouter IP address in display name
if (vmInfo.getVrouterIpAddress() != null) {
vm.setDisplayName(vmInfo.getVrouterIpAddress());
}
vm.setIdPerms(vCenterIdPerms);
apiConnector.create(vm);
s_logger.info("Created " + vm);
}
public void deleteVirtualMachine(VirtualMachineInfo vmInfo)
throws IOException {
if (vmInfo == null) {
s_logger.error("Cannot delete VM: null arguments");
throw new IllegalArgumentException("Null vmInfo argument");
}
if (mode == Mode.VCENTER_AS_COMPUTE) {
return;
}
VirtualMachine apiVm = (VirtualMachine) apiConnector.findById(
VirtualMachine.class, vmInfo.getUuid());
if (apiVm == null) {
s_logger.error("Cannot delete VM, it does not exist in the API server "
+ vmInfo);
return;
}
deleteVirtualMachineInterfaces(apiVm);
apiConnector.delete(apiVm);
vmInfo.apiVm = null;
s_logger.info("Deleted " + vmInfo);
}
public void createVirtualMachineInterface(
VirtualMachineInterfaceInfo vmiInfo)
throws IOException {
if (vmiInfo == null) {
s_logger.error("Cannot create API VMI: null arguments");
throw new IllegalArgumentException("Null vmiInfo argument");
}
if (mode == Mode.VCENTER_AS_COMPUTE) {
return;
}
VirtualMachineInfo vmInfo = vmiInfo.vmInfo;
VirtualNetworkInfo vnInfo = vmiInfo.vnInfo;
VirtualMachine vm = vmInfo.apiVm;
if (vm == null) {
vm = vmInfo.apiVm = (VirtualMachine) apiConnector.findById(
VirtualMachine.class, vmInfo.getUuid());
if (vm == null) {
s_logger.error("Cannot find " + vmInfo);
return;
}
}
VirtualNetwork network = vnInfo.apiVn;
if (network == null) {
network = (VirtualNetwork) apiConnector.findById(
VirtualNetwork.class, vnInfo.getUuid());
if (network == null) {
s_logger.error("Cannot find " + vnInfo);
return;
}
vnInfo.apiVn = network;
}
if (vmiInfo.apiVmi != null) {
return;
}
VirtualMachineInterface apiVmi = readVirtualMachineInterface(vmiInfo);
if (apiVmi != null) {
return;
}
if (mode == Mode.VCENTER_AS_COMPUTE) {
s_logger.error("VMI not found in the API server " + vmiInfo);
return;
}
// create Virtual machine interface
String vmInterfaceName = "vmi-" + vnInfo.getName()
+ "-" + vmInfo.getName();
VirtualMachineInterface vmInterface = new VirtualMachineInterface();
vmInterface.setDisplayName(vmInterfaceName);
if (vmiInfo.getUuid() == null) {
vmiInfo.setUuid(UUID.randomUUID().toString());
}
vmInterface.setUuid(vmiInfo.getUuid());
vmInterface.setName(vmiInfo.getUuid());
vmInterface.setParent(vCenterProject);
vmInterface.setSecurityGroup(vCenterDefSecGrp);
vmInterface.setPortSecurityEnabled(vmiInfo.getPortSecurityEnabled());
vmInterface.setVirtualNetwork(network);
vmInterface.addVirtualMachine(vm);
MacAddressesType macAddrType = new MacAddressesType();
macAddrType.addMacAddress(vmiInfo.getMacAddress());
vmInterface.setMacAddresses(macAddrType);
vmInterface.setIdPerms(vCenterIdPerms);
apiConnector.create(vmInterface);
apiConnector.read(vmInterface);
vmiInfo.apiVmi = vmInterface;
s_logger.debug("Created " + vmiInfo);
}
public void deleteVirtualMachineInterface(
VirtualMachineInterfaceInfo vmiInfo)
throws IOException {
if (vmiInfo == null) {
s_logger.error("Cannot delete API VMI: null arguments");
throw new IllegalArgumentException("Null vmiInfo argument");
}
if (mode == Mode.VCENTER_AS_COMPUTE) {
return;
}
VirtualMachineInterface apiVmi = (VirtualMachineInterface) apiConnector.findById(
VirtualMachineInterface.class, vmiInfo.getUuid());
if (apiVmi == null) {
s_logger.error("Cannot delete VMI, it does not exist " + vmiInfo);
return;
}
clearFloatingIp(apiVmi);
deleteInstanceIps(apiVmi);
apiConnector.delete(apiVmi);
vmiInfo.apiVmi = null;
s_logger.info("Deleted " + vmiInfo);
}
private void clearFloatingIp(VirtualMachineInterface apiVmi) throws IOException {
// Clear floating-ip associations if it exists on VMInterface
List<ObjectReference<ApiPropertyBase>> floatingIpRefs =
apiVmi.getFloatingIpBackRefs();
if ((floatingIpRefs != null) && !floatingIpRefs.isEmpty()) {
s_logger.info("floatingIp association exists for VMInterface:" + apiVmi.getUuid());
// there can be one floating-ip per VMI.
FloatingIp floatingIp = (FloatingIp)
apiConnector.findById(FloatingIp.class,
floatingIpRefs.get(0).getUuid());
// clear VMInterface back reference.
floatingIp.removeVirtualMachineInterface(apiVmi);
apiConnector.update(floatingIp);
s_logger.info("Removed floatingIp association for VMInterface:" + apiVmi.getUuid());
}
}
public void createInstanceIp(VirtualMachineInterfaceInfo vmiInfo)
throws IOException {
if (vmiInfo == null) {
s_logger.error("Cannot create API Instance IP: null arguments");
throw new IllegalArgumentException("Null vmiInfo argument");
}
if (mode == Mode.VCENTER_AS_COMPUTE) {
return;
}
VirtualNetwork network = vmiInfo.vnInfo.apiVn;
if (vmiInfo.vnInfo.getSubnetAddress() == null
|| vmiInfo.vnInfo.getSubnetMask() == null) {
return;
}
VirtualMachine vm = vmiInfo.vmInfo.apiVm;
if (vm == null) {
vm = vmiInfo.vmInfo.apiVm = (VirtualMachine) apiConnector.findById(
VirtualMachine.class, vmiInfo.vmInfo.getUuid());
if (vm == null) {
s_logger.error("Cannot find " + vmiInfo);
return;
}
}
VirtualMachineInterface vmIntf = vmiInfo.apiVmi;
String instanceIpName = "ip-" + network.getName() + "-" + vmiInfo.vmInfo.getName() ;
String instIpUuid = UUID.randomUUID().toString();
InstanceIp instanceIp = new InstanceIp();
if (vmiInfo.getIpAddress() != null) {
instanceIp.setAddress(vmiInfo.getIpAddress());
if (vmiInfo.vnInfo.getExternalIpam() == false) {
s_logger.error("Internal error address already set for DHCP");
}
}
instanceIp.setDisplayName(instanceIpName);
instanceIp.setUuid(instIpUuid);
instanceIp.setName(instIpUuid);
instanceIp.setVirtualNetwork(network);
instanceIp.setVirtualMachineInterface(vmIntf);
instanceIp.setIdPerms(vCenterIdPerms);
apiConnector.create(instanceIp);
apiConnector.read(instanceIp);
vmiInfo.apiInstanceIp = instanceIp;
vmiInfo.setIpAddress(instanceIp.getAddress());
s_logger.debug("Created instanceIP: " + instanceIp.getName() + ": " +
instanceIp.getAddress());
}
@SuppressWarnings("unchecked")
public SortedMap<String, VirtualNetworkInfo> readVirtualNetworks() {
s_logger.info("Start reading virtual networks from the API server ...");
SortedMap<String, VirtualNetworkInfo> map =
new ConcurrentSkipListMap<String, VirtualNetworkInfo>();
List<VirtualNetwork> apiObjs = null;
try {
apiObjs = (List<VirtualNetwork>)
apiConnector.list(VirtualNetwork.class, null);
} catch (Exception e) {
s_logger.error("Exception in api.list(VirtualNetworks): " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
return map;
}
for (VirtualNetwork vn : apiObjs) {
try {
apiConnector.read(vn);
// Ignore network ?
if (doIgnoreVirtualNetwork(vn.getName())) {
continue;
}
VirtualNetworkInfo vnInfo = new VirtualNetworkInfo(vn);
map.put(vnInfo.getUuid(), vnInfo);
} catch (Exception e) {
s_logger.error("Cannot read VN " + vn.getName());
s_logger.error("Exception in readVirtualNetworks");
s_logger.error(Throwables.getStackTraceAsString(e));
}
}
s_logger.info("Done reading from the API server, found " + map.size() + " Virtual Networks");
return map;
}
public VirtualNetwork findVirtualNetwork(String uuid)
throws IOException {
return (VirtualNetwork) apiConnector.findById(
VirtualNetwork.class, uuid);
}
@SuppressWarnings("unchecked")
SortedMap<String, VirtualMachineInfo> readVirtualMachines() {
s_logger.info("Start reading virtual machines from the API server ...");
List<VirtualMachine> apiVms = null;
SortedMap<String, VirtualMachineInfo> map =
new ConcurrentSkipListMap<String, VirtualMachineInfo>();
try {
apiVms = (List<VirtualMachine>)
apiConnector.list(VirtualMachine.class, null);
} catch (Exception e) {
s_logger.error("Exception in api.list(VirtualMachine): " + e);
s_logger.error(Throwables.getStackTraceAsString(e));
return map;
}
for (VirtualMachine vm : apiVms) {
try {
//TODO can we get rid of this call by reading everything with the list?
apiConnector.read(vm);
// Ignore objects where creator isn't "vcenter-plugin"
if ((mode == Mode.VCENTER_ONLY) &&
((vm.getIdPerms().getCreator() == null) ||
!(vm.getIdPerms().getCreator().equals(VNC_VCENTER_PLUGIN)))) {
continue;
}
VirtualMachineInfo vmInfo = new VirtualMachineInfo(vm);
readVirtualMachineInterfaces(vmInfo);
map.put(vmInfo.getUuid(), vmInfo);
} catch (Exception e) {
s_logger.error("Cannot sync VM " + vm.getName());
}
}
s_logger.info("Done reading from the API server, found " + map.size() + " Virtual Machines");
return map;
}
public void readVirtualMachineInterfaces(VirtualMachineInfo vmInfo)
throws IOException {
VirtualMachine vm = vmInfo.apiVm;
if (vm == null) {
vm = vmInfo.apiVm = (VirtualMachine) apiConnector.findById(
VirtualMachine.class, vmInfo.getUuid());
if (vm == null) {
s_logger.error("Cannot find " + vmInfo);
return;
}
}
List<ObjectReference<ApiPropertyBase>> vmInterfaceRefs =
vm.getVirtualMachineInterfaceBackRefs();
for (ObjectReference<ApiPropertyBase> vmInterfaceRef :
Utils.safe(vmInterfaceRefs)) {
String vmInterfaceUuid = vmInterfaceRef.getUuid();
VirtualMachineInterface vmInterface = (VirtualMachineInterface)
apiConnector.findById(VirtualMachineInterface.class,
vmInterfaceUuid);
apiConnector.read(vmInterface);
List<ObjectReference<ApiPropertyBase>> vnRefs =
vmInterface.getVirtualNetwork();
for (ObjectReference<ApiPropertyBase> vnRef : vnRefs) {
VirtualNetworkInfo vnInfo = MainDB.getVnById(vnRef.getUuid());
if (vnInfo == null) {
s_logger.info("Reading from the API server, skipping VMI in unmanaged network "
+ vnRef.getUuid());
continue;
}
VirtualMachineInterfaceInfo vmiInfo =
new VirtualMachineInterfaceInfo(vmInfo, vnInfo);
vmiInfo.apiVmi = vmInterface;
vmiInfo.setUuid(vmInterfaceUuid);
readMacAddress(vmiInfo);
readInstanceIp(vmiInfo);
vmInfo.created(vmiInfo);
}
}
}
private void readMacAddress(VirtualMachineInterfaceInfo vmiInfo)
throws IOException {
VirtualMachineInterface apiVmi = vmiInfo.apiVmi;
if (apiVmi == null) {
apiVmi = (VirtualMachineInterface) apiConnector.findById(
VirtualMachineInterface.class, vmiInfo.getUuid());
if (apiVmi == null) {
return;
}
vmiInfo.apiVmi = apiVmi;
}
List<String> macAddresses = apiVmi.getMacAddresses().getMacAddress();
if (macAddresses.size() > 0) {
vmiInfo.setMacAddress(macAddresses.get(0));
}
}
private void readInstanceIp(VirtualMachineInterfaceInfo vmiInfo)
throws IOException {
VirtualMachineInterface apiVmi =
(VirtualMachineInterface) apiConnector.findById(
VirtualMachineInterface.class, vmiInfo.getUuid());
if (apiVmi == null) {
return;
}
vmiInfo.apiVmi = apiVmi;
List<ObjectReference<ApiPropertyBase>> instanceIpRefs =
apiVmi.getInstanceIpBackRefs();
for (ObjectReference<ApiPropertyBase> instanceIpRef :
Utils.safe(instanceIpRefs)) {
InstanceIp inst = (InstanceIp)
apiConnector.findById(InstanceIp.class,
instanceIpRef.getUuid());
if (inst != null) {
List<ObjectReference<ApiPropertyBase>> vnRefs = inst.getVirtualNetwork();
for (ObjectReference<ApiPropertyBase> vnRef :
Utils.safe(vnRefs)) {
if (vnRef.getUuid().equals(vmiInfo.vnInfo.getUuid())) {
String ipAddress = inst.getAddress();
InetAddress ipAddr = InetAddress.getByName(ipAddress);
if (ipAddr instanceof Inet4Address) {
vmiInfo.setIpAddress(inst.getAddress());
vmiInfo.apiInstanceIp = inst;
// the VMI can have multiple IPv4 and IPv6 addresses,
// but we pick only the first IPv4 address
return;
}
}
}
}
}