Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
Ray Milkey | ff71020 | 2014-05-23 16:55:18 -0700 | [diff] [blame] | 3 | import java.util.Arrays; |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 4 | import java.util.Collections; |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 5 | import java.util.HashMap; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | /** |
| 9 | * Maintains a catalog of RestErrors and allows lookup by error code. |
| 10 | */ |
| 11 | public final class RestErrorCatalog { |
| 12 | |
| 13 | /** |
| 14 | * Hide the default constructor of a utility class. |
| 15 | */ |
| 16 | private RestErrorCatalog() { } |
| 17 | |
| 18 | /** |
| 19 | * Static list of known errors. Someday this will be read in from an |
| 20 | * external file. |
| 21 | */ |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 22 | private static final RestErrorCatalogEntry[] ERROR_LIST = { |
| 23 | new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_NOT_FOUND, |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 24 | "Intent not found", |
| 25 | "An intent with the identifier {} was not found."), |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 26 | new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_ALREADY_EXISTS, |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 27 | "Intent already exists", |
| 28 | "An intent with the identifier {} could not be created " + |
| 29 | "because one already exists."), |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 30 | new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_NO_PATH, |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 31 | "No path found", |
| 32 | "No path found between {} and {}"), |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 33 | new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_INVALID, |
| 34 | "Intent invalid", |
| 35 | "The intent provided is empty or invalid"), |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * Singleton implementation using the demand holder idiom. |
| 40 | */ |
| 41 | private static class RestErrorMapHolder { |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 42 | private static Map<Integer, RestErrorCatalogEntry> restErrorMap = initializeRestErrorMap(); |
| 43 | |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Load up the error map. |
| 46 | * |
| 47 | * @return REST error map |
| 48 | */ |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 49 | private static Map<Integer, RestErrorCatalogEntry> initializeRestErrorMap() { |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 50 | restErrorMap = new HashMap<>(); |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 51 | for (final RestErrorCatalogEntry restErrorCatalogEntry : ERROR_LIST) { |
| 52 | restErrorMap.put(restErrorCatalogEntry.getCode().ordinal(), restErrorCatalogEntry); |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 53 | } |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 54 | return Collections.unmodifiableMap(restErrorMap); |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Fetch the singleton map. |
| 59 | * |
| 60 | * @return map of the Rest Errors that was created from the known error |
| 61 | * list. |
| 62 | */ |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 63 | public static Map<Integer, RestErrorCatalogEntry> getRestErrorMap() { |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 64 | return restErrorMap; |
| 65 | } |
| 66 | |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Fetch the map of REST errors. |
| 71 | * |
| 72 | * @return map of possible REST errors. |
| 73 | */ |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 74 | public static Map<Integer, RestErrorCatalogEntry> getRestErrorMap() { |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 75 | return RestErrorMapHolder.getRestErrorMap(); |
| 76 | } |
| 77 | |
| 78 | /** |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 79 | * Fetch the RestErrorCatalogEntry for the given code. |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 80 | * |
| 81 | * @param code the code for the message to look up. |
| 82 | * @return the REST error for the code if one exists, null if it does not |
| 83 | * exist. |
| 84 | */ |
Ray Milkey | a8091b1 | 2014-05-16 14:42:47 -0700 | [diff] [blame] | 85 | public static RestErrorCatalogEntry getRestError(final RestErrorCodes.RestErrorCode code) { |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 86 | return getRestErrorMap().get(code.ordinal()); |
| 87 | } |
Ray Milkey | ff71020 | 2014-05-23 16:55:18 -0700 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Fetch the array of catalog entries. |
| 91 | * |
| 92 | * @return array of REST error catalog entries currently in use |
| 93 | */ |
| 94 | public static RestErrorCatalogEntry[] getCatalogEntries() { |
| 95 | return Arrays.copyOf(ERROR_LIST, ERROR_LIST.length); |
| 96 | } |
Ray Milkey | fbfd2da | 2014-05-09 17:30:14 -0700 | [diff] [blame] | 97 | } |