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