forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualRouterElement.java
More file actions
1230 lines (1062 loc) · 57.7 KB
/
VirtualRouterElement.java
File metadata and controls
1230 lines (1062 loc) · 57.7 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
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
// 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.element;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ejb.Local;
import javax.inject.Inject;
import org.apache.cloudstack.api.command.admin.router.ConfigureOvsElementCmd;
import org.apache.cloudstack.api.command.admin.router.ConfigureVirtualRouterElementCmd;
import org.apache.cloudstack.api.command.admin.router.CreateVirtualRouterElementCmd;
import org.apache.cloudstack.api.command.admin.router.ListOvsElementsCmd;
import org.apache.cloudstack.api.command.admin.router.ListVirtualRouterElementsCmd;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.network.topology.NetworkTopology;
import org.apache.cloudstack.network.topology.NetworkTopologyContext;
import org.apache.log4j.Logger;
import org.cloud.network.router.deployment.RouterDeploymentDefinition;
import org.cloud.network.router.deployment.RouterDeploymentDefinitionBuilder;
import com.cloud.agent.api.to.LoadBalancerTO;
import com.cloud.configuration.ConfigurationManager;
import com.cloud.dc.DataCenter;
import com.cloud.dc.DataCenter.NetworkType;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.deploy.DeployDestination;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.network.Network;
import com.cloud.network.Network.Capability;
import com.cloud.network.Network.Provider;
import com.cloud.network.Network.Service;
import com.cloud.network.NetworkMigrationResponder;
import com.cloud.network.NetworkModel;
import com.cloud.network.Networks;
import com.cloud.network.Networks.TrafficType;
import com.cloud.network.OvsProvider;
import com.cloud.network.PhysicalNetworkServiceProvider;
import com.cloud.network.PublicIpAddress;
import com.cloud.network.RemoteAccessVpn;
import com.cloud.network.VirtualRouterProvider;
import com.cloud.network.VirtualRouterProvider.Type;
import com.cloud.network.VpnUser;
import com.cloud.network.as.AutoScaleCounter;
import com.cloud.network.as.AutoScaleCounter.AutoScaleCounterType;
import com.cloud.network.dao.IPAddressDao;
import com.cloud.network.dao.LoadBalancerDao;
import com.cloud.network.dao.NetworkDao;
import com.cloud.network.dao.OvsProviderDao;
import com.cloud.network.dao.VirtualRouterProviderDao;
import com.cloud.network.lb.LoadBalancingRule;
import com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy;
import com.cloud.network.lb.LoadBalancingRulesManager;
import com.cloud.network.router.VirtualRouter;
import com.cloud.network.router.VirtualRouter.Role;
import com.cloud.network.router.VpcVirtualNetworkApplianceManager;
import com.cloud.network.rules.FirewallRule;
import com.cloud.network.rules.LbStickinessMethod;
import com.cloud.network.rules.LbStickinessMethod.StickinessMethodType;
import com.cloud.network.rules.LoadBalancerContainer;
import com.cloud.network.rules.PortForwardingRule;
import com.cloud.network.rules.RulesManager;
import com.cloud.network.rules.StaticNat;
import com.cloud.offering.NetworkOffering;
import com.cloud.offerings.NetworkOfferingVO;
import com.cloud.offerings.dao.NetworkOfferingDao;
import com.cloud.user.Account;
import com.cloud.user.AccountManager;
import com.cloud.utils.Pair;
import com.cloud.utils.component.AdapterBase;
import com.cloud.utils.crypt.DBEncryptionUtil;
import com.cloud.utils.db.QueryBuilder;
import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.net.NetUtils;
import com.cloud.vm.DomainRouterVO;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.UserVmManager;
import com.cloud.vm.UserVmVO;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.VirtualMachineProfile;
import com.cloud.vm.dao.DomainRouterDao;
import com.cloud.vm.dao.UserVmDao;
import com.google.gson.Gson;
@Local(value = { NetworkElement.class, FirewallServiceProvider.class, DhcpServiceProvider.class, UserDataServiceProvider.class, StaticNatServiceProvider.class,
LoadBalancingServiceProvider.class, PortForwardingServiceProvider.class, IpDeployer.class, RemoteAccessVPNServiceProvider.class, NetworkMigrationResponder.class })
public class VirtualRouterElement extends AdapterBase implements VirtualRouterElementService, DhcpServiceProvider, UserDataServiceProvider, SourceNatServiceProvider,
StaticNatServiceProvider, FirewallServiceProvider, LoadBalancingServiceProvider, PortForwardingServiceProvider, RemoteAccessVPNServiceProvider, IpDeployer,
NetworkMigrationResponder, AggregatedCommandExecutor {
private static final Logger s_logger = Logger.getLogger(VirtualRouterElement.class);
public static final AutoScaleCounterType AutoScaleCounterCpu = new AutoScaleCounterType("cpu");
public static final AutoScaleCounterType AutoScaleCounterMemory = new AutoScaleCounterType("memory");
protected static final Map<Service, Map<Capability, String>> capabilities = setCapabilities();
@Inject
NetworkDao _networksDao;
@Inject
NetworkModel _networkMdl;
@Inject
LoadBalancingRulesManager _lbMgr;
@Inject
NetworkOfferingDao _networkOfferingDao;
@Inject
VpcVirtualNetworkApplianceManager _routerMgr;
@Inject
ConfigurationManager _configMgr;
@Inject
RulesManager _rulesMgr;
@Inject
UserVmManager _userVmMgr;
@Inject
UserVmDao _userVmDao;
@Inject
DomainRouterDao _routerDao;
@Inject
LoadBalancerDao _lbDao;
@Inject
HostDao _hostDao;
@Inject
AccountManager _accountMgr;
@Inject
ConfigurationDao _configDao;
@Inject
VirtualRouterProviderDao _vrProviderDao;
@Inject
OvsProviderDao _ovsProviderDao;
@Inject
IPAddressDao _ipAddressDao;
@Inject
DataCenterDao _dcDao;
@Inject
NetworkTopologyContext networkTopologyContext;
@Inject
protected RouterDeploymentDefinitionBuilder routerDeploymentDefinitionBuilder;
protected boolean canHandle(final Network network, final Service service) {
final Long physicalNetworkId = _networkMdl.getPhysicalNetworkId(network);
if (physicalNetworkId == null) {
return false;
}
if (network.getVpcId() != null) {
return false;
}
if (!_networkMdl.isProviderEnabledInPhysicalNetwork(physicalNetworkId, Network.Provider.VirtualRouter.getName())) {
return false;
}
if (service == null) {
if (!_networkMdl.isProviderForNetwork(getProvider(), network.getId())) {
s_logger.trace("Element " + getProvider().getName() + " is not a provider for the network " + network);
return false;
}
} else {
if (!_networkMdl.isProviderSupportServiceInNetwork(network.getId(), service, getProvider())) {
s_logger.trace("Element " + getProvider().getName() + " doesn't support service " + service.getName() + " in the network " + network);
return false;
}
}
return true;
}
@Override
public boolean implement(final Network network, final NetworkOffering offering, final DeployDestination dest, final ReservationContext context)
throws ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException {
if (offering.isSystemOnly()) {
return false;
}
final Map<VirtualMachineProfile.Param, Object> params = new HashMap<VirtualMachineProfile.Param, Object>(1);
params.put(VirtualMachineProfile.Param.ReProgramGuestNetworks, true);
final RouterDeploymentDefinition routerDeploymentDefinition =
routerDeploymentDefinitionBuilder.create()
.setGuestNetwork(network)
.setDeployDestination(dest)
.setAccountOwner(_accountMgr.getAccount(network.getAccountId()))
.setParams(params)
.build();
final List<DomainRouterVO> routers = routerDeploymentDefinition.deployVirtualRouter();
int routerCounts = 1;
if (offering.getRedundantRouter()) {
routerCounts = 2;
}
if (routers == null || routers.size() < routerCounts) {
throw new ResourceUnavailableException("Can't find all necessary running routers!", DataCenter.class, network.getDataCenterId());
}
return true;
}
@Override
public boolean prepare(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final DeployDestination dest, final ReservationContext context)
throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
if (vm.getType() != VirtualMachine.Type.User || vm.getHypervisorType() == HypervisorType.BareMetal) {
return false;
}
if (!canHandle(network, null)) {
return false;
}
final NetworkOfferingVO offering = _networkOfferingDao.findById(network.getNetworkOfferingId());
if (offering.isSystemOnly()) {
return false;
}
if (!_networkMdl.isProviderEnabledInPhysicalNetwork(_networkMdl.getPhysicalNetworkId(network), getProvider().getName())) {
return false;
}
final RouterDeploymentDefinition routerDeploymentDefinition =
routerDeploymentDefinitionBuilder.create()
.setGuestNetwork(network)
.setDeployDestination(dest)
.setAccountOwner(_accountMgr.getAccount(network.getAccountId()))
.setParams(vm.getParameters())
.build();
final List<DomainRouterVO> routers = routerDeploymentDefinition.deployVirtualRouter();
if (routers == null || routers.size() == 0) {
throw new ResourceUnavailableException("Can't find at least one running router!", DataCenter.class, network.getDataCenterId());
}
return true;
}
@Override
public boolean applyFWRules(final Network network, final List<? extends FirewallRule> rules) throws ResourceUnavailableException {
if (canHandle(network, Service.Firewall)) {
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
s_logger.debug("Virtual router elemnt doesn't need to apply firewall rules on the backend; virtual " + "router doesn't exist in the network " + network.getId());
return true;
}
if (rules != null && rules.size() == 1) {
// for VR no need to add default egress rule to ALLOW traffic
//The default allow rule is added from the router defalut iptables rules iptables-router
if (rules.get(0).getTrafficType() == FirewallRule.TrafficType.Egress && rules.get(0).getType() == FirewallRule.FirewallRuleType.System
&& _networkMdl.getNetworkEgressDefaultPolicy(network.getId())) {
return true;
}
}
final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
if (!networkTopology.applyFirewallRules(network, rules, routers)) {
throw new CloudRuntimeException("Failed to apply firewall rules in network " + network.getId());
} else {
return true;
}
} else {
return true;
}
}
/*
* This function detects numbers like 12 ,32h ,42m .. etc,. 1) plain number
* like 12 2) time or tablesize like 12h, 34m, 45k, 54m , here last
* character is non-digit but from known characters .
*/
private static boolean containsOnlyNumbers(final String str, final String endChar) {
if (str == null) {
return false;
}
String number = str;
if (endChar != null) {
boolean matchedEndChar = false;
if (str.length() < 2) {
return false; // at least one numeric and one char. example:
}
// 3h
final char strEnd = str.toCharArray()[str.length() - 1];
for (final char c : endChar.toCharArray()) {
if (strEnd == c) {
number = str.substring(0, str.length() - 1);
matchedEndChar = true;
break;
}
}
if (!matchedEndChar) {
return false;
}
}
try {
Integer.parseInt(number);
} catch (final NumberFormatException e) {
return false;
}
return true;
}
public static boolean validateHAProxyLBRule(final LoadBalancingRule rule) {
final String timeEndChar = "dhms";
if (rule.getSourcePortStart() == NetUtils.HAPROXY_STATS_PORT) {
s_logger.debug("Can't create LB on port 8081, haproxy is listening for LB stats on this port");
return false;
}
for (final LbStickinessPolicy stickinessPolicy : rule.getStickinessPolicies()) {
final List<Pair<String, String>> paramsList = stickinessPolicy.getParams();
if (StickinessMethodType.LBCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
} else if (StickinessMethodType.SourceBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
String tablesize = "200k"; // optional
String expire = "30m"; // optional
/* overwrite default values with the stick parameters */
for (final Pair<String, String> paramKV : paramsList) {
final String key = paramKV.first();
final String value = paramKV.second();
if ("tablesize".equalsIgnoreCase(key)) {
tablesize = value;
}
if ("expire".equalsIgnoreCase(key)) {
expire = value;
}
}
if (expire != null && !containsOnlyNumbers(expire, timeEndChar)) {
throw new InvalidParameterValueException("Failed LB in validation rule id: " + rule.getId() + " Cause: expire is not in timeformat: " + expire);
}
if (tablesize != null && !containsOnlyNumbers(tablesize, "kmg")) {
throw new InvalidParameterValueException("Failed LB in validation rule id: " + rule.getId() + " Cause: tablesize is not in size format: " + tablesize);
}
} else if (StickinessMethodType.AppCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) {
String length = null; // optional
String holdTime = null; // optional
for (final Pair<String, String> paramKV : paramsList) {
final String key = paramKV.first();
final String value = paramKV.second();
if ("length".equalsIgnoreCase(key)) {
length = value;
}
if ("holdtime".equalsIgnoreCase(key)) {
holdTime = value;
}
}
if (length != null && !containsOnlyNumbers(length, null)) {
throw new InvalidParameterValueException("Failed LB in validation rule id: " + rule.getId() + " Cause: length is not a number: " + length);
}
if (holdTime != null && !containsOnlyNumbers(holdTime, timeEndChar) && !containsOnlyNumbers(holdTime, null)) {
throw new InvalidParameterValueException("Failed LB in validation rule id: " + rule.getId() + " Cause: holdtime is not in timeformat: " + holdTime);
}
}
}
return true;
}
@Override
public boolean validateLBRule(final Network network, final LoadBalancingRule rule) {
final List<LoadBalancingRule> rules = new ArrayList<LoadBalancingRule>();
rules.add(rule);
if (canHandle(network, Service.Lb) && canHandleLbRules(rules)) {
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
return true;
}
return validateHAProxyLBRule(rule);
}
return true;
}
@Override
public boolean applyLBRules(final Network network, final List<LoadBalancingRule> rules) throws ResourceUnavailableException {
if (canHandle(network, Service.Lb)) {
if (!canHandleLbRules(rules)) {
return false;
}
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
s_logger.debug("Virtual router elemnt doesn't need to apply lb rules on the backend; virtual " + "router doesn't exist in the network " + network.getId());
return true;
}
final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
if (!networkTopology.applyLoadBalancingRules(network, rules, routers)) {
throw new CloudRuntimeException("Failed to apply load balancing rules in network " + network.getId());
} else {
return true;
}
} else {
return false;
}
}
@Override
public String[] applyVpnUsers(final RemoteAccessVpn vpn, final List<? extends VpnUser> users) throws ResourceUnavailableException {
if (vpn.getNetworkId() == null) {
return null;
}
final Network network = _networksDao.findById(vpn.getNetworkId());
if (canHandle(network, Service.Vpn)) {
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
s_logger.debug("Virtual router elemnt doesn't need to apply vpn users on the backend; virtual router" + " doesn't exist in the network " + network.getId());
return null;
}
final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
return networkTopology.applyVpnUsers(network, users, routers);
} else {
s_logger.debug("Element " + getName() + " doesn't handle applyVpnUsers command");
return null;
}
}
@Override
public boolean startVpn(final RemoteAccessVpn vpn) throws ResourceUnavailableException {
if (vpn.getNetworkId() == null) {
return false;
}
final Network network = _networksDao.findById(vpn.getNetworkId());
if (canHandle(network, Service.Vpn)) {
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
s_logger.debug("Virtual router elemnt doesn't need stop vpn on the backend; virtual router doesn't" + " exist in the network " + network.getId());
return true;
}
return _routerMgr.startRemoteAccessVpn(network, vpn, routers);
} else {
s_logger.debug("Element " + getName() + " doesn't handle createVpn command");
return false;
}
}
@Override
public boolean stopVpn(final RemoteAccessVpn vpn) throws ResourceUnavailableException {
if (vpn.getNetworkId() == null) {
return false;
}
final Network network = _networksDao.findById(vpn.getNetworkId());
if (canHandle(network, Service.Vpn)) {
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
s_logger.debug("Virtual router elemnt doesn't need stop vpn on the backend; virtual router doesn't " + "exist in the network " + network.getId());
return true;
}
return _routerMgr.deleteRemoteAccessVpn(network, vpn, routers);
} else {
s_logger.debug("Element " + getName() + " doesn't handle removeVpn command");
return false;
}
}
@Override
public boolean applyIps(final Network network, final List<? extends PublicIpAddress> ipAddress, final Set<Service> services) throws ResourceUnavailableException {
boolean canHandle = true;
for (final Service service : services) {
if (!canHandle(network, service)) {
canHandle = false;
break;
}
}
if (canHandle) {
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
s_logger.debug("Virtual router elemnt doesn't need to associate ip addresses on the backend; virtual " + "router doesn't exist in the network " + network.getId());
return true;
}
final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
return networkTopology.associatePublicIP(network, ipAddress, routers);
} else {
return false;
}
}
@Override
public Provider getProvider() {
return Provider.VirtualRouter;
}
@Override
public Map<Service, Map<Capability, String>> getCapabilities() {
return capabilities;
}
public static String getHAProxyStickinessCapability() {
LbStickinessMethod method;
final List<LbStickinessMethod> methodList = new ArrayList<LbStickinessMethod>(1);
method = new LbStickinessMethod(StickinessMethodType.LBCookieBased, "This is loadbalancer cookie based stickiness method.");
method.addParam("cookie-name", false, "Cookie name passed in http header by the LB to the client.", false);
method.addParam("mode", false, "Valid values: insert, rewrite, prefix. Default value: insert. In the insert mode cookie will be created"
+ " by the LB. In other modes, cookie will be created by the server and LB modifies it.", false);
method.addParam("nocache", false, "This option is recommended in conjunction with the insert mode when there is a cache between the client"
+ " and HAProxy, as it ensures that a cacheable response will be tagged non-cacheable if a cookie needs "
+ "to be inserted. This is important because if all persistence cookies are added on a cacheable home page"
+ " for instance, then all customers will then fetch the page from an outer cache and will all share the "
+ "same persistence cookie, leading to one server receiving much more traffic than others. See also the " + "insert and postonly options. ", true);
method.addParam("indirect", false, "When this option is specified in insert mode, cookies will only be added when the server was not reached"
+ " after a direct access, which means that only when a server is elected after applying a load-balancing algorithm,"
+ " or after a redispatch, then the cookie will be inserted. If the client has all the required information"
+ " to connect to the same server next time, no further cookie will be inserted. In all cases, when the "
+ "indirect option is used in insert mode, the cookie is always removed from the requests transmitted to "
+ "the server. The persistence mechanism then becomes totally transparent from the application point of view.", true);
method.addParam("postonly", false, "This option ensures that cookie insertion will only be performed on responses to POST requests. It is an"
+ " alternative to the nocache option, because POST responses are not cacheable, so this ensures that the "
+ "persistence cookie will never get cached.Since most sites do not need any sort of persistence before the"
+ " first POST which generally is a login request, this is a very efficient method to optimize caching "
+ "without risking to find a persistence cookie in the cache. See also the insert and nocache options.", true);
method.addParam("domain", false, "This option allows to specify the domain at which a cookie is inserted. It requires exactly one parameter:"
+ " a valid domain name. If the domain begins with a dot, the browser is allowed to use it for any host "
+ "ending with that name. It is also possible to specify several domain names by invoking this option multiple"
+ " times. Some browsers might have small limits on the number of domains, so be careful when doing that. "
+ "For the record, sending 10 domains to MSIE 6 or Firefox 2 works as expected.", false);
methodList.add(method);
method = new LbStickinessMethod(StickinessMethodType.AppCookieBased,
"This is App session based sticky method. Define session stickiness on an existing application cookie. " + "It can be used only for a specific http traffic");
method.addParam("cookie-name", false, "This is the name of the cookie used by the application and which LB will "
+ "have to learn for each new session. Default value: Auto geneared based on ip", false);
method.addParam("length", false, "This is the max number of characters that will be memorized and checked in " + "each cookie value. Default value:52", false);
method.addParam("holdtime", false, "This is the time after which the cookie will be removed from memory if unused. The value should be in "
+ "the format Example : 20s or 30m or 4h or 5d . only seconds(s), minutes(m) hours(h) and days(d) are valid,"
+ " cannot use th combinations like 20h30m. Default value:3h ", false);
method.addParam(
"request-learn",
false,
"If this option is specified, then haproxy will be able to learn the cookie found in the request in case the server does not specify any in response. This is typically what happens with PHPSESSID cookies, or when haproxy's session expires before the application's session and the correct server is selected. It is recommended to specify this option to improve reliability",
true);
method.addParam(
"prefix",
false,
"When this option is specified, haproxy will match on the cookie prefix (or URL parameter prefix). "
+ "The appsession value is the data following this prefix. Example : appsession ASPSESSIONID len 64 timeout 3h prefix This will match the cookie ASPSESSIONIDXXXX=XXXXX, the appsession value will be XXXX=XXXXX.",
true);
method.addParam("mode", false, "This option allows to change the URL parser mode. 2 modes are currently supported : - path-parameters "
+ ": The parser looks for the appsession in the path parameters part (each parameter is separated by a semi-colon), "
+ "which is convenient for JSESSIONID for example.This is the default mode if the option is not set. - query-string :"
+ " In this mode, the parser will look for the appsession in the query string.", false);
methodList.add(method);
method = new LbStickinessMethod(StickinessMethodType.SourceBased, "This is source based Stickiness method, " + "it can be used for any type of protocol.");
method.addParam("tablesize", false, "Size of table to store source ip addresses. example: tablesize=200k or 300m" + " or 400g. Default value:200k", false);
method.addParam("expire", false, "Entry in source ip table will expire after expire duration. units can be s,m,h,d ."
+ " example: expire=30m 20s 50h 4d. Default value:3h", false);
methodList.add(method);
final Gson gson = new Gson();
final String capability = gson.toJson(methodList);
return capability;
}
private static Map<Service, Map<Capability, String>> setCapabilities() {
final Map<Service, Map<Capability, String>> capabilities = new HashMap<Service, Map<Capability, String>>();
// Set capabilities for LB service
final Map<Capability, String> lbCapabilities = new HashMap<Capability, String>();
lbCapabilities.put(Capability.SupportedLBAlgorithms, "roundrobin,leastconn,source");
lbCapabilities.put(Capability.SupportedLBIsolation, "dedicated");
lbCapabilities.put(Capability.SupportedProtocols, "tcp, udp");
lbCapabilities.put(Capability.SupportedStickinessMethods, getHAProxyStickinessCapability());
lbCapabilities.put(Capability.LbSchemes, LoadBalancerContainer.Scheme.Public.toString());
// specifies that LB rules can support autoscaling and the list of
// counters it supports
AutoScaleCounter counter;
final List<AutoScaleCounter> counterList = new ArrayList<AutoScaleCounter>();
counter = new AutoScaleCounter(AutoScaleCounterCpu);
counterList.add(counter);
counter = new AutoScaleCounter(AutoScaleCounterMemory);
counterList.add(counter);
final Gson gson = new Gson();
final String autoScaleCounterList = gson.toJson(counterList);
lbCapabilities.put(Capability.AutoScaleCounters, autoScaleCounterList);
capabilities.put(Service.Lb, lbCapabilities);
// Set capabilities for Firewall service
final Map<Capability, String> firewallCapabilities = new HashMap<Capability, String>();
firewallCapabilities.put(Capability.TrafficStatistics, "per public ip");
firewallCapabilities.put(Capability.SupportedProtocols, "tcp,udp,icmp");
firewallCapabilities.put(Capability.SupportedEgressProtocols, "tcp,udp,icmp, all");
firewallCapabilities.put(Capability.SupportedTrafficDirection, "ingress, egress");
firewallCapabilities.put(Capability.MultipleIps, "true");
capabilities.put(Service.Firewall, firewallCapabilities);
// Set capabilities for vpn
final Map<Capability, String> vpnCapabilities = new HashMap<Capability, String>();
vpnCapabilities.put(Capability.SupportedVpnProtocols, "pptp,l2tp,ipsec");
vpnCapabilities.put(Capability.VpnTypes, "removeaccessvpn");
capabilities.put(Service.Vpn, vpnCapabilities);
final Map<Capability, String> dnsCapabilities = new HashMap<Capability, String>();
dnsCapabilities.put(Capability.AllowDnsSuffixModification, "true");
capabilities.put(Service.Dns, dnsCapabilities);
capabilities.put(Service.UserData, null);
final Map<Capability, String> dhcpCapabilities = new HashMap<Capability, String>();
dhcpCapabilities.put(Capability.DhcpAccrossMultipleSubnets, "true");
capabilities.put(Service.Dhcp, dhcpCapabilities);
capabilities.put(Service.Gateway, null);
final Map<Capability, String> sourceNatCapabilities = new HashMap<Capability, String>();
sourceNatCapabilities.put(Capability.SupportedSourceNatTypes, "peraccount");
sourceNatCapabilities.put(Capability.RedundantRouter, "true");
capabilities.put(Service.SourceNat, sourceNatCapabilities);
capabilities.put(Service.StaticNat, null);
capabilities.put(Service.PortForwarding, null);
return capabilities;
}
@Override
public boolean applyStaticNats(final Network network, final List<? extends StaticNat> rules) throws ResourceUnavailableException {
if (canHandle(network, Service.StaticNat)) {
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
s_logger.debug("Virtual router elemnt doesn't need to apply static nat on the backend; virtual " + "router doesn't exist in the network " + network.getId());
return true;
}
final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
return networkTopology.applyStaticNats(network, rules, routers);
} else {
return true;
}
}
@Override
public boolean shutdown(final Network network, final ReservationContext context, final boolean cleanup) throws ConcurrentOperationException, ResourceUnavailableException {
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
return true;
}
boolean result = true;
for (final DomainRouterVO router : routers) {
result = result && _routerMgr.stop(router, false, context.getCaller(), context.getAccount()) != null;
if (cleanup) {
if (!result) {
s_logger.warn("Failed to stop virtual router element " + router + ", but would try to process clean up anyway.");
}
result = _routerMgr.destroyRouter(router.getId(), context.getAccount(), context.getCaller().getId()) != null;
if (!result) {
s_logger.warn("Failed to clean up virtual router element " + router);
}
}
}
return result;
}
@Override
public boolean destroy(final Network config, final ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException {
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(config.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
return true;
}
boolean result = true;
// NOTE that we need to pass caller account to destroyRouter, otherwise
// it will fail permission check there. Context passed in from
// deleteNetwork is the network account,
// not caller account
final Account callerAccount = _accountMgr.getAccount(context.getCaller().getAccountId());
for (final DomainRouterVO router : routers) {
result = result && _routerMgr.destroyRouter(router.getId(), callerAccount, context.getCaller().getId()) != null;
}
return result;
}
@Override
public boolean savePassword(final Network network, final NicProfile nic, final VirtualMachineProfile vm) throws ResourceUnavailableException {
if (!canHandle(network, null)) {
return false;
}
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
s_logger.debug("Can't find virtual router element in network " + network.getId());
return true;
}
final VirtualMachineProfile uservm = vm;
final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
// If any router is running then send save password command otherwise
// save the password in DB
for (final VirtualRouter router : routers) {
if (router.getState() == State.Running) {
return networkTopology.savePasswordToRouter(network, nic, uservm, routers);
}
}
final String password = (String) uservm.getParameter(VirtualMachineProfile.Param.VmPassword);
final String password_encrypted = DBEncryptionUtil.encrypt(password);
final UserVmVO userVmVO = _userVmDao.findById(vm.getId());
_userVmDao.loadDetails(userVmVO);
userVmVO.setDetail("password", password_encrypted);
_userVmDao.saveDetails(userVmVO);
userVmVO.setUpdateParameters(true);
_userVmDao.update(userVmVO.getId(), userVmVO);
return true;
}
@Override
public boolean saveSSHKey(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final String sshPublicKey) throws ResourceUnavailableException {
if (!canHandle(network, null)) {
return false;
}
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
s_logger.debug("Can't find virtual router element in network " + network.getId());
return true;
}
final VirtualMachineProfile uservm = vm;
final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
return networkTopology.saveSSHPublicKeyToRouter(network, nic, uservm, routers, sshPublicKey);
}
@Override
public boolean saveUserData(final Network network, final NicProfile nic, final VirtualMachineProfile vm) throws ResourceUnavailableException {
if (!canHandle(network, null)) {
return false;
}
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
s_logger.debug("Can't find virtual router element in network " + network.getId());
return true;
}
final VirtualMachineProfile uservm = vm;
final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
return networkTopology.saveUserDataToRouter(network, nic, uservm, routers);
}
@Override
public List<Class<?>> getCommands() {
final List<Class<?>> cmdList = new ArrayList<Class<?>>();
cmdList.add(CreateVirtualRouterElementCmd.class);
cmdList.add(ConfigureVirtualRouterElementCmd.class);
cmdList.add(ListVirtualRouterElementsCmd.class);
return cmdList;
}
@Override
public VirtualRouterProvider configure(final ConfigureVirtualRouterElementCmd cmd) {
final VirtualRouterProviderVO element = _vrProviderDao.findById(cmd.getId());
if (element == null || !(element.getType() == Type.VirtualRouter || element.getType() == Type.VPCVirtualRouter)) {
s_logger.debug("Can't find Virtual Router element with network service provider id " + cmd.getId());
return null;
}
element.setEnabled(cmd.getEnabled());
_vrProviderDao.persist(element);
return element;
}
@Override
public OvsProvider configure(final ConfigureOvsElementCmd cmd) {
final OvsProviderVO element = _ovsProviderDao.findById(cmd.getId());
if (element == null) {
s_logger.debug("Can't find Ovs element with network service provider id " + cmd.getId());
return null;
}
element.setEnabled(cmd.getEnabled());
_ovsProviderDao.persist(element);
return element;
}
@Override
public VirtualRouterProvider addElement(final Long nspId, final Type providerType) {
if (!(providerType == Type.VirtualRouter || providerType == Type.VPCVirtualRouter)) {
throw new InvalidParameterValueException("Element " + getName() + " supports only providerTypes: " + Type.VirtualRouter.toString() + " and " + Type.VPCVirtualRouter);
}
VirtualRouterProviderVO element = _vrProviderDao.findByNspIdAndType(nspId, providerType);
if (element != null) {
s_logger.debug("There is already a virtual router element with service provider id " + nspId);
return null;
}
element = new VirtualRouterProviderVO(nspId, providerType);
_vrProviderDao.persist(element);
return element;
}
@Override
public boolean applyPFRules(final Network network, final List<PortForwardingRule> rules) throws ResourceUnavailableException {
if (canHandle(network, Service.PortForwarding)) {
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.isEmpty()) {
s_logger.debug("Virtual router elemnt doesn't need to apply firewall rules on the backend; virtual " + "router doesn't exist in the network " + network.getId());
return true;
}
final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
if (!networkTopology.applyFirewallRules(network, rules, routers)) {
throw new CloudRuntimeException("Failed to apply firewall rules in network " + network.getId());
} else {
return true;
}
} else {
return true;
}
}
@Override
public boolean isReady(final PhysicalNetworkServiceProvider provider) {
final VirtualRouterProviderVO element = _vrProviderDao.findByNspIdAndType(provider.getId(), getVirtualRouterProvider());
if (element == null) {
return false;
}
return element.isEnabled();
}
@Override
public boolean shutdownProviderInstances(final PhysicalNetworkServiceProvider provider, final ReservationContext context) throws ConcurrentOperationException,
ResourceUnavailableException {
final VirtualRouterProviderVO element = _vrProviderDao.findByNspIdAndType(provider.getId(), getVirtualRouterProvider());
if (element == null) {
return true;
}
// Find domain routers
final long elementId = element.getId();
final List<DomainRouterVO> routers = _routerDao.listByElementId(elementId);
boolean result = true;
for (final DomainRouterVO router : routers) {
result = result && _routerMgr.destroyRouter(router.getId(), context.getAccount(), context.getCaller().getId()) != null;
}
_vrProviderDao.remove(elementId);
return result;
}
@Override
public boolean canEnableIndividualServices() {
return true;
}
public Long getIdByNspId(final Long nspId) {
final VirtualRouterProviderVO vr = _vrProviderDao.findByNspIdAndType(nspId, Type.VirtualRouter);
return vr.getId();
}
@Override
public VirtualRouterProvider getCreatedElement(final long id) {
final VirtualRouterProvider provider = _vrProviderDao.findById(id);
if (!(provider.getType() == Type.VirtualRouter || provider.getType() == Type.VPCVirtualRouter)) {
throw new InvalidParameterValueException("Unable to find provider by id");
}
return provider;
}
@Override
public boolean release(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final ReservationContext context) throws ConcurrentOperationException,
ResourceUnavailableException {
return true;
}
@Override
public boolean configDhcpSupportForSubnet(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final DeployDestination dest,
final ReservationContext context) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
if (canHandle(network, Service.Dhcp)) {
if (vm.getType() != VirtualMachine.Type.User) {
return false;
}
final VirtualMachineProfile uservm = vm;
final List<DomainRouterVO> routers = getRouters(network, dest);
if (routers == null || routers.size() == 0) {
throw new ResourceUnavailableException("Can't find at least one router!", DataCenter.class, network.getDataCenterId());
}
final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
return networkTopology.configDhcpForSubnet(network, nic, uservm, dest, routers);
}
return false;
}
@Override
public boolean removeDhcpSupportForSubnet(final Network network) throws ResourceUnavailableException {
if (canHandle(network, Service.Dhcp)) {
final List<DomainRouterVO> routers = _routerDao.listByNetworkAndRole(network.getId(), Role.VIRTUAL_ROUTER);
if (routers == null || routers.size() == 0) {
throw new ResourceUnavailableException("Can't find at least one router!", DataCenter.class, network.getDataCenterId());
}
try {
return _routerMgr.removeDhcpSupportForSubnet(network, routers);
} catch (final ResourceUnavailableException e) {
s_logger.debug("Router resource unavailable ");
}
}
return false;
}
@Override
public boolean addDhcpEntry(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final DeployDestination dest, final ReservationContext context)
throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
if (canHandle(network, Service.Dhcp)) {
if (vm.getType() != VirtualMachine.Type.User) {
return false;
}
final VirtualMachineProfile uservm = vm;
final List<DomainRouterVO> routers = getRouters(network, dest);
if (routers == null || routers.size() == 0) {
throw new ResourceUnavailableException("Can't find at least one router!", DataCenter.class, network.getDataCenterId());
}
final DataCenterVO dcVO = _dcDao.findById(network.getDataCenterId());
final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO);
return networkTopology.applyDhcpEntry(network, nic, uservm, dest, routers);
}
return false;
}
@Override
public boolean addPasswordAndUserdata(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final DeployDestination dest,
final ReservationContext context) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
if (canHandle(network, Service.UserData)) {
if (vm.getType() != VirtualMachine.Type.User) {
return false;
}
if (network.getIp6Gateway() != null) {
s_logger.info("Skip password and userdata service setup for IPv6 VM");
return true;
}
final VirtualMachineProfile uservm = vm;
final List<DomainRouterVO> routers = getRouters(network, dest);