2 * ============LICENSE_START==========================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.lockingservice;
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;
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
42 private static EELFLogger logger = EELFManager.getInstance().getLogger(MusicLockState.class);
43 LockStatus lockStatus;
44 boolean needToSyncQuorum = false;
46 long leasePeriod = Long.MAX_VALUE, leaseStartTime = -1;
48 public MusicLockState(LockStatus lockStatus, String lockHolder) {
49 this.lockStatus = lockStatus;
50 this.lockHolder = lockHolder;
53 public MusicLockState(LockStatus lockStatus, String lockHolder, boolean needToSyncQuorum) {
54 this.lockStatus = lockStatus;
55 this.lockHolder = lockHolder;
56 this.needToSyncQuorum = needToSyncQuorum;
60 public long getLeasePeriod() {
64 public boolean isNeedToSyncQuorum() {
65 return needToSyncQuorum;
70 public void setLeasePeriod(long leasePeriod) {
71 this.leasePeriod = leasePeriod;
75 public long getLeaseStartTime() {
76 return leaseStartTime;
80 public void setLeaseStartTime(long leaseStartTime) {
81 this.leaseStartTime = leaseStartTime;
86 public LockStatus getLockStatus() {
90 public void setLockStatus(LockStatus lockStatus) {
91 this.lockStatus = lockStatus;
94 public String getLockHolder() {
98 public void setLockHolder(String lockHolder) {
99 this.lockHolder = lockHolder;
102 public byte[] serialize() {
103 ByteArrayOutputStream bos = new ByteArrayOutputStream();
104 ObjectOutput out = null;
106 out = new ObjectOutputStream(bos);
107 out.writeObject(this);
108 } catch (IOException e) {
109 logger.error(e.getMessage());
111 return bos.toByteArray();
114 public static MusicLockState deSerialize(byte[] data) {
115 ByteArrayInputStream bis = new ByteArrayInputStream(data);
117 ObjectInput in = null;
119 in = new ObjectInputStream(bis);
121 } catch (ClassNotFoundException | IOException e) {
122 logger.error(e.getMessage());
124 return (MusicLockState) o;