1d7ce224b5620f438947eeb29139309cd865b7f2
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / utils / Status.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.sal.utils;
10
11 /**
12  * Represents the return object of the osgi service interfaces function calls.
13  * It contains a code {@code StatusCode} representing the result of the call and
14  * a string which describes a failure reason (if any) in human readable form.
15  */
16 public class Status {
17     private StatusCode code;
18     private String description;
19     private long requestId;
20
21     /**
22      * Generates an instance of the Status class. This is used as return code
23      * for internal API2 function calls. This constructor allows to specify,
24      * beside the Status Code, a custom human readable description to add more
25      * information about the status.
26      *
27      * @param errorCode
28      *            The status code. If passed as null, code will be stored as
29      *            {@code StatusCode.UNDEFINED}
30      * @param description
31      *            The human readable description of the status. If passed as
32      *            null, description will be inferred by the code
33      */
34     public Status(StatusCode errorCode, String description) {
35         this.code = (errorCode != null) ? errorCode : StatusCode.UNDEFINED;
36         this.description = (description != null) ? description : this.code
37                 .toString();
38         this.requestId = 0;
39     }
40
41     /**
42      * Generates an instance of the Status class based on the passed StatusCode
43      * only. The description field of the Status object will be inferred by the
44      * status code.
45      *
46      * @param errorCode
47      *            The status code. If passed as null, code will be stored as
48      *            {@code StatusCode.UNDEFINED}
49      */
50     public Status(StatusCode errorCode) {
51         this.code = (errorCode != null) ? errorCode : StatusCode.UNDEFINED;
52         this.description = (description != null) ? description : this.code
53                 .toString();
54         this.requestId = 0;
55     }
56
57     /**
58      * Generates an instance of the Status class to be used in case of
59      * asynchronous call. It is supposed to be created by the underlying
60      * infrastructure only when it was successful in allocating the asynchronous
61      * request id, hence caller should expect StatusCode to be successful.
62      *
63      * @param errorCode
64      *            The status code. If passed as null, code will be stored as
65      *            {@code StatusCode.UNDEFINED}
66      * @param requestId
67      *            The request id set by underlying infrastructure for this
68      *            request
69      */
70     public Status(StatusCode errorCode, long requestId) {
71         this.code = (errorCode != null) ? errorCode : StatusCode.UNDEFINED;
72         this.description = (description != null) ? description : this.code
73                 .toString();
74         this.requestId = requestId;
75     }
76
77     /**
78      * Returns the status code
79      *
80      * @return the {@code StatusCode} representing the status code
81      */
82     public StatusCode getCode() {
83         return code;
84     }
85
86     /**
87      * Returns a human readable description of the failure if any
88      *
89      * @return a string representing the reason of failure
90      */
91     public String getDescription() {
92         return description;
93     }
94
95     /**
96      * Tells whether the status is successful
97      *
98      * @return true if the Status code is {@code StatusCode.SUCCESS}
99      */
100     public boolean isSuccess() {
101         return code == StatusCode.SUCCESS;
102     }
103
104     /**
105      * Return the request id assigned by underlying infrastructure in case of
106      * asynchronous request. In case of synchronous requests, the returned id
107      * is expected to be 0
108      *
109      * @return The request id assigned for this asynchronous request
110      */
111     public long getRequestId() {
112         return requestId;
113     }
114
115     @Override
116     public String toString() {
117         return code + ": " + description + " (" + requestId + ")";
118     }
119
120     @Override
121     public int hashCode() {
122         final int prime = 31;
123         int result = 1;
124         result = prime * result + ((code == null) ? 0 : code.hashCode());
125         return result;
126     }
127
128     @Override
129     public boolean equals(Object obj) {
130         if (this == obj)
131             return true;
132         if (obj == null)
133             return false;
134         if (getClass() != obj.getClass())
135             return false;
136         Status other = (Status) obj;
137         if (code != other.code)
138             return false;
139         return true;
140     }
141 }