Jonathan Hart
Committed by Gerrit Code Review

Improve network config validation errors to show which fields are invalid.

Previously, uploading invalid config results in a generic error message
which makes it difficult to figure out what is wrong with the config

Change-Id: I307d2fc0669679b067389c722556eef3aae098b9
/*
* Copyright 2016 Open Networking Laboratory
*
* 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 org.onosproject.net.config;
/**
* Indicates an invalid configuration was supplied by the user.
*/
public class InvalidConfigException extends RuntimeException {
private final String subjectKey;
private final String subject;
private final String configKey;
/**
* Creates a new invalid config exception about a specific config.
*
* @param subjectKey config's subject key
* @param subject config's subject
* @param configKey config's config key
*/
public InvalidConfigException(String subjectKey, String subject, String configKey) {
this(subjectKey, subject, configKey, null);
}
/**
* Creates a new invalid config exception about a specific config with an
* exception regarding the cause of the invalidity.
*
* @param subjectKey config's subject key
* @param subject config's subject
* @param configKey config's config key
* @param cause cause of the invalidity
*/
public InvalidConfigException(String subjectKey, String subject, String configKey, Throwable cause) {
super(message(subjectKey, subject, configKey, cause), cause);
this.subjectKey = subjectKey;
this.subject = subject;
this.configKey = configKey;
}
/**
* Returns the subject key of the config.
*
* @return subject key
*/
public String subjectKey() {
return subjectKey;
}
/**
* Returns the string representation of the subject of the config.
*
* @return subject
*/
public String subject() {
return subject;
}
/**
* Returns the config key of the config.
*
* @return config key
*/
public String configKey() {
return configKey;
}
private static String message(String subjectKey, String subject, String configKey, Throwable cause) {
String error = "Error parsing config " + subjectKey + "/" + subject + "/" + configKey;
if (cause != null) {
error = error + ": " + cause.getMessage();
}
return error;
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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 org.onosproject.net.config;
/**
* Indicates a field of a configuration was invalid.
*/
public class InvalidFieldException extends RuntimeException {
private final String field;
private final String reason;
/**
* Creates a new invalid field exception about a given field.
*
* @param field field name
* @param reason reason the field is invalid
*/
public InvalidFieldException(String field, String reason) {
super(message(field, reason));
this.field = field;
this.reason = reason;
}
/**
* Creates a new invalid field exception about a given field.
*
* @param field field name
* @param cause throwable that occurred while trying to validate field
*/
public InvalidFieldException(String field, Throwable cause) {
super(message(field, cause.getMessage()));
this.field = field;
this.reason = cause.getMessage();
}
/**
* Returns the field name.
*
* @return field name
*/
public String field() {
return field;
}
/**
* Returns the reason the field failed to validate.
*
* @return reason
*/
public String reason() {
return reason;
}
private static String message(String field, String reason) {
return "Field \"" + field + "\" is invalid: " + reason;
}
}
......@@ -40,6 +40,7 @@ import org.onlab.util.Tools;
import org.onosproject.net.config.Config;
import org.onosproject.net.config.ConfigApplyDelegate;
import org.onosproject.net.config.ConfigFactory;
import org.onosproject.net.config.InvalidConfigException;
import org.onosproject.net.config.NetworkConfigEvent;
import org.onosproject.net.config.NetworkConfigStore;
import org.onosproject.net.config.NetworkConfigStoreDelegate;
......@@ -236,7 +237,17 @@ public class DistributedNetworkConfigStore
public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, JsonNode json) {
// Create the configuration and validate it.
C config = createConfig(subject, configClass, json);
checkArgument(config.isValid(), INVALID_CONFIG_JSON);
try {
checkArgument(config.isValid(), INVALID_CONFIG_JSON);
} catch (RuntimeException e) {
ConfigFactory<S, C> configFactory = getConfigFactory(configClass);
String subjectKey = configFactory.subjectFactory().subjectClassKey();
String subjectString = configFactory.subjectFactory().subjectKey(config.subject());
String configKey = config.key();
throw new InvalidConfigException(subjectKey, subjectString, configKey, e);
}
// Insert the validated configuration and get it back.
Versioned<JsonNode> versioned = configs.putAndGet(key(subject, configClass), json);
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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 org.onosproject.rest.exceptions;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onlab.rest.exceptions.AbstractMapper;
import org.onosproject.net.config.InvalidConfigException;
import org.onosproject.net.config.InvalidFieldException;
import javax.ws.rs.core.Response;
/**
* Maps InvalidConfigException to JSON output.
*/
public class InvalidConfigExceptionMapper extends AbstractMapper<InvalidConfigException> {
@Override
protected Response.Status responseStatus() {
return Response.Status.BAD_REQUEST;
}
@Override
protected Response.ResponseBuilder response(Response.Status status, Throwable exception) {
error = exception;
InvalidConfigException ex = (InvalidConfigException) exception;
ObjectMapper mapper = new ObjectMapper();
String message = messageFrom(exception);
ObjectNode result = mapper.createObjectNode()
.put("code", status.getStatusCode())
.put("message", message)
.put("subjectKey", ex.subjectKey())
.put("subject", ex.subject())
.put("configKey", ex.configKey());
if (ex.getCause() instanceof InvalidFieldException) {
InvalidFieldException fieldException = (InvalidFieldException) ex.getCause();
result.put("field", fieldException.field())
.put("reason", fieldException.reason());
}
return Response.status(status).entity(result.toString());
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* 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.
*/
/**
* REST exceptions.
*/
package org.onosproject.rest.exceptions;
......@@ -17,6 +17,7 @@
package org.onosproject.rest.resources;
import org.onlab.rest.AbstractWebApplication;
import org.onosproject.rest.exceptions.InvalidConfigExceptionMapper;
import java.util.Set;
......@@ -49,7 +50,8 @@ public class CoreWebApplication extends AbstractWebApplication {
RegionsWebResource.class,
TenantWebResource.class,
VirtualNetworkWebResource.class,
MastershipWebResource.class
MastershipWebResource.class,
InvalidConfigExceptionMapper.class
);
}
}
......