Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.restserver; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.List; |
| 7 | import java.util.Map; |
| 8 | |
Jonathan Hart | 285f2c2 | 2013-07-03 16:26:46 +1200 | [diff] [blame] | 9 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 10 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 11 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 12 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 13 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 14 | import org.restlet.Application; |
| 15 | import org.restlet.Component; |
| 16 | import org.restlet.Context; |
| 17 | import org.restlet.Request; |
| 18 | import org.restlet.Response; |
| 19 | import org.restlet.Restlet; |
Jonathan Hart | 285f2c2 | 2013-07-03 16:26:46 +1200 | [diff] [blame] | 20 | import org.restlet.Server; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 21 | import org.restlet.data.Protocol; |
| 22 | import org.restlet.data.Reference; |
| 23 | import org.restlet.data.Status; |
| 24 | import org.restlet.ext.jackson.JacksonRepresentation; |
| 25 | import org.restlet.representation.Representation; |
| 26 | import org.restlet.routing.Filter; |
| 27 | import org.restlet.routing.Router; |
| 28 | import org.restlet.routing.Template; |
| 29 | import org.restlet.service.StatusService; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 33 | public class RestApiServer |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | implements IFloodlightModule, IRestApiService { |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 35 | protected final static Logger logger = LoggerFactory.getLogger(RestApiServer.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 36 | protected List<RestletRoutable> restlets; |
| 37 | protected FloodlightModuleContext fmlContext; |
| 38 | protected int restPort = 8080; |
Jonathan Hart | 285f2c2 | 2013-07-03 16:26:46 +1200 | [diff] [blame] | 39 | protected String numThreads = null; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 40 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 41 | // *********** |
| 42 | // Application |
| 43 | // *********** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 45 | protected class RestApplication extends Application { |
| 46 | protected Context context; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 47 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 48 | public RestApplication() { |
| 49 | super(new Context()); |
| 50 | this.context = getContext(); |
| 51 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 53 | @Override |
| 54 | public Restlet createInboundRoot() { |
| 55 | Router baseRouter = new Router(context); |
| 56 | baseRouter.setDefaultMatchingMode(Template.MODE_STARTS_WITH); |
| 57 | for (RestletRoutable rr : restlets) { |
| 58 | baseRouter.attach(rr.basePath(), rr.getRestlet(context)); |
| 59 | } |
| 60 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | Filter slashFilter = new Filter() { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 62 | @Override |
| 63 | protected int beforeHandle(Request request, Response response) { |
| 64 | Reference ref = request.getResourceRef(); |
| 65 | String originalPath = ref.getPath(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | if (originalPath.contains("//")) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 67 | String newPath = originalPath.replaceAll("/+", "/"); |
| 68 | ref.setPath(newPath); |
| 69 | } |
| 70 | return Filter.CONTINUE; |
| 71 | } |
| 72 | |
| 73 | }; |
| 74 | slashFilter.setNext(baseRouter); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 76 | return slashFilter; |
| 77 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 79 | public void run(FloodlightModuleContext fmlContext, int restPort) { |
| 80 | setStatusService(new StatusService() { |
| 81 | @Override |
| 82 | public Representation getRepresentation(Status status, |
| 83 | Request request, |
| 84 | Response response) { |
| 85 | return new JacksonRepresentation<Status>(status); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 87 | }); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 89 | // Add everything in the module context to the rest |
| 90 | for (Class<? extends IFloodlightService> s : fmlContext.getAllServices()) { |
| 91 | if (logger.isTraceEnabled()) { |
| 92 | logger.trace("Adding {} for service {} into context", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 93 | s.getCanonicalName(), fmlContext.getServiceImpl(s)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 94 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | context.getAttributes().put(s.getCanonicalName(), |
| 96 | fmlContext.getServiceImpl(s)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 97 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 99 | // Start listening for REST requests |
| 100 | try { |
| 101 | final Component component = new Component(); |
Jonathan Hart | 285f2c2 | 2013-07-03 16:26:46 +1200 | [diff] [blame] | 102 | Server server = component.getServers().add(Protocol.HTTP, restPort); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | if (numThreads != null) { |
| 104 | logger.debug("Setting number of REST API threads to {}", numThreads); |
| 105 | server.getContext().getParameters().add("defaultThreads", numThreads); |
Jonathan Hart | 285f2c2 | 2013-07-03 16:26:46 +1200 | [diff] [blame] | 106 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 107 | component.getDefaultHost().attach(this); |
| 108 | component.start(); |
| 109 | } catch (Exception e) { |
| 110 | throw new RuntimeException(e); |
| 111 | } |
| 112 | } |
| 113 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 114 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 115 | // *************** |
| 116 | // IRestApiService |
| 117 | // *************** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 118 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 119 | @Override |
| 120 | public void addRestletRoutable(RestletRoutable routable) { |
| 121 | restlets.add(routable); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void run() { |
| 126 | if (logger.isDebugEnabled()) { |
| 127 | StringBuffer sb = new StringBuffer(); |
| 128 | sb.append("REST API routables: "); |
| 129 | for (RestletRoutable routable : restlets) { |
| 130 | sb.append(routable.getClass().getSimpleName()); |
| 131 | sb.append(" ("); |
| 132 | sb.append(routable.basePath()); |
| 133 | sb.append("), "); |
| 134 | } |
| 135 | logger.debug(sb.toString()); |
| 136 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 138 | RestApplication restApp = new RestApplication(); |
| 139 | restApp.run(fmlContext, restPort); |
| 140 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 141 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 142 | // ***************** |
| 143 | // IFloodlightModule |
| 144 | // ***************** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 145 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 146 | @Override |
| 147 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 148 | Collection<Class<? extends IFloodlightService>> services = |
| 149 | new ArrayList<Class<? extends IFloodlightService>>(1); |
| 150 | services.add(IRestApiService.class); |
| 151 | return services; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 156 | getServiceImpls() { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 157 | Map<Class<? extends IFloodlightService>, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 158 | IFloodlightService> m = |
| 159 | new HashMap<Class<? extends IFloodlightService>, |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 160 | IFloodlightService>(); |
| 161 | m.put(IRestApiService.class, this); |
| 162 | return m; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 167 | // We don't have any |
| 168 | return null; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void init(FloodlightModuleContext context) |
| 173 | throws FloodlightModuleException { |
| 174 | // This has to be done here since we don't know what order the |
| 175 | // startUp methods will be called |
| 176 | this.restlets = new ArrayList<RestletRoutable>(); |
| 177 | this.fmlContext = context; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 178 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 179 | // read our config options |
| 180 | Map<String, String> configOptions = context.getConfigParams(this); |
| 181 | String port = configOptions.get("port"); |
| 182 | if (port != null) { |
| 183 | restPort = Integer.parseInt(port); |
| 184 | } |
| 185 | logger.debug("REST port set to {}", restPort); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 186 | |
Jonathan Hart | 285f2c2 | 2013-07-03 16:26:46 +1200 | [diff] [blame] | 187 | String numThreads = configOptions.get("dispatcherthreads"); |
| 188 | if (numThreads != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 189 | this.numThreads = numThreads; |
Jonathan Hart | 285f2c2 | 2013-07-03 16:26:46 +1200 | [diff] [blame] | 190 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void startUp(FloodlightModuleContext Context) { |
| 195 | // no-op |
| 196 | } |
| 197 | } |