2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.sal.utils;
11 import java.io.Serializable;
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.
19 public class Status implements Serializable {
20 private static final long serialVersionUID = 0L;
21 private StatusCode code;
22 private String description;
23 private long requestId;
26 * Generates an instance of the Status class. This is used as return code
27 * for internal API2 function calls. This constructor allows to specify,
28 * beside the Status Code, a custom human readable description to add more
29 * information about the status.
32 * The status code. If passed as null, code will be stored as
33 * {@code StatusCode.UNDEFINED}
35 * The human readable description of the status. If passed as
36 * null, description will be inferred by the code
38 public Status(StatusCode errorCode, String description) {
39 this.code = (errorCode != null) ? errorCode : StatusCode.UNDEFINED;
40 this.description = (description != null) ? description : this.code
46 * Generates an instance of the Status class based on the passed StatusCode
47 * only. The description field of the Status object will be inferred by the
51 * The status code. If passed as null, code will be stored as
52 * {@code StatusCode.UNDEFINED}
54 public Status(StatusCode errorCode) {
55 this.code = (errorCode != null) ? errorCode : StatusCode.UNDEFINED;
56 this.description = (description != null) ? description : this.code
62 * Generates an instance of the Status class to be used in case of
63 * asynchronous call. It is supposed to be created by the underlying
64 * infrastructure only when it was successful in allocating the asynchronous
65 * request id, hence caller should expect StatusCode to be successful.
68 * The status code. If passed as null, code will be stored as
69 * {@code StatusCode.UNDEFINED}
71 * The request id set by underlying infrastructure for this
74 public Status(StatusCode errorCode, long requestId) {
75 this.code = (errorCode != null) ? errorCode : StatusCode.UNDEFINED;
76 this.description = (description != null) ? description : this.code
78 this.requestId = requestId;
82 * Returns the status code
84 * @return the {@code StatusCode} representing the status code
86 public StatusCode getCode() {
91 * Returns a human readable description of the failure if any
93 * @return a string representing the reason of failure
95 public String getDescription() {
100 * Tells whether the status is successful
102 * @return true if the Status code is {@code StatusCode.SUCCESS}
104 public boolean isSuccess() {
105 return code == StatusCode.SUCCESS || code == StatusCode.CREATED;
109 * Return the request id assigned by underlying infrastructure in case of
110 * asynchronous request. In case of synchronous requests, the returned id
111 * is expected to be 0
113 * @return The request id assigned for this asynchronous request
115 public long getRequestId() {
120 public String toString() {
121 return code + ": " + description + " (" + requestId + ")";
125 public int hashCode() {
126 final int prime = 31;
128 result = prime * result + ((code == null) ? 0 : code.calculateConsistentHashCode());
133 public boolean equals(Object obj) {
140 if (getClass() != obj.getClass()) {
143 Status other = (Status) obj;
144 if (code != other.code) {