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