5d051be4707bac99558f253dedb532af4ef3308c
[music.git] / src / main / java / org / onap / music / lockingservice / 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 package org.onap.music.lockingservice;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.ObjectInput;
28 import java.io.ObjectInputStream;
29 import java.io.ObjectOutput;
30 import java.io.ObjectOutputStream;
31 import java.io.Serializable;
32 import org.onap.music.eelf.logging.EELFLoggerDelegate;
33 import org.onap.music.eelf.logging.format.AppMessages;
34 import org.onap.music.eelf.logging.format.ErrorSeverity;
35 import org.onap.music.eelf.logging.format.ErrorTypes;
36
37 // the state variable that will be stored in zookeeper, capturing the transitions of
38 public class MusicLockState implements Serializable {
39     public enum LockStatus {
40         UNLOCKED, BEING_LOCKED, LOCKED
41     };// captures the state of the lock
42
43     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicLockState.class);
44     LockStatus lockStatus;
45     boolean needToSyncQuorum = false;
46     String lockHolder;
47     long leasePeriod = Long.MAX_VALUE, leaseStartTime = -1;
48
49     public MusicLockState(LockStatus lockStatus, String lockHolder) {
50         this.lockStatus = lockStatus;
51         this.lockHolder = lockHolder;
52     }
53
54     public MusicLockState(LockStatus lockStatus, String lockHolder, boolean needToSyncQuorum) {
55         this.lockStatus = lockStatus;
56         this.lockHolder = lockHolder;
57         this.needToSyncQuorum = needToSyncQuorum;
58     }
59
60
61     public long getLeasePeriod() {
62         return leasePeriod;
63     }
64
65     public boolean isNeedToSyncQuorum() {
66         return needToSyncQuorum;
67     }
68
69
70
71     public void setLeasePeriod(long leasePeriod) {
72         this.leasePeriod = leasePeriod;
73     }
74
75
76     public long getLeaseStartTime() {
77         return leaseStartTime;
78     }
79
80
81     public void setLeaseStartTime(long leaseStartTime) {
82         this.leaseStartTime = leaseStartTime;
83     }
84
85
86
87     public LockStatus getLockStatus() {
88         return lockStatus;
89     }
90
91     public void setLockStatus(LockStatus lockStatus) {
92         this.lockStatus = lockStatus;
93     }
94
95     public String getLockHolder() {
96         return lockHolder;
97     }
98
99     public void setLockHolder(String lockHolder) {
100         this.lockHolder = lockHolder;
101     }
102
103     public byte[] serialize() {
104         ByteArrayOutputStream bos = new ByteArrayOutputStream();
105         ObjectOutput out = null;
106         try {
107             out = new ObjectOutputStream(bos);
108             out.writeObject(this);
109         } catch (IOException e) {
110                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.IOERROR, ErrorSeverity.ERROR, ErrorTypes.CONNECTIONERROR);
111         }
112         return bos.toByteArray();
113     }
114
115     public static MusicLockState deSerialize(byte[] data) {
116         ByteArrayInputStream bis = new ByteArrayInputStream(data);
117         Object o = null;
118         ObjectInput in = null;
119         try {
120             in = new ObjectInputStream(bis);
121             o = in.readObject();
122         } catch (ClassNotFoundException | IOException e) {
123                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN);
124         }
125         return (MusicLockState) o;
126     }
127 }