Skip to content

Commit 975804e

Browse files
committed
In progress NSX isolated network
1 parent 6818a01 commit 975804e

File tree

10 files changed

+109
-45
lines changed

10 files changed

+109
-45
lines changed

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,28 @@
1919
import java.util.Objects;
2020

2121
public class CreateNsxTier1GatewayCommand extends NsxCommand {
22-
private long vpcId;
23-
private String vpcName;
22+
private Long networkResourceId;
23+
private String networkResourceName;
24+
private boolean isResourceVpc;
2425

25-
public CreateNsxTier1GatewayCommand(long domainId, long accountId, long zoneId, long vpcId, String vpcName) {
26+
public CreateNsxTier1GatewayCommand(long domainId, long accountId, long zoneId,
27+
Long networkResourceId, String networkResourceName, boolean isResourceVpc) {
2628
super(domainId, accountId, zoneId);
27-
this.vpcId = vpcId;
28-
this.vpcName = vpcName;
29+
this.networkResourceId = networkResourceId;
30+
this.networkResourceName = networkResourceName;
31+
this.isResourceVpc = isResourceVpc;
2932
}
3033

31-
public long getVpcId() {
32-
return vpcId;
34+
public Long getNetworkResourceId() {
35+
return networkResourceId;
3336
}
3437

35-
public String getVpcName() {
36-
return vpcName;
38+
public boolean isResourceVpc() {
39+
return isResourceVpc;
40+
}
41+
42+
public String getNetworkResourceName() {
43+
return networkResourceName;
3744
}
3845

3946
@Override
@@ -42,11 +49,11 @@ public boolean equals(Object o) {
4249
if (o == null || getClass() != o.getClass()) return false;
4350
if (!super.equals(o)) return false;
4451
CreateNsxTier1GatewayCommand that = (CreateNsxTier1GatewayCommand) o;
45-
return Objects.equals(vpcName, that.vpcName);
52+
return Objects.equals(networkResourceName, that.networkResourceName);
4653
}
4754

4855
@Override
4956
public int hashCode() {
50-
return Objects.hash(super.hashCode(), vpcName);
57+
return Objects.hash(super.hashCode(), networkResourceName);
5158
}
5259
}

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,27 @@
1818

1919
public class DeleteNsxTier1GatewayCommand extends NsxCommand {
2020

21-
private Long vpcId;
22-
private String vpcName;
21+
private Long networkResourceId;
22+
private String networkResourceName;
23+
private boolean isResourceVpc;
2324

24-
public DeleteNsxTier1GatewayCommand(long domainId, long accountId, long zoneId, Long vpcId, String vpcName) {
25+
public DeleteNsxTier1GatewayCommand(long domainId, long accountId, long zoneId,
26+
Long networkResourceId, String networkResourceName, boolean isResourceVpc) {
2527
super(domainId, accountId, zoneId);
26-
this.vpcId = vpcId;
27-
this.vpcName = vpcName;
28+
this.networkResourceId = networkResourceId;
29+
this.networkResourceName = networkResourceName;
30+
this.isResourceVpc = isResourceVpc;
2831
}
2932

30-
public Long getVpcId() {
31-
return vpcId;
33+
public Long getNetworkResourceId() {
34+
return networkResourceId;
3235
}
3336

34-
public String getVpcName() {
35-
return vpcName;
37+
public String getNetworkResourceName() {
38+
return networkResourceName;
39+
}
40+
41+
public boolean isResourceVpc() {
42+
return isResourceVpc;
3643
}
3744
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.HashMap;
4949
import java.util.List;
5050
import java.util.Map;
51+
import java.util.Objects;
5152
import java.util.stream.Collectors;
5253

5354
public class NsxResource implements ServerResource {
@@ -253,18 +254,20 @@ private Answer executeRequest(ReadyCommand cmd) {
253254
}
254255

255256
private Answer executeRequest(CreateNsxTier1GatewayCommand cmd) {
256-
String name = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(), cmd.getVpcId());
257+
String name = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(), cmd.getNetworkResourceId(), cmd.isResourceVpc());
257258
try {
258259
nsxApiClient.createTier1Gateway(name, tier0Gateway, edgeCluster);
259260
return new NsxAnswer(cmd, true, "");
260261
} catch (CloudRuntimeException e) {
261-
LOGGER.error(String.format("Cannot create tier 1 gateway %s (VPC: %s): %s", name, cmd.getVpcName(), e.getMessage()));
262+
String msg = String.format("Cannot create tier 1 gateway %s (%s: %s): %s", name,
263+
(cmd.isResourceVpc() ? "VPC" : "NETWORK"), cmd.getNetworkResourceName(), e.getMessage());
264+
LOGGER.error(msg);
262265
return new NsxAnswer(cmd, e);
263266
}
264267
}
265268

266269
private Answer executeRequest(DeleteNsxTier1GatewayCommand cmd) {
267-
String tier1Id = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(), cmd.getVpcId());
270+
String tier1Id = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(), cmd.getNetworkResourceId(), cmd.isResourceVpc());
268271
try {
269272
nsxApiClient.deleteTier1Gateway(tier1Id);
270273
} catch (Exception e) {
@@ -309,8 +312,11 @@ private Answer executeRequest(CreateNsxSegmentCommand cmd) {
309312
String segmentName = NsxControllerUtils.getNsxSegmentId(cmd.getDomainId(), cmd.getAccountId(), cmd.getZoneId(), cmd.getVpcId(), cmd.getNetworkId());
310313
String gatewayAddress = cmd.getNetworkGateway() + "/" + cmd.getNetworkCidr().split("/")[1];
311314

312-
nsxApiClient.createSegment(cmd.getZoneId(), cmd.getDomainId(), cmd.getAccountId(), cmd.getVpcId(),
313-
segmentName, gatewayAddress, tier0Gateway, enforcementPointPath, transportZones);
315+
Long networkResourceId = Objects.isNull(cmd.getVpcId()) ? cmd.getNetworkId() : cmd.getVpcId();
316+
boolean isResourceVpc = !Objects.isNull(cmd.getVpcId());
317+
String tier1GatewayName = NsxControllerUtils.getTier1GatewayName(cmd.getDomainId(), cmd.getAccountId(),
318+
cmd.getZoneId(), networkResourceId, isResourceVpc);
319+
nsxApiClient.createSegment(segmentName, tier1GatewayName, gatewayAddress, enforcementPointPath, transportZones);
314320
} catch (Exception e) {
315321
LOGGER.error(String.format("Failed to create network: %s", cmd.getNetworkName()));
316322
return new NsxAnswer(cmd, new CloudRuntimeException(e.getMessage()));

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
import java.util.List;
5252
import java.util.function.Function;
5353

54-
import static java.util.Objects.isNull;
55-
5654
public class NsxApiClient {
5755

5856
private final Function<Class<? extends Service>, Service> nsxService;
@@ -249,7 +247,8 @@ public TransportZoneListResult getTransportZones() {
249247
}
250248
}
251249

252-
public void createSegment(long zoneId, long domainId, long accountId, Long vpcId, String segmentName, String gatewayAddress, String tier0Gateway, String enforcementPointPath, List<TransportZone> transportZones) {
250+
public void createSegment(String segmentName, String tier1GatewayName, String gatewayAddress, String enforcementPointPath,
251+
List<TransportZone> transportZones) {
253252
try {
254253
Segments segmentService = (Segments) nsxService.apply(Segments.class);
255254
SegmentSubnet subnet = new SegmentSubnet.Builder()
@@ -259,8 +258,7 @@ public void createSegment(long zoneId, long domainId, long accountId, Long vpcId
259258
.setResourceType(SEGMENT_RESOURCE_TYPE)
260259
.setId(segmentName)
261260
.setDisplayName(segmentName)
262-
.setConnectivityPath(isNull(vpcId) ? TIER_0_GATEWAY_PATH_PREFIX + tier0Gateway
263-
: TIER_1_GATEWAY_PATH_PREFIX + NsxControllerUtils.getTier1GatewayName(domainId, accountId, zoneId, vpcId))
261+
.setConnectivityPath(TIER_1_GATEWAY_PATH_PREFIX + tier1GatewayName)
264262
.setAdminState(AdminState.UP.name())
265263
.setSubnets(List.of(subnet))
266264
.setTransportZonePath(enforcementPointPath + "/transport-zones/" + transportZones.get(0).getId())

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ public Network.Provider getProvider() {
179179

180180
@Override
181181
public boolean implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
182+
// Account account = accountMgr.getAccount(network.getAccountId());
183+
// DomainVO domain = domainDao.findById(network.getDomainId());
184+
// return nsxService.createNetwork(network.getDataCenterId(), account.getId(), domain.getId(), network.getId(), network.getName());
185+
// TODO: Check if the network is NSX based (was already implemented as part of the guru.setup()
182186
return true;
183187
}
184188

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.apache.cloudstack.NsxAnswer;
5050
import org.apache.cloudstack.agent.api.CreateNsxDhcpRelayConfigCommand;
5151
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
52+
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
5253
import org.apache.cloudstack.utils.NsxControllerUtils;
5354

5455
import org.apache.cloudstack.utils.NsxHelper;
@@ -270,14 +271,6 @@ public void commitMigration(NicProfile nic, Network network, VirtualMachineProfi
270271
}
271272

272273
private void createNsxSegment(NetworkVO networkVO, DataCenter zone) {
273-
String vpcName = null;
274-
if (nonNull(networkVO.getVpcId())) {
275-
VpcVO vpc = _vpcDao.findById(networkVO.getVpcId());
276-
if (isNull(vpc)) {
277-
throw new CloudRuntimeException(String.format("Failed to find VPC network with id: %s", networkVO.getVpcId()));
278-
}
279-
vpcName = vpc.getName();
280-
}
281274
Account account = accountDao.findById(networkVO.getAccountId());
282275
if (isNull(account)) {
283276
throw new CloudRuntimeException(String.format("Unable to find account with id: %s", networkVO.getAccountId()));
@@ -288,6 +281,23 @@ private void createNsxSegment(NetworkVO networkVO, DataCenter zone) {
288281
LOGGER.error(msg);
289282
throw new CloudRuntimeException(msg);
290283
}
284+
String vpcName = null;
285+
if (nonNull(networkVO.getVpcId())) {
286+
VpcVO vpc = _vpcDao.findById(networkVO.getVpcId());
287+
if (isNull(vpc)) {
288+
throw new CloudRuntimeException(String.format("Failed to find VPC network with id: %s", networkVO.getVpcId()));
289+
}
290+
vpcName = vpc.getName();
291+
} else {
292+
LOGGER.debug(String.format("Creating a Tier 1 Gateway for the network %s before creating the NSX segment", networkVO.getName()));
293+
CreateNsxTier1GatewayCommand nsxTier1GatewayCommand = NsxHelper.createNsxTier1GatewayCommand(domain, account, zone, networkVO.getId(), networkVO.getName(), false);
294+
NsxAnswer nsxAnswer = nsxControllerUtils.sendNsxCommand(nsxTier1GatewayCommand, zone.getId());
295+
if (!nsxAnswer.getResult()) {
296+
String msg = String.format("Could not create a Tier 1 Gateway for network %s: %s", networkVO.getName(), nsxAnswer.getDetails());
297+
LOGGER.error(msg);
298+
throw new CloudRuntimeException(msg);
299+
}
300+
}
291301
CreateNsxSegmentCommand command = NsxHelper.createNsxSegmentCommand(domain, account, zone, vpcName, networkVO);
292302
NsxAnswer answer = nsxControllerUtils.sendNsxCommand(command, zone.getId());
293303
if (!answer.getResult()) {

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
import com.cloud.network.dao.NetworkVO;
2020
import com.cloud.network.vpc.VpcVO;
2121
import com.cloud.network.vpc.dao.VpcDao;
22+
import com.cloud.utils.exception.CloudRuntimeException;
2223
import org.apache.cloudstack.NsxAnswer;
2324
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
2425
import org.apache.cloudstack.agent.api.DeleteNsxSegmentCommand;
2526
import org.apache.cloudstack.agent.api.DeleteNsxTier1GatewayCommand;
2627
import org.apache.cloudstack.utils.NsxControllerUtils;
28+
import org.apache.log4j.Logger;
2729

2830
import javax.inject.Inject;
2931
import java.util.Objects;
@@ -34,16 +36,25 @@ public class NsxServiceImpl implements NsxService {
3436
@Inject
3537
VpcDao vpcDao;
3638

37-
public boolean createVpcNetwork(Long zoneId, long accountId, long domainId, long vpcId, String vpcName) {
39+
private static final Logger LOGGER = Logger.getLogger(NsxServiceImpl.class);
40+
41+
public boolean createVpcNetwork(Long zoneId, long accountId, long domainId, Long vpcId, String vpcName) {
3842
CreateNsxTier1GatewayCommand createNsxTier1GatewayCommand =
39-
new CreateNsxTier1GatewayCommand(domainId, accountId, zoneId, vpcId, vpcName);
43+
new CreateNsxTier1GatewayCommand(domainId, accountId, zoneId, vpcId, vpcName, true);
44+
NsxAnswer result = nsxControllerUtils.sendNsxCommand(createNsxTier1GatewayCommand, zoneId);
45+
return result.getResult();
46+
}
47+
48+
public boolean createNetwork(Long zoneId, long accountId, long domainId, Long networkId, String networkName) {
49+
CreateNsxTier1GatewayCommand createNsxTier1GatewayCommand =
50+
new CreateNsxTier1GatewayCommand(domainId, accountId, zoneId, networkId, networkName, false);
4051
NsxAnswer result = nsxControllerUtils.sendNsxCommand(createNsxTier1GatewayCommand, zoneId);
4152
return result.getResult();
4253
}
4354

4455
public boolean deleteVpcNetwork(Long zoneId, long accountId, long domainId, Long vpcId, String vpcName) {
4556
DeleteNsxTier1GatewayCommand deleteNsxTier1GatewayCommand =
46-
new DeleteNsxTier1GatewayCommand(domainId, accountId, zoneId, vpcId, vpcName);
57+
new DeleteNsxTier1GatewayCommand(domainId, accountId, zoneId, vpcId, vpcName, true);
4758
NsxAnswer result = nsxControllerUtils.sendNsxCommand(deleteNsxTier1GatewayCommand, zoneId);
4859
return result.getResult();
4960
}
@@ -57,6 +68,16 @@ public boolean deleteNetwork(long zoneId, long accountId, long domainId, Network
5768
DeleteNsxSegmentCommand deleteNsxSegmentCommand = new DeleteNsxSegmentCommand(domainId, accountId, zoneId,
5869
network.getVpcId(), vpcName, network.getId(), network.getName());
5970
NsxAnswer result = nsxControllerUtils.sendNsxCommand(deleteNsxSegmentCommand, network.getDataCenterId());
71+
if (!result.getResult()) {
72+
String msg = String.format("Could not remove the NSX segment for network %s", network.getName());
73+
LOGGER.error(msg);
74+
throw new CloudRuntimeException(msg);
75+
}
76+
77+
if (Objects.isNull(network.getVpcId())) {
78+
DeleteNsxTier1GatewayCommand deleteNsxTier1GatewayCommand = new DeleteNsxTier1GatewayCommand(domainId, accountId, zoneId, network.getId(), network.getName(), false);
79+
result = nsxControllerUtils.sendNsxCommand(deleteNsxTier1GatewayCommand, zoneId);
80+
}
6081
return result.getResult();
6182
}
6283
}

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/utils/NsxControllerUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ public NsxAnswer sendNsxCommand(NsxCommand cmd, long zoneId) throws IllegalArgum
5656
return (NsxAnswer) answer;
5757
}
5858

59-
public static String getTier1GatewayName(long domainId, long accountId, long zoneId, long vpcId) {
60-
return String.format("D%s-A%s-Z%s-V%s", domainId, accountId, zoneId, vpcId);
59+
/**
60+
* Generates the Tier 1 Gateway name and identifier for the resource on the NSX manager
61+
*/
62+
public static String getTier1GatewayName(long domainId, long accountId, long zoneId,
63+
Long networkResourceId, boolean isResourceVpc) {
64+
String resourcePrefix = isResourceVpc ? "V" : "N";
65+
return String.format("D%s-A%s-Z%s-%s%s", domainId, accountId, zoneId, resourcePrefix, networkResourceId);
6166
}
6267

6368
public static String getNsxSegmentId(long domainId, long accountId, long zoneId, Long vpcId, long networkId) {

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/utils/NsxHelper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.cloud.user.Account;
2525
import org.apache.cloudstack.agent.api.CreateNsxDhcpRelayConfigCommand;
2626
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
27+
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
2728

2829
import java.util.List;
2930

@@ -38,4 +39,9 @@ public static CreateNsxSegmentCommand createNsxSegmentCommand(DomainVO domain, A
3839
return new CreateNsxSegmentCommand(domain.getId(), account.getId(), zone.getId(),
3940
networkVO.getVpcId(), vpcName, networkVO.getId(), networkVO.getName(), networkVO.getGateway(), networkVO.getCidr());
4041
}
42+
43+
public static CreateNsxTier1GatewayCommand createNsxTier1GatewayCommand(DomainVO domain, Account account, DataCenter zone,
44+
Long networkResourceId, String networkResourceName, boolean isResourceVpc) {
45+
return new CreateNsxTier1GatewayCommand(domain.getId(), account.getId(), zone.getId(), networkResourceId, networkResourceName, isResourceVpc);
46+
}
4147
}

plugins/network-elements/nsx/src/test/java/org/apache/cloudstack/resource/NsxResourceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void testConfigure_MissingParameter() throws ConfigurationException {
106106
@Test
107107
public void testCreateNsxTier1Gateway() {
108108
NsxCommand command = new CreateNsxTier1GatewayCommand(1L, 2L,
109-
1L, 3L, "VPC01");
109+
1L, 3L, "VPC01", true);
110110

111111
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
112112
assertTrue(answer.getResult());
@@ -115,7 +115,7 @@ public void testCreateNsxTier1Gateway() {
115115
@Test
116116
public void testDeleteTier1Gateway() {
117117
NsxCommand command = new DeleteNsxTier1GatewayCommand(1L, 1L,
118-
1L, 2L, "VPC01");
118+
1L, 2L, "VPC01", true);
119119

120120
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
121121
assertTrue(answer.getResult());

0 commit comments

Comments
 (0)