1919package org .apache .cloudstack .storage .datastore .provider ;
2020
2121import com .cloud .agent .AgentManager ;
22- import com .cloud .agent .api .Answer ;
23- import com .cloud .agent .api .ModifyStoragePoolAnswer ;
24- import com .cloud .agent .api .ModifyStoragePoolCommand ;
22+ import com .cloud .agent .api .*;
23+ import com .cloud .agent .api .to .NicTO ;
2524import com .cloud .alert .AlertManager ;
25+ import com .cloud .configuration .ConfigurationManager ;
2626import com .cloud .exception .StorageConflictException ;
27+ import com .cloud .host .HostVO ;
28+ import com .cloud .host .dao .HostDao ;
29+ import com .cloud .network .NetworkModel ;
30+ import com .cloud .network .dao .NetworkDao ;
31+ import com .cloud .network .dao .NetworkVO ;
32+ import com .cloud .offerings .NetworkOfferingVO ;
33+ import com .cloud .offerings .dao .NetworkOfferingDao ;
2734import com .cloud .storage .DataStoreRole ;
2835import com .cloud .storage .Storage ;
2936import com .cloud .storage .StoragePool ;
@@ -59,12 +66,66 @@ public class DefaultHostListener implements HypervisorHostListener {
5966 StoragePoolDetailsDao storagePoolDetailsDao ;
6067 @ Inject
6168 StorageManager storageManager ;
69+ @ Inject
70+ NetworkOfferingDao networkOfferingDao ;
71+ @ Inject
72+ HostDao hostDao ;
73+ @ Inject
74+ NetworkModel networkModel ;
75+ @ Inject
76+ ConfigurationManager configManager ;
77+ @ Inject
78+ NetworkDao networkDao ;
79+
6280
6381 @ Override
6482 public boolean hostAdded (long hostId ) {
6583 return true ;
6684 }
6785
86+ private boolean createPersistentNetworkResourcesOnHost (long hostId ) {
87+ HostVO host = hostDao .findById (hostId );
88+ if (host != null ) {
89+ List <NetworkVO > allPersistentNetworks = networkDao .getAllPersistentNetworksFromZone (host .getDataCenterId ());
90+
91+ for (NetworkVO networkVO : allPersistentNetworks ) {
92+ NetworkOfferingVO networkOfferingVO = networkOfferingDao .findById (networkVO .getNetworkOfferingId ());
93+
94+ SetupPersistentNetworkCommand persistentNetworkCommand =
95+ new SetupPersistentNetworkCommand (createNicTOFromNetworkAndOffering (networkVO , networkOfferingVO , host ));
96+ Answer answer = agentMgr .easySend (hostId , persistentNetworkCommand );
97+ if (answer == null ) {
98+ throw new CloudRuntimeException ("Unable to get answer to the setup persistent network command " + networkVO .getId ());
99+ }
100+ if (!answer .getResult ()) {
101+ String msg = "Unable to create L2 persistent network resources from network " + networkVO .getId () + " on the host" + hostId ;
102+ alertMgr .sendAlert (AlertManager .AlertType .ALERT_TYPE_HOST , networkVO .getDataCenterId (), host .getPodId (), msg , msg );
103+ throw new CloudRuntimeException ("Unable to create persistent network resources from network " + networkVO .getId () +
104+ " on " + hostId + " due to " + answer .getDetails ());
105+ }
106+ }
107+ return true ;
108+ }
109+ return false ;
110+ }
111+
112+ /**
113+ * Creates a dummy NicTO object which is used by the respective hypervisors to setup network elements / resources
114+ * - bridges(KVM), VLANs(Xen) and portgroups(VMWare) for L2 network
115+ */
116+ private NicTO createNicTOFromNetworkAndOffering (NetworkVO networkVO , NetworkOfferingVO networkOfferingVO , HostVO hostVO ) {
117+ NicTO to = new NicTO ();
118+ to .setName (networkModel .getNetworkTag (hostVO .getHypervisorType (), networkVO ));
119+ to .setBroadcastType (networkVO .getBroadcastDomainType ());
120+ to .setType (networkVO .getTrafficType ());
121+ to .setBroadcastUri (networkVO .getBroadcastUri ());
122+ to .setIsolationuri (networkVO .getBroadcastUri ());
123+ to .setNetworkRateMbps (configManager .getNetworkOfferingNetworkRate (networkOfferingVO .getId (), networkVO .getDataCenterId ()));
124+ to .setSecurityGroupEnabled (networkModel .isSecurityGroupSupportedInNetwork (networkVO ));
125+ return to ;
126+ }
127+
128+
68129 @ Override
69130 public boolean hostConnect (long hostId , long poolId ) throws StorageConflictException {
70131 StoragePool pool = (StoragePool )this .dataStoreMgr .getDataStore (poolId , DataStoreRole .Primary );
@@ -104,7 +165,8 @@ public boolean hostConnect(long hostId, long poolId) throws StorageConflictExcep
104165 }
105166
106167 s_logger .info ("Connection established between storage pool " + pool + " and host " + hostId );
107- return true ;
168+
169+ return createPersistentNetworkResourcesOnHost (hostId );
108170 }
109171
110172 private void updateStoragePoolHostVOAndDetails (StoragePool pool , long hostId , ModifyStoragePoolAnswer mspAnswer ) {
@@ -119,7 +181,7 @@ private void updateStoragePoolHostVOAndDetails(StoragePool pool, long hostId, Mo
119181 StoragePoolVO poolVO = this .primaryStoreDao .findById (pool .getId ());
120182 poolVO .setUsedBytes (mspAnswer .getPoolInfo ().getCapacityBytes () - mspAnswer .getPoolInfo ().getAvailableBytes ());
121183 poolVO .setCapacityBytes (mspAnswer .getPoolInfo ().getCapacityBytes ());
122- if (StringUtils .isNotEmpty (mspAnswer .getPoolType ())) {
184+ if (StringUtils .isNotEmpty (mspAnswer .getPoolType ())) {
123185 StoragePoolDetailVO poolType = storagePoolDetailsDao .findDetail (pool .getId (), "pool_type" );
124186 if (poolType == null ) {
125187 StoragePoolDetailVO storagePoolDetailVO = new StoragePoolDetailVO (pool .getId (), "pool_type" , mspAnswer .getPoolType (), false );
@@ -137,7 +199,26 @@ public boolean hostDisconnected(long hostId, long poolId) {
137199
138200 @ Override
139201 public boolean hostAboutToBeRemoved (long hostId ) {
140- return true ;
202+ // send host the cleanup persistent network resources
203+ HostVO host = hostDao .findById (hostId );
204+ if (host != null ) {
205+ List <NetworkVO > allPersistentNetworks = networkDao .getAllPersistentNetworksFromZone (host .getDataCenterId ()); // find zoneId of host
206+ for (NetworkVO persistentNetworkVO : allPersistentNetworks ) {
207+ NetworkOfferingVO networkOfferingVO = networkOfferingDao .findById (persistentNetworkVO .getNetworkOfferingId ());
208+ CleanupPersistentNetworkResourceCommand cleanupCmd =
209+ new CleanupPersistentNetworkResourceCommand (createNicTOFromNetworkAndOffering (persistentNetworkVO , networkOfferingVO , host ));
210+ Answer answer = agentMgr .easySend (hostId , cleanupCmd );
211+ if (answer == null ) {
212+ throw new CloudRuntimeException ("Unable to get answer to the cleanup persistent network command " + persistentNetworkVO .getId ());
213+ }
214+ if (!answer .getResult ()) {
215+ String msg = "Unable to cleanup L2 persistent network resources from network " + persistentNetworkVO .getId () + " on the host" + hostId ;
216+ alertMgr .sendAlert (AlertManager .AlertType .ALERT_TYPE_HOST , persistentNetworkVO .getDataCenterId (), host .getPodId (), msg , msg );
217+ }
218+ }
219+ return true ;
220+ }
221+ return false ;
141222 }
142223
143224 @ Override
0 commit comments