|
251 | 251 | import com.cloud.utils.db.TransactionCallbackNoReturn; |
252 | 252 | import com.cloud.utils.db.TransactionStatus; |
253 | 253 | import com.cloud.utils.exception.CloudRuntimeException; |
| 254 | +import com.cloud.utils.fsm.StateListener; |
254 | 255 | import com.cloud.utils.net.Ip; |
255 | 256 | import com.cloud.utils.net.MacAddress; |
256 | 257 | import com.cloud.utils.net.NetUtils; |
|
281 | 282 | /** |
282 | 283 | * VirtualNetworkApplianceManagerImpl manages the different types of virtual network appliances available in the Cloud Stack. |
283 | 284 | */ |
284 | | -@Local(value = {VirtualNetworkApplianceManager.class, VirtualNetworkApplianceService.class}) |
285 | | -public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements VirtualNetworkApplianceManager, VirtualNetworkApplianceService, VirtualMachineGuru, |
286 | | -Listener, Configurable { |
| 285 | +@Local(value = { VirtualNetworkApplianceManager.class, VirtualNetworkApplianceService.class }) |
| 286 | +public class VirtualNetworkApplianceManagerImpl extends ManagerBase implements VirtualNetworkApplianceManager, VirtualNetworkApplianceService, |
| 287 | + VirtualMachineGuru, Listener, Configurable, StateListener<State, VirtualMachine.Event, VirtualMachine> { |
287 | 288 | private static final Logger s_logger = Logger.getLogger(VirtualNetworkApplianceManagerImpl.class); |
288 | 289 |
|
289 | 290 | @Inject |
@@ -675,6 +676,8 @@ public boolean configure(final String name, final Map<String, Object> params) th |
675 | 676 | _checkExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("RouterStatusMonitor")); |
676 | 677 | _networkStatsUpdateExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("NetworkStatsUpdater")); |
677 | 678 |
|
| 679 | + VirtualMachine.State.getStateMachine().registerListener(this); |
| 680 | + |
678 | 681 | final Map<String, String> configs = _configDao.getConfiguration("AgentManager", params); |
679 | 682 |
|
680 | 683 | _routerRamSize = NumbersUtil.parseInt(configs.get("router.ram.size"), DEFAULT_ROUTER_VM_RAMSIZE); |
@@ -3435,7 +3438,6 @@ private void createVmDataCommand(final VirtualRouter router, final UserVm vm, fi |
3435 | 3438 | "vmdata", |
3436 | 3439 | generateVmDataCommand(router, nic.getIp4Address(), vm.getUserData(), serviceOffering, zoneName, nic.getIp4Address(), vm.getHostName(), vm.getInstanceName(), |
3437 | 3440 | vm.getId(), vm.getUuid(), publicKey, nic.getNetworkId())); |
3438 | | - |
3439 | 3441 | } |
3440 | 3442 |
|
3441 | 3443 | private void createVmDataCommandForVMs(final DomainRouterVO router, final Commands cmds, final long guestNetworkId) { |
@@ -4228,4 +4230,39 @@ public String getConfigComponentName() { |
4228 | 4230 | public ConfigKey<?>[] getConfigKeys() { |
4229 | 4231 | return new ConfigKey<?>[] {UseExternalDnsServers, routerVersionCheckEnabled, SetServiceMonitor}; |
4230 | 4232 | } |
| 4233 | + |
| 4234 | + @Override |
| 4235 | + public boolean preStateTransitionEvent(State oldState, VirtualMachine.Event event, State newState, VirtualMachine vo, boolean status, Object opaque) { |
| 4236 | + return true; |
| 4237 | + } |
| 4238 | + |
| 4239 | + @Override |
| 4240 | + public boolean postStateTransitionEvent(State oldState, VirtualMachine.Event event, State newState, VirtualMachine vo, boolean status, Object opaque) { |
| 4241 | + if (oldState == State.Stopped && event == VirtualMachine.Event.FollowAgentPowerOnReport && newState == State.Running) { |
| 4242 | + if (vo.getType() == VirtualMachine.Type.DomainRouter) { |
| 4243 | + s_logger.info("Schedule a router reboot task as router " + vo.getId() + " is powered-on out-of-band. we need to reboot to refresh network rules"); |
| 4244 | + _executor.schedule(new RebootTask(vo.getId()), 1000, TimeUnit.MICROSECONDS); |
| 4245 | + } |
| 4246 | + } |
| 4247 | + return true; |
| 4248 | + } |
| 4249 | + |
| 4250 | + protected class RebootTask extends ManagedContextRunnable { |
| 4251 | + |
| 4252 | + long _routerId; |
| 4253 | + |
| 4254 | + public RebootTask(long routerId) { |
| 4255 | + _routerId = routerId; |
| 4256 | + } |
| 4257 | + |
| 4258 | + @Override |
| 4259 | + protected void runInContext() { |
| 4260 | + try { |
| 4261 | + s_logger.info("Reboot router " + _routerId + " to refresh network rules"); |
| 4262 | + rebootRouter(_routerId, true); |
| 4263 | + } catch (Exception e) { |
| 4264 | + s_logger.warn("Error while rebooting the router", e); |
| 4265 | + } |
| 4266 | + } |
| 4267 | + } |
4231 | 4268 | } |
0 commit comments