Skip to content

Commit

Permalink
dcache: alarm command line, fix minor bug
Browse files Browse the repository at this point in the history
module: dcache, alarm command line interpreter

This patch does the following:

1.  Fixes bug in add and modify whereby includeInKey was not getting updated.  It also adds "group" to the help message for the attribute (missing previously).
2.  Prints out the full definition for confirmation/review before commit in both add and modify.
3.  Allows the elimination of an attribute (XML element) via the '-' character.

Target: master
Patch: http://rb.dcache.org/r/5573
Require-notes: yes
Require-book: no
Request: 2.6
Acked-by: Tigran

Testing: Tried out various options using add, remove and modify for illegal alteration by deletion of required elements; modified includeInKey, and tried to set group (with number and without, which is an error).

RELEASE NOTES:

For those who have tried out the dcache alarm definition commands, they may have noticed that "includeInKey" could not be modified once it was defined.  This bug has been fixed.  There is also a new feature allowing the deletion of a definition element using the '-' character.  The full xml for the definition is now also displayed before asking for confirmation to write.
  • Loading branch information
alrossi committed May 20, 2013
1 parent 21b6b73 commit 8b658b3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,14 @@ public static void main(String[] args) {

static void printError(Throwable t) {
if (t != null) {
System.err.println(t.getMessage());
if (t instanceof NullPointerException) {
/*
* a bug, shouldn't happen
*/
t.printStackTrace();
} else {
System.err.println(t.getMessage());
}
t = t.getCause();
}
while (t != null) {
Expand All @@ -205,7 +212,9 @@ private static void configure(AlarmDefinition definition,

try {
definition.validate();

Element alarmType = definition.toElement();
printDefinition(alarmType, outputter);

if (proceedWith("Add/Update definition", reader)) {
update(alarmType, outputter, rootNode, xmlFile);
Expand Down Expand Up @@ -357,7 +366,9 @@ private static void verifyInput(String option, AlarmDefinition definition,

while (true) {
try {
String input = getInput("hit return to skip", reader);
String input = getInput("hit return to skip, "
+ AlarmDefinition.RM
+ " to remove value", reader);
if (input == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public class AlarmDefinition {
public static final String REGEX_FLAGS_TAG = "regexFlags";
public static final String THREAD_TAG = "thread";
public static final String INCLUDE_IN_KEY_DELIMITER = "[\\s]";
public static final String RM = "-";

public static final ImmutableList<String> ATTRIBUTES
= new ImmutableList.Builder<String>()
Expand All @@ -235,6 +236,7 @@ public class AlarmDefinition {
= new ImmutableSet.Builder<String>()
.add(IAlarms.TIMESTAMP_TAG)
.add(IAlarms.MESSAGE_TAG)
.add(IAlarms.GROUP_TAG + "N")
.add(LOGGER_TAG)
.add(IAlarms.TYPE_TAG)
.add(IAlarms.DOMAIN_TAG)
Expand Down Expand Up @@ -510,6 +512,7 @@ public void setDepth(Integer depth) {
public void setIncludeInKey(String includeInKey) {
Preconditions.checkNotNull(includeInKey);
String[] keyNames = includeInKey.split(INCLUDE_IN_KEY_DELIMITER);
hashedKeyElements.clear();
Collections.addAll(hashedKeyElements, keyNames);
}

Expand Down Expand Up @@ -554,39 +557,39 @@ public Element toElement() {
.setText(String.valueOf(depth)));
}
String key = getIncludeInKey();
if (key != null) {
if (key != null && !key.isEmpty()) {
alarmType.addContent(new Element(INCLUDE_IN_KEY_TAG)
.setText(key));
}
if (level != null) {
alarmType.addContent(new Element(LEVEL_TAG)
.setText(level.toString()));
}
if (logger != null) {
if (logger != null && !logger.isEmpty()) {
alarmType.addContent(new Element(LOGGER_TAG)
.setText(logger));
}
if (matchException != null && !matchException) {
alarmType.addContent(new Element(MATCH_EXCEPTION_TAG)
.setText(matchException.toString()));
}
if (regexStr != null) {
if (regexStr != null && !regexStr.isEmpty()) {
alarmType.addContent(new Element(REGEX_TAG)
.setText(regexStr));
}
if (regexFlags != null) {
if (regexFlags != null && !regexFlags.isEmpty()) {
alarmType.addContent(new Element(REGEX_FLAGS_TAG)
.setText(regexFlags));
}
if (severity != null) {
alarmType.addContent(new Element(IAlarms.SEVERITY_TAG)
.setText(severity.toString()));
}
if (thread != null) {
if (thread != null && !thread.isEmpty()) {
alarmType.addContent(new Element(THREAD_TAG)
.setText(thread));
}
if (type != null) {
if (type != null && !type.isEmpty()) {
alarmType.addContent(new Element(IAlarms.TYPE_TAG)
.setText(type));
}
Expand All @@ -612,10 +615,10 @@ public void validate() throws AlarmDefinitionValidationException {

public void validateAndSet(String name, String value)
throws AlarmDefinitionValidationException {
if (value.length() == 0 ) {
value = value.trim();

if (value.length() == 0 || RM.equals(value)) {
value = null;
} else {
value = value.trim();
}

switch(name) {
Expand All @@ -633,23 +636,22 @@ public void validateAndSet(String name, String value)
break;
case INCLUDE_IN_KEY_TAG:
if (value == null) {
hashedKeyElements.clear();
return;
}

String[] parts = value.split("[\\s]");
for (String part : parts) {
if (!KEY_VALUES.contains(part.trim())) {
if (part.startsWith(IAlarms.GROUP_TAG)) {
try {
Integer.parseInt(part.substring(5));
} catch (NumberFormatException e) {
throw new AlarmDefinitionValidationException
(part + " must end in an integer");
}
} else {
if (part.startsWith(IAlarms.GROUP_TAG)) {
try {
Integer.parseInt(part.substring(5));
} catch (NumberFormatException e) {
throw new AlarmDefinitionValidationException
(part + " is not a valid key field");
(IAlarms.GROUP_TAG + " must end in an integer");
}
} else if (!KEY_VALUES.contains(part.trim())) {
throw new AlarmDefinitionValidationException
(part + " is not a valid key field");
}
}
setIncludeInKey(value);
Expand Down Expand Up @@ -705,6 +707,7 @@ public void validateAndSet(String name, String value)
+ " is not a valid flag");
}
}

regexFlags = value;
break;
case IAlarms.SEVERITY_TAG:
Expand Down

0 comments on commit 8b658b3

Please sign in to comment.