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