2 * ============LICENSE_START==========================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=============================================
22 * ====================================================================
25 package org.onap.music.lockingservice.cassandra;
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;
40 // the state variable that will be stored in the locking service, capturing the transitions of
41 public class MusicLockState implements Serializable {
42 // captures the state of the lock
43 public enum LockStatus {
44 UNLOCKED, BEING_LOCKED, LOCKED
48 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicLockState.class);
49 private LockStatus lockStatus;
50 private boolean needToSyncQuorum = false;
51 private String lockHolder;
52 private long leasePeriod = Long.MAX_VALUE;
53 private long leaseStartTime = -1;
54 private String errorMessage = null;
56 public MusicLockState(String errorMessage) {
57 this.errorMessage = errorMessage;
60 public MusicLockState(LockStatus lockStatus, String lockHolder) {
61 this.lockStatus = lockStatus;
62 this.lockHolder = lockHolder;
65 public MusicLockState(LockStatus lockStatus, String lockHolder, boolean needToSyncQuorum) {
66 this.lockStatus = lockStatus;
67 this.lockHolder = lockHolder;
68 this.needToSyncQuorum = needToSyncQuorum;
72 public long getLeasePeriod() {
76 public boolean isNeedToSyncQuorum() {
77 return needToSyncQuorum;
82 public void setLeasePeriod(long leasePeriod) {
83 this.leasePeriod = leasePeriod;
87 public long getLeaseStartTime() {
88 return leaseStartTime;
92 public void setLeaseStartTime(long leaseStartTime) {
93 this.leaseStartTime = leaseStartTime;
98 public LockStatus getLockStatus() {
102 public void setLockStatus(LockStatus lockStatus) {
103 this.lockStatus = lockStatus;
106 public String getLockHolder() {
110 public void setLockHolder(String lockHolder) {
111 this.lockHolder = lockHolder;
114 public String getErrorMessage() {
118 public byte[] serialize() {
119 ByteArrayOutputStream bos = new ByteArrayOutputStream();
120 ObjectOutput out = null;
122 out = new ObjectOutputStream(bos);
123 out.writeObject(this);
124 } catch (IOException e) {
125 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.IOERROR, ErrorSeverity.ERROR,
126 ErrorTypes.CONNECTIONERROR, e);
128 return bos.toByteArray();
131 public static MusicLockState deSerialize(byte[] data) {
132 ByteArrayInputStream bis = new ByteArrayInputStream(data);
134 ObjectInput in = null;
136 in = new ObjectInputStream(bis);
138 } catch (ClassNotFoundException | IOException e) {
139 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity
140 .ERROR, ErrorTypes.UNKNOWN, e);
142 return (MusicLockState) o;