use of org.opennms.netmgt.config.destinationPaths.Target in project opennms by OpenNMS.
the class DestinationWizardServlet method copyPath.
// have to copy a path field by field until we get a cloning method in the
// JAXB generated classes
private static Path copyPath(Path oldPath) {
Path newPath = new Path();
newPath.setName(oldPath.getName());
newPath.setInitialDelay(oldPath.getInitialDelay().orElse(null));
Collection<Target> targets = oldPath.getTargets();
Iterator<Target> it = targets.iterator();
while (it.hasNext()) {
newPath.addTarget(copyTarget(it.next()));
}
Collection<Escalate> escalations = oldPath.getEscalates();
Iterator<Escalate> ie = escalations.iterator();
while (ie.hasNext()) {
Escalate curEscalate = ie.next();
Escalate newEscalate = new Escalate();
newEscalate.setDelay(curEscalate.getDelay());
Collection<Target> esTargets = curEscalate.getTargets();
Iterator<Target> iet = esTargets.iterator();
while (iet.hasNext()) {
newEscalate.addTarget(copyTarget((Target) iet.next()));
}
newPath.addEscalate(newEscalate);
}
return newPath;
}
use of org.opennms.netmgt.config.destinationPaths.Target in project opennms by OpenNMS.
the class BroadcastEventProcessor method scheduleNoticesForEvent.
/**
*/
private void scheduleNoticesForEvent(Event event) {
boolean mapsToNotice = false;
try {
mapsToNotice = getNotificationManager().hasUei(event.getUei());
} catch (Throwable e) {
LOG.error("Couldn't map uei {} to a notification entry, not scheduling notice.", event.getUei(), e);
return;
}
if (mapsToNotice) {
// in the event
if (continueWithNotice(event)) {
Notification[] notifications = null;
try {
notifications = getNotificationManager().getNotifForEvent(event);
} catch (Throwable e) {
LOG.error("Couldn't get notification mapping for event {}, not scheduling notice.", event.getUei(), e);
return;
}
long nodeid = event.getNodeid();
String ipaddr = event.getInterface();
if (notifications != null) {
for (Notification notification : notifications) {
int noticeId = 0;
try {
noticeId = getNotificationManager().getNoticeId();
} catch (Throwable e) {
LOG.error("Failed to get a unique id # for notification, exiting this notification", e);
continue;
}
Map<String, String> paramMap = buildParameterMap(notification, event, noticeId);
String queueID = (notification.getNoticeQueue().orElse("default"));
if (LOG.isDebugEnabled()) {
LOG.debug("destination : {}", notification.getDestinationPath());
LOG.debug("text message: {}", paramMap.get(NotificationManager.PARAM_TEXT_MSG));
LOG.debug("num message : {}", paramMap.get(NotificationManager.PARAM_NUM_MSG));
LOG.debug("subject : {}", paramMap.get(NotificationManager.PARAM_SUBJECT));
LOG.debug("node : {}", paramMap.get(NotificationManager.PARAM_NODE));
LOG.debug("interface : {}", paramMap.get(NotificationManager.PARAM_INTERFACE));
LOG.debug("service : {}", paramMap.get(NotificationManager.PARAM_SERVICE));
}
// get the target and escalation information
Path path = null;
try {
path = getDestinationPathManager().getPath(notification.getDestinationPath());
if (path == null) {
LOG.warn("Unknown destination path {}. Please check the <destinationPath> tag for the notification {} in the notifications.xml file.", notification.getDestinationPath(), notification.getName());
// return;
continue;
}
} catch (Throwable e) {
LOG.error("Could not get destination path for {}, please check the destinationPath.xml for errors.", notification.getDestinationPath(), e);
return;
}
final String initialDelay = path.getInitialDelay().orElse(Path.DEFAULT_INITIAL_DELAY);
Target[] targets = path.getTargets().toArray(new Target[0]);
Escalate[] escalations = path.getEscalates().toArray(new Escalate[0]);
// notification, if none then generate an event a exit
try {
if (getUserCount(targets, escalations) == 0) {
LOG.warn("The path {} assigned to notification {} has no targets or escalations specified, not sending notice.", notification.getDestinationPath(), notification.getName());
sendNotifEvent(EventConstants.NOTIFICATION_WITHOUT_USERS, "The path " + notification.getDestinationPath() + " assigned to notification " + notification.getName() + " has no targets or escalations specified.", "The message of the notification is as follows: " + paramMap.get(NotificationManager.PARAM_TEXT_MSG));
return;
}
} catch (Throwable e) {
LOG.error("Failed to get count of users in destination path {}, exiting notification.", notification.getDestinationPath(), e);
return;
}
try {
LOG.info("Inserting notification #{} into database: {}", noticeId, paramMap.get(NotificationManager.PARAM_SUBJECT));
getNotificationManager().insertNotice(noticeId, paramMap, queueID, notification);
} catch (SQLException e) {
LOG.error("Failed to enter notification into database, exiting this notification", e);
return;
}
long startTime = System.currentTimeMillis() + TimeConverter.convertToMillis(initialDelay);
// Find the first outage which applies at this time
String scheduledOutageName = scheduledOutage(nodeid, ipaddr);
if (scheduledOutageName != null) {
// Must decide what to do
if (autoAckExistsForEvent(event.getUei())) {
// Defer starttime till the given outage ends -
// if the auto ack catches the other event
// before then,
// then the page will not be sent
Calendar endOfOutage = getPollOutagesConfigManager().getEndOfOutage(scheduledOutageName);
startTime = endOfOutage.getTime().getTime();
} else {
// with the next notification (for
continue;
// loop)
}
}
List<NotificationTask> targetSiblings = new ArrayList<NotificationTask>();
try {
synchronized (m_noticeQueues) {
NoticeQueue noticeQueue = m_noticeQueues.get(queueID);
processTargets(targets, targetSiblings, noticeQueue, startTime, paramMap, noticeId);
processEscalations(escalations, targetSiblings, noticeQueue, startTime, paramMap, noticeId);
}
} catch (Throwable e) {
LOG.error("notice not scheduled due to error: ", e);
}
}
} else {
LOG.debug("Event doesn't match a notice: {} : {} : {} : {}", event.getUei(), nodeid, ipaddr, event.getService());
}
}
} else {
LOG.debug("No notice match for uei: {}", event.getUei());
}
}
use of org.opennms.netmgt.config.destinationPaths.Target in project opennms by OpenNMS.
the class DestinationWizardServlet method copyTarget.
private static Target copyTarget(Target target) {
Target newTarget = new Target();
newTarget.setName(target.getName());
newTarget.setInterval(target.getInterval().orElse(null));
newTarget.setAutoNotify(target.getAutoNotify().orElse(null));
for (int i = 0; i < target.getCommands().toArray(new String[0]).length; i++) {
newTarget.addCommand(target.getCommands().toArray(new String[0])[i]);
}
return newTarget;
}
use of org.opennms.netmgt.config.destinationPaths.Target in project opennms by OpenNMS.
the class DestinationWizardServlet method doPost.
// FIXME: Unused
// private String SOURCE_PAGE_NAME = "pathName.jsp";
//
// private String SOURCE_PAGE_ESCALATE_REMOVE = "removeEscalation.jsp";
//
// private String SOURCE_PAGE_ESCALATE_ADD = "addEscalation.jsp";
/** {@inheritDoc} */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
DestinationPathFactory.init();
} catch (FileNotFoundException e1) {
throw new ServletException("Exception initializing DestinationPatchFactory " + e1.getMessage(), e1);
} catch (IOException e1) {
throw new ServletException("Exception initializing DestinationPatchFactory " + e1.getMessage(), e1);
}
String sourcePage = request.getParameter("sourcePage");
HttpSession user = request.getSession(true);
StringBuffer redirectString = new StringBuffer();
if (sourcePage.equals(SOURCE_PAGE_PATHS)) {
String action = request.getParameter("userAction");
if (action.equals("edit")) {
// get the path that was chosen in the select
try {
Path oldPath = DestinationPathFactory.getInstance().getPath(request.getParameter("paths"));
user.setAttribute(SESSION_ATTRIBUTE_OLD_PATH, oldPath);
user.setAttribute(SESSION_ATTRIBUTE_OLD_PATH_NAME, oldPath.getName());
// copy the old path into the new path
Path newPath = copyPath(oldPath);
user.setAttribute(SESSION_ATTRIBUTE_NEW_PATH, newPath);
redirectString.append(SOURCE_PAGE_OUTLINE);
} catch (Throwable e) {
throw new ServletException("Couldn't get path to edit.", e);
}
} else if (action.equals("delete")) {
try {
DestinationPathFactory.getInstance().removePath(request.getParameter("paths"));
redirectString.append(SOURCE_PAGE_PATHS);
} catch (Throwable e) {
throw new ServletException("Couldn't save/reload destination path configuration file.", e);
}
} else if (action.equals("new")) {
Path newPath = new Path();
user.setAttribute(SESSION_ATTRIBUTE_NEW_PATH, newPath);
// Make sure that no oldPath is set since we're creating a new path from scratch
user.removeAttribute(SESSION_ATTRIBUTE_OLD_PATH);
user.removeAttribute(SESSION_ATTRIBUTE_OLD_PATH_NAME);
redirectString.append(SOURCE_PAGE_OUTLINE);
}
} else if (sourcePage.equals(SOURCE_PAGE_OUTLINE)) {
String action = request.getParameter("userAction");
Path path = (Path) user.getAttribute(SESSION_ATTRIBUTE_NEW_PATH);
// http://issues.opennms.org/browse/NMS-5269
if (path == null) {
redirectString.append(SOURCE_PAGE_PATHS);
} else {
// load all changeable values from the outline page into the editing
// path
saveOutlineForm(path, request);
if (action.equals("add")) {
int index = WebSecurityUtils.safeParseInt(request.getParameter("index"));
Escalate newEscalate = new Escalate();
newEscalate.setDelay("0s");
path.addEscalate(index, newEscalate);
Map<String, String> requestParams = new HashMap<String, String>();
requestParams.put("targetIndex", request.getParameter("index"));
redirectString.append(SOURCE_PAGE_TARGETS).append(makeQueryString(requestParams));
} else if (action.equals("remove")) {
int index = WebSecurityUtils.safeParseInt(request.getParameter("index"));
removeEscalation(path, index);
redirectString.append(SOURCE_PAGE_OUTLINE);
} else if (action.equals("edit")) {
Map<String, String> requestParams = new HashMap<String, String>();
requestParams.put("targetIndex", request.getParameter("index"));
redirectString.append(SOURCE_PAGE_TARGETS).append(makeQueryString(requestParams));
} else if (action.equals("finish")) {
String oldName = (String) user.getAttribute(SESSION_ATTRIBUTE_OLD_PATH_NAME);
path.setName(request.getParameter("name"));
path.setInitialDelay(request.getParameter("initialDelay"));
try {
if (oldName != null && !oldName.equals(path.getName())) {
// replacing a path with a new name
DestinationPathFactory.getInstance().replacePath(oldName, path);
} else {
DestinationPathFactory.getInstance().addPath(path);
}
} catch (Throwable e) {
throw new ServletException("Couldn't save/reload destination path configuration file.", e);
}
// Must clear out this attribute for later edits
user.removeAttribute(SESSION_ATTRIBUTE_OLD_PATH);
user.removeAttribute(SESSION_ATTRIBUTE_OLD_PATH_NAME);
redirectString.append(SOURCE_PAGE_PATHS);
} else if (action.equals("cancel")) {
redirectString.append(SOURCE_PAGE_PATHS);
}
}
} else if (sourcePage.equals(SOURCE_PAGE_TARGETS)) {
// compare the list of targets chosen to the existing targets,
// replacing
// and creating new targets as necessary
String[] userTargets = request.getParameterValues("users");
String[] groupTargets = request.getParameterValues("groups");
String[] roleTargets = request.getParameterValues("roles");
String[] emailTargets = request.getParameterValues("emails");
Path newPath = (Path) user.getAttribute(SESSION_ATTRIBUTE_NEW_PATH);
int index = WebSecurityUtils.safeParseInt(request.getParameter("targetIndex"));
Target[] existingTargets = null;
try {
existingTargets = DestinationPathFactory.getInstance().getTargetList(index, newPath);
} catch (Throwable e) {
throw new ServletException("Unable to get targets for path " + newPath.getName(), e);
}
// remove all the targets from the path or escalation
if (index == -1) {
newPath.clearTargets();
} else {
final int index1 = index;
newPath.getEscalates().get(index1).clearTargets();
}
// reload the new targets into the path or escalation
if (userTargets != null) {
for (int i = 0; i < userTargets.length; i++) {
Target target = new Target();
target.setName(userTargets[i]);
// see if this target already exists
for (int j = 0; j < existingTargets.length; j++) {
if (userTargets[i].equals(existingTargets[j].getName())) {
target = existingTargets[j];
break;
}
}
if (index == -1)
newPath.addTarget(target);
else {
final int index1 = index;
newPath.getEscalates().get(index1).addTarget(target);
}
}
}
if (groupTargets != null) {
for (int k = 0; k < groupTargets.length; k++) {
Target target = new Target();
target.setName(groupTargets[k]);
// see if this target already exists
for (int j = 0; j < existingTargets.length; j++) {
if (groupTargets[k].equals(existingTargets[j].getName())) {
target = existingTargets[j];
break;
}
}
if (index == -1)
newPath.addTarget(target);
else {
final int index1 = index;
newPath.getEscalates().get(index1).addTarget(target);
}
}
}
if (roleTargets != null) {
for (int k = 0; k < roleTargets.length; k++) {
Target target = new Target();
target.setName(roleTargets[k]);
// see if this target already exists
for (int j = 0; j < existingTargets.length; j++) {
if (roleTargets[k].equals(existingTargets[j].getName())) {
target = existingTargets[j];
break;
}
}
if (index == -1)
newPath.addTarget(target);
else {
final int index1 = index;
newPath.getEscalates().get(index1).addTarget(target);
}
}
}
if (emailTargets != null) {
for (int l = 0; l < emailTargets.length; l++) {
Target target = new Target();
target.setName(emailTargets[l]);
target.addCommand("email");
// see if this target already exists
for (int m = 0; m < existingTargets.length; m++) {
if (emailTargets[l].equals(existingTargets[m].getName())) {
target = existingTargets[m];
break;
}
}
if (index == -1)
newPath.addTarget(target);
else {
final int index1 = index;
newPath.getEscalates().get(index1).addTarget(target);
}
}
}
Map<String, String> requestParams = new HashMap<String, String>();
requestParams.put("targetIndex", request.getParameter("targetIndex"));
String redirectPage = request.getParameter("nextPage");
redirectString.append(redirectPage);
if (redirectPage.equals(SOURCE_PAGE_INTERVALS)) {
String[] ignores = { "sourcePage", "nextPage", "users" };
redirectString.append("?").append(Util.makeQueryString(request, ignores));
} else {
redirectString.append(makeQueryString(requestParams));
}
} else if (sourcePage.equals(SOURCE_PAGE_INTERVALS)) {
Path newPath = (Path) user.getAttribute(SESSION_ATTRIBUTE_NEW_PATH);
int index = WebSecurityUtils.safeParseInt(request.getParameter("targetIndex"));
Target[] targets = null;
try {
targets = DestinationPathFactory.getInstance().getTargetList(index, newPath);
} catch (Throwable e) {
throw new ServletException("Couldn't get target list for path " + newPath.getName(), e);
}
for (int i = 0; i < targets.length; i++) {
String name = targets[i].getName();
if (request.getParameter(name + "Interval") != null) {
targets[i].setInterval(request.getParameter(name + "Interval"));
}
}
Map<String, String> requestParams = new HashMap<String, String>();
requestParams.put("targetIndex", request.getParameter("targetIndex"));
redirectString.append(SOURCE_PAGE_COMMANDS).append(makeQueryString(requestParams));
} else if (sourcePage.equals(SOURCE_PAGE_COMMANDS)) {
Path newPath = (Path) user.getAttribute(SESSION_ATTRIBUTE_NEW_PATH);
int index = WebSecurityUtils.safeParseInt(request.getParameter("targetIndex"));
Target[] targets = null;
try {
targets = DestinationPathFactory.getInstance().getTargetList(index, newPath);
} catch (Throwable e) {
throw new ServletException("Couldn't get target list for path " + newPath.getName(), e);
}
for (int i = 0; i < targets.length; i++) {
String name = targets[i].getName();
// don't overwrite the email target command
if (targets[i].getName().indexOf("@") == -1) {
targets[i].clearCommands();
String[] commands = request.getParameterValues(name + "Commands");
for (int j = 0; j < commands.length; j++) {
targets[i].addCommand(commands[j]);
}
}
String[] autoNotify = request.getParameterValues(name + "AutoNotify");
if (autoNotify[0] == null) {
autoNotify[0] = "auto";
}
targets[i].setAutoNotify(autoNotify[0]);
}
redirectString.append(SOURCE_PAGE_OUTLINE);
}
response.sendRedirect(redirectString.toString());
}
Aggregations