Skip to content

Commit 3f48ac4

Browse files
committed
cleanup and add skeletal code for network creation
1 parent 996821d commit 3f48ac4

File tree

5 files changed

+122
-52
lines changed

5 files changed

+122
-52
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ public NsxAnswer(final Command command, final boolean success, final String deta
2727
public NsxAnswer(final Command command, final Exception e) {
2828
super(command, e);
2929
}
30+
3031
}

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import javax.naming.ConfigurationException;
5151
import java.util.List;
5252
import java.util.Map;
53+
import java.util.function.Function;
5354

5455
public class NsxResource implements ServerResource {
5556
private static final Logger LOGGER = Logger.getLogger(NsxResource.class);
@@ -204,20 +205,20 @@ private Answer executeRequest(ReadyCommand cmd) {
204205
return new ReadyAnswer(cmd);
205206
}
206207

207-
private Service getService(Class serviceClass) {
208-
return nsxApi.getApiClient().createStub(serviceClass);
208+
private Function<Class, Service> nsxService = svcClass -> { return nsxApi.getApiClient().createStub(svcClass); };
209+
private Service getService(Class svcClass) {
210+
return nsxApi.getApiClient().createStub(svcClass);
209211
}
210-
211212
private Answer executeRequest(CreateNsxTier1GatewayCommand cmd) {
212-
String name = getVpcName(cmd);
213+
String name = getTier1GatewayName(cmd);
213214
Tier1 tier1 = getTier1Gateway(name);
214215
if (tier1 != null) {
215216
throw new InvalidParameterValueException(String.format("VPC network with name %s exists in NSX zone: %s and account %s", name, cmd.getZoneName(), cmd.getAccountName()));
216217
}
217218
List<com.vmware.nsx_policy.model.LocaleServices> localeServices = getTier0LocalServices(tier0Gateway);
218219
String tier0GatewayPath = TIER_0_GATEWAY_PATH_PREFIX + tier0Gateway;
219220

220-
Tier1s tier1service = (Tier1s) getService(Tier1s.class);
221+
Tier1s tier1service = (Tier1s) nsxService.apply(Tier1s.class);
221222
tier1 = new Tier1.Builder()
222223
.setTier0Path(tier0GatewayPath)
223224
.setResourceType(TIER_1_RESOURCE_TYPE)
@@ -230,6 +231,7 @@ private Answer executeRequest(CreateNsxTier1GatewayCommand cmd) {
230231
.setLocaleServices(
231232
new com.vmware.nsx_policy.model.LocaleServices.Builder()
232233
.setEdgeClusterPath(localeServices.get(0).getEdgeClusterPath())
234+
.setId(localeServices.get(0).getId())
233235
.setResourceType("LocaleServices")
234236
.build()
235237
).build())).build();
@@ -244,17 +246,17 @@ private Answer executeRequest(CreateNsxTier1GatewayCommand cmd) {
244246

245247
private Answer executeRequest(DeleteNsxTier1GatewayCommand cmd) {
246248
try {
247-
Tier1s tier1service = (Tier1s) getService(Tier1s.class);
248-
tier1service.delete(cmd.getVpcName());
249+
Tier1s tier1service = (Tier1s) nsxService.apply(Tier1s.class);
250+
tier1service.delete(getTier1GatewayName(cmd));
249251
} catch (Exception e) {
250-
return new Answer(cmd, new CloudRuntimeException(e.getMessage()));
252+
return new NsxAnswer(cmd, new CloudRuntimeException(e.getMessage()));
251253
}
252-
return new Answer(cmd, true, null);
254+
return new NsxAnswer(cmd, true, null);
253255
}
254256

255257
private List<com.vmware.nsx_policy.model.LocaleServices> getTier0LocalServices(String tier0Gateway) {
256258
try {
257-
LocaleServices tier0LocaleServices = (LocaleServices) getService(LocaleServices.class);
259+
LocaleServices tier0LocaleServices = (LocaleServices) nsxService.apply(LocaleServices.class);
258260
LocaleServicesListResult result =tier0LocaleServices.list(tier0Gateway, null, false, null, null, null, null);
259261
return result.getResults();
260262
} catch (Exception e) {
@@ -264,15 +266,15 @@ private List<com.vmware.nsx_policy.model.LocaleServices> getTier0LocalServices(S
264266

265267
private Tier1 getTier1Gateway(String tier1GatewayId) {
266268
try {
267-
Tier1s tier1service = (Tier1s) getService(Tier1s.class);
269+
Tier1s tier1service = (Tier1s) nsxService.apply(Tier1s.class);
268270
return tier1service.get(tier1GatewayId);
269271
} catch (Exception e) {
270272
LOGGER.debug(String.format("NSX Tier-1 gateway with name: %s not found", tier1GatewayId));
271273
}
272274
return null;
273275
}
274276

275-
private String getVpcName(CreateNsxTier1GatewayCommand cmd) {
277+
private String getTier1GatewayName(CreateNsxTier1GatewayCommand cmd) {
276278
return cmd.getZoneName() + "-" + cmd.getAccountName() + "-" + cmd.getVpcName();
277279
}
278280

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public NsxAnswer sendNsxCommand(NsxCommand cmd, long zoneId) throws IllegalArgum
4444
s_logger.error("No NSX controller was found!");
4545
throw new InvalidParameterValueException("Failed to find an NSX controller");
4646
}
47-
48-
//Answer answer = agentMgr.sendTo(zoneId, Hypervisor.HypervisorType.VMware, cmd);
4947
Answer answer = agentMgr.easySend(nsxProviderVO.getHostId(), cmd);
5048

5149
if (answer == null || !answer.getResult()) {

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

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,30 @@
1616
// under the License.
1717
package org.apache.cloudstack.service;
1818

19+
import com.amazonaws.util.CollectionUtils;
1920
import com.cloud.agent.AgentManager;
2021
import com.cloud.agent.Listener;
21-
import com.cloud.agent.api.StartupCommand;
22-
import com.cloud.agent.api.Answer;
23-
import com.cloud.agent.api.Command;
2422
import com.cloud.agent.api.AgentControlAnswer;
2523
import com.cloud.agent.api.AgentControlCommand;
24+
import com.cloud.agent.api.Answer;
25+
import com.cloud.agent.api.Command;
26+
import com.cloud.agent.api.StartupCommand;
2627
import com.cloud.dc.DataCenterVO;
2728
import com.cloud.dc.dao.DataCenterDao;
2829
import com.cloud.deploy.DeployDestination;
29-
import com.cloud.exception.InsufficientCapacityException;
30-
import com.cloud.exception.ResourceUnavailableException;
3130
import com.cloud.exception.ConcurrentOperationException;
32-
import com.cloud.exception.InvalidParameterValueException;
3331
import com.cloud.exception.ConnectionException;
32+
import com.cloud.exception.InsufficientCapacityException;
33+
import com.cloud.exception.InvalidParameterValueException;
34+
import com.cloud.exception.ResourceUnavailableException;
3435
import com.cloud.host.Host;
3536
import com.cloud.host.HostVO;
3637
import com.cloud.host.Status;
3738
import com.cloud.network.Network;
39+
import com.cloud.network.Networks;
3840
import com.cloud.network.PhysicalNetworkServiceProvider;
41+
import com.cloud.network.dao.PhysicalNetworkDao;
42+
import com.cloud.network.dao.PhysicalNetworkVO;
3943
import com.cloud.network.element.DhcpServiceProvider;
4044
import com.cloud.network.element.DnsServiceProvider;
4145
import com.cloud.network.element.VpcProvider;
@@ -50,21 +54,24 @@
5054
import com.cloud.resource.UnableDeleteHostException;
5155
import com.cloud.user.Account;
5256
import com.cloud.user.AccountManager;
57+
import com.cloud.utils.Pair;
5358
import com.cloud.utils.component.AdapterBase;
5459
import com.cloud.vm.NicProfile;
5560
import com.cloud.vm.ReservationContext;
5661
import com.cloud.vm.VirtualMachineProfile;
62+
import net.sf.ehcache.config.InvalidConfigurationException;
5763
import org.apache.cloudstack.StartupNsxCommand;
5864
import org.apache.log4j.Logger;
5965
import org.springframework.stereotype.Component;
6066

6167
import javax.inject.Inject;
6268
import javax.naming.ConfigurationException;
63-
import java.util.Map;
6469
import java.util.HashMap;
6570
import java.util.List;
66-
import java.util.Set;
71+
import java.util.Map;
6772
import java.util.Objects;
73+
import java.util.Set;
74+
import java.util.function.Function;
6875

6976
@Component
7077
public class NsxElement extends AdapterBase implements DhcpServiceProvider, DnsServiceProvider, VpcProvider,
@@ -80,6 +87,8 @@ public class NsxElement extends AdapterBase implements DhcpServiceProvider, DnsS
8087
AgentManager agentManager;
8188
@Inject
8289
ResourceManager resourceManager;
90+
@Inject
91+
PhysicalNetworkDao physicalNetworkDao;
8392

8493
private static final Logger LOGGER = Logger.getLogger(NsxElement.class);
8594

@@ -229,34 +238,48 @@ public DeleteHostAnswer deleteHost(HostVO host, boolean isForced, boolean isForc
229238

230239
@Override
231240
public boolean implementVpc(Vpc vpc, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
232-
DataCenterVO zone = dataCenterDao.findById(vpc.getZoneId());
233-
if (Network.Provider.Nsx.getName().equalsIgnoreCase(zone.getDhcpProvider())) {
234-
if (Objects.isNull(zone)) {
235-
throw new InvalidParameterValueException(String.format("Failed to find zone with id %s", vpc.getZoneId()));
236-
}
237-
Account account = accountMgr.getAccount(vpc.getAccountId());
238-
if (Objects.isNull(account)) {
239-
throw new InvalidParameterValueException(String.format("Failed to find account with id %s", vpc.getAccountId()));
240-
}
241-
return nsxService.createVpcNetwork(vpc.getZoneId(), zone.getName(), account.getAccountId(), account.getName(), vpc.getName());
241+
DataCenterVO zone = zoneFunction.apply(vpc.getZoneId());
242+
Pair<Boolean, Account> isNsxAndAccount = validateVpcConfigurationAndGetAccount(zone, vpc);
243+
if (!isNsxAndAccount.first()) {
244+
return true;
242245
}
243-
return true;
246+
if (isNsxAndAccount.first() && Objects.isNull(isNsxAndAccount.second())) {
247+
throw new InvalidParameterValueException(String.format("Failed to find account with id %s", vpc.getAccountId()));
248+
}
249+
Account account = isNsxAndAccount.second();
250+
return nsxService.createVpcNetwork(vpc.getZoneId(), zone.getName(), account.getAccountId(), account.getName(), vpc.getName());
244251
}
245252

246253
@Override
247254
public boolean shutdownVpc(Vpc vpc, ReservationContext context) throws ConcurrentOperationException {
248-
DataCenterVO zone = dataCenterDao.findById(vpc.getZoneId());
249-
if (Network.Provider.Nsx.getName().equalsIgnoreCase(zone.getDhcpProvider())) {
250-
if (Objects.isNull(zone)) {
251-
throw new InvalidParameterValueException(String.format("Failed to find zone with id %s", vpc.getZoneId()));
252-
}
253-
Account account = accountMgr.getAccount(vpc.getAccountId());
254-
if (Objects.isNull(account)) {
255-
throw new InvalidParameterValueException(String.format("Failed to find account with id %s", vpc.getAccountId()));
256-
}
257-
return nsxService.deleteVpcNetwork(vpc.getZoneId(), zone.getName(), account.getAccountId(), account.getName(), vpc.getName());
255+
DataCenterVO zone = zoneFunction.apply(vpc.getZoneId());
256+
Pair<Boolean, Account> isNsxAndAccount = validateVpcConfigurationAndGetAccount(zone, vpc);
257+
if (!isNsxAndAccount.first()) {
258+
return true;
258259
}
259-
return true;
260+
if (isNsxAndAccount.first() && Objects.isNull(isNsxAndAccount.second())) {
261+
throw new InvalidParameterValueException(String.format("Failed to find account with id %s", vpc.getAccountId()));
262+
}
263+
Account account = isNsxAndAccount.second();
264+
265+
return nsxService.deleteVpcNetwork(vpc.getZoneId(), zone.getName(), account.getAccountId(), account.getName(), vpc.getName());
266+
}
267+
268+
private Pair<Boolean, Account> validateVpcConfigurationAndGetAccount(DataCenterVO zone, Vpc vpc) {
269+
if (Objects.isNull(zone)) {
270+
throw new InvalidParameterValueException(String.format("Failed to find zone with id %s", vpc.getZoneId()));
271+
}
272+
Account account = null;
273+
boolean forNsx = false;
274+
List<PhysicalNetworkVO> physicalNetworks = physicalNetworkDao.listByZoneAndTrafficType(zone.getId(), Networks.TrafficType.Guest);
275+
if (CollectionUtils.isNullOrEmpty(physicalNetworks) || physicalNetworks.size() > 1 ) {
276+
throw new InvalidConfigurationException(String.format("Desired number of physical networks is not present in the zone %s for traffic type %s. ", zone.getName(), Networks.TrafficType.Guest.name()));
277+
}
278+
if (physicalNetworks.get(0).getIsolationMethods().contains(Network.Provider.Nsx.getName())) {
279+
account = accountMgr.getAccount(vpc.getAccountId());
280+
forNsx = true;
281+
}
282+
return new Pair<>(forNsx, account);
260283
}
261284

262285
@Override
@@ -333,4 +356,6 @@ public int getTimeout() {
333356
public boolean processTimeout(long agentId, long seq) {
334357
return false;
335358
}
359+
360+
private final Function<Long, DataCenterVO> zoneFunction = zoneId -> { return dataCenterDao.findById(zoneId); };
336361
}

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

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@
1818

1919
import com.cloud.dc.DataCenter;
2020
import com.cloud.deploy.DeployDestination;
21+
import com.cloud.deploy.DeploymentPlan;
22+
import com.cloud.exception.InsufficientAddressCapacityException;
23+
import com.cloud.exception.InsufficientVirtualNetworkCapacityException;
2124
import com.cloud.network.Network;
2225
import com.cloud.network.NetworkMigrationResponder;
26+
import com.cloud.network.NetworkProfile;
2327
import com.cloud.network.Networks;
2428
import com.cloud.network.PhysicalNetwork;
2529
import com.cloud.network.guru.GuestNetworkGuru;
2630
import com.cloud.offering.NetworkOffering;
2731
import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
32+
import com.cloud.user.Account;
33+
import com.cloud.utils.db.DB;
2834
import com.cloud.vm.NicProfile;
2935
import com.cloud.vm.ReservationContext;
3036
import com.cloud.vm.VirtualMachineProfile;
@@ -45,6 +51,52 @@ public NsxGuestNetworkGuru() {
4551
_isolationMethods = new PhysicalNetwork.IsolationMethod[] {new PhysicalNetwork.IsolationMethod("NSX")};
4652
}
4753

54+
@Override
55+
public boolean canHandle(NetworkOffering offering, DataCenter.NetworkType networkType,
56+
PhysicalNetwork physicalNetwork) {
57+
return networkType == DataCenter.NetworkType.Advanced && isMyTrafficType(offering.getTrafficType())
58+
&& isMyIsolationMethod(physicalNetwork) && networkOfferingServiceMapDao.isProviderForNetworkOffering(
59+
offering.getId(), Network.Provider.Tungsten);
60+
}
61+
62+
@Override
63+
public Network design(NetworkOffering offering, DeploymentPlan plan, Network userSpecified, Account owner) {
64+
return null;
65+
}
66+
67+
@Override
68+
@DB
69+
public void deallocate(Network config, NicProfile nic, VirtualMachineProfile vm) {
70+
71+
}
72+
73+
@Override
74+
public Network implement(Network network, NetworkOffering offering, DeployDestination dest,
75+
ReservationContext context) {
76+
return null;
77+
}
78+
79+
@Override
80+
public void reserve(final NicProfile nic, final Network network, final VirtualMachineProfile vm,
81+
final DeployDestination dest, final ReservationContext context)
82+
throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
83+
84+
}
85+
86+
@Override
87+
public boolean release(final NicProfile nic, final VirtualMachineProfile vm, final String reservationId) {
88+
return true;
89+
}
90+
91+
@Override
92+
public void shutdown(final NetworkProfile profile, final NetworkOffering offering) {
93+
94+
}
95+
96+
@Override
97+
public boolean trash(Network network, NetworkOffering offering) {
98+
return true;
99+
}
48100

49101
@Override
50102
public boolean prepareMigration(NicProfile nic, Network network, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) {
@@ -60,12 +112,4 @@ public void rollbackMigration(NicProfile nic, Network network, VirtualMachinePro
60112
public void commitMigration(NicProfile nic, Network network, VirtualMachineProfile vm, ReservationContext src, ReservationContext dst) {
61113

62114
}
63-
64-
@Override
65-
public boolean canHandle(NetworkOffering offering, DataCenter.NetworkType networkType,
66-
PhysicalNetwork physicalNetwork) {
67-
return networkType == DataCenter.NetworkType.Advanced && isMyTrafficType(offering.getTrafficType())
68-
&& isMyIsolationMethod(physicalNetwork) && networkOfferingServiceMapDao.isProviderForNetworkOffering(
69-
offering.getId(), Network.Provider.Tungsten);
70-
}
71115
}

0 commit comments

Comments
 (0)