Make sure invokeOperation is set once
[controller.git] / opendaylight / adsal / 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 @Deprecated
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;
24
25     /**
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.
30      *
31      * @param errorCode
32      *            The status code. If passed as null, code will be stored as
33      *            {@code StatusCode.UNDEFINED}
34      * @param description
35      *            The human readable description of the status. If passed as
36      *            null, description will be inferred by the code
37      */
38     public Status(StatusCode errorCode, String description) {
39         this.code = (errorCode != null) ? errorCode : StatusCode.UNDEFINED;
40         this.description = (description != null) ? description : this.code
41                 .toString();
42         this.requestId = 0;
43     }
44
45     /**
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
48      * status code.
49      *
50      * @param errorCode
51      *            The status code. If passed as null, code will be stored as
52      *            {@code StatusCode.UNDEFINED}
53      */
54     public Status(StatusCode errorCode) {
55         this.code = (errorCode != null) ? errorCode : StatusCode.UNDEFINED;
56         this.description = (description != null) ? description : this.code
57                 .toString();
58         this.requestId = 0;
59     }
60
61     /**
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.
66      *
67      * @param errorCode
68      *            The status code. If passed as null, code will be stored as
69      *            {@code StatusCode.UNDEFINED}
70      * @param requestId
71      *            The request id set by underlying infrastructure for this
72      *            request
73      */
74     public Status(StatusCode errorCode, long requestId) {
75         this.code = (errorCode != null) ? errorCode : StatusCode.UNDEFINED;
76         this.description = (description != null) ? description : this.code
77                 .toString();
78         this.requestId = requestId;
79     }
80
81     /**
82      * Returns the status code
83      *
84      * @return the {@code StatusCode} representing the status code
85      */
86     public StatusCode getCode() {
87         return code;
88     }
89
90     /**
91      * Returns a human readable description of the failure if any
92      *
93      * @return a string representing the reason of failure
94      */
95     public String getDescription() {
96         return description;
97     }
98
99     /**
100      * Tells whether the status is successful
101      *
102      * @return true if the Status code is {@code StatusCode.SUCCESS}
103      */
104     public boolean isSuccess() {
105         return code == StatusCode.SUCCESS || code == StatusCode.CREATED;
106     }
107
108     /**
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
112      *
113      * @return The request id assigned for this asynchronous request
114      */
115     public long getRequestId() {
116         return requestId;
117     }
118
119     @Override
120     public String toString() {
121         return code + ": " + description + " (" + requestId + ")";
122     }
123
124     @Override
125     public int hashCode() {
126         final int prime = 31;
127         int result = 1;
128         result = prime * result + ((code == null) ? 0 : code.calculateConsistentHashCode());
129         return result;
130     }
131
132     @Override
133     public boolean equals(Object obj) {
134         if (this == obj) {
135             return true;
136         }
137         if (obj == null) {
138             return false;
139         }
140         if (getClass() != obj.getClass()) {
141             return false;
142         }
143         Status other = (Status) obj;
144         if (code != other.code) {
145             return false;
146         }
147         return true;
148     }
149 }