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