Committed by
Gerrit Code Review
Remove deprecated code.
Change-Id: Ifd68e4ddfaade2a8dd7de43a83bf222b48b9291b
Showing
26 changed files
with
3 additions
and
2792 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
| 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.intent.impl; | ||
| 17 | - | ||
| 18 | -import com.codahale.metrics.Timer; | ||
| 19 | -import com.codahale.metrics.Timer.Context; | ||
| 20 | -import com.google.common.base.Verify; | ||
| 21 | -import com.google.common.cache.CacheBuilder; | ||
| 22 | -import com.google.common.cache.CacheLoader; | ||
| 23 | -import com.google.common.cache.LoadingCache; | ||
| 24 | -import com.google.common.collect.ImmutableSet; | ||
| 25 | -import org.apache.felix.scr.annotations.Activate; | ||
| 26 | -import org.apache.felix.scr.annotations.Component; | ||
| 27 | -import org.apache.felix.scr.annotations.Deactivate; | ||
| 28 | -import org.apache.felix.scr.annotations.Reference; | ||
| 29 | -import org.apache.felix.scr.annotations.ReferenceCardinality; | ||
| 30 | -import org.apache.felix.scr.annotations.Service; | ||
| 31 | -import org.onlab.metrics.MetricsService; | ||
| 32 | -import org.onlab.util.KryoNamespace; | ||
| 33 | -import org.onosproject.core.MetricsHelper; | ||
| 34 | -import org.onosproject.net.intent.Intent; | ||
| 35 | -import org.onosproject.net.intent.IntentEvent; | ||
| 36 | -import org.onosproject.net.intent.IntentId; | ||
| 37 | -import org.onosproject.net.intent.IntentState; | ||
| 38 | -import org.onosproject.net.intent.IntentStore; | ||
| 39 | -import org.onosproject.net.intent.IntentStoreDelegate; | ||
| 40 | -import org.onosproject.net.intent.Key; | ||
| 41 | -import org.onosproject.store.AbstractStore; | ||
| 42 | -import org.onosproject.store.serializers.KryoNamespaces; | ||
| 43 | -import org.onosproject.store.serializers.KryoSerializer; | ||
| 44 | -import org.onosproject.store.serializers.StoreSerializer; | ||
| 45 | -import org.onosproject.store.service.DatabaseAdminService; | ||
| 46 | -import org.onosproject.store.service.DatabaseService; | ||
| 47 | -import org.onosproject.store.service.impl.CMap; | ||
| 48 | -import org.slf4j.Logger; | ||
| 49 | - | ||
| 50 | -import java.util.EnumSet; | ||
| 51 | -import java.util.List; | ||
| 52 | -import java.util.Map; | ||
| 53 | -import java.util.Set; | ||
| 54 | -import java.util.concurrent.ConcurrentHashMap; | ||
| 55 | - | ||
| 56 | -import static org.onlab.metrics.MetricsUtil.startTimer; | ||
| 57 | -import static org.onlab.metrics.MetricsUtil.stopTimer; | ||
| 58 | -import static org.onosproject.net.intent.IntentState.FAILED; | ||
| 59 | -import static org.onosproject.net.intent.IntentState.INSTALLED; | ||
| 60 | -import static org.onosproject.net.intent.IntentState.INSTALL_REQ; | ||
| 61 | -import static org.onosproject.net.intent.IntentState.WITHDRAWN; | ||
| 62 | -import static org.slf4j.LoggerFactory.getLogger; | ||
| 63 | - | ||
| 64 | -//TODO Note: this store will be removed | ||
| 65 | - | ||
| 66 | -@Component(immediate = true, enabled = false) | ||
| 67 | -@Service | ||
| 68 | -public class DistributedIntentStore | ||
| 69 | - extends AbstractStore<IntentEvent, IntentStoreDelegate> | ||
| 70 | - implements IntentStore, MetricsHelper { | ||
| 71 | - | ||
| 72 | - /** Valid parking state, which can transition to INSTALLED. */ | ||
| 73 | - private static final Set<IntentState> PRE_INSTALLED = EnumSet.of(INSTALL_REQ, INSTALLED, FAILED); | ||
| 74 | - | ||
| 75 | - /** Valid parking state, which can transition to WITHDRAWN. */ | ||
| 76 | - private static final Set<IntentState> PRE_WITHDRAWN = EnumSet.of(INSTALLED, FAILED); | ||
| 77 | - | ||
| 78 | - private static final Set<IntentState> PARKING = EnumSet.of(INSTALL_REQ, INSTALLED, WITHDRAWN, FAILED); | ||
| 79 | - | ||
| 80 | - private final Logger log = getLogger(getClass()); | ||
| 81 | - | ||
| 82 | - // Assumption: IntentId will not have synonyms | ||
| 83 | - private static final String INTENTS_TABLE = "intents"; | ||
| 84 | - private CMap<IntentId, Intent> intents; | ||
| 85 | - | ||
| 86 | - private static final String STATES_TABLE = "intent-states"; | ||
| 87 | - private CMap<IntentId, IntentState> states; | ||
| 88 | - | ||
| 89 | - // TODO transient state issue remains for this impl.: ONOS-103 | ||
| 90 | - // Map to store instance local intermediate state transition | ||
| 91 | - private transient Map<IntentId, IntentState> transientStates = new ConcurrentHashMap<>(); | ||
| 92 | - | ||
| 93 | - private static final String INSTALLABLE_TABLE = "installable-intents"; | ||
| 94 | - private CMap<IntentId, List<Intent>> installable; | ||
| 95 | - | ||
| 96 | - private LoadingCache<IntentId, String> keyCache; | ||
| 97 | - | ||
| 98 | - private StoreSerializer serializer; | ||
| 99 | - | ||
| 100 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 101 | - protected DatabaseAdminService dbAdminService; | ||
| 102 | - | ||
| 103 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 104 | - protected DatabaseService dbService; | ||
| 105 | - | ||
| 106 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 107 | - protected MetricsService metricsService; | ||
| 108 | - | ||
| 109 | - // TODO make this configurable | ||
| 110 | - private boolean onlyLogTransitionError = true; | ||
| 111 | - | ||
| 112 | - private Timer getInstallableIntentsTimer; | ||
| 113 | - private Timer getIntentCountTimer; | ||
| 114 | - private Timer getIntentsTimer; | ||
| 115 | - private Timer getIntentTimer; | ||
| 116 | - private Timer getIntentStateTimer; | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - private Timer createResponseTimer(String methodName) { | ||
| 120 | - return createTimer("IntentStore", methodName, "responseTime"); | ||
| 121 | - } | ||
| 122 | - | ||
| 123 | - @Activate | ||
| 124 | - public void activate() { | ||
| 125 | - getInstallableIntentsTimer = createResponseTimer("getInstallableIntents"); | ||
| 126 | - getIntentCountTimer = createResponseTimer("getIntentCount"); | ||
| 127 | - getIntentsTimer = createResponseTimer("getIntents"); | ||
| 128 | - getIntentTimer = createResponseTimer("getIntent"); | ||
| 129 | - getIntentStateTimer = createResponseTimer("getIntentState"); | ||
| 130 | - | ||
| 131 | - // We need a way to add serializer for intents which has been plugged-in. | ||
| 132 | - // As a short term workaround, relax Kryo config to | ||
| 133 | - // registrationRequired=false | ||
| 134 | - serializer = new KryoSerializer() { | ||
| 135 | - | ||
| 136 | - @Override | ||
| 137 | - protected void setupKryoPool() { | ||
| 138 | - serializerPool = KryoNamespace.newBuilder() | ||
| 139 | - .setRegistrationRequired(false) | ||
| 140 | - .register(KryoNamespaces.API) | ||
| 141 | - .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) | ||
| 142 | - .build(); | ||
| 143 | - } | ||
| 144 | - }; | ||
| 145 | - | ||
| 146 | - keyCache = CacheBuilder.newBuilder() | ||
| 147 | - .softValues() | ||
| 148 | - .build(new CacheLoader<IntentId, String>() { | ||
| 149 | - | ||
| 150 | - @Override | ||
| 151 | - public String load(IntentId key) { | ||
| 152 | - return key.toString(); | ||
| 153 | - } | ||
| 154 | - }); | ||
| 155 | - | ||
| 156 | - intents = new IntentIdMap<>(dbAdminService, dbService, INTENTS_TABLE, serializer); | ||
| 157 | - | ||
| 158 | - states = new IntentIdMap<>(dbAdminService, dbService, STATES_TABLE, serializer); | ||
| 159 | - | ||
| 160 | - transientStates.clear(); | ||
| 161 | - | ||
| 162 | - installable = new IntentIdMap<>(dbAdminService, dbService, INSTALLABLE_TABLE, serializer); | ||
| 163 | - | ||
| 164 | - log.info("Started"); | ||
| 165 | - } | ||
| 166 | - | ||
| 167 | - @Deactivate | ||
| 168 | - public void deactivate() { | ||
| 169 | - log.info("Stopped"); | ||
| 170 | - } | ||
| 171 | - | ||
| 172 | - @Override | ||
| 173 | - public MetricsService metricsService() { | ||
| 174 | - return metricsService; | ||
| 175 | - } | ||
| 176 | - | ||
| 177 | - @Override | ||
| 178 | - public long getIntentCount() { | ||
| 179 | - Context timer = startTimer(getIntentCountTimer); | ||
| 180 | - try { | ||
| 181 | - return intents.size(); | ||
| 182 | - } finally { | ||
| 183 | - stopTimer(timer); | ||
| 184 | - } | ||
| 185 | - } | ||
| 186 | - | ||
| 187 | - @Override | ||
| 188 | - public Iterable<Intent> getIntents() { | ||
| 189 | - Context timer = startTimer(getIntentsTimer); | ||
| 190 | - try { | ||
| 191 | - return ImmutableSet.copyOf(intents.values()); | ||
| 192 | - } finally { | ||
| 193 | - stopTimer(timer); | ||
| 194 | - } | ||
| 195 | - } | ||
| 196 | - | ||
| 197 | - @Override | ||
| 198 | - public Intent getIntent(Key intentKey) { | ||
| 199 | - return null; | ||
| 200 | - } | ||
| 201 | - | ||
| 202 | - public Intent getIntent(IntentId intentId) { | ||
| 203 | - Context timer = startTimer(getIntentTimer); | ||
| 204 | - try { | ||
| 205 | - return intents.get(intentId); | ||
| 206 | - } finally { | ||
| 207 | - stopTimer(timer); | ||
| 208 | - } | ||
| 209 | - } | ||
| 210 | - | ||
| 211 | - @Override | ||
| 212 | - public IntentState getIntentState(Key key) { | ||
| 213 | - // TODO: either implement this or remove the class | ||
| 214 | - return IntentState.FAILED; | ||
| 215 | - /* | ||
| 216 | - Context timer = startTimer(getIntentStateTimer); | ||
| 217 | - try { | ||
| 218 | - final IntentState localState = transientStates.get(id); | ||
| 219 | - if (localState != null) { | ||
| 220 | - return localState; | ||
| 221 | - } | ||
| 222 | - return states.get(id); | ||
| 223 | - } finally { | ||
| 224 | - stopTimer(timer); | ||
| 225 | - } | ||
| 226 | - */ | ||
| 227 | - } | ||
| 228 | - | ||
| 229 | - private void verify(boolean expression, String errorMessageTemplate, Object... errorMessageArgs) { | ||
| 230 | - if (onlyLogTransitionError) { | ||
| 231 | - if (!expression) { | ||
| 232 | - log.error(errorMessageTemplate.replace("%s", "{}"), errorMessageArgs); | ||
| 233 | - } | ||
| 234 | - } else { | ||
| 235 | - Verify.verify(expression, errorMessageTemplate, errorMessageArgs); | ||
| 236 | - } | ||
| 237 | - } | ||
| 238 | - | ||
| 239 | - @Override | ||
| 240 | - public List<Intent> getInstallableIntents(Key intentKey) { | ||
| 241 | - // TODO: implement this or delete class | ||
| 242 | - return null; | ||
| 243 | - /* | ||
| 244 | - Context timer = startTimer(getInstallableIntentsTimer); | ||
| 245 | - try { | ||
| 246 | - return installable.get(intentId); | ||
| 247 | - } finally { | ||
| 248 | - stopTimer(timer); | ||
| 249 | - } | ||
| 250 | - */ | ||
| 251 | - } | ||
| 252 | - | ||
| 253 | - protected String strIntentId(IntentId key) { | ||
| 254 | - return keyCache.getUnchecked(key); | ||
| 255 | - } | ||
| 256 | - | ||
| 257 | - /** | ||
| 258 | - * Distributed Map from IntentId to some value. | ||
| 259 | - * | ||
| 260 | - * @param <V> Map value type | ||
| 261 | - */ | ||
| 262 | - final class IntentIdMap<V> extends CMap<IntentId, V> { | ||
| 263 | - | ||
| 264 | - /** | ||
| 265 | - * Creates a IntentIdMap instance. | ||
| 266 | - * | ||
| 267 | - * @param dbAdminService DatabaseAdminService to use for this instance | ||
| 268 | - * @param dbService DatabaseService to use for this instance | ||
| 269 | - * @param tableName table which this Map corresponds to | ||
| 270 | - * @param serializer Value serializer | ||
| 271 | - */ | ||
| 272 | - public IntentIdMap(DatabaseAdminService dbAdminService, | ||
| 273 | - DatabaseService dbService, | ||
| 274 | - String tableName, | ||
| 275 | - StoreSerializer serializer) { | ||
| 276 | - super(dbAdminService, dbService, tableName, serializer); | ||
| 277 | - } | ||
| 278 | - | ||
| 279 | - @Override | ||
| 280 | - protected String sK(IntentId key) { | ||
| 281 | - return strIntentId(key); | ||
| 282 | - } | ||
| 283 | - } | ||
| 284 | - | ||
| 285 | - /*@Override | ||
| 286 | - public List<Operation> batchWrite(BatchWrite batch) { | ||
| 287 | - if (batch.isEmpty()) { | ||
| 288 | - return Collections.emptyList(); | ||
| 289 | - } | ||
| 290 | - | ||
| 291 | - List<Operation> failed = new ArrayList<>(); | ||
| 292 | - final Builder builder = BatchWriteRequest.newBuilder(); | ||
| 293 | - List<IntentEvent> events = Lists.newArrayList(); | ||
| 294 | - | ||
| 295 | - final Set<IntentId> transitionedToParking = new HashSet<>(); | ||
| 296 | - | ||
| 297 | - for (Operation op : batch.operations()) { | ||
| 298 | - switch (op.type()) { | ||
| 299 | - case CREATE_INTENT: | ||
| 300 | - checkArgument(op.args().size() == 1, | ||
| 301 | - "CREATE_INTENT takes 1 argument. %s", op); | ||
| 302 | - Intent intent = op.arg(0); | ||
| 303 | - builder.putIfAbsent(INTENTS_TABLE, strIntentId(intent.id()), serializer.encode(intent)); | ||
| 304 | - builder.putIfAbsent(STATES_TABLE, strIntentId(intent.id()), serializer.encode(INSTALL_REQ)); | ||
| 305 | - events.add(IntentEvent.getEvent(INSTALL_REQ, intent)); | ||
| 306 | - break; | ||
| 307 | - | ||
| 308 | - case REMOVE_INTENT: | ||
| 309 | - checkArgument(op.args().size() == 1, | ||
| 310 | - "REMOVE_INTENT takes 1 argument. %s", op); | ||
| 311 | - IntentId intentId = (IntentId) op.arg(0); | ||
| 312 | - builder.remove(INTENTS_TABLE, strIntentId(intentId)); | ||
| 313 | - builder.remove(STATES_TABLE, strIntentId(intentId)); | ||
| 314 | - builder.remove(INSTALLABLE_TABLE, strIntentId(intentId)); | ||
| 315 | - break; | ||
| 316 | - | ||
| 317 | - case SET_STATE: | ||
| 318 | - checkArgument(op.args().size() == 2, | ||
| 319 | - "SET_STATE takes 2 arguments. %s", op); | ||
| 320 | - intent = op.arg(0); | ||
| 321 | - IntentState newState = op.arg(1); | ||
| 322 | - builder.put(STATES_TABLE, strIntentId(intent.id()), serializer.encode(newState)); | ||
| 323 | - if (PARKING.contains(newState)) { | ||
| 324 | - transitionedToParking.add(intent.id()); | ||
| 325 | - events.add(IntentEvent.getEvent(newState, intent)); | ||
| 326 | - } else { | ||
| 327 | - transitionedToParking.remove(intent.id()); | ||
| 328 | - } | ||
| 329 | - break; | ||
| 330 | - | ||
| 331 | - case SET_INSTALLABLE: | ||
| 332 | - checkArgument(op.args().size() == 2, | ||
| 333 | - "SET_INSTALLABLE takes 2 arguments. %s", op); | ||
| 334 | - intentId = op.arg(0); | ||
| 335 | - List<Intent> installableIntents = op.arg(1); | ||
| 336 | - builder.put(INSTALLABLE_TABLE, strIntentId(intentId), serializer.encode(installableIntents)); | ||
| 337 | - break; | ||
| 338 | - | ||
| 339 | - case REMOVE_INSTALLED: | ||
| 340 | - checkArgument(op.args().size() == 1, | ||
| 341 | - "REMOVE_INSTALLED takes 1 argument. %s", op); | ||
| 342 | - intentId = op.arg(0); | ||
| 343 | - builder.remove(INSTALLABLE_TABLE, strIntentId(intentId)); | ||
| 344 | - break; | ||
| 345 | - | ||
| 346 | - default: | ||
| 347 | - log.warn("Unknown Operation encountered: {}", op); | ||
| 348 | - failed.add(op); | ||
| 349 | - break; | ||
| 350 | - } | ||
| 351 | - } | ||
| 352 | - | ||
| 353 | - BatchWriteResult batchWriteResult = dbService.batchWrite(builder.build()); | ||
| 354 | - if (batchWriteResult.isSuccessful()) { | ||
| 355 | - // no-failure (except for invalid input) | ||
| 356 | - transitionedToParking.forEach((intentId) -> transientStates.remove(intentId)); | ||
| 357 | - notifyDelegate(events); | ||
| 358 | - return failed; | ||
| 359 | - } else { | ||
| 360 | - // everything failed | ||
| 361 | - return batch.operations(); | ||
| 362 | - } | ||
| 363 | - }*/ | ||
| 364 | -} |
| 1 | -/* | ||
| 2 | - * Copyright 2014-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 | -package org.onosproject.store.resource.impl; | ||
| 17 | - | ||
| 18 | -import java.util.ArrayList; | ||
| 19 | -import java.util.Collection; | ||
| 20 | -import java.util.HashMap; | ||
| 21 | -import java.util.HashSet; | ||
| 22 | -import java.util.List; | ||
| 23 | -import java.util.Map; | ||
| 24 | -import java.util.Set; | ||
| 25 | - | ||
| 26 | -import org.apache.felix.scr.annotations.Activate; | ||
| 27 | -import org.apache.felix.scr.annotations.Component; | ||
| 28 | -import org.apache.felix.scr.annotations.Deactivate; | ||
| 29 | -import org.apache.felix.scr.annotations.Reference; | ||
| 30 | -import org.apache.felix.scr.annotations.ReferenceCardinality; | ||
| 31 | -import org.apache.felix.scr.annotations.Service; | ||
| 32 | -import org.onlab.util.PositionalParameterStringFormatter; | ||
| 33 | -import org.onosproject.net.AnnotationKeys; | ||
| 34 | -import org.onosproject.net.Link; | ||
| 35 | -import org.onosproject.net.LinkKey; | ||
| 36 | -import org.onosproject.net.intent.IntentId; | ||
| 37 | -import org.onosproject.net.link.LinkService; | ||
| 38 | -import org.onosproject.net.resource.Bandwidth; | ||
| 39 | -import org.onosproject.net.resource.BandwidthResourceAllocation; | ||
| 40 | -import org.onosproject.net.resource.Lambda; | ||
| 41 | -import org.onosproject.net.resource.LambdaResourceAllocation; | ||
| 42 | -import org.onosproject.net.resource.LinkResourceAllocations; | ||
| 43 | -import org.onosproject.net.resource.LinkResourceEvent; | ||
| 44 | -import org.onosproject.net.resource.LinkResourceStore; | ||
| 45 | -import org.onosproject.net.resource.MplsLabel; | ||
| 46 | -import org.onosproject.net.resource.MplsLabelResourceAllocation; | ||
| 47 | -import org.onosproject.net.resource.ResourceAllocation; | ||
| 48 | -import org.onosproject.net.resource.ResourceAllocationException; | ||
| 49 | -import org.onosproject.net.resource.ResourceType; | ||
| 50 | -import org.onosproject.store.serializers.KryoSerializer; | ||
| 51 | -import org.onosproject.store.serializers.StoreSerializer; | ||
| 52 | -import org.onosproject.store.service.BatchWriteRequest; | ||
| 53 | -import org.onosproject.store.service.BatchWriteRequest.Builder; | ||
| 54 | -import org.onosproject.store.service.BatchWriteResult; | ||
| 55 | -import org.onosproject.store.service.DatabaseAdminService; | ||
| 56 | -import org.onosproject.store.service.DatabaseException; | ||
| 57 | -import org.onosproject.store.service.DatabaseService; | ||
| 58 | -import org.onosproject.store.service.VersionedValue; | ||
| 59 | -import org.onosproject.store.service.WriteRequest; | ||
| 60 | -import org.onosproject.store.service.WriteResult; | ||
| 61 | -import org.slf4j.Logger; | ||
| 62 | - | ||
| 63 | -import com.google.common.base.Function; | ||
| 64 | -import com.google.common.collect.FluentIterable; | ||
| 65 | -import com.google.common.collect.ImmutableList; | ||
| 66 | -import com.google.common.collect.ImmutableSet; | ||
| 67 | -import com.google.common.collect.Sets; | ||
| 68 | - | ||
| 69 | -import static com.google.common.base.Preconditions.checkArgument; | ||
| 70 | -import static com.google.common.base.Preconditions.checkNotNull; | ||
| 71 | -import static com.google.common.base.Preconditions.checkState; | ||
| 72 | -import static com.google.common.base.Predicates.notNull; | ||
| 73 | -import static org.onlab.util.HexString.toHexString; | ||
| 74 | -import static org.slf4j.LoggerFactory.getLogger; | ||
| 75 | - | ||
| 76 | -/** | ||
| 77 | - * Manages link resources using database service. | ||
| 78 | - */ | ||
| 79 | -@Component(immediate = true, enabled = false) | ||
| 80 | -@Service | ||
| 81 | -public class DistributedLinkResourceStore implements LinkResourceStore { | ||
| 82 | - | ||
| 83 | - private final Logger log = getLogger(getClass()); | ||
| 84 | - | ||
| 85 | - private static final Bandwidth DEFAULT_BANDWIDTH = Bandwidth.mbps(1_000); | ||
| 86 | - | ||
| 87 | - // table to store current allocations | ||
| 88 | - /** LinkKey -> List<LinkResourceAllocations>. */ | ||
| 89 | - private static final String LINK_RESOURCE_ALLOCATIONS = "LinkResourceAllocations"; | ||
| 90 | - | ||
| 91 | - /** IntentId -> LinkResourceAllocations. */ | ||
| 92 | - private static final String INTENT_ALLOCATIONS = "IntentAllocations"; | ||
| 93 | - | ||
| 94 | - private static final Bandwidth EMPTY_BW = Bandwidth.bps(0); | ||
| 95 | - | ||
| 96 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 97 | - protected DatabaseAdminService databaseAdminService; | ||
| 98 | - | ||
| 99 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 100 | - protected DatabaseService databaseService; | ||
| 101 | - | ||
| 102 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 103 | - protected LinkService linkService; | ||
| 104 | - | ||
| 105 | - // Link annotation key name to use as bandwidth in Mbps | ||
| 106 | - private String bandwidthAnnotation = AnnotationKeys.BANDWIDTH; | ||
| 107 | - // Link annotation key name to use as max lambda | ||
| 108 | - private String wavesAnnotation = AnnotationKeys.OPTICAL_WAVES; | ||
| 109 | - | ||
| 110 | - // Max MPLS labels: 2^20 – 1 | ||
| 111 | - private int maxMplsLabel = 0xFFFFF; | ||
| 112 | - private StoreSerializer serializer; | ||
| 113 | - | ||
| 114 | - void createTable(String tableName) { | ||
| 115 | - boolean tableReady = false; | ||
| 116 | - do { | ||
| 117 | - try { | ||
| 118 | - if (!databaseAdminService.listTables().contains(tableName)) { | ||
| 119 | - databaseAdminService.createTable(tableName); | ||
| 120 | - } | ||
| 121 | - tableReady = true; | ||
| 122 | - } catch (DatabaseException e) { | ||
| 123 | - log.debug("Failed creating table, retrying", e); | ||
| 124 | - try { | ||
| 125 | - Thread.sleep(200); | ||
| 126 | - } catch (InterruptedException e1) { | ||
| 127 | - throw new DatabaseException(e1); | ||
| 128 | - } | ||
| 129 | - } | ||
| 130 | - } while (!tableReady); | ||
| 131 | - } | ||
| 132 | - | ||
| 133 | - @Activate | ||
| 134 | - public void activate() { | ||
| 135 | - | ||
| 136 | - serializer = new KryoSerializer(); | ||
| 137 | - | ||
| 138 | - createTable(LINK_RESOURCE_ALLOCATIONS); | ||
| 139 | - createTable(INTENT_ALLOCATIONS); | ||
| 140 | - | ||
| 141 | - log.info("Started"); | ||
| 142 | - } | ||
| 143 | - | ||
| 144 | - @Deactivate | ||
| 145 | - public void deactivate() { | ||
| 146 | - log.info("Stopped"); | ||
| 147 | - } | ||
| 148 | - | ||
| 149 | - private Set<? extends ResourceAllocation> getResourceCapacity(ResourceType type, Link link) { | ||
| 150 | - if (type == ResourceType.BANDWIDTH) { | ||
| 151 | - return ImmutableSet.of(getBandwidthResourceCapacity(link)); | ||
| 152 | - } | ||
| 153 | - if (type == ResourceType.LAMBDA) { | ||
| 154 | - return getLambdaResourceCapacity(link); | ||
| 155 | - } | ||
| 156 | - if (type == ResourceType.MPLS_LABEL) { | ||
| 157 | - return getMplsResourceCapacity(); | ||
| 158 | - } | ||
| 159 | - return null; | ||
| 160 | - } | ||
| 161 | - | ||
| 162 | - private Set<LambdaResourceAllocation> getLambdaResourceCapacity(Link link) { | ||
| 163 | - Set<LambdaResourceAllocation> allocations = new HashSet<>(); | ||
| 164 | - try { | ||
| 165 | - final int waves = Integer.parseInt(link.annotations().value(wavesAnnotation)); | ||
| 166 | - for (int i = 1; i <= waves; i++) { | ||
| 167 | - allocations.add(new LambdaResourceAllocation(Lambda.valueOf(i))); | ||
| 168 | - } | ||
| 169 | - } catch (NumberFormatException e) { | ||
| 170 | - log.debug("No {} annotation on link %s", wavesAnnotation, link); | ||
| 171 | - } | ||
| 172 | - return allocations; | ||
| 173 | - } | ||
| 174 | - | ||
| 175 | - private BandwidthResourceAllocation getBandwidthResourceCapacity(Link link) { | ||
| 176 | - | ||
| 177 | - // if Link annotation exist, use them | ||
| 178 | - // if all fails, use DEFAULT_BANDWIDTH | ||
| 179 | - | ||
| 180 | - Bandwidth bandwidth = null; | ||
| 181 | - String strBw = link.annotations().value(bandwidthAnnotation); | ||
| 182 | - if (strBw != null) { | ||
| 183 | - try { | ||
| 184 | - bandwidth = Bandwidth.mbps(Double.parseDouble(strBw)); | ||
| 185 | - } catch (NumberFormatException e) { | ||
| 186 | - // do nothings | ||
| 187 | - bandwidth = null; | ||
| 188 | - } | ||
| 189 | - } | ||
| 190 | - | ||
| 191 | - if (bandwidth == null) { | ||
| 192 | - // fall back, use fixed default | ||
| 193 | - bandwidth = DEFAULT_BANDWIDTH; | ||
| 194 | - } | ||
| 195 | - return new BandwidthResourceAllocation(bandwidth); | ||
| 196 | - } | ||
| 197 | - | ||
| 198 | - private Set<MplsLabelResourceAllocation> getMplsResourceCapacity() { | ||
| 199 | - Set<MplsLabelResourceAllocation> allocations = new HashSet<>(); | ||
| 200 | - //Ignoring reserved labels of 0 through 15 | ||
| 201 | - for (int i = 16; i <= maxMplsLabel; i++) { | ||
| 202 | - allocations.add(new MplsLabelResourceAllocation(MplsLabel | ||
| 203 | - .valueOf(i))); | ||
| 204 | - | ||
| 205 | - } | ||
| 206 | - return allocations; | ||
| 207 | - } | ||
| 208 | - | ||
| 209 | - private Map<ResourceType, Set<? extends ResourceAllocation>> getResourceCapacity(Link link) { | ||
| 210 | - Map<ResourceType, Set<? extends ResourceAllocation>> caps = new HashMap<>(); | ||
| 211 | - for (ResourceType type : ResourceType.values()) { | ||
| 212 | - Set<? extends ResourceAllocation> cap = getResourceCapacity(type, link); | ||
| 213 | - if (cap != null) { | ||
| 214 | - caps.put(type, cap); | ||
| 215 | - } | ||
| 216 | - } | ||
| 217 | - return caps; | ||
| 218 | - } | ||
| 219 | - | ||
| 220 | - @Override | ||
| 221 | - public Set<ResourceAllocation> getFreeResources(Link link) { | ||
| 222 | - Map<ResourceType, Set<? extends ResourceAllocation>> freeResources = getFreeResourcesEx(link); | ||
| 223 | - Set<ResourceAllocation> allFree = new HashSet<>(); | ||
| 224 | - for (Set<? extends ResourceAllocation> r:freeResources.values()) { | ||
| 225 | - allFree.addAll(r); | ||
| 226 | - } | ||
| 227 | - return allFree; | ||
| 228 | - } | ||
| 229 | - | ||
| 230 | - private Map<ResourceType, Set<? extends ResourceAllocation>> getFreeResourcesEx(Link link) { | ||
| 231 | - // returns capacity - allocated | ||
| 232 | - | ||
| 233 | - checkNotNull(link); | ||
| 234 | - Map<ResourceType, Set<? extends ResourceAllocation>> free = new HashMap<>(); | ||
| 235 | - final Map<ResourceType, Set<? extends ResourceAllocation>> caps = getResourceCapacity(link); | ||
| 236 | - final Iterable<LinkResourceAllocations> allocations = getAllocations(link); | ||
| 237 | - | ||
| 238 | - for (ResourceType type : ResourceType.values()) { | ||
| 239 | - // there should be class/category of resources | ||
| 240 | - switch (type) { | ||
| 241 | - case BANDWIDTH: | ||
| 242 | - { | ||
| 243 | - Set<? extends ResourceAllocation> bw = caps.get(ResourceType.BANDWIDTH); | ||
| 244 | - if (bw == null || bw.isEmpty()) { | ||
| 245 | - bw = Sets.newHashSet(new BandwidthResourceAllocation(EMPTY_BW)); | ||
| 246 | - } | ||
| 247 | - | ||
| 248 | - BandwidthResourceAllocation cap = (BandwidthResourceAllocation) bw.iterator().next(); | ||
| 249 | - double freeBw = cap.bandwidth().toDouble(); | ||
| 250 | - | ||
| 251 | - // enumerate current allocations, subtracting resources | ||
| 252 | - for (LinkResourceAllocations alloc : allocations) { | ||
| 253 | - Set<ResourceAllocation> types = alloc.getResourceAllocation(link); | ||
| 254 | - for (ResourceAllocation a : types) { | ||
| 255 | - if (a instanceof BandwidthResourceAllocation) { | ||
| 256 | - BandwidthResourceAllocation bwA = (BandwidthResourceAllocation) a; | ||
| 257 | - freeBw -= bwA.bandwidth().toDouble(); | ||
| 258 | - } | ||
| 259 | - } | ||
| 260 | - } | ||
| 261 | - | ||
| 262 | - free.put(type, Sets.newHashSet(new BandwidthResourceAllocation(Bandwidth.bps(freeBw)))); | ||
| 263 | - break; | ||
| 264 | - } | ||
| 265 | - | ||
| 266 | - case LAMBDA: | ||
| 267 | - { | ||
| 268 | - Set<? extends ResourceAllocation> lmd = caps.get(type); | ||
| 269 | - if (lmd == null || lmd.isEmpty()) { | ||
| 270 | - // nothing left | ||
| 271 | - break; | ||
| 272 | - } | ||
| 273 | - Set<LambdaResourceAllocation> freeL = new HashSet<>(); | ||
| 274 | - for (ResourceAllocation r : lmd) { | ||
| 275 | - if (r instanceof LambdaResourceAllocation) { | ||
| 276 | - freeL.add((LambdaResourceAllocation) r); | ||
| 277 | - } | ||
| 278 | - } | ||
| 279 | - | ||
| 280 | - // enumerate current allocations, removing resources | ||
| 281 | - for (LinkResourceAllocations alloc : allocations) { | ||
| 282 | - Set<ResourceAllocation> types = alloc.getResourceAllocation(link); | ||
| 283 | - for (ResourceAllocation a : types) { | ||
| 284 | - if (a instanceof LambdaResourceAllocation) { | ||
| 285 | - freeL.remove(a); | ||
| 286 | - } | ||
| 287 | - } | ||
| 288 | - } | ||
| 289 | - | ||
| 290 | - free.put(type, freeL); | ||
| 291 | - break; | ||
| 292 | - } | ||
| 293 | - case MPLS_LABEL: | ||
| 294 | - { | ||
| 295 | - Set<? extends ResourceAllocation> mpls = caps.get(type); | ||
| 296 | - if (mpls == null || mpls.isEmpty()) { | ||
| 297 | - // nothing left | ||
| 298 | - break; | ||
| 299 | - } | ||
| 300 | - Set<MplsLabelResourceAllocation> freeLabel = new HashSet<>(); | ||
| 301 | - for (ResourceAllocation r : mpls) { | ||
| 302 | - if (r instanceof MplsLabelResourceAllocation) { | ||
| 303 | - freeLabel.add((MplsLabelResourceAllocation) r); | ||
| 304 | - } | ||
| 305 | - } | ||
| 306 | - | ||
| 307 | - // enumerate current allocations, removing resources | ||
| 308 | - for (LinkResourceAllocations alloc : allocations) { | ||
| 309 | - Set<ResourceAllocation> types = alloc | ||
| 310 | - .getResourceAllocation(link); | ||
| 311 | - for (ResourceAllocation a : types) { | ||
| 312 | - if (a instanceof MplsLabelResourceAllocation) { | ||
| 313 | - freeLabel.remove(a); | ||
| 314 | - } | ||
| 315 | - } | ||
| 316 | - } | ||
| 317 | - | ||
| 318 | - free.put(type, freeLabel); | ||
| 319 | - break; | ||
| 320 | - } | ||
| 321 | - | ||
| 322 | - default: | ||
| 323 | - break; | ||
| 324 | - } | ||
| 325 | - } | ||
| 326 | - return free; | ||
| 327 | - } | ||
| 328 | - | ||
| 329 | - private LinkResourceAllocations getIntentAllocations(IntentId id) { | ||
| 330 | - VersionedValue vv | ||
| 331 | - = databaseService.get(INTENT_ALLOCATIONS, toIntentDbKey(checkNotNull(id))); | ||
| 332 | - if (vv == null || vv.value() == null) { | ||
| 333 | - return null; | ||
| 334 | - } | ||
| 335 | - return decodeIntentAllocations(vv.value()); | ||
| 336 | - } | ||
| 337 | - | ||
| 338 | - private Builder putIntentAllocations(Builder ctx, | ||
| 339 | - IntentId id, | ||
| 340 | - LinkResourceAllocations alloc) { | ||
| 341 | - return ctx.put(INTENT_ALLOCATIONS, | ||
| 342 | - toIntentDbKey(id), | ||
| 343 | - encodeIntentAllocations(alloc)); | ||
| 344 | - } | ||
| 345 | - | ||
| 346 | - @Override | ||
| 347 | - public void allocateResources(LinkResourceAllocations allocations) { | ||
| 348 | - checkNotNull(allocations); | ||
| 349 | - | ||
| 350 | - Builder tx = BatchWriteRequest.newBuilder(); | ||
| 351 | - | ||
| 352 | - // TODO: Should IntentId -> Allocation be updated conditionally? | ||
| 353 | - putIntentAllocations(tx, allocations.intendId(), allocations); | ||
| 354 | - | ||
| 355 | - for (Link link : allocations.links()) { | ||
| 356 | - allocateLinkResource(tx, link, allocations); | ||
| 357 | - } | ||
| 358 | - | ||
| 359 | - BatchWriteRequest batch = tx.build(); | ||
| 360 | -// log.info("Intent: {}", databaseService.getAll(INTENT_ALLOCATIONS)); | ||
| 361 | -// log.info("Link: {}", | ||
| 362 | - // databaseService.getAll(LINK_RESOURCE_ALLOCATIONS)); | ||
| 363 | - | ||
| 364 | - BatchWriteResult result = databaseService.batchWrite(batch); | ||
| 365 | - if (!result.isSuccessful()) { | ||
| 366 | - log.error("Allocation Failed."); | ||
| 367 | - if (log.isDebugEnabled()) { | ||
| 368 | - logFailureDetail(batch, result); | ||
| 369 | - } | ||
| 370 | - checkState(result.isSuccessful(), "Allocation failed"); | ||
| 371 | - } | ||
| 372 | - } | ||
| 373 | - | ||
| 374 | - private void logFailureDetail(BatchWriteRequest batch, | ||
| 375 | - BatchWriteResult result) { | ||
| 376 | - for (int i = 0; i < batch.batchSize(); ++i) { | ||
| 377 | - final WriteRequest req = batch.getAsList().get(i); | ||
| 378 | - final WriteResult fail = result.getAsList().get(i); | ||
| 379 | - switch (fail.status()) { | ||
| 380 | - case ABORTED: | ||
| 381 | - log.debug("ABORTED: {}@{}", req.key(), req.tableName()); | ||
| 382 | - break; | ||
| 383 | - case PRECONDITION_VIOLATION: | ||
| 384 | - switch (req.type()) { | ||
| 385 | - case PUT_IF_ABSENT: | ||
| 386 | - log.debug("{}: {}@{} : {}", req.type(), | ||
| 387 | - req.key(), req.tableName(), fail.previousValue()); | ||
| 388 | - break; | ||
| 389 | - case PUT_IF_VALUE: | ||
| 390 | - case REMOVE_IF_VALUE: | ||
| 391 | - log.debug("{}: {}@{} : was {}, expected {}", req.type(), | ||
| 392 | - req.key(), req.tableName(), | ||
| 393 | - fail.previousValue(), | ||
| 394 | - toHexString(req.oldValue())); | ||
| 395 | - break; | ||
| 396 | - case PUT_IF_VERSION: | ||
| 397 | - case REMOVE_IF_VERSION: | ||
| 398 | - log.debug("{}: {}@{} : was {}, expected {}", req.type(), | ||
| 399 | - req.key(), req.tableName(), | ||
| 400 | - fail.previousValue().version(), | ||
| 401 | - req.previousVersion()); | ||
| 402 | - break; | ||
| 403 | - default: | ||
| 404 | - log.error("Should never reach here."); | ||
| 405 | - break; | ||
| 406 | - } | ||
| 407 | - break; | ||
| 408 | - default: | ||
| 409 | - log.error("Should never reach here."); | ||
| 410 | - break; | ||
| 411 | - } | ||
| 412 | - } | ||
| 413 | - } | ||
| 414 | - | ||
| 415 | - private Builder allocateLinkResource(Builder builder, Link link, | ||
| 416 | - LinkResourceAllocations allocations) { | ||
| 417 | - | ||
| 418 | - // requested resources | ||
| 419 | - Set<ResourceAllocation> reqs = allocations.getResourceAllocation(link); | ||
| 420 | - | ||
| 421 | - Map<ResourceType, Set<? extends ResourceAllocation>> available = getFreeResourcesEx(link); | ||
| 422 | - for (ResourceAllocation req : reqs) { | ||
| 423 | - Set<? extends ResourceAllocation> avail = available.get(req.type()); | ||
| 424 | - if (req instanceof BandwidthResourceAllocation) { | ||
| 425 | - // check if allocation should be accepted | ||
| 426 | - if (avail.isEmpty()) { | ||
| 427 | - checkState(!avail.isEmpty(), | ||
| 428 | - "There's no Bandwidth resource on %s?", | ||
| 429 | - link); | ||
| 430 | - } | ||
| 431 | - BandwidthResourceAllocation bw = (BandwidthResourceAllocation) avail.iterator().next(); | ||
| 432 | - double bwLeft = bw.bandwidth().toDouble(); | ||
| 433 | - bwLeft -= ((BandwidthResourceAllocation) req).bandwidth().toDouble(); | ||
| 434 | - BandwidthResourceAllocation bwReq = ((BandwidthResourceAllocation) req); | ||
| 435 | - if (bwLeft < 0) { | ||
| 436 | - throw new ResourceAllocationException( | ||
| 437 | - PositionalParameterStringFormatter.format( | ||
| 438 | - "Unable to allocate bandwidth for link {} " | ||
| 439 | - + " requested amount is {} current allocation is {}", | ||
| 440 | - link, | ||
| 441 | - bwReq.bandwidth().toDouble(), | ||
| 442 | - bw)); | ||
| 443 | - } | ||
| 444 | - } else if (req instanceof LambdaResourceAllocation) { | ||
| 445 | - final LambdaResourceAllocation lambdaAllocation = (LambdaResourceAllocation) req; | ||
| 446 | - // check if allocation should be accepted | ||
| 447 | - if (!avail.contains(req)) { | ||
| 448 | - // requested lambda was not available | ||
| 449 | - throw new ResourceAllocationException( | ||
| 450 | - PositionalParameterStringFormatter.format( | ||
| 451 | - "Unable to allocate lambda for link {} lambda is {}", | ||
| 452 | - link, | ||
| 453 | - lambdaAllocation.lambda().toInt())); | ||
| 454 | - } | ||
| 455 | - } else if (req instanceof MplsLabelResourceAllocation) { | ||
| 456 | - | ||
| 457 | - final MplsLabelResourceAllocation mplsAllocation = (MplsLabelResourceAllocation) req; | ||
| 458 | - // check if allocation should be accepted | ||
| 459 | - if (!avail.contains(req)) { | ||
| 460 | - // requested mpls label was not available | ||
| 461 | - throw new ResourceAllocationException( | ||
| 462 | - PositionalParameterStringFormatter | ||
| 463 | - .format("Unable to allocate MPLS label for " | ||
| 464 | - + "link {} MPLS label is {}", | ||
| 465 | - link, | ||
| 466 | - mplsAllocation | ||
| 467 | - .mplsLabel() | ||
| 468 | - .toString())); | ||
| 469 | - } | ||
| 470 | - } | ||
| 471 | - } | ||
| 472 | - // all requests allocatable => add allocation | ||
| 473 | - final List<LinkResourceAllocations> before = getAllocations(link); | ||
| 474 | - List<LinkResourceAllocations> after = new ArrayList<>(before.size()); | ||
| 475 | - after.addAll(before); | ||
| 476 | - after.add(allocations); | ||
| 477 | - replaceLinkAllocations(builder, LinkKey.linkKey(link), before, after); | ||
| 478 | - return builder; | ||
| 479 | - } | ||
| 480 | - | ||
| 481 | - private Builder replaceLinkAllocations(Builder builder, LinkKey linkKey, | ||
| 482 | - List<LinkResourceAllocations> before, | ||
| 483 | - List<LinkResourceAllocations> after) { | ||
| 484 | - | ||
| 485 | - byte[] oldValue = encodeLinkAllocations(before); | ||
| 486 | - byte[] newValue = encodeLinkAllocations(after); | ||
| 487 | - builder.putIfValueMatches(LINK_RESOURCE_ALLOCATIONS, toLinkDbKey(linkKey), oldValue, newValue); | ||
| 488 | - return builder; | ||
| 489 | - } | ||
| 490 | - | ||
| 491 | - @Override | ||
| 492 | - public LinkResourceEvent releaseResources(LinkResourceAllocations allocations) { | ||
| 493 | - checkNotNull(allocations); | ||
| 494 | - | ||
| 495 | - final IntentId intendId = allocations.intendId(); | ||
| 496 | - final String dbIntentId = toIntentDbKey(intendId); | ||
| 497 | - final Collection<Link> links = allocations.links(); | ||
| 498 | - | ||
| 499 | - boolean success; | ||
| 500 | - do { | ||
| 501 | - Builder tx = BatchWriteRequest.newBuilder(); | ||
| 502 | - | ||
| 503 | - // TODO: Should IntentId -> Allocation be updated conditionally? | ||
| 504 | - tx.remove(INTENT_ALLOCATIONS, dbIntentId); | ||
| 505 | - | ||
| 506 | - for (Link link : links) { | ||
| 507 | - final LinkKey linkId = LinkKey.linkKey(link); | ||
| 508 | - final String dbLinkId = toLinkDbKey(linkId); | ||
| 509 | - VersionedValue vv = databaseService.get(LINK_RESOURCE_ALLOCATIONS, dbLinkId); | ||
| 510 | - if (vv == null || vv.value() == null) { | ||
| 511 | - // something is wrong, but it is already freed | ||
| 512 | - log.warn("There was no resource left to release on {}", linkId); | ||
| 513 | - continue; | ||
| 514 | - } | ||
| 515 | - List<LinkResourceAllocations> before = decodeLinkAllocations(vv.value()); | ||
| 516 | - List<LinkResourceAllocations> after = new ArrayList<>(before); | ||
| 517 | - after.remove(allocations); | ||
| 518 | - byte[] oldValue = encodeLinkAllocations(before); | ||
| 519 | - byte[] newValue = encodeLinkAllocations(after); | ||
| 520 | - tx.putIfValueMatches(LINK_RESOURCE_ALLOCATIONS, dbLinkId, oldValue, newValue); | ||
| 521 | - } | ||
| 522 | - | ||
| 523 | - BatchWriteResult batchWrite = databaseService.batchWrite(tx.build()); | ||
| 524 | - success = batchWrite.isSuccessful(); | ||
| 525 | - } while (!success); | ||
| 526 | - | ||
| 527 | - // Issue events to force recompilation of intents. | ||
| 528 | - | ||
| 529 | - final List<LinkResourceAllocations> releasedResources = ImmutableList.of(allocations); | ||
| 530 | - return new LinkResourceEvent( | ||
| 531 | - LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE, | ||
| 532 | - releasedResources); | ||
| 533 | - } | ||
| 534 | - | ||
| 535 | - @Override | ||
| 536 | - public LinkResourceAllocations getAllocations(IntentId intentId) { | ||
| 537 | - checkNotNull(intentId); | ||
| 538 | - VersionedValue vv = databaseService.get(INTENT_ALLOCATIONS, toIntentDbKey(intentId)); | ||
| 539 | - if (vv == null) { | ||
| 540 | - return null; | ||
| 541 | - } | ||
| 542 | - LinkResourceAllocations allocations = decodeIntentAllocations(vv.value()); | ||
| 543 | - return allocations; | ||
| 544 | - } | ||
| 545 | - | ||
| 546 | - private String toLinkDbKey(LinkKey linkid) { | ||
| 547 | -// introduce cache if necessary | ||
| 548 | - return linkid.toString(); | ||
| 549 | -// Note: Above is irreversible, if we need reverse conversion | ||
| 550 | -// we may need something like below, due to String only limitation | ||
| 551 | -// byte[] bytes = serializer.encode(linkid); | ||
| 552 | -// StringBuilder builder = new StringBuilder(bytes.length * 4); | ||
| 553 | -// boolean isFirst = true; | ||
| 554 | -// for (byte b : bytes) { | ||
| 555 | -// if (!isFirst) { | ||
| 556 | -// builder.append(','); | ||
| 557 | -// } | ||
| 558 | -// builder.append(b); | ||
| 559 | -// isFirst = false; | ||
| 560 | -// } | ||
| 561 | -// return builder.toString(); | ||
| 562 | - } | ||
| 563 | - | ||
| 564 | -// private LinkKey toLinkKey(String linkKey) { | ||
| 565 | -// String[] bytes = linkKey.split(","); | ||
| 566 | -// ByteBuffer buf = ByteBuffer.allocate(bytes.length); | ||
| 567 | -// for (String bs : bytes) { | ||
| 568 | -// buf.put(Byte.parseByte(bs)); | ||
| 569 | -// } | ||
| 570 | -// buf.flip(); | ||
| 571 | -// return serializer.decode(buf); | ||
| 572 | -// } | ||
| 573 | - | ||
| 574 | - private String toIntentDbKey(IntentId intentid) { | ||
| 575 | - return intentid.toString(); | ||
| 576 | - } | ||
| 577 | - | ||
| 578 | - private IntentId toIntentId(String intentid) { | ||
| 579 | - checkArgument(intentid.startsWith("0x")); | ||
| 580 | - return IntentId.valueOf(Long.parseLong(intentid.substring(2))); | ||
| 581 | - } | ||
| 582 | - | ||
| 583 | - private LinkResourceAllocations decodeIntentAllocations(byte[] bytes) { | ||
| 584 | - return serializer.decode(bytes); | ||
| 585 | - } | ||
| 586 | - | ||
| 587 | - private byte[] encodeIntentAllocations(LinkResourceAllocations alloc) { | ||
| 588 | - return serializer.encode(checkNotNull(alloc)); | ||
| 589 | - } | ||
| 590 | - | ||
| 591 | - private List<LinkResourceAllocations> decodeLinkAllocations(byte[] bytes) { | ||
| 592 | - return serializer.decode(bytes); | ||
| 593 | - } | ||
| 594 | - | ||
| 595 | - private byte[] encodeLinkAllocations(List<LinkResourceAllocations> alloc) { | ||
| 596 | - return serializer.encode(checkNotNull(alloc)); | ||
| 597 | - } | ||
| 598 | - | ||
| 599 | - @Override | ||
| 600 | - public List<LinkResourceAllocations> getAllocations(Link link) { | ||
| 601 | - checkNotNull(link); | ||
| 602 | - final LinkKey key = LinkKey.linkKey(link); | ||
| 603 | - final String dbKey = toLinkDbKey(key); | ||
| 604 | - VersionedValue vv = databaseService.get(LINK_RESOURCE_ALLOCATIONS, dbKey); | ||
| 605 | - if (vv == null) { | ||
| 606 | - // write empty so that all other update can be replace operation | ||
| 607 | - byte[] emptyList = encodeLinkAllocations(new ArrayList<>()); | ||
| 608 | - boolean written = databaseService.putIfAbsent(LINK_RESOURCE_ALLOCATIONS, dbKey, emptyList); | ||
| 609 | - log.trace("Empty allocation write success? {}", written); | ||
| 610 | - vv = databaseService.get(LINK_RESOURCE_ALLOCATIONS, dbKey); | ||
| 611 | - if (vv == null) { | ||
| 612 | - log.error("Failed to re-read allocation for {}", dbKey); | ||
| 613 | - // note: cannot be Collections.emptyList(); | ||
| 614 | - return new ArrayList<>(); | ||
| 615 | - } | ||
| 616 | - } | ||
| 617 | - List<LinkResourceAllocations> allocations = decodeLinkAllocations(vv.value()); | ||
| 618 | - return allocations; | ||
| 619 | - } | ||
| 620 | - | ||
| 621 | - @Override | ||
| 622 | - public Iterable<LinkResourceAllocations> getAllocations() { | ||
| 623 | - //IntentId -> LinkResourceAllocations | ||
| 624 | - Map<String, VersionedValue> all = databaseService.getAll(INTENT_ALLOCATIONS); | ||
| 625 | - | ||
| 626 | - return FluentIterable.from(all.values()) | ||
| 627 | - .transform(new Function<VersionedValue, LinkResourceAllocations>() { | ||
| 628 | - | ||
| 629 | - @Override | ||
| 630 | - public LinkResourceAllocations apply(VersionedValue input) { | ||
| 631 | - if (input == null || input.value() == null) { | ||
| 632 | - return null; | ||
| 633 | - } | ||
| 634 | - return decodeIntentAllocations(input.value()); | ||
| 635 | - } | ||
| 636 | - }) | ||
| 637 | - .filter(notNull()); | ||
| 638 | - } | ||
| 639 | -} |
| 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