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