From: <pn...@hy...> - 2009-10-23 22:24:52
|
Author: pnguyen Date: 2009-10-23 15:24:38 -0700 (Fri, 23 Oct 2009) New Revision: 13900 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=13900 Modified: trunk/src/org/hyperic/hq/bizapp/server/session/EventsBossEJBImpl.java trunk/src/org/hyperic/hq/events/server/session/AlertDefinitionManagerEJBImpl.java Log: [HQ-1894] While creating and updating alert defs, create (if necessary) the EnableAlertDefAction alert action for recovery alerts and the MetricAlertAction alert action for threshold, change, and baseline condition types. Modified: trunk/src/org/hyperic/hq/bizapp/server/session/EventsBossEJBImpl.java =================================================================== --- trunk/src/org/hyperic/hq/bizapp/server/session/EventsBossEJBImpl.java 2009-10-23 08:44:31 UTC (rev 13899) +++ trunk/src/org/hyperic/hq/bizapp/server/session/EventsBossEJBImpl.java 2009-10-23 22:24:38 UTC (rev 13900) @@ -458,6 +458,9 @@ adval.setAppdefId(aetid.getId()); adval.setParentId(EventConstants.TYPE_ALERT_DEF_ID); + // Create a measurement AlertLogAction if necessary + setMetricAlertAction(adval); + // Now create the alert definition if (debug) watch.markTimeBegin("createParentAlertDefinition"); parent = getADM().createAlertDefinition(subject, adval); @@ -531,9 +534,6 @@ // Make sure the actions have the proper parentId cloneParentActions(id, adval, parent.getActions()); - // Create a measurement AlertLogAction if necessary - setMetricAlertAction(adval); - // Now create the alert definition if (debug) childWatch.markTimeBegin("createAlertDefinition"); AlertDefinitionValue newAdval = adm.createAlertDefinition(subject, adval); @@ -560,6 +560,21 @@ return parent; } + /** + * Get the MetricAlertAction ActionValue from an + * AlertDefinitionValue. If none exists, return null. + */ + private ActionValue getMetricAlertAction(AlertDefinitionValue adv) { + ActionValue[] actions = adv.getActions(); + for (int i = 0; i < actions.length; ++i) { + String actionClass = actions[i].getClassname(); + if (MetricAlertAction.class.getName().equals(actionClass)) { + return actions[i]; + } + } + return null; + } + private void setMetricAlertAction(AlertDefinitionValue adval) { AlertConditionValue[] conds = adval.getConditions(); for (int i = 0; i < conds.length; i++) { @@ -567,19 +582,25 @@ conds[i].getType() == EventConstants.TYPE_BASELINE || conds[i].getType() == EventConstants.TYPE_CHANGE) { - ActionValue action = new ActionValue(); - action.setClassname(MetricAlertAction.class.getName()); + ActionValue action = getMetricAlertAction(adval); + + // if MetricAlertAction doesn't exist, add one + if (action == null) { + action = new ActionValue(); + action.setClassname(MetricAlertAction.class.getName()); - ConfigResponse config = new ConfigResponse(); - try { - action.setConfig(config.encode()); - } catch (EncodingException e) { - // This should never happen - _log.error("Empty ConfigResponse threw an encoding error", - e); + ConfigResponse config = new ConfigResponse(); + try { + action.setConfig(config.encode()); + } catch (EncodingException e) { + // This should never happen + _log.error("Empty ConfigResponse threw an encoding error", + e); + } + + adval.addAction(action); } - - adval.addAction(action); + break; } } @@ -816,6 +837,9 @@ adval.getAppdefType() == AppdefEntityConstants.APPDEF_TYPE_GROUP) { // A little more work to do for group and type alert definition + // Create a measurement AlertLogAction if necessary + setMetricAlertAction(adval); + if (debug) watch.markTimeBegin("updateParentAlertDefinition"); adval = adm.updateAlertDefinition(adval); @@ -894,6 +918,9 @@ // Now create the new triggers createTriggers(subject, adval); + // Create a measurement AlertLogAction if necessary + setMetricAlertAction(adval); + // Now update the alert definition adm.updateAlertDefinition(adval); } Modified: trunk/src/org/hyperic/hq/events/server/session/AlertDefinitionManagerEJBImpl.java =================================================================== --- trunk/src/org/hyperic/hq/events/server/session/AlertDefinitionManagerEJBImpl.java 2009-10-23 08:44:31 UTC (rev 13899) +++ trunk/src/org/hyperic/hq/events/server/session/AlertDefinitionManagerEJBImpl.java 2009-10-23 22:24:38 UTC (rev 13900) @@ -273,25 +273,7 @@ } if (cond.getType() == EventConstants.TYPE_ALERT) { - ActionValue recoverAction = new ActionValue(); - - EnableAlertDefActionConfig action = - new EnableAlertDefActionConfig(); - action.setAlertDefId(cond.getMeasurementId()); - - recoverAction.setClassname(action.getImplementor()); - try { - recoverAction - .setConfig(action.getConfigResponse().encode()); - } catch (EncodingException e) { - log.debug("Error encoding EnableAlertDefAction", e); - } catch (InvalidOptionException e) { - log.debug("Error encoding EnableAlertDefAction", e); - } catch (InvalidOptionValueException e) { - log.debug("Error encoding EnableAlertDefAction", e); - } - - a.addAction(recoverAction); + setEnableAlertDefAction(a, cond.getMeasurementId()); } acDAO.save(cond); @@ -408,7 +390,7 @@ * Get the EnableAlertDefAction ActionValue from an * AlertDefinitionValue. If none exists, return null. */ - private ActionValue getEnableAction(AlertDefinitionValue adv) { + private ActionValue getEnableAlertDefAction(AlertDefinitionValue adv) { ActionValue[] actions = adv.getActions(); EnableAlertDefActionConfig cfg = new EnableAlertDefActionConfig(); for (int i = 0; i < actions.length; ++i) { @@ -419,6 +401,53 @@ return null; } + private void setEnableAlertDefAction(AlertDefinitionValue adval, int recoverId) { + EnableAlertDefActionConfig action = + new EnableAlertDefActionConfig(); + + // Find recovery actions first + ActionValue recoverAction = getEnableAlertDefAction(adval); + + if (recoverAction != null) { + try { + ConfigResponse configResponse = + ConfigResponse.decode(recoverAction.getConfig()); + action.init(configResponse); + + if (action.getAlertDefId() != recoverId) { + action.setAlertDefId(recoverId); + recoverAction.setConfig(action + .getConfigResponse() + .encode()); + adval.updateAction(recoverAction); + } + } catch (Exception e) { + adval.removeAction(recoverAction); + recoverAction = null; + } + } + + // Add action if doesn't exist + if (recoverAction == null) { + recoverAction = new ActionValue(); + action.setAlertDefId(recoverId); + recoverAction.setClassname(action.getImplementor()); + + try { + recoverAction + .setConfig(action.getConfigResponse().encode()); + } catch (EncodingException e) { + log.debug("Error encoding EnableAlertDefAction", e); + } catch (InvalidOptionException e) { + log.debug("Error encoding EnableAlertDefAction", e); + } catch (InvalidOptionValueException e) { + log.debug("Error encoding EnableAlertDefAction", e); + } + + adval.addAction(recoverAction); + } + } + /** * Update an alert definition * @ejb:interface-method @@ -430,11 +459,8 @@ AlertDefinitionDAO dao = getAlertDefDAO(); ActionDAO actDao = getActionDAO(); AlertDefinition aldef = dao.findById(adval.getId()); - - // Find recovery actions first - ActionValue recoverAction = getEnableAction(adval); - adval.removeAction(recoverAction); - + int recoverId = -1; + // See if the conditions changed if (adval.getAddedConditions().size() > 0 || adval.getUpdatedConditions().size() > 0 || @@ -452,61 +478,34 @@ if (conds[i].getTriggerId() != null) trigger = registeredTriggerManager.findById(conds[i].getTriggerId()); - if (conds[i].getType() == EventConstants.TYPE_ALERT) { - EnableAlertDefActionConfig action = - new EnableAlertDefActionConfig(); - if (recoverAction != null) { - try { - ConfigResponse configResponse = - ConfigResponse.decode(recoverAction.getConfig()); - action.init(configResponse); - - if (action.getAlertDefId() != - conds[i].getMeasurementId()) { - action.setAlertDefId(conds[i].getMeasurementId()); - recoverAction.setConfig(action - .getConfigResponse() - .encode()); - adval.updateAction(recoverAction); - } - } catch (Exception e) { - recoverAction = null; - } - } - - // Add action if doesn't exist - if (recoverAction == null) { - recoverAction = new ActionValue(); - action.setAlertDefId(conds[i].getMeasurementId()); - recoverAction.setClassname(action.getImplementor()); - - try { - recoverAction - .setConfig(action.getConfigResponse().encode()); - } catch (EncodingException e) { - log.debug("Error encoding EnableAlertDefAction", e); - } catch (InvalidOptionException e) { - log.debug("Error encoding EnableAlertDefAction", e); - } catch (InvalidOptionValueException e) { - log.debug("Error encoding EnableAlertDefAction", e); - } - - adval.addAction(recoverAction); - } + if (conds[i].getType() == EventConstants.TYPE_ALERT) { + recoverId = conds[i].getMeasurementId(); } aldef.createCondition(conds[i], trigger); } } + + if (recoverId > 0) { + setEnableAlertDefAction(adval, recoverId); + } else { + // Remove recover action if exists + ActionValue recoverAction = getEnableAlertDefAction(adval); + if (recoverAction != null) { + adval.removeAction(recoverAction); + } + } + // See if the actions changed if (adval.getAddedActions().size() > 0 || adval.getUpdatedActions().size() > 0 || - adval.getRemovedActions().size() > 0) + adval.getRemovedActions().size() > 0 || + adval.getActions().length != aldef.getActions().size()) { // We need to keep old actions around for the logs. So // we'll create new actions and update the alert - // definition, but we won't remove the old conditions. + // definition, but we won't remove the old actions. ActionValue[] actions = adval.getActions(); aldef.clearActions(); for (int i = 0; i < actions.length; i++) { |