Yuta HIGUCHI

DatabaseService subsystem: add admin commands, etc.

Change-Id: I24124579f5e0b03ccbf35a03230ae5a7aff95f22
/*
* 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.onlab.onos.cli;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onlab.onos.cluster.ControllerNode;
import org.onlab.onos.cluster.DefaultControllerNode;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.store.service.DatabaseAdminService;
import org.onlab.packet.IpAddress;
/**
* 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;
// TODO context aware completer to get IP from ClusterService?
@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.onlab.onos.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.onlab.onos.cluster.ClusterService;
import org.onlab.onos.cluster.ControllerNode;
import org.onlab.onos.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.onlab.onos.cli;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onlab.onos.cluster.ClusterService;
import org.onlab.onos.cluster.ControllerNode;
import org.onlab.onos.cluster.NodeId;
import org.onlab.onos.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);
}
}
}
......@@ -20,6 +20,26 @@
<action class="org.onlab.onos.cli.SummaryCommand"/>
</command>
<command>
<action class="org.onlab.onos.cli.TabletMemberCommand"/>
</command>
<command>
<action class="org.onlab.onos.cli.TabletAddCommand"/>
<completers>
<ref component-id="nodeIdCompleter"/>
<null/>
<null/>
</completers>
</command>
<command>
<action class="org.onlab.onos.cli.TabletRemoveCommand"/>
<completers>
<ref component-id="nodeIdCompleter"/>
<null/>
<null/>
</completers>
</command>
<command>
<action class="org.onlab.onos.cli.NodesListCommand"/>
</command>
<command>
......
package org.onlab.onos.store.service;
import java.util.Collection;
import java.util.List;
import org.onlab.onos.cluster.ControllerNode;
/**
* Service interface for running administrative tasks on a Database.
*/
......@@ -32,4 +35,26 @@ public interface DatabaseAdminService {
* 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();
}
......
......@@ -169,7 +169,9 @@ public class ClusterMessagingProtocol
@Override
public ProtocolClient createClient(TcpMember member) {
ControllerNode remoteNode = getControllerNode(member.host(), member.port());
checkNotNull(remoteNode, "A valid controller node is expected");
checkNotNull(remoteNode,
"A valid controller node is expected for %s:%s",
member.host(), member.port());
return new ClusterMessagingProtocolClient(
clusterCommunicator, clusterService.getLocalNode(), remoteNode);
}
......
/*
* 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.onlab.onos.store.service.impl;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.slf4j.LoggerFactory.getLogger;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.onlab.onos.cluster.DefaultControllerNode;
import org.onlab.onos.cluster.NodeId;
import org.onlab.packet.IpAddress;
import org.slf4j.Logger;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* Allows for reading and writing tablet definition as a JSON file.
*/
public class TabletDefinitionStore {
private final Logger log = getLogger(getClass());
private final File file;
/**
* Creates a reader/writer of the tablet definition file.
*
* @param filePath location of the definition file
*/
public TabletDefinitionStore(String filePath) {
file = new File(filePath);
}
/**
* Creates a reader/writer of the tablet definition file.
*
* @param filePath location of the definition file
*/
public TabletDefinitionStore(File filePath) {
file = checkNotNull(filePath);
}
/**
* Returns the Map from tablet name to set of initial member nodes.
*
* @return Map from tablet name to set of initial member nodes
* @throws IOException when I/O exception of some sort has occurred.
*/
public Map<String, Set<DefaultControllerNode>> read() throws IOException {
final Map<String, Set<DefaultControllerNode>> tablets = new HashMap<>();
final ObjectMapper mapper = new ObjectMapper();
final ObjectNode tabletNodes = (ObjectNode) mapper.readTree(file);
final Iterator<Entry<String, JsonNode>> fields = tabletNodes.fields();
while (fields.hasNext()) {
final Entry<String, JsonNode> next = fields.next();
final Set<DefaultControllerNode> nodes = new HashSet<>();
final Iterator<JsonNode> elements = next.getValue().elements();
while (elements.hasNext()) {
ObjectNode nodeDef = (ObjectNode) elements.next();
nodes.add(new DefaultControllerNode(new NodeId(nodeDef.get("id").asText()),
IpAddress.valueOf(nodeDef.get("ip").asText()),
nodeDef.get("tcpPort").asInt(9876)));
}
tablets.put(next.getKey(), nodes);
}
return tablets;
}
/**
* Updates the Map from tablet name to set of member nodes.
*
* @param tabletName name of the tablet to update
* @param nodes set of initial member nodes
* @throws IOException when I/O exception of some sort has occurred.
*/
public void write(String tabletName, Set<DefaultControllerNode> nodes) throws IOException {
checkNotNull(tabletName);
checkArgument(tabletName.isEmpty(), "Tablet name cannot be empty");
// TODO should validate if tabletName is allowed in JSON
// load current
Map<String, Set<DefaultControllerNode>> config;
try {
config = read();
} catch (IOException e) {
log.info("Reading tablet config failed, assuming empty definition.");
config = new HashMap<>();
}
// update with specified
config.put(tabletName, nodes);
// write back to file
final ObjectMapper mapper = new ObjectMapper();
final ObjectNode tabletNodes = mapper.createObjectNode();
for (Entry<String, Set<DefaultControllerNode>> tablet : config.entrySet()) {
ArrayNode nodeDefs = mapper.createArrayNode();
tabletNodes.set(tablet.getKey(), nodeDefs);
for (DefaultControllerNode node : tablet.getValue()) {
ObjectNode nodeDef = mapper.createObjectNode();
nodeDef.put("id", node.id().toString())
.put("ip", node.ip().toString())
.put("tcpPort", node.tcpPort());
nodeDefs.add(nodeDef);
}
}
mapper.writeTree(new JsonFactory().createGenerator(file, JsonEncoding.UTF8),
tabletNodes);
}
}
onos-config command will copy files contained in this directory to ONOS instances according to cell definition
......@@ -25,3 +25,18 @@ ssh $remote "
>> $ONOS_INSTALL_DIR/$KARAF_DIST/etc/system.properties
"
scp -q $CDEF_FILE $remote:$ONOS_INSTALL_DIR/config/
# Generate a default tablets.json from the ON* environment variables
TDEF_FILE=/tmp/tablets.json
echo "{ \"default\":[" > $TDEF_FILE
for node in $(env | sort | egrep "OC[2-9]+" | cut -d= -f2); do
echo " { \"id\": \"$node\", \"ip\": \"$node\", \"tcpPort\": 9876 }," >> $TDEF_FILE
done
echo " { \"id\": \"$OC1\", \"ip\": \"$OC1\", \"tcpPort\": 9876 }" >> $TDEF_FILE
echo "]}" >> $TDEF_FILE
scp -q $TDEF_FILE $remote:$ONOS_INSTALL_DIR/config/
# copy tools/package/config/ to remote
scp -qr ${ONOS_ROOT}/tools/package/config/ $remote:$ONOS_INSTALL_DIR/
......