Madan Jampani
Committed by Gerrit Code Review

Remove deprecated code.

Change-Id: Ifd68e4ddfaade2a8dd7de43a83bf222b48b9291b
Showing 26 changed files with 3 additions and 1789 deletions
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onlab.packet.IpAddress;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.DefaultControllerNode;
import org.onosproject.cluster.NodeId;
import org.onosproject.store.service.DatabaseAdminService;
/**
* Adds a new controller cluster node.
*/
@Command(scope = "onos", name = "tablet-add",
description = "Adds a new member to tablet")
public class TabletAddCommand extends AbstractShellCommand {
@Argument(index = 0, name = "nodeId", description = "Node ID",
required = true, multiValued = false)
String nodeId = null;
@Argument(index = 1, name = "ip", description = "Node IP address",
required = true, multiValued = false)
String ip = null;
@Argument(index = 2, name = "tcpPort", description = "Node TCP listen port",
required = false, multiValued = false)
int tcpPort = 9876;
// TODO add tablet name argument when we support multiple tablets
@Override
protected void execute() {
DatabaseAdminService service = get(DatabaseAdminService.class);
ControllerNode node = new DefaultControllerNode(new NodeId(nodeId),
IpAddress.valueOf(ip),
tcpPort);
service.addMember(node);
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.store.service.DatabaseAdminService;
import java.util.Optional;
/**
* Lists mastership roles of nodes for each device.
*/
@Command(scope = "onos", name = "tablet-leader",
description = "Prints the current leader of a tablet.")
public class TabletLeaderCommand extends AbstractShellCommand {
@Override
protected void execute() {
final DatabaseAdminService dbAdminService = get(DatabaseAdminService.class);
Optional<ControllerNode> leader = dbAdminService.leader();
if (leader.isPresent()) {
print("Leader: %s", leader.get());
} else {
print("No Leader");
}
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.store.service.DatabaseAdminService;
import java.util.Collections;
import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
/**
* Lists all controller cluster nodes.
*/
@Command(scope = "onos", name = "tablet-member",
description = "Lists all member nodes")
public class TabletMemberCommand extends AbstractShellCommand {
// TODO add tablet name argument when we support multiple tablets
@Override
protected void execute() {
DatabaseAdminService service = get(DatabaseAdminService.class);
ClusterService clusterService = get(ClusterService.class);
List<ControllerNode> nodes = newArrayList(service.listMembers());
Collections.sort(nodes, Comparators.NODE_COMPARATOR);
if (outputJson()) {
print("%s", json(service, nodes));
} else {
ControllerNode self = clusterService.getLocalNode();
for (ControllerNode node : nodes) {
print("id=%s, address=%s:%s %s",
node.id(), node.ip(), node.tcpPort(),
node.equals(self) ? "*" : "");
}
}
}
// Produces JSON structure.
private JsonNode json(DatabaseAdminService service, List<ControllerNode> nodes) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode result = mapper.createArrayNode();
for (ControllerNode node : nodes) {
result.add(mapper.createObjectNode()
.put("id", node.id().toString())
.put("ip", node.ip().toString())
.put("tcpPort", node.tcpPort()));
}
return result;
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.cli;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.NodeId;
import org.onosproject.store.service.DatabaseAdminService;
/**
* Removes a controller cluster node.
*/
@Command(scope = "onos", name = "tablet-remove",
description = "Removes a member from tablet")
public class TabletRemoveCommand extends AbstractShellCommand {
@Argument(index = 0, name = "nodeId", description = "Node ID",
required = true, multiValued = false)
String nodeId = null;
// TODO add tablet name argument when we support multiple tablets
@Override
protected void execute() {
DatabaseAdminService service = get(DatabaseAdminService.class);
ClusterService clusterService = get(ClusterService.class);
ControllerNode node = clusterService.getNode(new NodeId(nodeId));
if (node != null) {
service.removeMember(node);
}
}
}
......@@ -53,33 +53,6 @@
</command>
<command>
<action class="org.onosproject.cli.TabletMemberCommand"/>
</command>
<command>
<action class="org.onosproject.cli.TabletLeaderCommand"/>
</command>
<!--
<command>
<action class="org.onosproject.cli.TabletAddCommand"/>
<completers>
<ref component-id="nodeIdCompleter"/>
<null/>
<null/>
</completers>
</command>
<command>
<action class="org.onosproject.cli.TabletRemoveCommand"/>
<completers>
<ref component-id="nodeIdCompleter"/>
<null/>
<null/>
</completers>
</command>
-->
<command>
<action class="org.onosproject.cli.NodesListCommand"/>
</command>
<!--
......
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import java.util.List;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
/**
* Collection of read requests to be submitted as one batch.
*/
public final class BatchReadRequest {
private final List<ReadRequest> readRequests;
/**
* Creates a new BatchReadRequest object from the specified list of read requests.
* @param readRequests read requests.
* @return BatchReadRequest object.
*/
public static BatchReadRequest create(List<ReadRequest> readRequests) {
return new BatchReadRequest(readRequests);
}
private BatchReadRequest(List<ReadRequest> readRequests) {
this.readRequests = ImmutableList.copyOf(readRequests);
}
/**
* Returns the number of requests in this batch.
* @return size of request batch.
*/
public int batchSize() {
return readRequests.size();
}
/**
* Returns the requests in this batch as a list.
* @return list of read requests
*/
public List<ReadRequest> getAsList() {
return readRequests;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("readRequests", readRequests)
.toString();
}
/**
* Builder for BatchReadRequest.
*/
public static class Builder {
private final List<ReadRequest> readRequests = Lists.newLinkedList();
/**
* Append a get request.
* @param tableName table name
* @param key key to fetch.
* @return this Builder
*/
public Builder get(String tableName, String key) {
readRequests.add(new ReadRequest(tableName, key));
return this;
}
/**
* Builds a BatchReadRequest.
* @return BatchReadRequest
*/
public BatchReadRequest build() {
return new BatchReadRequest(readRequests);
}
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import java.util.List;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
/**
* Result of a batch read operation.
*/
public class BatchReadResult {
private final List<ReadResult> readResults;
public BatchReadResult(List<ReadResult> readResults) {
this.readResults = ImmutableList.copyOf(readResults);
}
/**
* Returns the results as a list.
* @return list of results
*/
public List<ReadResult> getAsList() {
return readResults;
}
/**
* Returns the batch size.
* @return batch size
*/
public int batchSize() {
return readResults.size();
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("readResults", readResults)
.toString();
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import java.util.List;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
/**
* Collection of write requests to be submitted as one batch.
*/
public final class BatchWriteRequest {
private final List<WriteRequest> writeRequests;
/**
* Creates a new BatchWriteRequest object from the specified list of write requests.
* @param writeRequests write requests.
* @return BatchWriteRequest object.
*/
public static BatchWriteRequest create(List<WriteRequest> writeRequests) {
return new BatchWriteRequest(writeRequests);
}
private BatchWriteRequest(List<WriteRequest> writeRequests) {
this.writeRequests = ImmutableList.copyOf(writeRequests);
}
/**
* Returns the requests in this batch as a list.
* @return list of write requests
*/
public List<WriteRequest> getAsList() {
return writeRequests;
}
/**
* Returns the number of requests in this batch.
* @return size of request batch.
*/
public int batchSize() {
return writeRequests.size();
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("writeRequests", writeRequests)
.toString();
}
public static Builder newBuilder() {
return new Builder();
}
/**
* Builder for BatchWriteRequest.
*/
public static class Builder {
private final List<WriteRequest> writeRequests = Lists.newLinkedList();
public Builder put(String tableName, String key, byte[] value) {
writeRequests.add(WriteRequest.put(tableName, key, value));
return this;
}
public Builder putIfAbsent(String tableName, String key, byte[] value) {
writeRequests.add(WriteRequest.putIfAbsent(tableName, key, value));
return this;
}
public Builder putIfValueMatches(String tableName, String key, byte[] oldValue, byte[] newValue) {
writeRequests.add(WriteRequest.putIfValueMatches(tableName, key, oldValue, newValue));
return this;
}
public Builder putIfVersionMatches(String tableName, String key, byte[] value, long version) {
writeRequests.add(WriteRequest.putIfVersionMatches(tableName, key, value, version));
return this;
}
public Builder remove(String tableName, String key) {
writeRequests.add(WriteRequest.remove(tableName, key));
return this;
}
public Builder removeIfVersionMatches(String tableName, String key, long version) {
writeRequests.add(WriteRequest.removeIfVersionMatches(tableName, key, version));
return this;
}
public Builder removeIfValueMatches(String tableName, String key, byte[] value) {
writeRequests.add(WriteRequest.removeIfValueMatches(tableName, key, value));
return this;
}
public BatchWriteRequest build() {
return new BatchWriteRequest(writeRequests);
}
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import java.util.List;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
/**
* Result of a batch write operation.
*/
public class BatchWriteResult {
private final List<WriteResult> writeResults;
public BatchWriteResult(List<WriteResult> writeResults) {
this.writeResults = ImmutableList.copyOf(writeResults);
}
/**
* Returns true if this batch write operation was successful.
* @return true if successful, false otherwise.
*/
public boolean isSuccessful() {
for (WriteResult result : writeResults) {
if (result.status() != WriteStatus.OK) {
return false;
}
}
return true;
}
/**
* Returns the results as a List.
* @return list of batch results.
*/
public List<WriteResult> getAsList() {
return this.writeResults;
}
/**
* Returns the size of this batch.
* @return batch size.
*/
public int batchSize() {
return writeResults.size();
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("writeResults", writeResults)
.toString();
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import org.onosproject.cluster.ControllerNode;
/**
* Service interface for running administrative tasks on a Database.
*/
public interface DatabaseAdminService {
/**
* Creates a new table.
* Table creation is idempotent. Attempting to create a table
* that already exists will be a noop.
* @param name table name.
* @return true if the table was created by this call, false otherwise.
*/
public boolean createTable(String name);
/**
* Creates a new table where last update time will be used to track and expire old entries.
* Table creation is idempotent. Attempting to create a table
* that already exists will be a noop.
* @param name table name.
* @param ttlMillis total duration in millis since last update time when entries will be expired.
* @return true if the table was created by this call, false otherwise.
*/
public boolean createTable(String name, int ttlMillis);
/**
* Lists all the tables in the database.
* @return set of table names.
*/
public Set<String> listTables();
/**
* Deletes a table from the database.
* @param name name of the table to delete.
*/
public void dropTable(String name);
/**
* Deletes all tables from the database.
*/
public void dropAllTables();
/**
* Add member to default Tablet.
*
* @param node to add
*/
public void addMember(ControllerNode node);
/**
* Remove member from default Tablet.
*
* @param node node to remove
*/
public void removeMember(ControllerNode node);
/**
* List members forming default Tablet.
*
* @return Copied collection of members forming default Tablet.
*/
public Collection<ControllerNode> listMembers();
/**
* Returns the current Leader of the default Tablet.
*
* @return leader node
*/
public Optional<ControllerNode> leader();
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
/**
* Base exception type for database failures.
*/
@SuppressWarnings("serial")
public class DatabaseException extends RuntimeException {
public DatabaseException(String message, Throwable t) {
super(message, t);
}
public DatabaseException(String message) {
super(message);
}
public DatabaseException(Throwable t) {
super(t);
}
public DatabaseException() {
};
public static class Timeout extends DatabaseException {
public Timeout(String message, Throwable t) {
super(message, t);
}
public Timeout(String message) {
super(message);
}
public Timeout(Throwable t) {
super(t);
}
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import java.util.Map;
/**
* Service interface for a strongly consistent and durable
* key value data store.
*/
public interface DatabaseService {
/**
* Reads the specified key.
* @param tableName name of the table associated with this operation.
* @param key key to read.
* @return value (and version) associated with this key. This calls returns null if the key does not exist.
*/
VersionedValue get(String tableName, String key);
/**
* Reads the whole table.
*
* @param tableName name of the table associated with this operation.
* @return the whole table
*/
Map<String, VersionedValue> getAll(String tableName);
/**
* Associate the key with a value.
* @param tableName table name in which this key/value resides.
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with the specified key, or null if there was no mapping for the key.
*/
VersionedValue put(String tableName, String key, byte[] value);
/**
* If the specified key is not already associated with a value, associate it with the given value.
* @param tableName table name in which this key/value resides.
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return true if put was successful, false if there is already a value associated with this key
*/
boolean putIfAbsent(String tableName, String key, byte[] value);
/**
* Sets the key to the specified value if the version in the database (for that key)
* matches the specified version.
* @param tableName name of table associated with this operation.
* @param key key
* @param value value
* @param version version that should present in the database for the put to be successful.
* @return true if put was successful, false if there version in database is different from what is specified.
*/
boolean putIfVersionMatches(String tableName, String key, byte[] value, long version);
/**
* Replaces the entry for a key only if currently mapped to a given value.
* @param tableName name of table associated with this operation.
* @param key with which the specified value is associated
* @param oldValue value expected to be associated with the specified key
* @param newValue value to be associated with the specified key
* @return true if put was successful, false if there version in database is different from what is specified.
*/
boolean putIfValueMatches(String tableName, String key, byte[] oldValue, byte[] newValue);
/**
* Removes the key (and associated value).
* @param tableName name of table associated with this operation.
* @param key key to remove
* @return value previously associated with the key. This call returns null if the key does not exist.
*/
VersionedValue remove(String tableName, String key);
/**
* Removes the key (and associated value) if the version in the database matches specified version.
* @param tableName name of table associated with this operation.
* @param key key to remove
* @param version version that should present in the database for the remove to be successful.
* @return true if remove was successful, false if there version in database is different from what is specified.
*/
boolean removeIfVersionMatches(String tableName, String key, long version);
/**
* Removes the key (and associated value) if the value in the database matches specified value.
* @param tableName name of table associated with this operation.
* @param key key to remove
* @param value value that should present in the database for the remove to be successful.
* @return true if remove was successful, false if there value in database is different from what is specified.
*/
boolean removeIfValueMatches(String tableName, String key, byte[] value);
/**
* Performs a batch read operation and returns the results.
* @param batchRequest batch request.
* @return result of the batch operation.
*/
BatchReadResult batchRead(BatchReadRequest batchRequest);
/**
* Performs a batch write operation and returns the results.
* This method provides transactional semantics. Either all writes succeed or none do.
* Even a single write failure would cause the entire batch to be aborted.
* In the case of unsuccessful operation, the batch result can be inspected to determine
* which operation(s) caused the batch to fail.
* @param batchRequest batch request.
* @return result of the batch operation.
*/
BatchWriteResult batchWrite(BatchWriteRequest batchRequest);
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
/**
* Exception thrown when an operation (read or write) is requested for
* a table that does not exist.
*/
@SuppressWarnings("serial")
public class NoSuchTableException extends DatabaseException {
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
/**
* A container object which either has a result or an exception.
* <p>
* If a result is present, get() will return it otherwise get() will throw
* the exception that was encountered in the process of generating the result.
* </p>
* @param <R> type of result.
* @param <E> exception encountered in generating the result.
*/
public interface OptionalResult<R, E extends Throwable> {
/**
* Returns the result or throws an exception if there is no
* valid result.
* @return result
*/
public R get();
/**
* Returns true if there is a valid result.
* @return true is yes, false otherwise.
*/
public boolean hasValidResult();
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import com.google.common.base.MoreObjects;
/**
* Database read request.
*/
public class ReadRequest {
private final String tableName;
private final String key;
/**
* Creates a read request,
* which will retrieve the specified key from the table.
*
* @param tableName name of the table
* @param key key in the table
* @return ReadRequest
*/
public static ReadRequest get(String tableName, String key) {
return new ReadRequest(tableName, key);
}
public ReadRequest(String tableName, String key) {
this.tableName = checkNotNull(tableName);
this.key = checkNotNull(key);
}
/**
* Return the name of the table.
* @return table name.
*/
public String tableName() {
return tableName;
}
/**
* Returns the key.
* @return key.
*/
public String key() {
return key;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("tableName", tableName)
.add("key", key)
.toString();
}
@Override
public int hashCode() {
return Objects.hash(key, tableName);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ReadRequest other = (ReadRequest) obj;
return Objects.equals(this.key, other.key) &&
Objects.equals(this.tableName, other.tableName);
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import com.google.common.base.MoreObjects;
/**
* Database read result.
*/
public class ReadResult {
private final String tableName;
private final String key;
private final VersionedValue value;
private final ReadStatus status;
public ReadResult(ReadStatus status, String tableName, String key, VersionedValue value) {
this.status = status;
this.tableName = tableName;
this.key = key;
this.value = value;
}
/**
* Returns the status of the read operation.
* @return read operation status
*/
public ReadStatus status() {
return status;
}
/**
* Returns database table name.
* @return table name
*/
public String tableName() {
return tableName;
}
/**
* Returns database table key.
* @return key
*/
public String key() {
return key;
}
/**
* Returns true if database table contained value for the key.
*
* @return true if database table contained value for the key
*/
public boolean valueExists() {
return value != null;
}
/**
* Returns value associated with the key.
* @return non-null value if the table contains one, null otherwise.
*/
public VersionedValue value() {
return value;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("status", status)
.add("tableName", tableName)
.add("key", key)
.add("value", value)
.toString();
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
/**
* Status of completed read request.
*/
public enum ReadStatus {
/**
* Read completed successfully.
*/
OK,
/**
* Read failed due to an invalid table name being specified.
*/
NO_SUCH_TABLE
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import java.util.Arrays;
import org.onlab.util.ByteArraySizeHashPrinter;
import com.google.common.base.MoreObjects;
/**
* Wrapper object that holds the object (as byte array) and its version.
*/
public class VersionedValue {
private final byte[] value;
private final long version;
/**
* Creates a new instance with the specified value and version.
* @param value value
* @param version version
*/
public VersionedValue(byte[] value, long version) {
this.value = value;
this.version = version;
}
/**
* Returns the value.
* @return value.
*/
public byte[] value() {
return value;
}
/**
* Returns the version.
* @return version.
*/
public long version() {
return version;
}
/**
* Creates a copy of given VersionedValue.
*
* @param original VersionedValue to create a copy
* @return same as original if original or it's value is null,
* otherwise creates a copy.
*/
public static VersionedValue copy(VersionedValue original) {
if (original == null) {
return null;
}
if (original.value == null) {
// immutable, no need to copy
return original;
} else {
return new VersionedValue(
Arrays.copyOf(original.value,
original.value.length),
original.version);
}
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("version", version)
.add("value", ByteArraySizeHashPrinter.orNull(value))
.toString();
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.store.service.WriteRequest.Type.*;
import java.util.Objects;
import org.onlab.util.ByteArraySizeHashPrinter;
import com.google.common.base.MoreObjects;
/**
* Database write request.
*/
public class WriteRequest {
public static final int ANY_VERSION = -1;
private final String tableName;
private final String key;
private final Type type;
private final byte[] newValue;
private final long previousVersion;
private final byte[] oldValue;
/**
* Creates a write request, which will
* put the specified value to the table regardless of the previous value.
*
* @param tableName name of the table
* @param key key in the table
* @param newValue value to write, must not be null
* @return WriteRequest
*/
public static WriteRequest put(String tableName, String key,
byte[] newValue) {
return new WriteRequest(PUT, tableName, key,
checkNotNull(newValue), ANY_VERSION, null);
}
/**
* Creates a write request, which will
* put the specified value to the table if the previous version matches.
*
* @param tableName name of the table
* @param key key in the table
* @param newValue value to write, must not be null
* @param previousVersion previous version expected
* @return WriteRequest
*/
public static WriteRequest putIfVersionMatches(String tableName, String key,
byte[] newValue,
long previousVersion) {
checkArgument(previousVersion >= 0);
return new WriteRequest(PUT_IF_VERSION, tableName, key,
checkNotNull(newValue), previousVersion, null);
}
/**
* Creates a write request, which will
* put the specified value to the table if the previous value matches.
*
* @param tableName name of the table
* @param key key in the table
* @param oldValue previous value expected, must not be null
* @param newValue value to write, must not be null
* @return WriteRequest
*/
public static WriteRequest putIfValueMatches(String tableName, String key,
byte[] oldValue,
byte[] newValue) {
return new WriteRequest(PUT_IF_VALUE, tableName, key,
checkNotNull(newValue), ANY_VERSION,
checkNotNull(oldValue));
}
/**
* Creates a write request, which will
* put the specified value to the table if the previous value does not exist.
*
* @param tableName name of the table
* @param key key in the table
* @param newValue value to write, must not be null
* @return WriteRequest
*/
public static WriteRequest putIfAbsent(String tableName, String key,
byte[] newValue) {
return new WriteRequest(PUT_IF_ABSENT, tableName, key,
checkNotNull(newValue), ANY_VERSION, null);
}
/**
* Creates a write request, which will
* remove the specified entry from the table regardless of the previous value.
*
* @param tableName name of the table
* @param key key in the table
* @return WriteRequest
*/
public static WriteRequest remove(String tableName, String key) {
return new WriteRequest(REMOVE, tableName, key,
null, ANY_VERSION, null);
}
/**
* Creates a write request, which will
* remove the specified entry from the table if the previous version matches.
*
* @param tableName name of the table
* @param key key in the table
* @param previousVersion previous version expected
* @return WriteRequest
*/
public static WriteRequest removeIfVersionMatches(String tableName, String key,
long previousVersion) {
return new WriteRequest(REMOVE_IF_VERSION, tableName, key,
null, previousVersion, null);
}
/**
* Creates a write request, which will
* remove the specified entry from the table if the previous value matches.
*
* @param tableName name of the table
* @param key key in the table
* @param oldValue previous value expected, must not be null
* @return WriteRequest
*/
public static WriteRequest removeIfValueMatches(String tableName, String key,
byte[] oldValue) {
return new WriteRequest(REMOVE_IF_VALUE, tableName, key,
null, ANY_VERSION, checkNotNull(oldValue));
}
public enum Type {
PUT,
PUT_IF_VERSION,
PUT_IF_VALUE,
PUT_IF_ABSENT,
REMOVE,
REMOVE_IF_VERSION,
REMOVE_IF_VALUE,
}
// hidden constructor
protected WriteRequest(Type type, String tableName, String key,
byte[] newValue,
long previousVersion, byte[] oldValue) {
checkNotNull(tableName);
checkNotNull(key);
this.tableName = tableName;
this.key = key;
this.type = type;
this.newValue = newValue;
this.previousVersion = previousVersion;
this.oldValue = oldValue;
}
public String tableName() {
return tableName;
}
public String key() {
return key;
}
public WriteRequest.Type type() {
return type;
}
public byte[] newValue() {
return newValue;
}
public long previousVersion() {
return previousVersion;
}
public byte[] oldValue() {
return oldValue;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("type", type)
.add("tableName", tableName)
.add("key", key)
.add("newValue", ByteArraySizeHashPrinter.orNull(newValue))
.add("previousVersion", previousVersion)
.add("oldValue", ByteArraySizeHashPrinter.orNull(oldValue))
.toString();
}
// TODO: revisit hashCode, equals condition
@Override
public int hashCode() {
return Objects.hash(type, key, tableName, previousVersion);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
WriteRequest other = (WriteRequest) obj;
return Objects.equals(this.type, other.type) &&
Objects.equals(this.key, other.key) &&
Objects.equals(this.tableName, other.tableName) &&
Objects.equals(this.previousVersion, other.previousVersion);
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
import com.google.common.base.MoreObjects;
/**
* Database write result.
*/
public class WriteResult {
private final WriteStatus status;
private final VersionedValue previousValue;
public WriteResult(WriteStatus status, VersionedValue previousValue) {
this.status = status;
this.previousValue = previousValue;
}
public VersionedValue previousValue() {
return previousValue;
}
public WriteStatus status() {
return status;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("status", status)
.add("previousValue", previousValue)
.toString();
}
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service;
/**
* Status of completed write request.
*/
public enum WriteStatus {
/**
* Write completed successfully.
*/
OK,
/**
* Write was aborted (ex: if one or more write operations in a batch fail, others are aborted).
*/
ABORTED,
/**
* Write failed due to pre-condition failure. (ex: version or value mis-match).
*/
PRECONDITION_VIOLATION,
/**
* Write failed due to an invalid table name being specified.
*/
NO_SUCH_TABLE,
}
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.service.impl;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.notNull;
import java.util.Map;
import org.onosproject.store.serializers.StoreSerializer;
import org.onosproject.store.service.DatabaseAdminService;
import org.onosproject.store.service.DatabaseException;
import org.onosproject.store.service.DatabaseService;
import org.onosproject.store.service.VersionedValue;
import com.google.common.base.Function;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.FluentIterable;
/**
* Map like interface wrapper around DatabaseService.
*
* @param <K> Key type of the map.
* The type must have toString(), which can uniquely identify the entry.
* @param <V> Value type
*/
public class CMap<K, V> {
@SuppressWarnings("unused")
private final DatabaseAdminService dbAdminService;
private final DatabaseService dbService;
private final String tableName;
private final StoreSerializer serializer;
private final LoadingCache<K, String> keyCache;
/**
* Creates a CMap instance.
* It will create the table if necessary.
*
* @param dbAdminService DatabaseAdminService to use for this instance
* @param dbService DatabaseService to use for this instance
* @param tableName table which this Map corresponds to
* @param serializer Value serializer
*/
public CMap(DatabaseAdminService dbAdminService,
DatabaseService dbService,
String tableName,
StoreSerializer serializer) {
this.dbAdminService = checkNotNull(dbAdminService);
this.dbService = checkNotNull(dbService);
this.tableName = checkNotNull(tableName);
this.serializer = checkNotNull(serializer);
boolean tableReady = false;
do {
try {
if (!dbAdminService.listTables().contains(tableName)) {
dbAdminService.createTable(tableName);
}
tableReady = true;
} catch (DatabaseException e) {
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
throw new DatabaseException(e1);
}
}
} while (!tableReady);
keyCache = CacheBuilder.newBuilder()
.softValues()
.build(new CacheLoader<K, String>() {
@Override
public String load(K key) {
return key.toString();
}
});
}
protected String sK(K key) {
return keyCache.getUnchecked(key);
}
protected byte[] sV(V val) {
return serializer.encode(val);
}
protected V dV(byte[] valBytes) {
return serializer.decode(valBytes);
}
/**
* Puts an entry to the map, if not already present.
*
* @param key the key of the value to put if absent
* @param value the value to be put if previous value does not exist
* @return true if put was successful.
*/
public boolean putIfAbsent(K key, V value) {
return dbService.putIfAbsent(tableName, sK(key), sV(value));
}
/**
* Removes an entry associated to specified key.
*
* @param key key of the value to remove
* @return previous value in the map for the key
*/
public V remove(K key) {
VersionedValue removed = dbService.remove(tableName, sK(key));
if (removed == null) {
return null;
}
return dV(removed.value());
}
/**
* Returns the size of the map.
*
* @return size of the map
*/
public long size() {
// TODO this is very inefficient
return dbService.getAll(tableName).size();
}
/**
* Returns all the values contained in the map.
*
* @return values containd in this map
*/
public Iterable<V> values() {
Map<String, VersionedValue> all = dbService.getAll(tableName);
return FluentIterable.from(all.values())
.transform(new Function<VersionedValue, V>() {
@Override
public V apply(VersionedValue input) {
if (input == null) {
return null;
}
return dV(input.value());
}
})
.filter(notNull());
}
/**
* Gets the value in the map.
*
* @param key to get from the map
* @return value associated with the key, null if not such entry
*/
public V get(K key) {
VersionedValue vv = dbService.get(tableName, sK(key));
if (vv == null) {
return null;
}
return dV(vv.value());
}
/**
* Replaces the value in the map if the value matches the expected.
*
* @param key of the entry to replace
* @param oldVal value expected to be in the map
* @param newVal value to be replaced with
* @return true if successfully replaced
*/
public boolean replace(K key, V oldVal, V newVal) {
return dbService.putIfValueMatches(tableName, sK(key), sV(oldVal), sV(newVal));
}
/**
* Puts a value int the map.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return previous value or null if not such entry
*/
public V put(K key, V value) {
VersionedValue vv = dbService.put(tableName, sK(key), sV(value));
if (vv == null) {
return null;
}
return dV(vv.value());
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Utility services and backing mechanisms for implementations of distributed stores.
*/
package org.onosproject.store.service.impl;
\ No newline at end of file
......@@ -18,6 +18,7 @@ package org.onosproject.store.serializers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.onlab.packet.ChassisId;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.Ip4Prefix;
......@@ -119,15 +120,7 @@ import org.onosproject.net.resource.MplsLabel;
import org.onosproject.net.resource.MplsLabelResourceAllocation;
import org.onosproject.net.resource.MplsLabelResourceRequest;
import org.onosproject.store.Timestamp;
import org.onosproject.store.service.BatchReadRequest;
import org.onosproject.store.service.BatchWriteRequest;
import org.onosproject.store.service.ReadRequest;
import org.onosproject.store.service.ReadResult;
import org.onosproject.store.service.ReadStatus;
import org.onosproject.store.service.VersionedValue;
import org.onosproject.store.service.WriteRequest;
import org.onosproject.store.service.WriteResult;
import org.onosproject.store.service.WriteStatus;
import org.onosproject.store.service.Versioned;
import java.net.URI;
import java.time.Duration;
......@@ -339,16 +332,7 @@ public final class KryoNamespaces {
.register(new MastershipTermSerializer(), MastershipTerm.class)
.register(new HostLocationSerializer(), HostLocation.class)
.register(new DefaultOutboundPacketSerializer(), DefaultOutboundPacket.class)
.register(ReadRequest.class)
.register(WriteRequest.class)
.register(WriteRequest.Type.class)
.register(WriteResult.class)
.register(ReadResult.class)
.register(BatchReadRequest.class)
.register(BatchWriteRequest.class)
.register(ReadStatus.class)
.register(WriteStatus.class)
.register(VersionedValue.class)
.register(Versioned.class)
.register(DefaultGroupId.class)
.register(
MplsIntent.class,
......