lowered some code smells
[music.git] / music-core / src / main / java / org / onap / music / lockingservice / cassandra / MusicLockState.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  * ===================================================================
7  *  Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  you may not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  * 
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  *  Unless required by applicable law or agreed to in writing, software
16  *  distributed under the License is distributed on an "AS IS" BASIS,
17  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *  See the License for the specific language governing permissions and
19  *  limitations under the License.
20  * 
21  * ============LICENSE_END=============================================
22  * ====================================================================
23  */
24
25 package org.onap.music.lockingservice.cassandra;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.ObjectInput;
31 import java.io.ObjectInputStream;
32 import java.io.ObjectOutput;
33 import java.io.ObjectOutputStream;
34 import java.io.Serializable;
35 import org.onap.music.eelf.logging.EELFLoggerDelegate;
36 import org.onap.music.eelf.logging.format.AppMessages;
37 import org.onap.music.eelf.logging.format.ErrorSeverity;
38 import org.onap.music.eelf.logging.format.ErrorTypes;
39
40 // the state variable that will be stored in the locking service, capturing the transitions of
41 public class MusicLockState implements Serializable {
42     // captures the state of the lock
43     public enum LockStatus {
44         UNLOCKED, BEING_LOCKED, LOCKED
45     }
46   
47
48     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicLockState.class);
49     private LockStatus lockStatus;
50     private boolean needToSyncQuorum = false;
51     private String lockHolder;
52     private long leasePeriod = Long.MAX_VALUE;
53     private long leaseStartTime = -1;
54     private String errorMessage = null;
55     
56     public MusicLockState(String errorMessage) {
57         this.errorMessage = errorMessage;
58     }
59
60     public MusicLockState(LockStatus lockStatus, String lockHolder) {
61         this.lockStatus = lockStatus;
62         this.lockHolder = lockHolder;
63     }
64
65     public MusicLockState(LockStatus lockStatus, String lockHolder, boolean needToSyncQuorum) {
66         this.lockStatus = lockStatus;
67         this.lockHolder = lockHolder;
68         this.needToSyncQuorum = needToSyncQuorum;
69     }
70
71
72     public long getLeasePeriod() {
73         return leasePeriod;
74     }
75
76     public boolean isNeedToSyncQuorum() {
77         return needToSyncQuorum;
78     }
79
80
81
82     public void setLeasePeriod(long leasePeriod) {
83         this.leasePeriod = leasePeriod;
84     }
85
86
87     public long getLeaseStartTime() {
88         return leaseStartTime;
89     }
90
91
92     public void setLeaseStartTime(long leaseStartTime) {
93         this.leaseStartTime = leaseStartTime;
94     }
95
96
97
98     public LockStatus getLockStatus() {
99         return lockStatus;
100     }
101
102     public void setLockStatus(LockStatus lockStatus) {
103         this.lockStatus = lockStatus;
104     }
105
106     public String getLockHolder() {
107         return lockHolder;
108     }
109
110     public void setLockHolder(String lockHolder) {
111         this.lockHolder = lockHolder;
112     }
113
114     public String getErrorMessage() {
115         return errorMessage;
116     }
117     
118     public byte[] serialize() {
119         ByteArrayOutputStream bos = new ByteArrayOutputStream();
120         ObjectOutput out = null;
121         try {
122             out = new ObjectOutputStream(bos);
123             out.writeObject(this);
124         } catch (IOException e) {
125             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.IOERROR, ErrorSeverity.ERROR,
126                 ErrorTypes.CONNECTIONERROR, e);
127         }
128         return bos.toByteArray();
129     }
130
131     public static MusicLockState deSerialize(byte[] data) {
132         ByteArrayInputStream bis = new ByteArrayInputStream(data);
133         Object o = null;
134         ObjectInput in = null;
135         try {
136             in = new ObjectInputStream(bis);
137             o = in.readObject();
138         } catch (ClassNotFoundException | IOException e) {
139             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity
140                 .ERROR, ErrorTypes.UNKNOWN, e);
141         }
142         return (MusicLockState) o;
143     }
144 }