Committed by
Gerrit Code Review
Remove deprecated code.
Change-Id: Ifd68e4ddfaade2a8dd7de43a83bf222b48b9291b
Showing
26 changed files
with
3 additions
and
1789 deletions
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.cli; | ||
| 17 | - | ||
| 18 | -import org.apache.karaf.shell.commands.Argument; | ||
| 19 | -import org.apache.karaf.shell.commands.Command; | ||
| 20 | -import org.onlab.packet.IpAddress; | ||
| 21 | -import org.onosproject.cluster.ControllerNode; | ||
| 22 | -import org.onosproject.cluster.DefaultControllerNode; | ||
| 23 | -import org.onosproject.cluster.NodeId; | ||
| 24 | -import org.onosproject.store.service.DatabaseAdminService; | ||
| 25 | - | ||
| 26 | -/** | ||
| 27 | - * Adds a new controller cluster node. | ||
| 28 | - */ | ||
| 29 | -@Command(scope = "onos", name = "tablet-add", | ||
| 30 | - description = "Adds a new member to tablet") | ||
| 31 | -public class TabletAddCommand extends AbstractShellCommand { | ||
| 32 | - | ||
| 33 | - @Argument(index = 0, name = "nodeId", description = "Node ID", | ||
| 34 | - required = true, multiValued = false) | ||
| 35 | - String nodeId = null; | ||
| 36 | - | ||
| 37 | - @Argument(index = 1, name = "ip", description = "Node IP address", | ||
| 38 | - required = true, multiValued = false) | ||
| 39 | - String ip = null; | ||
| 40 | - | ||
| 41 | - @Argument(index = 2, name = "tcpPort", description = "Node TCP listen port", | ||
| 42 | - required = false, multiValued = false) | ||
| 43 | - int tcpPort = 9876; | ||
| 44 | - | ||
| 45 | - // TODO add tablet name argument when we support multiple tablets | ||
| 46 | - | ||
| 47 | - @Override | ||
| 48 | - protected void execute() { | ||
| 49 | - DatabaseAdminService service = get(DatabaseAdminService.class); | ||
| 50 | - ControllerNode node = new DefaultControllerNode(new NodeId(nodeId), | ||
| 51 | - IpAddress.valueOf(ip), | ||
| 52 | - tcpPort); | ||
| 53 | - service.addMember(node); | ||
| 54 | - } | ||
| 55 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.cli; | ||
| 17 | - | ||
| 18 | -import org.apache.karaf.shell.commands.Command; | ||
| 19 | -import org.onosproject.cluster.ControllerNode; | ||
| 20 | -import org.onosproject.store.service.DatabaseAdminService; | ||
| 21 | - | ||
| 22 | -import java.util.Optional; | ||
| 23 | - | ||
| 24 | -/** | ||
| 25 | - * Lists mastership roles of nodes for each device. | ||
| 26 | - */ | ||
| 27 | -@Command(scope = "onos", name = "tablet-leader", | ||
| 28 | - description = "Prints the current leader of a tablet.") | ||
| 29 | -public class TabletLeaderCommand extends AbstractShellCommand { | ||
| 30 | - | ||
| 31 | - @Override | ||
| 32 | - protected void execute() { | ||
| 33 | - final DatabaseAdminService dbAdminService = get(DatabaseAdminService.class); | ||
| 34 | - | ||
| 35 | - Optional<ControllerNode> leader = dbAdminService.leader(); | ||
| 36 | - if (leader.isPresent()) { | ||
| 37 | - print("Leader: %s", leader.get()); | ||
| 38 | - } else { | ||
| 39 | - print("No Leader"); | ||
| 40 | - } | ||
| 41 | - } | ||
| 42 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.cli; | ||
| 17 | - | ||
| 18 | -import com.fasterxml.jackson.databind.JsonNode; | ||
| 19 | -import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 20 | -import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| 21 | - | ||
| 22 | -import org.apache.karaf.shell.commands.Command; | ||
| 23 | -import org.onosproject.cluster.ClusterService; | ||
| 24 | -import org.onosproject.cluster.ControllerNode; | ||
| 25 | -import org.onosproject.store.service.DatabaseAdminService; | ||
| 26 | - | ||
| 27 | -import java.util.Collections; | ||
| 28 | -import java.util.List; | ||
| 29 | - | ||
| 30 | -import static com.google.common.collect.Lists.newArrayList; | ||
| 31 | - | ||
| 32 | -/** | ||
| 33 | - * Lists all controller cluster nodes. | ||
| 34 | - */ | ||
| 35 | -@Command(scope = "onos", name = "tablet-member", | ||
| 36 | - description = "Lists all member nodes") | ||
| 37 | -public class TabletMemberCommand extends AbstractShellCommand { | ||
| 38 | - | ||
| 39 | - // TODO add tablet name argument when we support multiple tablets | ||
| 40 | - | ||
| 41 | - @Override | ||
| 42 | - protected void execute() { | ||
| 43 | - DatabaseAdminService service = get(DatabaseAdminService.class); | ||
| 44 | - ClusterService clusterService = get(ClusterService.class); | ||
| 45 | - List<ControllerNode> nodes = newArrayList(service.listMembers()); | ||
| 46 | - Collections.sort(nodes, Comparators.NODE_COMPARATOR); | ||
| 47 | - if (outputJson()) { | ||
| 48 | - print("%s", json(service, nodes)); | ||
| 49 | - } else { | ||
| 50 | - ControllerNode self = clusterService.getLocalNode(); | ||
| 51 | - for (ControllerNode node : nodes) { | ||
| 52 | - print("id=%s, address=%s:%s %s", | ||
| 53 | - node.id(), node.ip(), node.tcpPort(), | ||
| 54 | - node.equals(self) ? "*" : ""); | ||
| 55 | - } | ||
| 56 | - } | ||
| 57 | - } | ||
| 58 | - | ||
| 59 | - // Produces JSON structure. | ||
| 60 | - private JsonNode json(DatabaseAdminService service, List<ControllerNode> nodes) { | ||
| 61 | - ObjectMapper mapper = new ObjectMapper(); | ||
| 62 | - ArrayNode result = mapper.createArrayNode(); | ||
| 63 | - for (ControllerNode node : nodes) { | ||
| 64 | - result.add(mapper.createObjectNode() | ||
| 65 | - .put("id", node.id().toString()) | ||
| 66 | - .put("ip", node.ip().toString()) | ||
| 67 | - .put("tcpPort", node.tcpPort())); | ||
| 68 | - } | ||
| 69 | - return result; | ||
| 70 | - } | ||
| 71 | - | ||
| 72 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.cli; | ||
| 17 | - | ||
| 18 | -import org.apache.karaf.shell.commands.Argument; | ||
| 19 | -import org.apache.karaf.shell.commands.Command; | ||
| 20 | -import org.onosproject.cluster.ClusterService; | ||
| 21 | -import org.onosproject.cluster.ControllerNode; | ||
| 22 | -import org.onosproject.cluster.NodeId; | ||
| 23 | -import org.onosproject.store.service.DatabaseAdminService; | ||
| 24 | - | ||
| 25 | -/** | ||
| 26 | - * Removes a controller cluster node. | ||
| 27 | - */ | ||
| 28 | -@Command(scope = "onos", name = "tablet-remove", | ||
| 29 | - description = "Removes a member from tablet") | ||
| 30 | -public class TabletRemoveCommand extends AbstractShellCommand { | ||
| 31 | - | ||
| 32 | - @Argument(index = 0, name = "nodeId", description = "Node ID", | ||
| 33 | - required = true, multiValued = false) | ||
| 34 | - String nodeId = null; | ||
| 35 | - | ||
| 36 | - // TODO add tablet name argument when we support multiple tablets | ||
| 37 | - | ||
| 38 | - @Override | ||
| 39 | - protected void execute() { | ||
| 40 | - DatabaseAdminService service = get(DatabaseAdminService.class); | ||
| 41 | - ClusterService clusterService = get(ClusterService.class); | ||
| 42 | - ControllerNode node = clusterService.getNode(new NodeId(nodeId)); | ||
| 43 | - if (node != null) { | ||
| 44 | - service.removeMember(node); | ||
| 45 | - } | ||
| 46 | - } | ||
| 47 | -} |
| ... | @@ -53,33 +53,6 @@ | ... | @@ -53,33 +53,6 @@ |
| 53 | </command> | 53 | </command> |
| 54 | 54 | ||
| 55 | <command> | 55 | <command> |
| 56 | - <action class="org.onosproject.cli.TabletMemberCommand"/> | ||
| 57 | - </command> | ||
| 58 | - | ||
| 59 | - <command> | ||
| 60 | - <action class="org.onosproject.cli.TabletLeaderCommand"/> | ||
| 61 | - </command> | ||
| 62 | - | ||
| 63 | -<!-- | ||
| 64 | - <command> | ||
| 65 | - <action class="org.onosproject.cli.TabletAddCommand"/> | ||
| 66 | - <completers> | ||
| 67 | - <ref component-id="nodeIdCompleter"/> | ||
| 68 | - <null/> | ||
| 69 | - <null/> | ||
| 70 | - </completers> | ||
| 71 | - </command> | ||
| 72 | - <command> | ||
| 73 | - <action class="org.onosproject.cli.TabletRemoveCommand"/> | ||
| 74 | - <completers> | ||
| 75 | - <ref component-id="nodeIdCompleter"/> | ||
| 76 | - <null/> | ||
| 77 | - <null/> | ||
| 78 | - </completers> | ||
| 79 | - </command> | ||
| 80 | ---> | ||
| 81 | - | ||
| 82 | - <command> | ||
| 83 | <action class="org.onosproject.cli.NodesListCommand"/> | 56 | <action class="org.onosproject.cli.NodesListCommand"/> |
| 84 | </command> | 57 | </command> |
| 85 | <!-- | 58 | <!-- | ... | ... |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -import java.util.List; | ||
| 19 | - | ||
| 20 | -import com.google.common.base.MoreObjects; | ||
| 21 | -import com.google.common.collect.ImmutableList; | ||
| 22 | -import com.google.common.collect.Lists; | ||
| 23 | - | ||
| 24 | -/** | ||
| 25 | - * Collection of read requests to be submitted as one batch. | ||
| 26 | - */ | ||
| 27 | -public final class BatchReadRequest { | ||
| 28 | - | ||
| 29 | - private final List<ReadRequest> readRequests; | ||
| 30 | - | ||
| 31 | - /** | ||
| 32 | - * Creates a new BatchReadRequest object from the specified list of read requests. | ||
| 33 | - * @param readRequests read requests. | ||
| 34 | - * @return BatchReadRequest object. | ||
| 35 | - */ | ||
| 36 | - public static BatchReadRequest create(List<ReadRequest> readRequests) { | ||
| 37 | - return new BatchReadRequest(readRequests); | ||
| 38 | - } | ||
| 39 | - | ||
| 40 | - private BatchReadRequest(List<ReadRequest> readRequests) { | ||
| 41 | - this.readRequests = ImmutableList.copyOf(readRequests); | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - /** | ||
| 45 | - * Returns the number of requests in this batch. | ||
| 46 | - * @return size of request batch. | ||
| 47 | - */ | ||
| 48 | - public int batchSize() { | ||
| 49 | - return readRequests.size(); | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | - /** | ||
| 53 | - * Returns the requests in this batch as a list. | ||
| 54 | - * @return list of read requests | ||
| 55 | - */ | ||
| 56 | - public List<ReadRequest> getAsList() { | ||
| 57 | - return readRequests; | ||
| 58 | - } | ||
| 59 | - | ||
| 60 | - @Override | ||
| 61 | - public String toString() { | ||
| 62 | - return MoreObjects.toStringHelper(getClass()) | ||
| 63 | - .add("readRequests", readRequests) | ||
| 64 | - .toString(); | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - /** | ||
| 68 | - * Builder for BatchReadRequest. | ||
| 69 | - */ | ||
| 70 | - public static class Builder { | ||
| 71 | - | ||
| 72 | - private final List<ReadRequest> readRequests = Lists.newLinkedList(); | ||
| 73 | - | ||
| 74 | - /** | ||
| 75 | - * Append a get request. | ||
| 76 | - * @param tableName table name | ||
| 77 | - * @param key key to fetch. | ||
| 78 | - * @return this Builder | ||
| 79 | - */ | ||
| 80 | - public Builder get(String tableName, String key) { | ||
| 81 | - readRequests.add(new ReadRequest(tableName, key)); | ||
| 82 | - return this; | ||
| 83 | - } | ||
| 84 | - | ||
| 85 | - /** | ||
| 86 | - * Builds a BatchReadRequest. | ||
| 87 | - * @return BatchReadRequest | ||
| 88 | - */ | ||
| 89 | - public BatchReadRequest build() { | ||
| 90 | - return new BatchReadRequest(readRequests); | ||
| 91 | - } | ||
| 92 | - } | ||
| 93 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -import java.util.List; | ||
| 19 | - | ||
| 20 | -import com.google.common.base.MoreObjects; | ||
| 21 | -import com.google.common.collect.ImmutableList; | ||
| 22 | - | ||
| 23 | -/** | ||
| 24 | - * Result of a batch read operation. | ||
| 25 | - */ | ||
| 26 | -public class BatchReadResult { | ||
| 27 | - | ||
| 28 | - private final List<ReadResult> readResults; | ||
| 29 | - | ||
| 30 | - public BatchReadResult(List<ReadResult> readResults) { | ||
| 31 | - this.readResults = ImmutableList.copyOf(readResults); | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | - /** | ||
| 35 | - * Returns the results as a list. | ||
| 36 | - * @return list of results | ||
| 37 | - */ | ||
| 38 | - public List<ReadResult> getAsList() { | ||
| 39 | - return readResults; | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | - /** | ||
| 43 | - * Returns the batch size. | ||
| 44 | - * @return batch size | ||
| 45 | - */ | ||
| 46 | - public int batchSize() { | ||
| 47 | - return readResults.size(); | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - @Override | ||
| 51 | - public String toString() { | ||
| 52 | - return MoreObjects.toStringHelper(getClass()) | ||
| 53 | - .add("readResults", readResults) | ||
| 54 | - .toString(); | ||
| 55 | - } | ||
| 56 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -import java.util.List; | ||
| 19 | - | ||
| 20 | -import com.google.common.base.MoreObjects; | ||
| 21 | -import com.google.common.collect.ImmutableList; | ||
| 22 | -import com.google.common.collect.Lists; | ||
| 23 | - | ||
| 24 | -/** | ||
| 25 | - * Collection of write requests to be submitted as one batch. | ||
| 26 | - */ | ||
| 27 | -public final class BatchWriteRequest { | ||
| 28 | - | ||
| 29 | - private final List<WriteRequest> writeRequests; | ||
| 30 | - | ||
| 31 | - /** | ||
| 32 | - * Creates a new BatchWriteRequest object from the specified list of write requests. | ||
| 33 | - * @param writeRequests write requests. | ||
| 34 | - * @return BatchWriteRequest object. | ||
| 35 | - */ | ||
| 36 | - public static BatchWriteRequest create(List<WriteRequest> writeRequests) { | ||
| 37 | - return new BatchWriteRequest(writeRequests); | ||
| 38 | - } | ||
| 39 | - | ||
| 40 | - private BatchWriteRequest(List<WriteRequest> writeRequests) { | ||
| 41 | - this.writeRequests = ImmutableList.copyOf(writeRequests); | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - /** | ||
| 45 | - * Returns the requests in this batch as a list. | ||
| 46 | - * @return list of write requests | ||
| 47 | - */ | ||
| 48 | - public List<WriteRequest> getAsList() { | ||
| 49 | - return writeRequests; | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | - /** | ||
| 53 | - * Returns the number of requests in this batch. | ||
| 54 | - * @return size of request batch. | ||
| 55 | - */ | ||
| 56 | - public int batchSize() { | ||
| 57 | - return writeRequests.size(); | ||
| 58 | - } | ||
| 59 | - | ||
| 60 | - @Override | ||
| 61 | - public String toString() { | ||
| 62 | - return MoreObjects.toStringHelper(getClass()) | ||
| 63 | - .add("writeRequests", writeRequests) | ||
| 64 | - .toString(); | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - public static Builder newBuilder() { | ||
| 68 | - return new Builder(); | ||
| 69 | - } | ||
| 70 | - | ||
| 71 | - /** | ||
| 72 | - * Builder for BatchWriteRequest. | ||
| 73 | - */ | ||
| 74 | - public static class Builder { | ||
| 75 | - | ||
| 76 | - private final List<WriteRequest> writeRequests = Lists.newLinkedList(); | ||
| 77 | - | ||
| 78 | - public Builder put(String tableName, String key, byte[] value) { | ||
| 79 | - writeRequests.add(WriteRequest.put(tableName, key, value)); | ||
| 80 | - return this; | ||
| 81 | - } | ||
| 82 | - | ||
| 83 | - public Builder putIfAbsent(String tableName, String key, byte[] value) { | ||
| 84 | - writeRequests.add(WriteRequest.putIfAbsent(tableName, key, value)); | ||
| 85 | - return this; | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | - public Builder putIfValueMatches(String tableName, String key, byte[] oldValue, byte[] newValue) { | ||
| 89 | - writeRequests.add(WriteRequest.putIfValueMatches(tableName, key, oldValue, newValue)); | ||
| 90 | - return this; | ||
| 91 | - } | ||
| 92 | - | ||
| 93 | - public Builder putIfVersionMatches(String tableName, String key, byte[] value, long version) { | ||
| 94 | - writeRequests.add(WriteRequest.putIfVersionMatches(tableName, key, value, version)); | ||
| 95 | - return this; | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | - public Builder remove(String tableName, String key) { | ||
| 99 | - writeRequests.add(WriteRequest.remove(tableName, key)); | ||
| 100 | - return this; | ||
| 101 | - } | ||
| 102 | - | ||
| 103 | - public Builder removeIfVersionMatches(String tableName, String key, long version) { | ||
| 104 | - writeRequests.add(WriteRequest.removeIfVersionMatches(tableName, key, version)); | ||
| 105 | - return this; | ||
| 106 | - } | ||
| 107 | - | ||
| 108 | - public Builder removeIfValueMatches(String tableName, String key, byte[] value) { | ||
| 109 | - writeRequests.add(WriteRequest.removeIfValueMatches(tableName, key, value)); | ||
| 110 | - return this; | ||
| 111 | - } | ||
| 112 | - | ||
| 113 | - public BatchWriteRequest build() { | ||
| 114 | - return new BatchWriteRequest(writeRequests); | ||
| 115 | - } | ||
| 116 | - } | ||
| 117 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -import java.util.List; | ||
| 19 | - | ||
| 20 | -import com.google.common.base.MoreObjects; | ||
| 21 | -import com.google.common.collect.ImmutableList; | ||
| 22 | - | ||
| 23 | -/** | ||
| 24 | - * Result of a batch write operation. | ||
| 25 | - */ | ||
| 26 | -public class BatchWriteResult { | ||
| 27 | - | ||
| 28 | - private final List<WriteResult> writeResults; | ||
| 29 | - | ||
| 30 | - public BatchWriteResult(List<WriteResult> writeResults) { | ||
| 31 | - this.writeResults = ImmutableList.copyOf(writeResults); | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | - /** | ||
| 35 | - * Returns true if this batch write operation was successful. | ||
| 36 | - * @return true if successful, false otherwise. | ||
| 37 | - */ | ||
| 38 | - public boolean isSuccessful() { | ||
| 39 | - for (WriteResult result : writeResults) { | ||
| 40 | - if (result.status() != WriteStatus.OK) { | ||
| 41 | - return false; | ||
| 42 | - } | ||
| 43 | - } | ||
| 44 | - return true; | ||
| 45 | - } | ||
| 46 | - | ||
| 47 | - /** | ||
| 48 | - * Returns the results as a List. | ||
| 49 | - * @return list of batch results. | ||
| 50 | - */ | ||
| 51 | - public List<WriteResult> getAsList() { | ||
| 52 | - return this.writeResults; | ||
| 53 | - } | ||
| 54 | - | ||
| 55 | - /** | ||
| 56 | - * Returns the size of this batch. | ||
| 57 | - * @return batch size. | ||
| 58 | - */ | ||
| 59 | - public int batchSize() { | ||
| 60 | - return writeResults.size(); | ||
| 61 | - } | ||
| 62 | - | ||
| 63 | - @Override | ||
| 64 | - public String toString() { | ||
| 65 | - return MoreObjects.toStringHelper(getClass()) | ||
| 66 | - .add("writeResults", writeResults) | ||
| 67 | - .toString(); | ||
| 68 | - } | ||
| 69 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -import java.util.Collection; | ||
| 19 | -import java.util.Optional; | ||
| 20 | -import java.util.Set; | ||
| 21 | - | ||
| 22 | -import org.onosproject.cluster.ControllerNode; | ||
| 23 | - | ||
| 24 | -/** | ||
| 25 | - * Service interface for running administrative tasks on a Database. | ||
| 26 | - */ | ||
| 27 | -public interface DatabaseAdminService { | ||
| 28 | - | ||
| 29 | - /** | ||
| 30 | - * Creates a new table. | ||
| 31 | - * Table creation is idempotent. Attempting to create a table | ||
| 32 | - * that already exists will be a noop. | ||
| 33 | - * @param name table name. | ||
| 34 | - * @return true if the table was created by this call, false otherwise. | ||
| 35 | - */ | ||
| 36 | - public boolean createTable(String name); | ||
| 37 | - | ||
| 38 | - /** | ||
| 39 | - * Creates a new table where last update time will be used to track and expire old entries. | ||
| 40 | - * Table creation is idempotent. Attempting to create a table | ||
| 41 | - * that already exists will be a noop. | ||
| 42 | - * @param name table name. | ||
| 43 | - * @param ttlMillis total duration in millis since last update time when entries will be expired. | ||
| 44 | - * @return true if the table was created by this call, false otherwise. | ||
| 45 | - */ | ||
| 46 | - public boolean createTable(String name, int ttlMillis); | ||
| 47 | - | ||
| 48 | - /** | ||
| 49 | - * Lists all the tables in the database. | ||
| 50 | - * @return set of table names. | ||
| 51 | - */ | ||
| 52 | - public Set<String> listTables(); | ||
| 53 | - | ||
| 54 | - /** | ||
| 55 | - * Deletes a table from the database. | ||
| 56 | - * @param name name of the table to delete. | ||
| 57 | - */ | ||
| 58 | - public void dropTable(String name); | ||
| 59 | - | ||
| 60 | - /** | ||
| 61 | - * Deletes all tables from the database. | ||
| 62 | - */ | ||
| 63 | - public void dropAllTables(); | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - /** | ||
| 67 | - * Add member to default Tablet. | ||
| 68 | - * | ||
| 69 | - * @param node to add | ||
| 70 | - */ | ||
| 71 | - public void addMember(ControllerNode node); | ||
| 72 | - | ||
| 73 | - /** | ||
| 74 | - * Remove member from default Tablet. | ||
| 75 | - * | ||
| 76 | - * @param node node to remove | ||
| 77 | - */ | ||
| 78 | - public void removeMember(ControllerNode node); | ||
| 79 | - | ||
| 80 | - /** | ||
| 81 | - * List members forming default Tablet. | ||
| 82 | - * | ||
| 83 | - * @return Copied collection of members forming default Tablet. | ||
| 84 | - */ | ||
| 85 | - public Collection<ControllerNode> listMembers(); | ||
| 86 | - | ||
| 87 | - /** | ||
| 88 | - * Returns the current Leader of the default Tablet. | ||
| 89 | - * | ||
| 90 | - * @return leader node | ||
| 91 | - */ | ||
| 92 | - public Optional<ControllerNode> leader(); | ||
| 93 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -/** | ||
| 19 | - * Base exception type for database failures. | ||
| 20 | - */ | ||
| 21 | -@SuppressWarnings("serial") | ||
| 22 | -public class DatabaseException extends RuntimeException { | ||
| 23 | - public DatabaseException(String message, Throwable t) { | ||
| 24 | - super(message, t); | ||
| 25 | - } | ||
| 26 | - | ||
| 27 | - public DatabaseException(String message) { | ||
| 28 | - super(message); | ||
| 29 | - } | ||
| 30 | - | ||
| 31 | - public DatabaseException(Throwable t) { | ||
| 32 | - super(t); | ||
| 33 | - } | ||
| 34 | - | ||
| 35 | - public DatabaseException() { | ||
| 36 | - }; | ||
| 37 | - | ||
| 38 | - public static class Timeout extends DatabaseException { | ||
| 39 | - public Timeout(String message, Throwable t) { | ||
| 40 | - super(message, t); | ||
| 41 | - } | ||
| 42 | - | ||
| 43 | - public Timeout(String message) { | ||
| 44 | - super(message); | ||
| 45 | - } | ||
| 46 | - | ||
| 47 | - public Timeout(Throwable t) { | ||
| 48 | - super(t); | ||
| 49 | - } | ||
| 50 | - } | ||
| 51 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -import java.util.Map; | ||
| 19 | - | ||
| 20 | -/** | ||
| 21 | - * Service interface for a strongly consistent and durable | ||
| 22 | - * key value data store. | ||
| 23 | - */ | ||
| 24 | -public interface DatabaseService { | ||
| 25 | - | ||
| 26 | - /** | ||
| 27 | - * Reads the specified key. | ||
| 28 | - * @param tableName name of the table associated with this operation. | ||
| 29 | - * @param key key to read. | ||
| 30 | - * @return value (and version) associated with this key. This calls returns null if the key does not exist. | ||
| 31 | - */ | ||
| 32 | - VersionedValue get(String tableName, String key); | ||
| 33 | - | ||
| 34 | - /** | ||
| 35 | - * Reads the whole table. | ||
| 36 | - * | ||
| 37 | - * @param tableName name of the table associated with this operation. | ||
| 38 | - * @return the whole table | ||
| 39 | - */ | ||
| 40 | - Map<String, VersionedValue> getAll(String tableName); | ||
| 41 | - | ||
| 42 | - /** | ||
| 43 | - * Associate the key with a value. | ||
| 44 | - * @param tableName table name in which this key/value resides. | ||
| 45 | - * @param key key with which the specified value is to be associated | ||
| 46 | - * @param value value to be associated with the specified key | ||
| 47 | - * @return the previous value associated with the specified key, or null if there was no mapping for the key. | ||
| 48 | - */ | ||
| 49 | - VersionedValue put(String tableName, String key, byte[] value); | ||
| 50 | - | ||
| 51 | - /** | ||
| 52 | - * If the specified key is not already associated with a value, associate it with the given value. | ||
| 53 | - * @param tableName table name in which this key/value resides. | ||
| 54 | - * @param key key with which the specified value is to be associated | ||
| 55 | - * @param value value to be associated with the specified key | ||
| 56 | - * @return true if put was successful, false if there is already a value associated with this key | ||
| 57 | - */ | ||
| 58 | - boolean putIfAbsent(String tableName, String key, byte[] value); | ||
| 59 | - | ||
| 60 | - /** | ||
| 61 | - * Sets the key to the specified value if the version in the database (for that key) | ||
| 62 | - * matches the specified version. | ||
| 63 | - * @param tableName name of table associated with this operation. | ||
| 64 | - * @param key key | ||
| 65 | - * @param value value | ||
| 66 | - * @param version version that should present in the database for the put to be successful. | ||
| 67 | - * @return true if put was successful, false if there version in database is different from what is specified. | ||
| 68 | - */ | ||
| 69 | - boolean putIfVersionMatches(String tableName, String key, byte[] value, long version); | ||
| 70 | - | ||
| 71 | - /** | ||
| 72 | - * Replaces the entry for a key only if currently mapped to a given value. | ||
| 73 | - * @param tableName name of table associated with this operation. | ||
| 74 | - * @param key with which the specified value is associated | ||
| 75 | - * @param oldValue value expected to be associated with the specified key | ||
| 76 | - * @param newValue value to be associated with the specified key | ||
| 77 | - * @return true if put was successful, false if there version in database is different from what is specified. | ||
| 78 | - */ | ||
| 79 | - boolean putIfValueMatches(String tableName, String key, byte[] oldValue, byte[] newValue); | ||
| 80 | - | ||
| 81 | - /** | ||
| 82 | - * Removes the key (and associated value). | ||
| 83 | - * @param tableName name of table associated with this operation. | ||
| 84 | - * @param key key to remove | ||
| 85 | - * @return value previously associated with the key. This call returns null if the key does not exist. | ||
| 86 | - */ | ||
| 87 | - VersionedValue remove(String tableName, String key); | ||
| 88 | - | ||
| 89 | - /** | ||
| 90 | - * Removes the key (and associated value) if the version in the database matches specified version. | ||
| 91 | - * @param tableName name of table associated with this operation. | ||
| 92 | - * @param key key to remove | ||
| 93 | - * @param version version that should present in the database for the remove to be successful. | ||
| 94 | - * @return true if remove was successful, false if there version in database is different from what is specified. | ||
| 95 | - */ | ||
| 96 | - boolean removeIfVersionMatches(String tableName, String key, long version); | ||
| 97 | - | ||
| 98 | - /** | ||
| 99 | - * Removes the key (and associated value) if the value in the database matches specified value. | ||
| 100 | - * @param tableName name of table associated with this operation. | ||
| 101 | - * @param key key to remove | ||
| 102 | - * @param value value that should present in the database for the remove to be successful. | ||
| 103 | - * @return true if remove was successful, false if there value in database is different from what is specified. | ||
| 104 | - */ | ||
| 105 | - boolean removeIfValueMatches(String tableName, String key, byte[] value); | ||
| 106 | - | ||
| 107 | - /** | ||
| 108 | - * Performs a batch read operation and returns the results. | ||
| 109 | - * @param batchRequest batch request. | ||
| 110 | - * @return result of the batch operation. | ||
| 111 | - */ | ||
| 112 | - BatchReadResult batchRead(BatchReadRequest batchRequest); | ||
| 113 | - | ||
| 114 | - /** | ||
| 115 | - * Performs a batch write operation and returns the results. | ||
| 116 | - * This method provides transactional semantics. Either all writes succeed or none do. | ||
| 117 | - * Even a single write failure would cause the entire batch to be aborted. | ||
| 118 | - * In the case of unsuccessful operation, the batch result can be inspected to determine | ||
| 119 | - * which operation(s) caused the batch to fail. | ||
| 120 | - * @param batchRequest batch request. | ||
| 121 | - * @return result of the batch operation. | ||
| 122 | - */ | ||
| 123 | - BatchWriteResult batchWrite(BatchWriteRequest batchRequest); | ||
| 124 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | - | ||
| 19 | -/** | ||
| 20 | - * Exception thrown when an operation (read or write) is requested for | ||
| 21 | - * a table that does not exist. | ||
| 22 | - */ | ||
| 23 | -@SuppressWarnings("serial") | ||
| 24 | -public class NoSuchTableException extends DatabaseException { | ||
| 25 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -/** | ||
| 19 | - * A container object which either has a result or an exception. | ||
| 20 | - * <p> | ||
| 21 | - * If a result is present, get() will return it otherwise get() will throw | ||
| 22 | - * the exception that was encountered in the process of generating the result. | ||
| 23 | - * </p> | ||
| 24 | - * @param <R> type of result. | ||
| 25 | - * @param <E> exception encountered in generating the result. | ||
| 26 | - */ | ||
| 27 | -public interface OptionalResult<R, E extends Throwable> { | ||
| 28 | - | ||
| 29 | - /** | ||
| 30 | - * Returns the result or throws an exception if there is no | ||
| 31 | - * valid result. | ||
| 32 | - * @return result | ||
| 33 | - */ | ||
| 34 | - public R get(); | ||
| 35 | - | ||
| 36 | - /** | ||
| 37 | - * Returns true if there is a valid result. | ||
| 38 | - * @return true is yes, false otherwise. | ||
| 39 | - */ | ||
| 40 | - public boolean hasValidResult(); | ||
| 41 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -import static com.google.common.base.Preconditions.checkNotNull; | ||
| 19 | - | ||
| 20 | -import java.util.Objects; | ||
| 21 | - | ||
| 22 | -import com.google.common.base.MoreObjects; | ||
| 23 | - | ||
| 24 | -/** | ||
| 25 | - * Database read request. | ||
| 26 | - */ | ||
| 27 | -public class ReadRequest { | ||
| 28 | - | ||
| 29 | - private final String tableName; | ||
| 30 | - private final String key; | ||
| 31 | - | ||
| 32 | - /** | ||
| 33 | - * Creates a read request, | ||
| 34 | - * which will retrieve the specified key from the table. | ||
| 35 | - * | ||
| 36 | - * @param tableName name of the table | ||
| 37 | - * @param key key in the table | ||
| 38 | - * @return ReadRequest | ||
| 39 | - */ | ||
| 40 | - public static ReadRequest get(String tableName, String key) { | ||
| 41 | - return new ReadRequest(tableName, key); | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - public ReadRequest(String tableName, String key) { | ||
| 45 | - this.tableName = checkNotNull(tableName); | ||
| 46 | - this.key = checkNotNull(key); | ||
| 47 | - } | ||
| 48 | - | ||
| 49 | - /** | ||
| 50 | - * Return the name of the table. | ||
| 51 | - * @return table name. | ||
| 52 | - */ | ||
| 53 | - public String tableName() { | ||
| 54 | - return tableName; | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - /** | ||
| 58 | - * Returns the key. | ||
| 59 | - * @return key. | ||
| 60 | - */ | ||
| 61 | - public String key() { | ||
| 62 | - return key; | ||
| 63 | - } | ||
| 64 | - | ||
| 65 | - @Override | ||
| 66 | - public String toString() { | ||
| 67 | - return MoreObjects.toStringHelper(getClass()) | ||
| 68 | - .add("tableName", tableName) | ||
| 69 | - .add("key", key) | ||
| 70 | - .toString(); | ||
| 71 | - } | ||
| 72 | - | ||
| 73 | - @Override | ||
| 74 | - public int hashCode() { | ||
| 75 | - return Objects.hash(key, tableName); | ||
| 76 | - } | ||
| 77 | - | ||
| 78 | - @Override | ||
| 79 | - public boolean equals(Object obj) { | ||
| 80 | - if (this == obj) { | ||
| 81 | - return true; | ||
| 82 | - } | ||
| 83 | - if (obj == null) { | ||
| 84 | - return false; | ||
| 85 | - } | ||
| 86 | - if (getClass() != obj.getClass()) { | ||
| 87 | - return false; | ||
| 88 | - } | ||
| 89 | - ReadRequest other = (ReadRequest) obj; | ||
| 90 | - return Objects.equals(this.key, other.key) && | ||
| 91 | - Objects.equals(this.tableName, other.tableName); | ||
| 92 | - } | ||
| 93 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -import com.google.common.base.MoreObjects; | ||
| 19 | - | ||
| 20 | - | ||
| 21 | -/** | ||
| 22 | - * Database read result. | ||
| 23 | - */ | ||
| 24 | -public class ReadResult { | ||
| 25 | - | ||
| 26 | - private final String tableName; | ||
| 27 | - private final String key; | ||
| 28 | - private final VersionedValue value; | ||
| 29 | - private final ReadStatus status; | ||
| 30 | - | ||
| 31 | - public ReadResult(ReadStatus status, String tableName, String key, VersionedValue value) { | ||
| 32 | - this.status = status; | ||
| 33 | - this.tableName = tableName; | ||
| 34 | - this.key = key; | ||
| 35 | - this.value = value; | ||
| 36 | - } | ||
| 37 | - | ||
| 38 | - /** | ||
| 39 | - * Returns the status of the read operation. | ||
| 40 | - * @return read operation status | ||
| 41 | - */ | ||
| 42 | - public ReadStatus status() { | ||
| 43 | - return status; | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | - /** | ||
| 47 | - * Returns database table name. | ||
| 48 | - * @return table name | ||
| 49 | - */ | ||
| 50 | - public String tableName() { | ||
| 51 | - return tableName; | ||
| 52 | - } | ||
| 53 | - | ||
| 54 | - /** | ||
| 55 | - * Returns database table key. | ||
| 56 | - * @return key | ||
| 57 | - */ | ||
| 58 | - public String key() { | ||
| 59 | - return key; | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - /** | ||
| 63 | - * Returns true if database table contained value for the key. | ||
| 64 | - * | ||
| 65 | - * @return true if database table contained value for the key | ||
| 66 | - */ | ||
| 67 | - public boolean valueExists() { | ||
| 68 | - return value != null; | ||
| 69 | - } | ||
| 70 | - | ||
| 71 | - /** | ||
| 72 | - * Returns value associated with the key. | ||
| 73 | - * @return non-null value if the table contains one, null otherwise. | ||
| 74 | - */ | ||
| 75 | - public VersionedValue value() { | ||
| 76 | - return value; | ||
| 77 | - } | ||
| 78 | - | ||
| 79 | - @Override | ||
| 80 | - public String toString() { | ||
| 81 | - return MoreObjects.toStringHelper(getClass()) | ||
| 82 | - .add("status", status) | ||
| 83 | - .add("tableName", tableName) | ||
| 84 | - .add("key", key) | ||
| 85 | - .add("value", value) | ||
| 86 | - .toString(); | ||
| 87 | - } | ||
| 88 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -/** | ||
| 19 | - * Status of completed read request. | ||
| 20 | - */ | ||
| 21 | -public enum ReadStatus { | ||
| 22 | - | ||
| 23 | - /** | ||
| 24 | - * Read completed successfully. | ||
| 25 | - */ | ||
| 26 | - OK, | ||
| 27 | - | ||
| 28 | - /** | ||
| 29 | - * Read failed due to an invalid table name being specified. | ||
| 30 | - */ | ||
| 31 | - NO_SUCH_TABLE | ||
| 32 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -import java.util.Arrays; | ||
| 19 | - | ||
| 20 | -import org.onlab.util.ByteArraySizeHashPrinter; | ||
| 21 | - | ||
| 22 | -import com.google.common.base.MoreObjects; | ||
| 23 | - | ||
| 24 | -/** | ||
| 25 | - * Wrapper object that holds the object (as byte array) and its version. | ||
| 26 | - */ | ||
| 27 | -public class VersionedValue { | ||
| 28 | - | ||
| 29 | - private final byte[] value; | ||
| 30 | - private final long version; | ||
| 31 | - | ||
| 32 | - /** | ||
| 33 | - * Creates a new instance with the specified value and version. | ||
| 34 | - * @param value value | ||
| 35 | - * @param version version | ||
| 36 | - */ | ||
| 37 | - public VersionedValue(byte[] value, long version) { | ||
| 38 | - this.value = value; | ||
| 39 | - this.version = version; | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | - /** | ||
| 43 | - * Returns the value. | ||
| 44 | - * @return value. | ||
| 45 | - */ | ||
| 46 | - public byte[] value() { | ||
| 47 | - return value; | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - /** | ||
| 51 | - * Returns the version. | ||
| 52 | - * @return version. | ||
| 53 | - */ | ||
| 54 | - public long version() { | ||
| 55 | - return version; | ||
| 56 | - } | ||
| 57 | - | ||
| 58 | - /** | ||
| 59 | - * Creates a copy of given VersionedValue. | ||
| 60 | - * | ||
| 61 | - * @param original VersionedValue to create a copy | ||
| 62 | - * @return same as original if original or it's value is null, | ||
| 63 | - * otherwise creates a copy. | ||
| 64 | - */ | ||
| 65 | - public static VersionedValue copy(VersionedValue original) { | ||
| 66 | - if (original == null) { | ||
| 67 | - return null; | ||
| 68 | - } | ||
| 69 | - if (original.value == null) { | ||
| 70 | - // immutable, no need to copy | ||
| 71 | - return original; | ||
| 72 | - } else { | ||
| 73 | - return new VersionedValue( | ||
| 74 | - Arrays.copyOf(original.value, | ||
| 75 | - original.value.length), | ||
| 76 | - original.version); | ||
| 77 | - } | ||
| 78 | - } | ||
| 79 | - | ||
| 80 | - @Override | ||
| 81 | - public String toString() { | ||
| 82 | - return MoreObjects.toStringHelper(getClass()) | ||
| 83 | - .add("version", version) | ||
| 84 | - .add("value", ByteArraySizeHashPrinter.orNull(value)) | ||
| 85 | - .toString(); | ||
| 86 | - } | ||
| 87 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -import static com.google.common.base.Preconditions.checkArgument; | ||
| 19 | -import static com.google.common.base.Preconditions.checkNotNull; | ||
| 20 | -import static org.onosproject.store.service.WriteRequest.Type.*; | ||
| 21 | - | ||
| 22 | -import java.util.Objects; | ||
| 23 | - | ||
| 24 | -import org.onlab.util.ByteArraySizeHashPrinter; | ||
| 25 | - | ||
| 26 | -import com.google.common.base.MoreObjects; | ||
| 27 | - | ||
| 28 | -/** | ||
| 29 | - * Database write request. | ||
| 30 | - */ | ||
| 31 | -public class WriteRequest { | ||
| 32 | - | ||
| 33 | - public static final int ANY_VERSION = -1; | ||
| 34 | - | ||
| 35 | - private final String tableName; | ||
| 36 | - private final String key; | ||
| 37 | - | ||
| 38 | - private final Type type; | ||
| 39 | - | ||
| 40 | - private final byte[] newValue; | ||
| 41 | - private final long previousVersion; | ||
| 42 | - private final byte[] oldValue; | ||
| 43 | - | ||
| 44 | - /** | ||
| 45 | - * Creates a write request, which will | ||
| 46 | - * put the specified value to the table regardless of the previous value. | ||
| 47 | - * | ||
| 48 | - * @param tableName name of the table | ||
| 49 | - * @param key key in the table | ||
| 50 | - * @param newValue value to write, must not be null | ||
| 51 | - * @return WriteRequest | ||
| 52 | - */ | ||
| 53 | - public static WriteRequest put(String tableName, String key, | ||
| 54 | - byte[] newValue) { | ||
| 55 | - return new WriteRequest(PUT, tableName, key, | ||
| 56 | - checkNotNull(newValue), ANY_VERSION, null); | ||
| 57 | - } | ||
| 58 | - | ||
| 59 | - /** | ||
| 60 | - * Creates a write request, which will | ||
| 61 | - * put the specified value to the table if the previous version matches. | ||
| 62 | - * | ||
| 63 | - * @param tableName name of the table | ||
| 64 | - * @param key key in the table | ||
| 65 | - * @param newValue value to write, must not be null | ||
| 66 | - * @param previousVersion previous version expected | ||
| 67 | - * @return WriteRequest | ||
| 68 | - */ | ||
| 69 | - public static WriteRequest putIfVersionMatches(String tableName, String key, | ||
| 70 | - byte[] newValue, | ||
| 71 | - long previousVersion) { | ||
| 72 | - checkArgument(previousVersion >= 0); | ||
| 73 | - return new WriteRequest(PUT_IF_VERSION, tableName, key, | ||
| 74 | - checkNotNull(newValue), previousVersion, null); | ||
| 75 | - } | ||
| 76 | - | ||
| 77 | - /** | ||
| 78 | - * Creates a write request, which will | ||
| 79 | - * put the specified value to the table if the previous value matches. | ||
| 80 | - * | ||
| 81 | - * @param tableName name of the table | ||
| 82 | - * @param key key in the table | ||
| 83 | - * @param oldValue previous value expected, must not be null | ||
| 84 | - * @param newValue value to write, must not be null | ||
| 85 | - * @return WriteRequest | ||
| 86 | - */ | ||
| 87 | - public static WriteRequest putIfValueMatches(String tableName, String key, | ||
| 88 | - byte[] oldValue, | ||
| 89 | - byte[] newValue) { | ||
| 90 | - return new WriteRequest(PUT_IF_VALUE, tableName, key, | ||
| 91 | - checkNotNull(newValue), ANY_VERSION, | ||
| 92 | - checkNotNull(oldValue)); | ||
| 93 | - } | ||
| 94 | - | ||
| 95 | - /** | ||
| 96 | - * Creates a write request, which will | ||
| 97 | - * put the specified value to the table if the previous value does not exist. | ||
| 98 | - * | ||
| 99 | - * @param tableName name of the table | ||
| 100 | - * @param key key in the table | ||
| 101 | - * @param newValue value to write, must not be null | ||
| 102 | - * @return WriteRequest | ||
| 103 | - */ | ||
| 104 | - public static WriteRequest putIfAbsent(String tableName, String key, | ||
| 105 | - byte[] newValue) { | ||
| 106 | - return new WriteRequest(PUT_IF_ABSENT, tableName, key, | ||
| 107 | - checkNotNull(newValue), ANY_VERSION, null); | ||
| 108 | - } | ||
| 109 | - | ||
| 110 | - /** | ||
| 111 | - * Creates a write request, which will | ||
| 112 | - * remove the specified entry from the table regardless of the previous value. | ||
| 113 | - * | ||
| 114 | - * @param tableName name of the table | ||
| 115 | - * @param key key in the table | ||
| 116 | - * @return WriteRequest | ||
| 117 | - */ | ||
| 118 | - public static WriteRequest remove(String tableName, String key) { | ||
| 119 | - return new WriteRequest(REMOVE, tableName, key, | ||
| 120 | - null, ANY_VERSION, null); | ||
| 121 | - } | ||
| 122 | - | ||
| 123 | - /** | ||
| 124 | - * Creates a write request, which will | ||
| 125 | - * remove the specified entry from the table if the previous version matches. | ||
| 126 | - * | ||
| 127 | - * @param tableName name of the table | ||
| 128 | - * @param key key in the table | ||
| 129 | - * @param previousVersion previous version expected | ||
| 130 | - * @return WriteRequest | ||
| 131 | - */ | ||
| 132 | - public static WriteRequest removeIfVersionMatches(String tableName, String key, | ||
| 133 | - long previousVersion) { | ||
| 134 | - return new WriteRequest(REMOVE_IF_VERSION, tableName, key, | ||
| 135 | - null, previousVersion, null); | ||
| 136 | - } | ||
| 137 | - | ||
| 138 | - /** | ||
| 139 | - * Creates a write request, which will | ||
| 140 | - * remove the specified entry from the table if the previous value matches. | ||
| 141 | - * | ||
| 142 | - * @param tableName name of the table | ||
| 143 | - * @param key key in the table | ||
| 144 | - * @param oldValue previous value expected, must not be null | ||
| 145 | - * @return WriteRequest | ||
| 146 | - */ | ||
| 147 | - public static WriteRequest removeIfValueMatches(String tableName, String key, | ||
| 148 | - byte[] oldValue) { | ||
| 149 | - return new WriteRequest(REMOVE_IF_VALUE, tableName, key, | ||
| 150 | - null, ANY_VERSION, checkNotNull(oldValue)); | ||
| 151 | - } | ||
| 152 | - | ||
| 153 | - public enum Type { | ||
| 154 | - PUT, | ||
| 155 | - PUT_IF_VERSION, | ||
| 156 | - PUT_IF_VALUE, | ||
| 157 | - PUT_IF_ABSENT, | ||
| 158 | - REMOVE, | ||
| 159 | - REMOVE_IF_VERSION, | ||
| 160 | - REMOVE_IF_VALUE, | ||
| 161 | - } | ||
| 162 | - | ||
| 163 | - // hidden constructor | ||
| 164 | - protected WriteRequest(Type type, String tableName, String key, | ||
| 165 | - byte[] newValue, | ||
| 166 | - long previousVersion, byte[] oldValue) { | ||
| 167 | - | ||
| 168 | - checkNotNull(tableName); | ||
| 169 | - checkNotNull(key); | ||
| 170 | - | ||
| 171 | - this.tableName = tableName; | ||
| 172 | - this.key = key; | ||
| 173 | - this.type = type; | ||
| 174 | - this.newValue = newValue; | ||
| 175 | - this.previousVersion = previousVersion; | ||
| 176 | - this.oldValue = oldValue; | ||
| 177 | - } | ||
| 178 | - | ||
| 179 | - public String tableName() { | ||
| 180 | - return tableName; | ||
| 181 | - } | ||
| 182 | - | ||
| 183 | - public String key() { | ||
| 184 | - return key; | ||
| 185 | - } | ||
| 186 | - | ||
| 187 | - public WriteRequest.Type type() { | ||
| 188 | - return type; | ||
| 189 | - } | ||
| 190 | - | ||
| 191 | - public byte[] newValue() { | ||
| 192 | - return newValue; | ||
| 193 | - } | ||
| 194 | - | ||
| 195 | - public long previousVersion() { | ||
| 196 | - return previousVersion; | ||
| 197 | - } | ||
| 198 | - | ||
| 199 | - public byte[] oldValue() { | ||
| 200 | - return oldValue; | ||
| 201 | - } | ||
| 202 | - | ||
| 203 | - @Override | ||
| 204 | - public String toString() { | ||
| 205 | - return MoreObjects.toStringHelper(getClass()) | ||
| 206 | - .add("type", type) | ||
| 207 | - .add("tableName", tableName) | ||
| 208 | - .add("key", key) | ||
| 209 | - .add("newValue", ByteArraySizeHashPrinter.orNull(newValue)) | ||
| 210 | - .add("previousVersion", previousVersion) | ||
| 211 | - .add("oldValue", ByteArraySizeHashPrinter.orNull(oldValue)) | ||
| 212 | - .toString(); | ||
| 213 | - } | ||
| 214 | - | ||
| 215 | - // TODO: revisit hashCode, equals condition | ||
| 216 | - @Override | ||
| 217 | - public int hashCode() { | ||
| 218 | - return Objects.hash(type, key, tableName, previousVersion); | ||
| 219 | - } | ||
| 220 | - | ||
| 221 | - @Override | ||
| 222 | - public boolean equals(Object obj) { | ||
| 223 | - if (this == obj) { | ||
| 224 | - return true; | ||
| 225 | - } | ||
| 226 | - if (obj == null) { | ||
| 227 | - return false; | ||
| 228 | - } | ||
| 229 | - if (getClass() != obj.getClass()) { | ||
| 230 | - return false; | ||
| 231 | - } | ||
| 232 | - WriteRequest other = (WriteRequest) obj; | ||
| 233 | - return Objects.equals(this.type, other.type) && | ||
| 234 | - Objects.equals(this.key, other.key) && | ||
| 235 | - Objects.equals(this.tableName, other.tableName) && | ||
| 236 | - Objects.equals(this.previousVersion, other.previousVersion); | ||
| 237 | - } | ||
| 238 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -import com.google.common.base.MoreObjects; | ||
| 19 | - | ||
| 20 | - | ||
| 21 | -/** | ||
| 22 | - * Database write result. | ||
| 23 | - */ | ||
| 24 | -public class WriteResult { | ||
| 25 | - | ||
| 26 | - private final WriteStatus status; | ||
| 27 | - private final VersionedValue previousValue; | ||
| 28 | - | ||
| 29 | - public WriteResult(WriteStatus status, VersionedValue previousValue) { | ||
| 30 | - this.status = status; | ||
| 31 | - this.previousValue = previousValue; | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | - public VersionedValue previousValue() { | ||
| 35 | - return previousValue; | ||
| 36 | - } | ||
| 37 | - | ||
| 38 | - public WriteStatus status() { | ||
| 39 | - return status; | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | - @Override | ||
| 43 | - public String toString() { | ||
| 44 | - return MoreObjects.toStringHelper(getClass()) | ||
| 45 | - .add("status", status) | ||
| 46 | - .add("previousValue", previousValue) | ||
| 47 | - .toString(); | ||
| 48 | - } | ||
| 49 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | -package org.onosproject.store.service; | ||
| 17 | - | ||
| 18 | -/** | ||
| 19 | - * Status of completed write request. | ||
| 20 | - */ | ||
| 21 | -public enum WriteStatus { | ||
| 22 | - | ||
| 23 | - /** | ||
| 24 | - * Write completed successfully. | ||
| 25 | - */ | ||
| 26 | - OK, | ||
| 27 | - | ||
| 28 | - /** | ||
| 29 | - * Write was aborted (ex: if one or more write operations in a batch fail, others are aborted). | ||
| 30 | - */ | ||
| 31 | - ABORTED, | ||
| 32 | - | ||
| 33 | - /** | ||
| 34 | - * Write failed due to pre-condition failure. (ex: version or value mis-match). | ||
| 35 | - */ | ||
| 36 | - PRECONDITION_VIOLATION, | ||
| 37 | - | ||
| 38 | - /** | ||
| 39 | - * Write failed due to an invalid table name being specified. | ||
| 40 | - */ | ||
| 41 | - NO_SUCH_TABLE, | ||
| 42 | -} |
core/store/dist/src/main/java/org/onosproject/store/intent/impl/DistributedIntentStore.java
deleted
100644 → 0
This diff is collapsed. Click to expand it.
This diff is collapsed. Click to expand it.
| 1 | -/* | ||
| 2 | - * Copyright 2014 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | - | ||
| 17 | -package org.onosproject.store.service.impl; | ||
| 18 | - | ||
| 19 | -import static com.google.common.base.Preconditions.checkNotNull; | ||
| 20 | -import static com.google.common.base.Predicates.notNull; | ||
| 21 | - | ||
| 22 | -import java.util.Map; | ||
| 23 | - | ||
| 24 | -import org.onosproject.store.serializers.StoreSerializer; | ||
| 25 | -import org.onosproject.store.service.DatabaseAdminService; | ||
| 26 | -import org.onosproject.store.service.DatabaseException; | ||
| 27 | -import org.onosproject.store.service.DatabaseService; | ||
| 28 | -import org.onosproject.store.service.VersionedValue; | ||
| 29 | - | ||
| 30 | -import com.google.common.base.Function; | ||
| 31 | -import com.google.common.cache.CacheBuilder; | ||
| 32 | -import com.google.common.cache.CacheLoader; | ||
| 33 | -import com.google.common.cache.LoadingCache; | ||
| 34 | -import com.google.common.collect.FluentIterable; | ||
| 35 | - | ||
| 36 | -/** | ||
| 37 | - * Map like interface wrapper around DatabaseService. | ||
| 38 | - * | ||
| 39 | - * @param <K> Key type of the map. | ||
| 40 | - * The type must have toString(), which can uniquely identify the entry. | ||
| 41 | - * @param <V> Value type | ||
| 42 | - */ | ||
| 43 | -public class CMap<K, V> { | ||
| 44 | - | ||
| 45 | - @SuppressWarnings("unused") | ||
| 46 | - private final DatabaseAdminService dbAdminService; | ||
| 47 | - | ||
| 48 | - private final DatabaseService dbService; | ||
| 49 | - | ||
| 50 | - private final String tableName; | ||
| 51 | - private final StoreSerializer serializer; | ||
| 52 | - | ||
| 53 | - private final LoadingCache<K, String> keyCache; | ||
| 54 | - | ||
| 55 | - /** | ||
| 56 | - * Creates a CMap instance. | ||
| 57 | - * It will create the table if necessary. | ||
| 58 | - * | ||
| 59 | - * @param dbAdminService DatabaseAdminService to use for this instance | ||
| 60 | - * @param dbService DatabaseService to use for this instance | ||
| 61 | - * @param tableName table which this Map corresponds to | ||
| 62 | - * @param serializer Value serializer | ||
| 63 | - */ | ||
| 64 | - public CMap(DatabaseAdminService dbAdminService, | ||
| 65 | - DatabaseService dbService, | ||
| 66 | - String tableName, | ||
| 67 | - StoreSerializer serializer) { | ||
| 68 | - | ||
| 69 | - this.dbAdminService = checkNotNull(dbAdminService); | ||
| 70 | - this.dbService = checkNotNull(dbService); | ||
| 71 | - this.tableName = checkNotNull(tableName); | ||
| 72 | - this.serializer = checkNotNull(serializer); | ||
| 73 | - | ||
| 74 | - boolean tableReady = false; | ||
| 75 | - do { | ||
| 76 | - try { | ||
| 77 | - if (!dbAdminService.listTables().contains(tableName)) { | ||
| 78 | - dbAdminService.createTable(tableName); | ||
| 79 | - } | ||
| 80 | - tableReady = true; | ||
| 81 | - } catch (DatabaseException e) { | ||
| 82 | - try { | ||
| 83 | - Thread.sleep(200); | ||
| 84 | - } catch (InterruptedException e1) { | ||
| 85 | - throw new DatabaseException(e1); | ||
| 86 | - } | ||
| 87 | - } | ||
| 88 | - } while (!tableReady); | ||
| 89 | - | ||
| 90 | - keyCache = CacheBuilder.newBuilder() | ||
| 91 | - .softValues() | ||
| 92 | - .build(new CacheLoader<K, String>() { | ||
| 93 | - | ||
| 94 | - @Override | ||
| 95 | - public String load(K key) { | ||
| 96 | - return key.toString(); | ||
| 97 | - } | ||
| 98 | - }); | ||
| 99 | - } | ||
| 100 | - | ||
| 101 | - protected String sK(K key) { | ||
| 102 | - return keyCache.getUnchecked(key); | ||
| 103 | - } | ||
| 104 | - | ||
| 105 | - protected byte[] sV(V val) { | ||
| 106 | - return serializer.encode(val); | ||
| 107 | - } | ||
| 108 | - | ||
| 109 | - protected V dV(byte[] valBytes) { | ||
| 110 | - return serializer.decode(valBytes); | ||
| 111 | - } | ||
| 112 | - | ||
| 113 | - /** | ||
| 114 | - * Puts an entry to the map, if not already present. | ||
| 115 | - * | ||
| 116 | - * @param key the key of the value to put if absent | ||
| 117 | - * @param value the value to be put if previous value does not exist | ||
| 118 | - * @return true if put was successful. | ||
| 119 | - */ | ||
| 120 | - public boolean putIfAbsent(K key, V value) { | ||
| 121 | - return dbService.putIfAbsent(tableName, sK(key), sV(value)); | ||
| 122 | - } | ||
| 123 | - | ||
| 124 | - /** | ||
| 125 | - * Removes an entry associated to specified key. | ||
| 126 | - * | ||
| 127 | - * @param key key of the value to remove | ||
| 128 | - * @return previous value in the map for the key | ||
| 129 | - */ | ||
| 130 | - public V remove(K key) { | ||
| 131 | - VersionedValue removed = dbService.remove(tableName, sK(key)); | ||
| 132 | - if (removed == null) { | ||
| 133 | - return null; | ||
| 134 | - } | ||
| 135 | - return dV(removed.value()); | ||
| 136 | - } | ||
| 137 | - | ||
| 138 | - /** | ||
| 139 | - * Returns the size of the map. | ||
| 140 | - * | ||
| 141 | - * @return size of the map | ||
| 142 | - */ | ||
| 143 | - public long size() { | ||
| 144 | - // TODO this is very inefficient | ||
| 145 | - return dbService.getAll(tableName).size(); | ||
| 146 | - } | ||
| 147 | - | ||
| 148 | - /** | ||
| 149 | - * Returns all the values contained in the map. | ||
| 150 | - * | ||
| 151 | - * @return values containd in this map | ||
| 152 | - */ | ||
| 153 | - public Iterable<V> values() { | ||
| 154 | - Map<String, VersionedValue> all = dbService.getAll(tableName); | ||
| 155 | - return FluentIterable.from(all.values()) | ||
| 156 | - .transform(new Function<VersionedValue, V>() { | ||
| 157 | - | ||
| 158 | - @Override | ||
| 159 | - public V apply(VersionedValue input) { | ||
| 160 | - if (input == null) { | ||
| 161 | - return null; | ||
| 162 | - } | ||
| 163 | - return dV(input.value()); | ||
| 164 | - } | ||
| 165 | - }) | ||
| 166 | - .filter(notNull()); | ||
| 167 | - } | ||
| 168 | - | ||
| 169 | - /** | ||
| 170 | - * Gets the value in the map. | ||
| 171 | - * | ||
| 172 | - * @param key to get from the map | ||
| 173 | - * @return value associated with the key, null if not such entry | ||
| 174 | - */ | ||
| 175 | - public V get(K key) { | ||
| 176 | - VersionedValue vv = dbService.get(tableName, sK(key)); | ||
| 177 | - if (vv == null) { | ||
| 178 | - return null; | ||
| 179 | - } | ||
| 180 | - return dV(vv.value()); | ||
| 181 | - } | ||
| 182 | - | ||
| 183 | - /** | ||
| 184 | - * Replaces the value in the map if the value matches the expected. | ||
| 185 | - * | ||
| 186 | - * @param key of the entry to replace | ||
| 187 | - * @param oldVal value expected to be in the map | ||
| 188 | - * @param newVal value to be replaced with | ||
| 189 | - * @return true if successfully replaced | ||
| 190 | - */ | ||
| 191 | - public boolean replace(K key, V oldVal, V newVal) { | ||
| 192 | - return dbService.putIfValueMatches(tableName, sK(key), sV(oldVal), sV(newVal)); | ||
| 193 | - } | ||
| 194 | - | ||
| 195 | - /** | ||
| 196 | - * Puts a value int the map. | ||
| 197 | - * | ||
| 198 | - * @param key key with which the specified value is to be associated | ||
| 199 | - * @param value value to be associated with the specified key | ||
| 200 | - * @return previous value or null if not such entry | ||
| 201 | - */ | ||
| 202 | - public V put(K key, V value) { | ||
| 203 | - VersionedValue vv = dbService.put(tableName, sK(key), sV(value)); | ||
| 204 | - if (vv == null) { | ||
| 205 | - return null; | ||
| 206 | - } | ||
| 207 | - return dV(vv.value()); | ||
| 208 | - } | ||
| 209 | -} |
core/store/dist/src/main/java/org/onosproject/store/service/impl/package-info.java
deleted
100644 → 0
| 1 | -/* | ||
| 2 | - * Copyright 2015 Open Networking Laboratory | ||
| 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 | - */ | ||
| 16 | - | ||
| 17 | -/** | ||
| 18 | - * Utility services and backing mechanisms for implementations of distributed stores. | ||
| 19 | - */ | ||
| 20 | -package org.onosproject.store.service.impl; | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -18,6 +18,7 @@ package org.onosproject.store.serializers; | ... | @@ -18,6 +18,7 @@ package org.onosproject.store.serializers; |
| 18 | import com.google.common.collect.ImmutableList; | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableMap; | 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.ImmutableSet; | 20 | import com.google.common.collect.ImmutableSet; |
| 21 | + | ||
| 21 | import org.onlab.packet.ChassisId; | 22 | import org.onlab.packet.ChassisId; |
| 22 | import org.onlab.packet.Ip4Address; | 23 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onlab.packet.Ip4Prefix; | 24 | import org.onlab.packet.Ip4Prefix; |
| ... | @@ -119,15 +120,7 @@ import org.onosproject.net.resource.MplsLabel; | ... | @@ -119,15 +120,7 @@ import org.onosproject.net.resource.MplsLabel; |
| 119 | import org.onosproject.net.resource.MplsLabelResourceAllocation; | 120 | import org.onosproject.net.resource.MplsLabelResourceAllocation; |
| 120 | import org.onosproject.net.resource.MplsLabelResourceRequest; | 121 | import org.onosproject.net.resource.MplsLabelResourceRequest; |
| 121 | import org.onosproject.store.Timestamp; | 122 | import org.onosproject.store.Timestamp; |
| 122 | -import org.onosproject.store.service.BatchReadRequest; | 123 | +import org.onosproject.store.service.Versioned; |
| 123 | -import org.onosproject.store.service.BatchWriteRequest; | ||
| 124 | -import org.onosproject.store.service.ReadRequest; | ||
| 125 | -import org.onosproject.store.service.ReadResult; | ||
| 126 | -import org.onosproject.store.service.ReadStatus; | ||
| 127 | -import org.onosproject.store.service.VersionedValue; | ||
| 128 | -import org.onosproject.store.service.WriteRequest; | ||
| 129 | -import org.onosproject.store.service.WriteResult; | ||
| 130 | -import org.onosproject.store.service.WriteStatus; | ||
| 131 | 124 | ||
| 132 | import java.net.URI; | 125 | import java.net.URI; |
| 133 | import java.time.Duration; | 126 | import java.time.Duration; |
| ... | @@ -339,16 +332,7 @@ public final class KryoNamespaces { | ... | @@ -339,16 +332,7 @@ public final class KryoNamespaces { |
| 339 | .register(new MastershipTermSerializer(), MastershipTerm.class) | 332 | .register(new MastershipTermSerializer(), MastershipTerm.class) |
| 340 | .register(new HostLocationSerializer(), HostLocation.class) | 333 | .register(new HostLocationSerializer(), HostLocation.class) |
| 341 | .register(new DefaultOutboundPacketSerializer(), DefaultOutboundPacket.class) | 334 | .register(new DefaultOutboundPacketSerializer(), DefaultOutboundPacket.class) |
| 342 | - .register(ReadRequest.class) | 335 | + .register(Versioned.class) |
| 343 | - .register(WriteRequest.class) | ||
| 344 | - .register(WriteRequest.Type.class) | ||
| 345 | - .register(WriteResult.class) | ||
| 346 | - .register(ReadResult.class) | ||
| 347 | - .register(BatchReadRequest.class) | ||
| 348 | - .register(BatchWriteRequest.class) | ||
| 349 | - .register(ReadStatus.class) | ||
| 350 | - .register(WriteStatus.class) | ||
| 351 | - .register(VersionedValue.class) | ||
| 352 | .register(DefaultGroupId.class) | 336 | .register(DefaultGroupId.class) |
| 353 | .register( | 337 | .register( |
| 354 | MplsIntent.class, | 338 | MplsIntent.class, | ... | ... |
-
Please register or login to post a comment