Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.segmentrouting.web; |
| 2 | |
Srikanth Vavilapalli | 4215081 | 2014-10-20 16:53:30 -0700 | [diff] [blame] | 3 | import java.io.IOException; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.List; |
| 6 | |
| 7 | import net.onrc.onos.apps.segmentrouting.ISegmentRoutingService; |
| 8 | import net.onrc.onos.core.util.Dpid; |
| 9 | |
| 10 | import org.codehaus.jackson.map.ObjectMapper; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 11 | import org.restlet.resource.Delete; |
| 12 | import org.restlet.resource.Get; |
| 13 | import org.restlet.resource.Post; |
| 14 | import org.restlet.resource.ServerResource; |
| 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | import net.onrc.onos.apps.segmentrouting.ISegmentRoutingService; |
| 25 | import net.onrc.onos.apps.segmentrouting.SegmentRoutingManager; |
| 26 | import net.onrc.onos.apps.segmentrouting.SegmentRoutingManager.TunnelInfo; |
| 27 | import net.onrc.onos.apps.segmentrouting.SegmentRoutingManager.TunnelRouteInfo; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Base class for return router statistics |
| 30 | * |
| 31 | */ |
| 32 | public class SegmentRouterTunnelResource extends ServerResource { |
| 33 | protected final static Logger log = |
| 34 | LoggerFactory.getLogger(SegmentRouterTunnelResource.class); |
| 35 | |
| 36 | @Post("json") |
| 37 | public String createTunnel(String tunnelParams) { |
Srikanth Vavilapalli | 4215081 | 2014-10-20 16:53:30 -0700 | [diff] [blame] | 38 | ISegmentRoutingService segmentRoutingService = |
| 39 | (ISegmentRoutingService) getContext().getAttributes(). |
| 40 | get(ISegmentRoutingService.class.getCanonicalName()); |
| 41 | ObjectMapper mapper = new ObjectMapper(); |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 42 | SegmentRouterTunnelRESTParams createParams = null; |
Srikanth Vavilapalli | 4215081 | 2014-10-20 16:53:30 -0700 | [diff] [blame] | 43 | try { |
| 44 | if (tunnelParams != null) { |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 45 | createParams = mapper.readValue(tunnelParams, |
| 46 | SegmentRouterTunnelRESTParams.class); |
Srikanth Vavilapalli | 4215081 | 2014-10-20 16:53:30 -0700 | [diff] [blame] | 47 | } |
| 48 | } catch (IOException ex) { |
| 49 | log.error("Exception occurred parsing inbound JSON", ex); |
| 50 | return "fail"; |
| 51 | } |
| 52 | log.debug("createTunnel with tunnelId {} tunnelPath{}", |
| 53 | createParams.getTunnel_id(), createParams.getTunnel_path()); |
| 54 | List<Dpid> tunnelDpids = new ArrayList<Dpid>(); |
| 55 | for (String dpid : createParams.getTunnel_path()) { |
| 56 | tunnelDpids.add(new Dpid(dpid)); |
| 57 | } |
| 58 | boolean result = segmentRoutingService.createTunnel(createParams.getTunnel_id(), |
| 59 | tunnelDpids); |
| 60 | return (result == true) ? "success" : "fail"; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Delete("json") |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 64 | public String deleteTunnel(String tunnelParams) { |
| 65 | ISegmentRoutingService segmentRoutingService = |
| 66 | (ISegmentRoutingService) getContext().getAttributes(). |
| 67 | get(ISegmentRoutingService.class.getCanonicalName()); |
| 68 | ObjectMapper mapper = new ObjectMapper(); |
| 69 | SegmentRouterTunnelRESTParams createParams = null; |
| 70 | try { |
| 71 | if (tunnelParams != null) { |
| 72 | createParams = mapper.readValue(tunnelParams, |
| 73 | SegmentRouterTunnelRESTParams.class); |
| 74 | } |
| 75 | } catch (IOException ex) { |
| 76 | log.error("Exception occurred parsing inbound JSON", ex); |
| 77 | return "fail"; |
| 78 | } |
| 79 | log.debug("deleteTunnel with Id {}", createParams.getTunnel_id()); |
| 80 | boolean result = segmentRoutingService.removeTunnel( |
| 81 | createParams.getTunnel_id()); |
| 82 | return (result == true) ? "deleted" : "fail"; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Get("json") |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 86 | public Object getTunnel() { |
| 87 | System.out.println("Got into getTunnel"); |
| 88 | ISegmentRoutingService segmentRoutingService = |
| 89 | (ISegmentRoutingService) getContext().getAttributes(). |
| 90 | get(ISegmentRoutingService.class.getCanonicalName()); |
| 91 | Iterator<TunnelInfo> ttI = segmentRoutingService.getTunnelTable().iterator(); |
| 92 | List<SegmentRouterTunnelInfo> infoList = new ArrayList<SegmentRouterTunnelInfo>(); |
| 93 | while(ttI.hasNext()){ |
| 94 | TunnelInfo tunnelInfo = ttI.next(); |
| 95 | Iterator<TunnelRouteInfo>trI = tunnelInfo.getRoutes().iterator(); |
| 96 | List<List<String>> labelStack = new ArrayList<List<String>>(); |
| 97 | while(trI.hasNext()){ |
| 98 | TunnelRouteInfo label = trI.next(); |
| 99 | labelStack.add(label.getRoute()); |
| 100 | } |
| 101 | SegmentRouterTunnelInfo info = new SegmentRouterTunnelInfo(tunnelInfo.getTunnelId(), |
| 102 | tunnelInfo.getDpids(), labelStack ); |
| 103 | infoList.add(info); |
| 104 | //TODO Add Group/DPID |
| 105 | |
| 106 | } |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 107 | log.debug("getTunnel with params"); |
Fahad Naeem Khan | a40f9b6 | 2014-10-20 18:33:45 -0700 | [diff] [blame] | 108 | Map <String,List<SegmentRouterTunnelInfo>>result = new HashMap<String,List<SegmentRouterTunnelInfo>>(); |
| 109 | result.put("tunnels", infoList); |
| 110 | return infoList; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 111 | } |
| 112 | } |