Search in sources :

Example 1 with InvalidConfigException

use of org.onosproject.net.config.InvalidConfigException in project onos by opennetworkinglab.

the class DistributedNetworkConfigStore method applyConfig.

@Override
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);
    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);
    // was supplanted by someone else already.
    return versioned.value() == json ? config : createConfig(subject, configClass, versioned.value());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) InvalidConfigException(org.onosproject.net.config.InvalidConfigException) ConfigFactory(org.onosproject.net.config.ConfigFactory)

Example 2 with InvalidConfigException

use of org.onosproject.net.config.InvalidConfigException in project onos by opennetworkinglab.

the class InvalidConfigExceptionMapper method response.

@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());
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InvalidConfigException(org.onosproject.net.config.InvalidConfigException) InvalidFieldException(org.onosproject.net.config.InvalidFieldException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

InvalidConfigException (org.onosproject.net.config.InvalidConfigException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ConfigFactory (org.onosproject.net.config.ConfigFactory)1 InvalidFieldException (org.onosproject.net.config.InvalidFieldException)1