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