blob: fde61333a75b36ec0e71aa69dbec484649fc9a86 [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 /**
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 Milkeya8091b12014-05-16 14:42:47 -070022 private static final RestErrorCatalogEntry[] ERROR_LIST = {
23 new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_NOT_FOUND,
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070024 "Intent not found",
25 "An intent with the identifier {} was not found."),
Ray Milkeya8091b12014-05-16 14:42:47 -070026 new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_ALREADY_EXISTS,
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070027 "Intent already exists",
28 "An intent with the identifier {} could not be created " +
29 "because one already exists."),
Ray Milkeya8091b12014-05-16 14:42:47 -070030 new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_NO_PATH,
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070031 "No path found",
32 "No path found between {} and {}"),
Ray Milkeya8091b12014-05-16 14:42:47 -070033 new RestErrorCatalogEntry(RestErrorCodes.RestErrorCode.INTENT_INVALID,
34 "Intent invalid",
35 "The intent provided is empty or invalid"),
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070036 };
37
38 /**
39 * Singleton implementation using the demand holder idiom.
40 */
41 private static 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 /**
45 * Load up the error map.
46 *
47 * @return REST error map
48 */
Ray Milkeya8091b12014-05-16 14:42:47 -070049 private static Map<Integer, RestErrorCatalogEntry> initializeRestErrorMap() {
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070050 restErrorMap = new HashMap<>();
Ray Milkeya8091b12014-05-16 14:42:47 -070051 for (final RestErrorCatalogEntry restErrorCatalogEntry : ERROR_LIST) {
52 restErrorMap.put(restErrorCatalogEntry.getCode().ordinal(), restErrorCatalogEntry);
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070053 }
Ray Milkeya8091b12014-05-16 14:42:47 -070054 return Collections.unmodifiableMap(restErrorMap);
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070055 }
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 Milkeya8091b12014-05-16 14:42:47 -070063 public static Map<Integer, RestErrorCatalogEntry> getRestErrorMap() {
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070064 return restErrorMap;
65 }
66
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070067 }
68
69 /**
70 * Fetch the map of REST errors.
71 *
72 * @return map of possible REST errors.
73 */
Ray Milkeya8091b12014-05-16 14:42:47 -070074 public static Map<Integer, RestErrorCatalogEntry> getRestErrorMap() {
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070075 return RestErrorMapHolder.getRestErrorMap();
76 }
77
78 /**
Ray Milkeya8091b12014-05-16 14:42:47 -070079 * Fetch the RestErrorCatalogEntry for the given code.
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070080 *
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 Milkeya8091b12014-05-16 14:42:47 -070085 public static RestErrorCatalogEntry getRestError(final RestErrorCodes.RestErrorCode code) {
Ray Milkeyfbfd2da2014-05-09 17:30:14 -070086 return getRestErrorMap().get(code.ordinal());
87 }
Ray Milkeyff710202014-05-23 16:55:18 -070088
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 Milkeyfbfd2da2014-05-09 17:30:14 -070097}