Improve Patch Alarm Definition parsing

Change-Id: Id192a20fa8fc4e29cdbda89cc4470fec58ce7275
This commit is contained in:
Ryan Brandt 2015-05-07 09:51:15 -06:00
parent 957f468d25
commit c3b76f17c9
4 changed files with 116 additions and 61 deletions

View File

@ -0,0 +1,103 @@
/*
* Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package monasca.api.app.command;
import java.util.List;
import monasca.api.app.validation.AlarmValidation;
public class PatchAlarmDefinitionCommand {
public String name;
public String description;
public String severity;
public String expression;
public List<String> matchBy;
public Boolean actionsEnabled;
public List<String> alarmActions;
public List<String> okActions;
public List<String> undeterminedActions;
public PatchAlarmDefinitionCommand() {}
public void validate() {
AlarmValidation.validate(name, description, severity, alarmActions, okActions,
undeterminedActions);
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof PatchAlarmDefinitionCommand))
return false;
PatchAlarmDefinitionCommand other = (PatchAlarmDefinitionCommand) obj;
if (alarmActions == null) {
if (other.alarmActions != null)
return false;
} else if (!alarmActions.equals(other.alarmActions))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (expression == null) {
if (other.expression != null)
return false;
} else if (!expression.equals(other.expression))
return false;
if (matchBy == null) {
if (other.matchBy != null)
return false;
} else if (!matchBy.equals(other.matchBy))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (okActions == null) {
if (other.okActions != null)
return false;
} else if (!okActions.equals(other.okActions))
return false;
if (severity == null) {
if (other.severity != null)
return false;
} else if (!severity.equals(other.severity))
return false;
if (undeterminedActions == null) {
if (other.undeterminedActions != null)
return false;
} else if (!undeterminedActions.equals(other.undeterminedActions))
return false;
return true;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((alarmActions == null) ? 0 : alarmActions.hashCode());
result = prime * result + ((description == null) ? 0 : description.hashCode());
result = prime * result + ((expression == null) ? 0 : expression.hashCode());
result = prime * result + ((matchBy == null) ? 0 : matchBy.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((okActions == null) ? 0 : okActions.hashCode());
result = prime * result + ((severity == null) ? 0 : severity.hashCode());
result = prime * result + ((undeterminedActions == null) ? 0 : undeterminedActions.hashCode());
return result;
}
}

View File

@ -183,40 +183,4 @@ public final class Validation {
return Boolean.parseBoolean(mergeMetricsFlag);
}
}
public static String parseString(Object input, String parameterName) {
try {
return (String) input;
} catch (ClassCastException ex) {
throw Exceptions.unprocessableEntity("Field '%s' requires a string", parameterName);
}
}
public static List<String> parseListOfStrings(Object input, String parameterName){
try {
return (List<String>) input;
} catch (ClassCastException ex) {
}
try {
String inputStr = (String) input;
List<String> stringList = new ArrayList<>();
stringList.add(inputStr);
return stringList;
} catch (ClassCastException exInner) {
throw Exceptions.unprocessableEntity("Field '%s' requires a string or list of strings",
parameterName);
}
}
public static Boolean parseBoolean(Object input, String parameterName) {
try {
return (Boolean) input;
} catch (ClassCastException ex) {
}
try {
return Boolean.parseBoolean((String) input);
} catch (ClassCastException ex) {
throw Exceptions.unprocessableEntity("Field '%s' must be true or false", parameterName);
}
}
}

View File

@ -18,11 +18,8 @@ import com.google.common.base.Strings;
import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.databind.JsonMappingException;
import org.hibernate.validator.constraints.NotEmpty;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
@ -44,6 +41,7 @@ import javax.ws.rs.core.UriInfo;
import monasca.api.app.AlarmDefinitionService;
import monasca.api.app.command.CreateAlarmDefinitionCommand;
import monasca.api.app.command.PatchAlarmDefinitionCommand;
import monasca.api.app.command.UpdateAlarmDefinitionCommand;
import monasca.api.app.validation.AlarmValidation;
import monasca.api.app.validation.Validation;
@ -137,31 +135,21 @@ public class AlarmDefinitionResource {
@Path("/{alarm_definition_id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("unchecked")
public AlarmDefinition patch(@Context UriInfo uriInfo,
@HeaderParam("X-Tenant-Id") String tenantId,
@PathParam("alarm_definition_id") String alarmDefinitionId,
@NotEmpty Map<String, Object> fields) throws JsonMappingException {
String name = Validation.parseString(fields.get("name"), "name");
String description = Validation.parseString(fields.get("description"), "description");
String severity = Validation.parseString(fields.get("severity"), "severity");
String expression = Validation.parseString(fields.get("expression"), "expression");
List<String> matchBy = Validation.parseListOfStrings(fields.get("match_by"), "match_by");
Boolean enabled = Validation.parseBoolean(fields.get("actions_enabled"), "actions_enabled");
List<String> alarmActions =
Validation.parseListOfStrings(fields.get("alarm_actions"), "alarm_actions");
List<String> okActions = Validation.parseListOfStrings(fields.get("ok_actions"), "ok_actions");
List<String> undeterminedActions =
Validation.parseListOfStrings(fields.get("undetermined_actions"), "undetermined_actions");
AlarmValidation.validate(name, description, severity, alarmActions, okActions,
undeterminedActions);
@Valid PatchAlarmDefinitionCommand command) throws JsonMappingException {
command.validate();
AlarmExpression alarmExpression =
expression == null ? null : AlarmValidation.validateNormalizeAndGet(expression);
command.expression == null ? null : AlarmValidation
.validateNormalizeAndGet(command.expression);
return Links.hydrate(service.patch(tenantId, alarmDefinitionId, name, description, severity,
expression, alarmExpression, matchBy, enabled, alarmActions,
okActions, undeterminedActions), uriInfo, true);
return Links.hydrate(service.patch(tenantId, alarmDefinitionId, command.name,
command.description, command.severity, command.expression,
alarmExpression, command.matchBy, command.actionsEnabled,
command.alarmActions, command.okActions,
command.undeterminedActions),
uriInfo, true);
}
@DELETE

View File

@ -173,9 +173,9 @@ public class AlarmResource {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Alarm patch(@Context UriInfo uriInfo, @HeaderParam("X-Tenant-Id") String tenantId,
@PathParam("alarm_id") String alarmId, @NotEmpty Map<String, Object> fields)
@PathParam("alarm_id") String alarmId, @NotEmpty Map<String, String> fields)
throws JsonMappingException {
String stateStr = Validation.parseString(fields.get("state"), "state");
String stateStr = fields.get("state");
AlarmState state =
stateStr == null ? null : Validation.parseAndValidate(AlarmState.class, stateStr);