blob: e1418fcc48f35c2b26e590b0c6e4292cd1640055 [file] [log] [blame]
Ray Milkeyfbfd2da2014-05-09 17:30:14 -07001package net.onrc.onos.api.rest;
2
Ray Milkeyff710202014-05-23 16:55:18 -07003import java.util.Arrays;
Ray Milkeya8091b12014-05-16 14:42:47 -07004import java.util.Collections;
Ray Milkeyfbfd2da2014-05-09 17:30:14 -07005import java.util.HashMap;
6import java.util.Map;
7
8/**
9 * Maintains a catalog of RestErrors and allows lookup by error code.
10 */
11public final class RestErrorCatalog {
12
13 /**
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070014 * Static list of known errors. Someday this will be read in from an
15 * external file.
16 */
Ray Milkeya8091b12014-05-16 14:42:47 -070017 private static final RestErrorCatalogEntry[] ERROR_LIST = {
Ray Milkey6fdd91d2014-07-30 16:27:07 -070018 new RestErrorCatalogEntry(RestErrorCode.INTENT_NOT_FOUND,
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070019 "Intent not found",
20 "An intent with the identifier {} was not found."),
Ray Milkey6fdd91d2014-07-30 16:27:07 -070021 new RestErrorCatalogEntry(RestErrorCode.INTENT_ALREADY_EXISTS,
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070022 "Intent already exists",
23 "An intent with the identifier {} could not be created " +
24 "because one already exists."),
Ray Milkey6fdd91d2014-07-30 16:27:07 -070025 new RestErrorCatalogEntry(RestErrorCode.INTENT_NO_PATH,
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070026 "No path found",
27 "No path found between {} and {}"),
Ray Milkey6fdd91d2014-07-30 16:27:07 -070028 new RestErrorCatalogEntry(RestErrorCode.INTENT_INVALID,
Ray Milkeya8091b12014-05-16 14:42:47 -070029 "Intent invalid",
30 "The intent provided is empty or invalid"),
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070031 };
32
33 /**
Ray Milkey6fdd91d2014-07-30 16:27:07 -070034 * Hide the default constructor of a utility class.
35 */
36 private RestErrorCatalog() { }
37
38 /**
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070039 * Singleton implementation using the demand holder idiom.
40 */
Ray Milkey6fdd91d2014-07-30 16:27:07 -070041 private static final class RestErrorMapHolder {
Ray Milkeya8091b12014-05-16 14:42:47 -070042 private static Map<Integer, RestErrorCatalogEntry> restErrorMap = initializeRestErrorMap();
43
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070044 /**
Ray Milkey6fdd91d2014-07-30 16:27:07 -070045 * Hide the default constructor.
46 */
47 private RestErrorMapHolder() {}
48
49 /**
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070050 * Load up the error map.
51 *
52 * @return REST error map
53 */
Ray Milkeya8091b12014-05-16 14:42:47 -070054 private static Map<Integer, RestErrorCatalogEntry> initializeRestErrorMap() {
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070055 restErrorMap = new HashMap<>();
Ray Milkeya8091b12014-05-16 14:42:47 -070056 for (final RestErrorCatalogEntry restErrorCatalogEntry : ERROR_LIST) {
57 restErrorMap.put(restErrorCatalogEntry.getCode().ordinal(), restErrorCatalogEntry);
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070058 }
Ray Milkeya8091b12014-05-16 14:42:47 -070059 return Collections.unmodifiableMap(restErrorMap);
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070060 }
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 Milkeya8091b12014-05-16 14:42:47 -070068 public static Map<Integer, RestErrorCatalogEntry> getRestErrorMap() {
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070069 return restErrorMap;
70 }
71
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070072 }
73
74 /**
75 * Fetch the map of REST errors.
76 *
77 * @return map of possible REST errors.
78 */
Ray Milkeya8091b12014-05-16 14:42:47 -070079 public static Map<Integer, RestErrorCatalogEntry> getRestErrorMap() {
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070080 return RestErrorMapHolder.getRestErrorMap();
81 }
82
83 /**
Ray Milkeya8091b12014-05-16 14:42:47 -070084 * Fetch the RestErrorCatalogEntry for the given code.
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070085 *
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 Milkey6fdd91d2014-07-30 16:27:07 -070090 public static RestErrorCatalogEntry getRestError(final RestErrorCode code) {
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070091 return getRestErrorMap().get(code.ordinal());
92 }
Ray Milkeyff710202014-05-23 16:55:18 -070093
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 Milkeyfbfd2da2014-05-09 17:30:14 -0700102}