00187dc86d5ef6e223d7e8ea9652854d048ed213
[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     public enum LockStatus {
43         UNLOCKED, BEING_LOCKED, LOCKED
44     };// captures the state of the lock
45
46     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicLockState.class);
47     private LockStatus lockStatus;
48     private boolean needToSyncQuorum = false;
49     private String lockHolder;
50     private long leasePeriod = Long.MAX_VALUE, leaseStartTime = -1;
51     private String errorMessage = null;
52     
53     public MusicLockState(String errorMessage) {
54         this.errorMessage = errorMessage;
55     }
56
57     public MusicLockState(LockStatus lockStatus, String lockHolder) {
58         this.lockStatus = lockStatus;
59         this.lockHolder = lockHolder;
60     }
61
62     public MusicLockState(LockStatus lockStatus, String lockHolder, boolean needToSyncQuorum) {
63         this.lockStatus = lockStatus;
64         this.lockHolder = lockHolder;
65         this.needToSyncQuorum = needToSyncQuorum;
66     }
67
68
69     public long getLeasePeriod() {
70         return leasePeriod;
71     }
72
73     public boolean isNeedToSyncQuorum() {
74         return needToSyncQuorum;
75     }
76
77
78
79     public void setLeasePeriod(long leasePeriod) {
80         this.leasePeriod = leasePeriod;
81     }
82
83
84     public long getLeaseStartTime() {
85         return leaseStartTime;
86     }
87
88
89     public void setLeaseStartTime(long leaseStartTime) {
90         this.leaseStartTime = leaseStartTime;
91     }
92
93
94
95     public LockStatus getLockStatus() {
96         return lockStatus;
97     }
98
99     public void setLockStatus(LockStatus lockStatus) {
100         this.lockStatus = lockStatus;
101     }
102
103     public String getLockHolder() {
104         return lockHolder;
105     }
106
107     public void setLockHolder(String lockHolder) {
108         this.lockHolder = lockHolder;
109     }
110
111     public String getErrorMessage() {
112         return errorMessage;
113     }
114     
115     public byte[] serialize() {
116         ByteArrayOutputStream bos = new ByteArrayOutputStream();
117         ObjectOutput out = null;
118         try {
119             out = new ObjectOutputStream(bos);
120             out.writeObject(this);
121         } catch (IOException e) {
122             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.IOERROR, ErrorSeverity.ERROR,
123                 ErrorTypes.CONNECTIONERROR, e);
124         }
125         return bos.toByteArray();
126     }
127
128     public static MusicLockState deSerialize(byte[] data) {
129         ByteArrayInputStream bis = new ByteArrayInputStream(data);
130         Object o = null;
131         ObjectInput in = null;
132         try {
133             in = new ObjectInputStream(bis);
134             o = in.readObject();
135         } catch (ClassNotFoundException | IOException e) {
136             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity
137                 .ERROR, ErrorTypes.UNKNOWN, e);
138         }
139         return (MusicLockState) o;
140     }
141 }