blob: e1418fcc48f35c2b26e590b0c6e4292cd1640055 [file] [log] [blame]
package net.onrc.onos.api.rest;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Maintains a catalog of RestErrors and allows lookup by error code.
*/
public final class RestErrorCatalog {
/**
* Static list of known errors. Someday this will be read in from an
* external file.
*/
private static final RestErrorCatalogEntry[] ERROR_LIST = {
new RestErrorCatalogEntry(RestErrorCode.INTENT_NOT_FOUND,
"Intent not found",
"An intent with the identifier {} was not found."),
new RestErrorCatalogEntry(RestErrorCode.INTENT_ALREADY_EXISTS,
"Intent already exists",
"An intent with the identifier {} could not be created " +
"because one already exists."),
new RestErrorCatalogEntry(RestErrorCode.INTENT_NO_PATH,
"No path found",
"No path found between {} and {}"),
new RestErrorCatalogEntry(RestErrorCode.INTENT_INVALID,
"Intent invalid",
"The intent provided is empty or invalid"),
};
/**
* Hide the default constructor of a utility class.
*/
private RestErrorCatalog() { }
/**
* Singleton implementation using the demand holder idiom.
*/
private static final class RestErrorMapHolder {
private static Map<Integer, RestErrorCatalogEntry> restErrorMap = initializeRestErrorMap();
/**
* Hide the default constructor.
*/
private RestErrorMapHolder() {}
/**
* Load up the error map.
*
* @return REST error map
*/
private static Map<Integer, RestErrorCatalogEntry> initializeRestErrorMap() {
restErrorMap = new HashMap<>();
for (final RestErrorCatalogEntry restErrorCatalogEntry : ERROR_LIST) {
restErrorMap.put(restErrorCatalogEntry.getCode().ordinal(), restErrorCatalogEntry);
}
return Collections.unmodifiableMap(restErrorMap);
}
/**
* Fetch the singleton map.
*
* @return map of the Rest Errors that was created from the known error
* list.
*/
public static Map<Integer, RestErrorCatalogEntry> getRestErrorMap() {
return restErrorMap;
}
}
/**
* Fetch the map of REST errors.
*
* @return map of possible REST errors.
*/
public static Map<Integer, RestErrorCatalogEntry> getRestErrorMap() {
return RestErrorMapHolder.getRestErrorMap();
}
/**
* Fetch the RestErrorCatalogEntry for the given code.
*
* @param code the code for the message to look up.
* @return the REST error for the code if one exists, null if it does not
* exist.
*/
public static RestErrorCatalogEntry getRestError(final RestErrorCode code) {
return getRestErrorMap().get(code.ordinal());
}
/**
* Fetch the array of catalog entries.
*
* @return array of REST error catalog entries currently in use
*/
public static RestErrorCatalogEntry[] getCatalogEntries() {
return Arrays.copyOf(ERROR_LIST, ERROR_LIST.length);
}
}