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