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