1616// under the License.
1717package org .apache .cloudstack .service ;
1818
19+ import com .amazonaws .util .CollectionUtils ;
1920import com .cloud .agent .AgentManager ;
2021import 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 ;
2422import com .cloud .agent .api .AgentControlAnswer ;
2523import 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 ;
2627import com .cloud .dc .DataCenterVO ;
2728import com .cloud .dc .dao .DataCenterDao ;
2829import com .cloud .deploy .DeployDestination ;
29- import com .cloud .exception .InsufficientCapacityException ;
30- import com .cloud .exception .ResourceUnavailableException ;
3130import com .cloud .exception .ConcurrentOperationException ;
32- import com .cloud .exception .InvalidParameterValueException ;
3331import com .cloud .exception .ConnectionException ;
32+ import com .cloud .exception .InsufficientCapacityException ;
33+ import com .cloud .exception .InvalidParameterValueException ;
34+ import com .cloud .exception .ResourceUnavailableException ;
3435import com .cloud .host .Host ;
3536import com .cloud .host .HostVO ;
3637import com .cloud .host .Status ;
3738import com .cloud .network .Network ;
39+ import com .cloud .network .Networks ;
3840import com .cloud .network .PhysicalNetworkServiceProvider ;
41+ import com .cloud .network .dao .PhysicalNetworkDao ;
42+ import com .cloud .network .dao .PhysicalNetworkVO ;
3943import com .cloud .network .element .DhcpServiceProvider ;
4044import com .cloud .network .element .DnsServiceProvider ;
4145import com .cloud .network .element .VpcProvider ;
5054import com .cloud .resource .UnableDeleteHostException ;
5155import com .cloud .user .Account ;
5256import com .cloud .user .AccountManager ;
57+ import com .cloud .utils .Pair ;
5358import com .cloud .utils .component .AdapterBase ;
5459import com .cloud .vm .NicProfile ;
5560import com .cloud .vm .ReservationContext ;
5661import com .cloud .vm .VirtualMachineProfile ;
62+ import net .sf .ehcache .config .InvalidConfigurationException ;
5763import org .apache .cloudstack .StartupNsxCommand ;
5864import org .apache .log4j .Logger ;
5965import org .springframework .stereotype .Component ;
6066
6167import javax .inject .Inject ;
6268import javax .naming .ConfigurationException ;
63- import java .util .Map ;
6469import java .util .HashMap ;
6570import java .util .List ;
66- import java .util .Set ;
71+ import java .util .Map ;
6772import java .util .Objects ;
73+ import java .util .Set ;
74+ import java .util .function .Function ;
6875
6976@ Component
7077public 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}
0 commit comments