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