blob: 5cfbc845f94a4e03d96283a9375d1236f3582b68 [file] [log] [blame]
Jordan Halterman47432582018-01-25 16:56:45 -08001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.store.primitives.resources.impl;
17
18import io.atomix.protocols.raft.operation.OperationId;
19import io.atomix.protocols.raft.operation.OperationType;
20import org.onlab.util.KryoNamespace;
21import org.onosproject.store.serializers.KryoNamespaces;
22
23import static com.google.common.base.MoreObjects.toStringHelper;
24
25/**
26 * {@link org.onosproject.store.service.DistributedLock} operations.
27 * <p>
28 * WARNING: Do not refactor enum values. Only add to them.
29 * Changing values risk breaking the ability to backup/restore/upgrade clusters.
30 */
31public enum AtomixDistributedLockOperations implements OperationId {
32 LOCK(OperationType.COMMAND),
33 UNLOCK(OperationType.COMMAND);
34
35 private final OperationType type;
36
37 AtomixDistributedLockOperations(OperationType type) {
38 this.type = type;
39 }
40
41 @Override
42 public String id() {
43 return name();
44 }
45
46 @Override
47 public OperationType type() {
48 return type;
49 }
50
51 public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder()
52 .register(KryoNamespaces.BASIC)
53 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
54 .register(Lock.class)
55 .register(Unlock.class)
56 .build(AtomixDistributedLockOperations.class.getSimpleName());
57
58 /**
59 * Abstract lock operation.
60 */
61 public abstract static class LockOperation {
62 @Override
63 public String toString() {
64 return toStringHelper(this).toString();
65 }
66 }
67
68 /**
69 * Lock command.
70 */
71 public static class Lock extends LockOperation {
72 private final int id;
73 private final long timeout;
74
75 public Lock() {
76 this(0, 0);
77 }
78
79 public Lock(int id, long timeout) {
80 this.id = id;
81 this.timeout = timeout;
82 }
83
84 /**
85 * Returns the lock identifier.
86 *
87 * @return the lock identifier
88 */
89 public int id() {
90 return id;
91 }
92
93 /**
94 * Returns the lock attempt timeout.
95 *
96 * @return the lock attempt timeout
97 */
98 public long timeout() {
99 return timeout;
100 }
101
102 @Override
103 public String toString() {
104 return toStringHelper(this)
105 .add("id", id)
106 .add("timeout", timeout)
107 .toString();
108 }
109 }
110
111 /**
112 * Unlock command.
113 */
114 public static class Unlock extends LockOperation {
115 private final int id;
116
117 public Unlock(int id) {
118 this.id = id;
119 }
120
121 /**
122 * Returns the lock identifier.
123 *
124 * @return the lock identifier
125 */
126 public int id() {
127 return id;
128 }
129
130 @Override
131 public String toString() {
132 return toStringHelper(this)
133 .add("id", id)
134 .toString();
135 }
136 }
137}