Added initial REST API to the Hazelcast Datagrid storage.
For now it is needed/used only for debugging purpose.
diff --git a/src/main/java/net/onrc/onos/datagrid/HazelcastDatagrid.java b/src/main/java/net/onrc/onos/datagrid/HazelcastDatagrid.java
index 41b4957..3b34a35 100644
--- a/src/main/java/net/onrc/onos/datagrid/HazelcastDatagrid.java
+++ b/src/main/java/net/onrc/onos/datagrid/HazelcastDatagrid.java
@@ -17,7 +17,9 @@
import net.floodlightcontroller.core.module.FloodlightModuleException;
import net.floodlightcontroller.core.module.IFloodlightModule;
import net.floodlightcontroller.core.module.IFloodlightService;
+import net.floodlightcontroller.restserver.IRestApiService;
+import net.onrc.onos.datagrid.web.DatagridWebRoutable;
import net.onrc.onos.ofcontroller.flowmanager.IFlowEventHandlerService;
import net.onrc.onos.ofcontroller.topology.TopologyElement;
import net.onrc.onos.ofcontroller.util.FlowEntry;
@@ -48,6 +50,7 @@
protected final static Logger log = LoggerFactory.getLogger(HazelcastDatagrid.class);
protected IFloodlightProviderService floodlightProvider;
+ protected IRestApiService restApi;
protected static final String HazelcastConfigFile = "datagridConfig";
private HazelcastInstance hazelcastInstance = null;
@@ -385,6 +388,7 @@
Collection<Class<? extends IFloodlightService>> l =
new ArrayList<Class<? extends IFloodlightService>>();
l.add(IFloodlightProviderService.class);
+ l.add(IRestApiService.class);
return l;
}
@@ -397,6 +401,7 @@
public void init(FloodlightModuleContext context)
throws FloodlightModuleException {
floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
+ restApi = context.getServiceImpl(IRestApiService.class);
// Get the configuration file name and configure the Datagrid
Map<String, String> configMap = context.getConfigParams(this);
@@ -412,6 +417,8 @@
@Override
public void startUp(FloodlightModuleContext context) {
hazelcastInstance = Hazelcast.newHazelcastInstance(hazelcastConfig);
+
+ restApi.addRestletRoutable(new DatagridWebRoutable());
}
/**
diff --git a/src/main/java/net/onrc/onos/datagrid/web/DatagridWebRoutable.java b/src/main/java/net/onrc/onos/datagrid/web/DatagridWebRoutable.java
new file mode 100644
index 0000000..2c99ece
--- /dev/null
+++ b/src/main/java/net/onrc/onos/datagrid/web/DatagridWebRoutable.java
@@ -0,0 +1,30 @@
+package net.onrc.onos.datagrid.web;
+
+import net.floodlightcontroller.restserver.RestletRoutable;
+
+import org.restlet.Context;
+import org.restlet.Restlet;
+import org.restlet.routing.Router;
+
+/**
+ * REST API implementation for the Datagrid.
+ */
+public class DatagridWebRoutable implements RestletRoutable {
+ /**
+ * Create the Restlet router and bind to the proper resources.
+ */
+ @Override
+ public Restlet getRestlet(Context context) {
+ Router router = new Router(context);
+ router.attach("/get/map/{map-name}/json", GetMapResource.class);
+ return router;
+ }
+
+ /**
+ * Set the base path for the Topology
+ */
+ @Override
+ public String basePath() {
+ return "/wm/datagrid";
+ }
+}
diff --git a/src/main/java/net/onrc/onos/datagrid/web/GetMapResource.java b/src/main/java/net/onrc/onos/datagrid/web/GetMapResource.java
new file mode 100644
index 0000000..6c66c90
--- /dev/null
+++ b/src/main/java/net/onrc/onos/datagrid/web/GetMapResource.java
@@ -0,0 +1,84 @@
+package net.onrc.onos.datagrid.web;
+
+import java.util.Collection;
+
+import net.onrc.onos.datagrid.IDatagridService;
+import net.onrc.onos.ofcontroller.topology.TopologyElement;
+import net.onrc.onos.ofcontroller.util.FlowEntry;
+import net.onrc.onos.ofcontroller.util.FlowPath;
+
+import org.restlet.resource.Get;
+import org.restlet.resource.ServerResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Datagrid REST API implementation: Get the state of a map.
+ *
+ * Valid map names:
+ * - "all" : Get all maps
+ * - "flow" : Get the Flows
+ * - "flow-entry" : Get the Flow Entries
+ * - "topology" : Get the Topology
+ *
+ * GET /wm/datagrid/get/map/{map-name}/json
+ */
+public class GetMapResource extends ServerResource {
+ protected final static Logger log = LoggerFactory.getLogger(GetMapResource.class);
+
+ /**
+ * Implement the API.
+ *
+ * @return a string with the state of the map(s).
+ */
+ @Get("json")
+ public String retrieve() {
+ String result = "";
+
+ IDatagridService datagridService =
+ (IDatagridService)getContext().getAttributes().
+ get(IDatagridService.class.getCanonicalName());
+
+ if (datagridService == null) {
+ log.debug("ONOS Datagrid Service not found");
+ return result;
+ }
+
+ // Extract the arguments
+ String mapNameStr = (String)getRequestAttributes().get("map-name");
+
+ log.debug("Get Datagrid Map: " + mapNameStr);
+
+ //
+ // Get the Flows
+ //
+ if (mapNameStr.equals("flow") || mapNameStr.equals("all")) {
+ Collection<FlowPath> flowPaths = datagridService.getAllFlows();
+ result += "Flows:\n";
+ for (FlowPath flowPath : flowPaths) {
+ result += flowPath.toString() + "\n";
+ }
+ }
+
+ //
+ // Get the Flow Entries
+ //
+ if (mapNameStr.equals("flow-entry") || mapNameStr.equals("all")) {
+ Collection<FlowEntry> flowEntries = datagridService.getAllFlowEntries();
+ result += "Flow Entries:\n";
+ for (FlowEntry flowEntry : flowEntries) {
+ result += flowEntry.toString() + "\n";
+ }
+ }
+
+ if (mapNameStr.equals("topology") || mapNameStr.equals("all")) {
+ Collection<TopologyElement> topologyElements = datagridService.getAllTopologyElements();
+ result += "Topology:\n";
+ for (TopologyElement topologyElement : topologyElements) {
+ result += topologyElements.toString() + "\n";
+ }
+ }
+
+ return result;
+ }
+}
diff --git a/web/get_datagrid.py b/web/get_datagrid.py
new file mode 100755
index 0000000..2d26846
--- /dev/null
+++ b/web/get_datagrid.py
@@ -0,0 +1,84 @@
+#! /usr/bin/env python
+# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-
+
+import pprint
+import os
+import sys
+import subprocess
+import json
+import argparse
+import io
+import time
+
+from flask import Flask, json, Response, render_template, make_response, request
+
+## Global Var ##
+ControllerIP="127.0.0.1"
+ControllerPort=8080
+
+DEBUG=0
+pp = pprint.PrettyPrinter(indent=4)
+
+app = Flask(__name__)
+
+## Worker Functions ##
+def log_error(txt):
+ print '%s' % (txt)
+
+def debug(txt):
+ if DEBUG:
+ print '%s' % (txt)
+
+# @app.route("/wm/datagrid/get/map/<map-name>/json ")
+# Sample output:
+
+def print_datagrid_map(parsedResult):
+ print '%s' % (parsedResult)
+
+def get_datagrid_map(map_name):
+ try:
+ command = "curl -s \"http://%s:%s/wm/datagrid/get/map/%s/json\"" % (ControllerIP, ControllerPort, map_name)
+ debug("get_datagrid_map %s" % command)
+
+ result = os.popen(command).read()
+ debug("result %s" % result)
+ if len(result) == 0:
+ print "No Map found"
+ return;
+
+ # TODO: For now, the string is not JSON-formatted
+ # parsedResult = json.loads(result)
+ parsedResult = result
+ debug("parsed %s" % parsedResult)
+ except:
+ log_error("Controller IF has issue")
+ exit(1)
+
+ print_datagrid_map(parsedResult)
+
+
+if __name__ == "__main__":
+ usage_msg1 = "Usage:\n"
+ usage_msg2 = "%s <map_name> : Print datagrid map with name of <map_name>\n" % (sys.argv[0])
+ usage_msg3 = " Valid map names:\n"
+ usage_msg4 = " all : Print all maps\n"
+ usage_msg5 = " flow : Print all flows\n"
+ usage_msg6 = " flow-entry : Print all flow entries\n"
+ usage_msg7 = " topology : Print the topology\n"
+ usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5
+ usage_msg = usage_msg + usage_msg6 + usage_msg7
+
+ # app.debug = False;
+
+ # Usage info
+ if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
+ print(usage_msg)
+ exit(0)
+
+ # Check arguments
+ if len(sys.argv) < 2:
+ log_error(usage_msg)
+ exit(1)
+
+ # Do the work
+ get_datagrid_map(sys.argv[1])