Skip to content

Commit d6cd15f

Browse files
committed
Big refactor
1 parent 909fbf7 commit d6cd15f

File tree

13 files changed

+189
-166
lines changed

13 files changed

+189
-166
lines changed

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/agent/api/CreateNsxDhcpRelayConfigCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public class CreateNsxDhcpRelayConfigCommand extends NsxCommand {
2424
private String networkName;
2525
private List<String> addresses;
2626

27-
public CreateNsxDhcpRelayConfigCommand(String zoneName, Long zoneId, String accountName, Long accountId,
27+
public CreateNsxDhcpRelayConfigCommand(String domainName, String accountName, String zoneName,
2828
String vpcName, String networkName, List<String> addresses) {
29-
super(zoneName, zoneId, accountName, accountId);
29+
super(domainName, accountName, zoneName);
3030
this.vpcName = vpcName;
3131
this.networkName = networkName;
3232
this.addresses = addresses;

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/agent/api/CreateNsxSegmentCommand.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,38 @@
1616
// under the License.
1717
package org.apache.cloudstack.agent.api;
1818

19-
import com.cloud.network.dao.NetworkVO;
20-
2119
import java.util.Objects;
2220

23-
public class CreateNsxSegmentCommand extends CreateNsxTier1GatewayCommand {
24-
private NetworkVO tierNetwork;
25-
public CreateNsxSegmentCommand(String zoneName, Long zoneId, String accountName, Long accountId, String vpcName, NetworkVO tierNetwork) {
26-
super(zoneName, zoneId, accountName, accountId, vpcName);
27-
this.tierNetwork = tierNetwork;
21+
public class CreateNsxSegmentCommand extends NsxCommand {
22+
23+
private String vpcName;
24+
private String networkName;
25+
private String networkGateway;
26+
private String networkCidr;
27+
28+
public CreateNsxSegmentCommand(String domainName, String accountName, String zoneName,
29+
String vpcName, String networkName, String networkGateway, String networkCidr) {
30+
super(domainName, accountName, zoneName);
31+
this.vpcName = vpcName;
32+
this.networkName = networkName;
33+
this.networkGateway = networkGateway;
34+
this.networkCidr = networkCidr;
35+
}
36+
37+
public String getVpcName() {
38+
return vpcName;
39+
}
40+
41+
public String getNetworkName() {
42+
return networkName;
2843
}
2944

30-
public NetworkVO getTierNetwork() {
31-
return tierNetwork;
45+
public String getNetworkGateway() {
46+
return networkGateway;
3247
}
3348

34-
public void setTierNetwork(NetworkVO tierNetwork) {
35-
this.tierNetwork = tierNetwork;
49+
public String getNetworkCidr() {
50+
return networkCidr;
3651
}
3752

3853
@Override
@@ -41,11 +56,11 @@ public boolean equals(Object o) {
4156
if (o == null || getClass() != o.getClass()) return false;
4257
if (!super.equals(o)) return false;
4358
CreateNsxSegmentCommand command = (CreateNsxSegmentCommand) o;
44-
return Objects.equals(tierNetwork, command.tierNetwork);
59+
return Objects.equals(networkName, command.networkName);
4560
}
4661

4762
@Override
4863
public int hashCode() {
49-
return Objects.hash(super.hashCode(), tierNetwork);
64+
return Objects.hash(super.hashCode(), networkName);
5065
}
5166
}

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/agent/api/CreateNsxTier1GatewayCommand.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,15 @@
2121
public class CreateNsxTier1GatewayCommand extends NsxCommand {
2222
private String vpcName;
2323

24-
public CreateNsxTier1GatewayCommand(String zoneName, Long zoneId, String accountName, Long accountId, String vpcName) {
25-
super(zoneName, zoneId, accountName, accountId);
24+
public CreateNsxTier1GatewayCommand(String domainName, String accountName, String zoneName, String vpcName) {
25+
super(domainName, accountName, zoneName);
2626
this.vpcName = vpcName;
2727
}
2828

2929
public String getVpcName() {
3030
return vpcName;
3131
}
3232

33-
public void setVpcName(String vpcName) {
34-
this.vpcName = vpcName;
35-
}
36-
3733
@Override
3834
public boolean equals(Object o) {
3935
if (this == o) return true;

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/agent/api/DeleteNsxSegmentCommand.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,22 @@
1616
// under the License.
1717
package org.apache.cloudstack.agent.api;
1818

19-
import com.cloud.network.dao.NetworkVO;
19+
public class DeleteNsxSegmentCommand extends NsxCommand {
2020

21-
public class DeleteNsxSegmentCommand extends CreateNsxSegmentCommand {
22-
public DeleteNsxSegmentCommand(String zoneName, String accountName, String vpcName, NetworkVO network) {
23-
super(zoneName, network.getDataCenterId(), accountName, network.getAccountId(), vpcName, network);
21+
private String vpcName;
22+
private String networkName;
23+
24+
public DeleteNsxSegmentCommand(String domainName, String accountName, String zoneName, String vpcName, String networkName) {
25+
super(domainName, accountName, zoneName);
26+
this.vpcName = vpcName;
27+
this.networkName = networkName;
28+
}
29+
30+
public String getVpcName() {
31+
return vpcName;
32+
}
33+
34+
public String getNetworkName() {
35+
return networkName;
2436
}
2537
}

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/agent/api/DeleteNsxTier1GatewayCommand.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@
1616
// under the License.
1717
package org.apache.cloudstack.agent.api;
1818

19-
public class DeleteNsxTier1GatewayCommand extends CreateNsxTier1GatewayCommand {
19+
public class DeleteNsxTier1GatewayCommand extends NsxCommand {
2020

21-
public DeleteNsxTier1GatewayCommand(String zoneName, Long zoneId, String accountName, Long accountId, String vpcName) {
22-
super(zoneName, zoneId, accountName, accountId, vpcName);
21+
private String vpcName;
22+
23+
public DeleteNsxTier1GatewayCommand(String domainName, String accountName, String zoneName, String vpcName) {
24+
super(domainName, accountName, zoneName);
25+
this.vpcName = vpcName;
26+
}
27+
28+
public String getVpcName() {
29+
return vpcName;
2330
}
2431
}

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/agent/api/NsxCommand.java

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,27 @@
2222

2323
public class NsxCommand extends Command {
2424
private String zoneName;
25-
private Long zoneId;
2625
private String accountName;
27-
private Long accountId;
26+
private String domainName;
2827

29-
public NsxCommand(String zoneName, Long zoneId, String accountName, Long accountId) {
28+
public NsxCommand(String domainName, String accountName, String zoneName) {
3029
this.zoneName = zoneName;
31-
this.zoneId = zoneId;
3230
this.accountName = accountName;
33-
this.accountId = accountId;
31+
this.domainName = domainName;
3432
}
3533

3634
public String getZoneName() {
3735
return zoneName;
3836
}
3937

40-
public void setZoneName(String zoneName) {
41-
this.zoneName = zoneName;
42-
}
43-
44-
public Long getZoneId() {
45-
return zoneId;
46-
}
47-
48-
public void setZoneId(Long zoneId) {
49-
this.zoneId = zoneId;
50-
}
51-
5238
public String getAccountName() {
5339
return accountName;
5440
}
5541

56-
public void setAccountName(String accountName) {
57-
this.accountName = accountName;
42+
public String getDomainName() {
43+
return domainName;
5844
}
5945

60-
public Long getAccountId() {
61-
return accountId;
62-
}
63-
64-
public void setAccountId(Long accountId) {
65-
this.accountId = accountId;
66-
}
6746
@Override
6847
public boolean executeInSequence() {
6948
return false;
@@ -75,11 +54,11 @@ public boolean equals(Object o) {
7554
if (o == null || getClass() != o.getClass()) return false;
7655
if (!super.equals(o)) return false;
7756
NsxCommand that = (NsxCommand) o;
78-
return Objects.equals(zoneName, that.zoneName) && Objects.equals(zoneId, that.zoneId) && Objects.equals(accountName, that.accountName) && Objects.equals(accountId, that.accountId);
57+
return Objects.equals(zoneName, that.zoneName) && Objects.equals(accountName, that.accountName) && Objects.equals(domainName, that.domainName);
7958
}
8059

8160
@Override
8261
public int hashCode() {
83-
return Objects.hash(super.hashCode(), zoneName, zoneId, accountName, accountId);
62+
return Objects.hash(super.hashCode(), zoneName, accountName, domainName);
8463
}
8564
}

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/resource/NsxResource.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,41 +275,42 @@ private Answer executeRequest(CreateNsxSegmentCommand cmd) {
275275
try {
276276
SiteListResult sites = nsxApiClient.getSites();
277277
String errorMsg;
278+
String networkName = cmd.getNetworkName();
278279
if (CollectionUtils.isEmpty(sites.getResults())) {
279-
errorMsg = String.format("Failed to create network: %s as no sites are found in the linked NSX infrastructure", cmd.getTierNetwork().getName());
280+
errorMsg = String.format("Failed to create network: %s as no sites are found in the linked NSX infrastructure", networkName);
280281
LOGGER.error(errorMsg);
281282
return new NsxAnswer(cmd, new CloudRuntimeException(errorMsg));
282283
}
283284
String siteId = sites.getResults().get(0).getId();
284285

285286
EnforcementPointListResult epList = nsxApiClient.getEnforcementPoints(siteId);
286287
if (CollectionUtils.isEmpty(epList.getResults())) {
287-
errorMsg = String.format("Failed to create network: %s as no enforcement points are found in the linked NSX infrastructure", cmd.getTierNetwork().getName());
288+
errorMsg = String.format("Failed to create network: %s as no enforcement points are found in the linked NSX infrastructure", networkName);
288289
LOGGER.error(errorMsg);
289290
return new NsxAnswer(cmd, new CloudRuntimeException(errorMsg));
290291
}
291292
String enforcementPointPath = epList.getResults().get(0).getPath();
292293

293294
TransportZoneListResult transportZoneListResult = nsxApiClient.getTransportZones();
294295
if (CollectionUtils.isEmpty(transportZoneListResult.getResults())) {
295-
errorMsg = String.format("Failed to create network: %s as no transport zones were found in the linked NSX infrastructure", cmd.getTierNetwork().getName());
296+
errorMsg = String.format("Failed to create network: %s as no transport zones were found in the linked NSX infrastructure", networkName);
296297
LOGGER.error(errorMsg);
297298
return new NsxAnswer(cmd, new CloudRuntimeException(errorMsg));
298299
}
299300
List<TransportZone> transportZones = transportZoneListResult.getResults().stream().filter(tz -> tz.getDisplayName().equals(transportZone)).collect(Collectors.toList());
300301
if (CollectionUtils.isEmpty(transportZones)) {
301-
errorMsg = String.format("Failed to create network: %s as no transport zone of name %s was found in the linked NSX infrastructure", cmd.getTierNetwork().getName(), transportZone);
302+
errorMsg = String.format("Failed to create network: %s as no transport zone of name %s was found in the linked NSX infrastructure", networkName, transportZone);
302303
LOGGER.error(errorMsg);
303304
return new NsxAnswer(cmd, new CloudRuntimeException(errorMsg));
304305
}
305306

306-
String segmentName = NsxControllerUtils.getNsxSegmentId(cmd.getAccountName(), cmd.getVpcName(), cmd.getTierNetwork().getName());
307-
String gatewayAddress = cmd.getTierNetwork().getGateway() + "/" + cmd.getTierNetwork().getCidr().split("/")[1];
307+
String segmentName = NsxControllerUtils.getNsxSegmentId(cmd.getAccountName(), cmd.getVpcName(), networkName);
308+
String gatewayAddress = cmd.getNetworkGateway() + "/" + cmd.getNetworkCidr().split("/")[1];
308309

309310
nsxApiClient.createSegment(cmd.getZoneName(), cmd.getAccountName(), cmd.getVpcName(),
310311
segmentName, gatewayAddress, tier0Gateway, enforcementPointPath, transportZones);
311312
} catch (Exception e) {
312-
LOGGER.error(String.format("Failed to create network: %s", cmd.getTierNetwork().getName()));
313+
LOGGER.error(String.format("Failed to create network: %s", cmd.getNetworkName()));
313314
return new NsxAnswer(cmd, new CloudRuntimeException(e.getMessage()));
314315
}
315316
return new NsxAnswer(cmd, true, null);
@@ -318,10 +319,10 @@ private Answer executeRequest(CreateNsxSegmentCommand cmd) {
318319
private NsxAnswer executeRequest(DeleteNsxSegmentCommand cmd) {
319320
try {
320321
Thread.sleep(30*1000);
321-
String segmentName = NsxControllerUtils.getNsxSegmentId(cmd.getAccountName(), cmd.getVpcName(), cmd.getTierNetwork().getName());
322-
nsxApiClient.deleteSegment(cmd.getZoneName(), cmd.getAccountName(), cmd.getVpcName(), cmd.getTierNetwork().getName(), segmentName);
322+
String segmentName = NsxControllerUtils.getNsxSegmentId(cmd.getAccountName(), cmd.getVpcName(), cmd.getNetworkName());
323+
nsxApiClient.deleteSegment(cmd.getZoneName(), cmd.getAccountName(), cmd.getVpcName(), cmd.getNetworkName(), segmentName);
323324
} catch (Exception e) {
324-
LOGGER.error(String.format("Failed to delete NSX segment: %s", NsxControllerUtils.getNsxSegmentId(cmd.getAccountName(), cmd.getVpcName(), cmd.getTierNetwork().getName())));
325+
LOGGER.error(String.format("Failed to delete NSX segment: %s", NsxControllerUtils.getNsxSegmentId(cmd.getAccountName(), cmd.getVpcName(), cmd.getNetworkName())));
325326
return new NsxAnswer(cmd, new CloudRuntimeException(e.getMessage()));
326327
}
327328
return new NsxAnswer(cmd, true, null);

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxApiClient.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,13 @@
4545
import com.vmware.vapi.internal.protocol.client.rest.authn.BasicAuthenticationAppender;
4646
import com.vmware.vapi.protocol.HttpConfiguration;
4747
import com.vmware.vapi.std.errors.Error;
48-
import org.apache.cloudstack.utils.NsxApiClientUtils;
4948
import org.apache.cloudstack.utils.NsxControllerUtils;
5049
import org.apache.log4j.Logger;
5150

5251
import java.util.List;
5352
import java.util.function.Function;
5453

5554
import static java.util.Objects.isNull;
56-
import static org.apache.cloudstack.utils.NsxApiClientUtils.FailoverMode.PREEMPTIVE;
57-
import static org.apache.cloudstack.utils.NsxApiClientUtils.HAMode.ACTIVE_STANDBY;
58-
import static org.apache.cloudstack.utils.NsxApiClientUtils.PoolAllocation.ROUTING;
59-
import static org.apache.cloudstack.utils.NsxApiClientUtils.RouteAdvertisementType.TIER1_CONNECTED;
60-
import static org.apache.cloudstack.utils.NsxApiClientUtils.RouteAdvertisementType.TIER1_IPSEC_LOCAL_ENDPOINT;
61-
import static org.apache.cloudstack.utils.NsxApiClientUtils.TransportType.OVERLAY;
6255

6356
public class NsxApiClient {
6457

@@ -74,6 +67,22 @@ public class NsxApiClient {
7467
private static final String TIER_0_GATEWAY_PATH_PREFIX = "/infra/tier-0s/";
7568
private static final String TIER_1_GATEWAY_PATH_PREFIX = "/infra/tier-1s/";
7669

70+
private enum PoolAllocation { ROUTING, LB_SMALL, LB_MEDIUM, LB_LARGE, LB_XLARGE }
71+
72+
private enum TYPE { ROUTED, NATTED }
73+
74+
private enum HAMode { ACTIVE_STANDBY, ACTIVE_ACTIVE }
75+
76+
private enum FailoverMode { PREEMPTIVE, NON_PREEMPTIVE }
77+
78+
private enum AdminState { UP, DOWN }
79+
80+
private enum TransportType { OVERLAY, VLAN }
81+
82+
public enum RouteAdvertisementType { TIER1_STATIC_ROUTES, TIER1_CONNECTED, TIER1_NAT,
83+
TIER1_LB_VIP, TIER1_LB_SNAT, TIER1_DNS_FORWARDER_IP, TIER1_IPSEC_LOCAL_ENDPOINT
84+
}
85+
7786
public NsxApiClient(String hostname, String port, String username, char[] password) {
7887
String controllerUrl = String.format("https://%s:%s", hostname, port);
7988
HttpConfiguration.SslConfiguration.Builder sslConfigBuilder = new HttpConfiguration.SslConfiguration.Builder();
@@ -187,10 +196,10 @@ public void createTier1Gateway(String name, String tier0Gateway, String edgeClus
187196
tier1 = new Tier1.Builder()
188197
.setTier0Path(tier0GatewayPath)
189198
.setResourceType(TIER_1_RESOURCE_TYPE)
190-
.setPoolAllocation(ROUTING.name())
191-
.setHaMode(ACTIVE_STANDBY.name())
192-
.setFailoverMode(PREEMPTIVE.name())
193-
.setRouteAdvertisementTypes(List.of(TIER1_CONNECTED.name(), TIER1_IPSEC_LOCAL_ENDPOINT.name()))
199+
.setPoolAllocation(PoolAllocation.ROUTING.name())
200+
.setHaMode(HAMode.ACTIVE_STANDBY.name())
201+
.setFailoverMode(FailoverMode.PREEMPTIVE.name())
202+
.setRouteAdvertisementTypes(List.of(RouteAdvertisementType.TIER1_CONNECTED.name(), RouteAdvertisementType.TIER1_IPSEC_LOCAL_ENDPOINT.name()))
194203
.setId(name)
195204
.setDisplayName(name)
196205
.build();
@@ -234,7 +243,7 @@ public EnforcementPointListResult getEnforcementPoints(String siteId) {
234243
public TransportZoneListResult getTransportZones() {
235244
try {
236245
com.vmware.nsx.TransportZones transportZones = (com.vmware.nsx.TransportZones) nsxService.apply(com.vmware.nsx.TransportZones.class);
237-
return transportZones.list(null, null, true, null, true, null, null, null, OVERLAY.name(), null);
246+
return transportZones.list(null, null, true, null, true, null, null, null, TransportType.OVERLAY.name(), null);
238247
} catch (Exception e) {
239248
throw new CloudRuntimeException(String.format("Failed to fetch service segment list due to %s", e.getMessage()));
240249
}
@@ -252,7 +261,7 @@ public void createSegment(String zoneName, String accountName, String vpcName, S
252261
.setDisplayName(segmentName)
253262
.setConnectivityPath(isNull(vpcName) ? TIER_0_GATEWAY_PATH_PREFIX + tier0Gateway
254263
: TIER_1_GATEWAY_PATH_PREFIX + NsxControllerUtils.getTier1GatewayName(zoneName, accountName, vpcName))
255-
.setAdminState(NsxApiClientUtils.AdminState.UP.name())
264+
.setAdminState(AdminState.UP.name())
256265
.setSubnets(List.of(subnet))
257266
.setTransportZonePath(enforcementPointPath + "/transport-zones/" + transportZones.get(0).getId())
258267
.build();

0 commit comments

Comments
 (0)