blob: 32d9cc614a93b72c5f5be9e731af9b49c600c615 [file] [log] [blame]
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -07001package net.onrc.onos.apps.segmentrouting.web;
2
Srikanth Vavilapalli42150812014-10-20 16:53:30 -07003import java.io.IOException;
4import java.util.ArrayList;
Fahad Naeem Khan12fa63a2014-10-21 17:01:27 -07005import java.util.Collection;
Srikanth Vavilapalli1f6a5742014-10-21 13:45:11 -07006import java.util.HashMap;
7import java.util.Iterator;
Srikanth Vavilapalli42150812014-10-20 16:53:30 -07008import java.util.List;
Srikanth Vavilapalli1f6a5742014-10-21 13:45:11 -07009import java.util.Map;
Srikanth Vavilapalli42150812014-10-20 16:53:30 -070010
11import net.onrc.onos.apps.segmentrouting.ISegmentRoutingService;
Fahad Naeem Khan12fa63a2014-10-21 17:01:27 -070012import net.onrc.onos.apps.segmentrouting.SegmentRoutingManager.PolicyInfo;
Srikanth Vavilapalli1f6a5742014-10-21 13:45:11 -070013import net.onrc.onos.apps.segmentrouting.SegmentRoutingManager.TunnelInfo;
14import net.onrc.onos.apps.segmentrouting.SegmentRoutingManager.TunnelRouteInfo;
Srikanth Vavilapalli42150812014-10-20 16:53:30 -070015
16import org.codehaus.jackson.map.ObjectMapper;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -070017import org.restlet.resource.Delete;
18import org.restlet.resource.Get;
19import org.restlet.resource.Post;
20import org.restlet.resource.ServerResource;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -070023/**
24 * Base class for return router statistics
25 *
26 */
27public 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 Vavilapalli42150812014-10-20 16:53:30 -070033 ISegmentRoutingService segmentRoutingService =
34 (ISegmentRoutingService) getContext().getAttributes().
35 get(ISegmentRoutingService.class.getCanonicalName());
36 ObjectMapper mapper = new ObjectMapper();
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070037 SegmentRouterTunnelRESTParams createParams = null;
Srikanth Vavilapalli42150812014-10-20 16:53:30 -070038 try {
39 if (tunnelParams != null) {
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070040 createParams = mapper.readValue(tunnelParams,
41 SegmentRouterTunnelRESTParams.class);
Srikanth Vavilapalli42150812014-10-20 16:53:30 -070042 }
43 } catch (IOException ex) {
44 log.error("Exception occurred parsing inbound JSON", ex);
45 return "fail";
46 }
Srikanth Vavilapalli1f6a5742014-10-21 13:45:11 -070047 log.debug("createTunnel with tunnelId {} Label Path{}",
48 createParams.getTunnel_id(), createParams.getLabel_path());
Srikanth Vavilapalli42150812014-10-20 16:53:30 -070049 boolean result = segmentRoutingService.createTunnel(createParams.getTunnel_id(),
Srikanth Vavilapalli1f6a5742014-10-21 13:45:11 -070050 createParams.getLabel_path());
Srikanth Vavilapalli42150812014-10-20 16:53:30 -070051 return (result == true) ? "success" : "fail";
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -070052 }
53
54 @Delete("json")
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070055 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 Vavilapallib1f168d2014-10-19 21:31:52 -070074 }
75
76 @Get("json")
Fahad Naeem Khana40f9b62014-10-20 18:33:45 -070077 public Object getTunnel() {
Fahad Naeem Khana40f9b62014-10-20 18:33:45 -070078 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 Khan12fa63a2014-10-21 17:01:27 -070085 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 Khana40f9b62014-10-20 18:33:45 -070098 Iterator<TunnelRouteInfo>trI = tunnelInfo.getRoutes().iterator();
99 List<List<String>> labelStack = new ArrayList<List<String>>();
Fahad Naeem Khan95aa4012014-10-21 14:07:00 -0700100 List<String> dpidGroup = new ArrayList<String>();
Fahad Naeem Khana40f9b62014-10-20 18:33:45 -0700101 while(trI.hasNext()){
102 TunnelRouteInfo label = trI.next();
103 labelStack.add(label.getRoute());
Fahad Naeem Khan12fa63a2014-10-21 17:01:27 -0700104 Integer gId = segmentRoutingService.getTunnelGroupId(tunnelId,
Fahad Naeem Khan95aa4012014-10-21 14:07:00 -0700105 label.getSrcSwDpid());
106 dpidGroup.add(label.getSrcSwDpid() + "/"+ gId);
Fahad Naeem Khana40f9b62014-10-20 18:33:45 -0700107 }
Fahad Naeem Khan12fa63a2014-10-21 17:01:27 -0700108 SegmentRouterTunnelInfo info = new SegmentRouterTunnelInfo(tunnelId,
109 labelStack, dpidGroup, policiesId);
Fahad Naeem Khana40f9b62014-10-20 18:33:45 -0700110 infoList.add(info);
Fahad Naeem Khana40f9b62014-10-20 18:33:45 -0700111 }
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700112 log.debug("getTunnel with params");
Fahad Naeem Khana40f9b62014-10-20 18:33:45 -0700113 return infoList;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700114 }
115}