@Test public void testHandleEvent_withContext_noRule() throws Exception { List<Rule> rules = new ArrayList<>( 0 ); Application app = new Application( "app", new TestApplicationTemplate()); AutonomicApplicationContext ctx = Mockito.spy( new AutonomicApplicationContext( app )); Mockito.when( ctx.findRulesToExecute()).thenReturn( rules ); this.autonomicMngr.appNameToContext.put( "app", ctx ); ManagedApplication ma = new ManagedApplication( app ); this.autonomicMngr.handleEvent( ma, new MsgNotifAutonomic( "app", "/root", "event", null )); Mockito.verifyZeroInteractions( this.commandsMngr ); Mockito.verifyZeroInteractions( this.preferencesMngr ); Map<?,?> eventNameToLastRecordTime = TestUtils.getInternalField( ctx, "eventNameToLastRecordTime", Map.class ); Assert.assertEquals( 1, eventNameToLastRecordTime.size()); Assert.assertNotNull( eventNameToLastRecordTime.get( "event" )); }
@Override public void handleEvent( ManagedApplication ma, MsgNotifAutonomic event ) { try { this.logger.fine( "Autonomic event '" + event.getEventName() + "' is about to be recorded." ); AutonomicApplicationContext ctx = this.appNameToContext.get( ma.getName()); if( ctx == null ) { this.logger.fine( "No autonomic context was found for application " + ma.getApplication() + "." ); } else { ctx.registerEvent( event.getEventName()); List<Rule> rulesToExecute = ctx.findRulesToExecute(); if( rulesToExecute.isEmpty()) { this.logger.fine( "No rule was found after the event '" + event.getEventName() + "' occurred." ); } else { String strictMaxVmAS = this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_STRICT_MAX_VM_NUMBER, "true" ); boolean strictMaxVm = Boolean.parseBoolean( strictMaxVmAS ); String maxVmCountAS = this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_MAX_VM_NUMBER, "" + Integer.MAX_VALUE ); int maxVmCount = Integer.parseInt( maxVmCountAS ); if( maxVmCount < 0 ) maxVmCount = Integer.MAX_VALUE; if( maxVmCount <= this.autonomicVmCount.get()) strictMaxVm = true; CommandExecutionContext execCtx = new CommandExecutionContext( this.autonomicVmCount, ctx.getVmCount(), maxVmCount, strictMaxVm, AUTONOMIC_MARKER, "" ); for( Rule rule : rulesToExecute ) { this.logger.fine( "Applying rule '" + rule.getRuleName() + "' for event '" + event.getEventName() + "'." ); ctx.recordPreExecution( rule.getRuleName()); for( String commandName : rule.getCommandsToInvoke()) this.commandsMngr.execute( ma.getApplication(), commandName, execCtx, CommandHistoryItem.ORIGIN_AUTONOMIC, rule.getRuleName()); } } } } catch( Exception e ) { this.logger.warning( "An autonomic event could not be handled. " + e.getMessage()); Utils.logException( this.logger, e ); } }
AutonomicMngrImpl implements IAutonomicMngr { @Override public void handleEvent( ManagedApplication ma, MsgNotifAutonomic event ) { try { this.logger.fine( "Autonomic event '" + event.getEventName() + "' is about to be recorded." ); AutonomicApplicationContext ctx = this.appNameToContext.get( ma.getName()); if( ctx == null ) { this.logger.fine( "No autonomic context was found for application " + ma.getApplication() + "." ); } else { ctx.registerEvent( event.getEventName()); List<Rule> rulesToExecute = ctx.findRulesToExecute(); if( rulesToExecute.isEmpty()) { this.logger.fine( "No rule was found after the event '" + event.getEventName() + "' occurred." ); } else { String strictMaxVmAS = this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_STRICT_MAX_VM_NUMBER, "true" ); boolean strictMaxVm = Boolean.parseBoolean( strictMaxVmAS ); String maxVmCountAS = this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_MAX_VM_NUMBER, "" + Integer.MAX_VALUE ); int maxVmCount = Integer.parseInt( maxVmCountAS ); if( maxVmCount < 0 ) maxVmCount = Integer.MAX_VALUE; if( maxVmCount <= this.autonomicVmCount.get()) strictMaxVm = true; CommandExecutionContext execCtx = new CommandExecutionContext( this.autonomicVmCount, ctx.getVmCount(), maxVmCount, strictMaxVm, AUTONOMIC_MARKER, "" ); for( Rule rule : rulesToExecute ) { this.logger.fine( "Applying rule '" + rule.getRuleName() + "' for event '" + event.getEventName() + "'." ); ctx.recordPreExecution( rule.getRuleName()); for( String commandName : rule.getCommandsToInvoke()) this.commandsMngr.execute( ma.getApplication(), commandName, execCtx, CommandHistoryItem.ORIGIN_AUTONOMIC, rule.getRuleName()); } } } } catch( Exception e ) { this.logger.warning( "An autonomic event could not be handled. " + e.getMessage()); Utils.logException( this.logger, e ); } } }
AutonomicMngrImpl implements IAutonomicMngr { @Override public void handleEvent( ManagedApplication ma, MsgNotifAutonomic event ) { try { this.logger.fine( "Autonomic event '" + event.getEventName() + "' is about to be recorded." ); AutonomicApplicationContext ctx = this.appNameToContext.get( ma.getName()); if( ctx == null ) { this.logger.fine( "No autonomic context was found for application " + ma.getApplication() + "." ); } else { ctx.registerEvent( event.getEventName()); List<Rule> rulesToExecute = ctx.findRulesToExecute(); if( rulesToExecute.isEmpty()) { this.logger.fine( "No rule was found after the event '" + event.getEventName() + "' occurred." ); } else { String strictMaxVmAS = this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_STRICT_MAX_VM_NUMBER, "true" ); boolean strictMaxVm = Boolean.parseBoolean( strictMaxVmAS ); String maxVmCountAS = this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_MAX_VM_NUMBER, "" + Integer.MAX_VALUE ); int maxVmCount = Integer.parseInt( maxVmCountAS ); if( maxVmCount < 0 ) maxVmCount = Integer.MAX_VALUE; if( maxVmCount <= this.autonomicVmCount.get()) strictMaxVm = true; CommandExecutionContext execCtx = new CommandExecutionContext( this.autonomicVmCount, ctx.getVmCount(), maxVmCount, strictMaxVm, AUTONOMIC_MARKER, "" ); for( Rule rule : rulesToExecute ) { this.logger.fine( "Applying rule '" + rule.getRuleName() + "' for event '" + event.getEventName() + "'." ); ctx.recordPreExecution( rule.getRuleName()); for( String commandName : rule.getCommandsToInvoke()) this.commandsMngr.execute( ma.getApplication(), commandName, execCtx, CommandHistoryItem.ORIGIN_AUTONOMIC, rule.getRuleName()); } } } } catch( Exception e ) { this.logger.warning( "An autonomic event could not be handled. " + e.getMessage()); Utils.logException( this.logger, e ); } } AutonomicMngrImpl( ICommandsMngr commandsMngr ); }
AutonomicMngrImpl implements IAutonomicMngr { @Override public void handleEvent( ManagedApplication ma, MsgNotifAutonomic event ) { try { this.logger.fine( "Autonomic event '" + event.getEventName() + "' is about to be recorded." ); AutonomicApplicationContext ctx = this.appNameToContext.get( ma.getName()); if( ctx == null ) { this.logger.fine( "No autonomic context was found for application " + ma.getApplication() + "." ); } else { ctx.registerEvent( event.getEventName()); List<Rule> rulesToExecute = ctx.findRulesToExecute(); if( rulesToExecute.isEmpty()) { this.logger.fine( "No rule was found after the event '" + event.getEventName() + "' occurred." ); } else { String strictMaxVmAS = this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_STRICT_MAX_VM_NUMBER, "true" ); boolean strictMaxVm = Boolean.parseBoolean( strictMaxVmAS ); String maxVmCountAS = this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_MAX_VM_NUMBER, "" + Integer.MAX_VALUE ); int maxVmCount = Integer.parseInt( maxVmCountAS ); if( maxVmCount < 0 ) maxVmCount = Integer.MAX_VALUE; if( maxVmCount <= this.autonomicVmCount.get()) strictMaxVm = true; CommandExecutionContext execCtx = new CommandExecutionContext( this.autonomicVmCount, ctx.getVmCount(), maxVmCount, strictMaxVm, AUTONOMIC_MARKER, "" ); for( Rule rule : rulesToExecute ) { this.logger.fine( "Applying rule '" + rule.getRuleName() + "' for event '" + event.getEventName() + "'." ); ctx.recordPreExecution( rule.getRuleName()); for( String commandName : rule.getCommandsToInvoke()) this.commandsMngr.execute( ma.getApplication(), commandName, execCtx, CommandHistoryItem.ORIGIN_AUTONOMIC, rule.getRuleName()); } } } } catch( Exception e ) { this.logger.warning( "An autonomic event could not be handled. " + e.getMessage()); Utils.logException( this.logger, e ); } } AutonomicMngrImpl( ICommandsMngr commandsMngr ); void setPreferencesMngr( IPreferencesMngr preferencesMngr ); @Override void loadApplicationRules( Application app ); @Override void refreshApplicationRules( Application app, String ruleFileName ); @Override void unloadApplicationRules( Application app ); @Override void notifyVmWasDeletedByHand( Instance rootInstance ); @Override int getAutonomicInstancesCount(); @Override void handleEvent( ManagedApplication ma, MsgNotifAutonomic event ); }
AutonomicMngrImpl implements IAutonomicMngr { @Override public void handleEvent( ManagedApplication ma, MsgNotifAutonomic event ) { try { this.logger.fine( "Autonomic event '" + event.getEventName() + "' is about to be recorded." ); AutonomicApplicationContext ctx = this.appNameToContext.get( ma.getName()); if( ctx == null ) { this.logger.fine( "No autonomic context was found for application " + ma.getApplication() + "." ); } else { ctx.registerEvent( event.getEventName()); List<Rule> rulesToExecute = ctx.findRulesToExecute(); if( rulesToExecute.isEmpty()) { this.logger.fine( "No rule was found after the event '" + event.getEventName() + "' occurred." ); } else { String strictMaxVmAS = this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_STRICT_MAX_VM_NUMBER, "true" ); boolean strictMaxVm = Boolean.parseBoolean( strictMaxVmAS ); String maxVmCountAS = this.preferencesMngr.get( IPreferencesMngr.AUTONOMIC_MAX_VM_NUMBER, "" + Integer.MAX_VALUE ); int maxVmCount = Integer.parseInt( maxVmCountAS ); if( maxVmCount < 0 ) maxVmCount = Integer.MAX_VALUE; if( maxVmCount <= this.autonomicVmCount.get()) strictMaxVm = true; CommandExecutionContext execCtx = new CommandExecutionContext( this.autonomicVmCount, ctx.getVmCount(), maxVmCount, strictMaxVm, AUTONOMIC_MARKER, "" ); for( Rule rule : rulesToExecute ) { this.logger.fine( "Applying rule '" + rule.getRuleName() + "' for event '" + event.getEventName() + "'." ); ctx.recordPreExecution( rule.getRuleName()); for( String commandName : rule.getCommandsToInvoke()) this.commandsMngr.execute( ma.getApplication(), commandName, execCtx, CommandHistoryItem.ORIGIN_AUTONOMIC, rule.getRuleName()); } } } } catch( Exception e ) { this.logger.warning( "An autonomic event could not be handled. " + e.getMessage()); Utils.logException( this.logger, e ); } } AutonomicMngrImpl( ICommandsMngr commandsMngr ); void setPreferencesMngr( IPreferencesMngr preferencesMngr ); @Override void loadApplicationRules( Application app ); @Override void refreshApplicationRules( Application app, String ruleFileName ); @Override void unloadApplicationRules( Application app ); @Override void notifyVmWasDeletedByHand( Instance rootInstance ); @Override int getAutonomicInstancesCount(); @Override void handleEvent( ManagedApplication ma, MsgNotifAutonomic event );  }