Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.segmentrouting.web; |
| 2 | |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 3 | import java.io.IOException; |
Fahad Naeem Khan | 788895c | 2014-10-21 19:00:24 -0700 | [diff] [blame] | 4 | import java.util.ArrayList; |
| 5 | import java.util.Collection; |
| 6 | import java.util.Iterator; |
| 7 | import java.util.List; |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 8 | |
| 9 | import net.onrc.onos.apps.segmentrouting.ISegmentRoutingService; |
Sangho Shin | f65b4da | 2014-10-28 16:58:13 -0700 | [diff] [blame^] | 10 | import net.onrc.onos.apps.segmentrouting.SegmentRoutingPolicy; |
| 11 | import net.onrc.onos.apps.segmentrouting.SegmentRoutingPolicy.PolicyType; |
| 12 | import net.onrc.onos.apps.segmentrouting.SegmentRoutingPolicyTunnel; |
Fahad Naeem Khan | 788895c | 2014-10-21 19:00:24 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.packet.IPv4; |
| 15 | import net.onrc.onos.core.util.IPv4Net; |
| 16 | |
| 17 | import org.codehaus.jackson.map.ObjectMapper; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 18 | import org.restlet.resource.Delete; |
| 19 | import org.restlet.resource.Get; |
| 20 | import org.restlet.resource.Post; |
| 21 | import org.restlet.resource.ServerResource; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | /** |
| 26 | * Base class for return router statistics |
| 27 | * |
| 28 | */ |
| 29 | public class SegmentRouterPolicyResource extends ServerResource { |
| 30 | protected final static Logger log = |
| 31 | LoggerFactory.getLogger(SegmentRouterPolicyResource.class); |
| 32 | |
| 33 | @Post("json") |
| 34 | public String createPolicy(String policyParams) { |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 35 | ISegmentRoutingService segmentRoutingService = |
| 36 | (ISegmentRoutingService) getContext().getAttributes(). |
| 37 | get(ISegmentRoutingService.class.getCanonicalName()); |
| 38 | ObjectMapper mapper = new ObjectMapper(); |
| 39 | SegmentRouterPolicyRESTParams createParams = null; |
| 40 | try { |
| 41 | if (policyParams != null) { |
| 42 | createParams = mapper.readValue(policyParams, |
| 43 | SegmentRouterPolicyRESTParams.class); |
| 44 | } |
| 45 | } catch (IOException ex) { |
| 46 | log.error("Exception occurred parsing inbound JSON", ex); |
| 47 | return "fail"; |
| 48 | } |
| 49 | |
Srikanth Vavilapalli | 9cde381 | 2014-10-21 21:49:10 -0700 | [diff] [blame] | 50 | log.debug("createPolicy of type {} with params id {} src_ip {} dst_ip {}" |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 51 | + "proto {} src_port {} dst_port {} priority {} tunnel_id {}", |
Srikanth Vavilapalli | 9cde381 | 2014-10-21 21:49:10 -0700 | [diff] [blame] | 52 | createParams.getPolicy_type(), |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 53 | createParams.getPolicy_id(), createParams.getSrc_ip(), |
| 54 | createParams.getDst_ip(), createParams.getProto_type(), |
| 55 | createParams.getSrc_tp_port(), createParams.getDst_tp_port(), |
| 56 | createParams.getPriority(), createParams.getTunnel_id()); |
| 57 | |
Srikanth Vavilapalli | 204d298 | 2014-10-23 14:58:49 -0700 | [diff] [blame] | 58 | IPv4Net src_ip = (createParams.getSrc_ip() != null) ? |
| 59 | new IPv4Net(createParams.getSrc_ip()) : null; |
| 60 | IPv4Net dst_ip = (createParams.getDst_ip() != null) ? |
| 61 | new IPv4Net(createParams.getDst_ip()) : null; |
Srikanth Vavilapalli | b563708 | 2014-10-24 13:01:58 -0700 | [diff] [blame] | 62 | Byte protoType = (createParams.getProto_type() != null) ? |
| 63 | getProtoTypeByte(createParams.getProto_type()) : null; |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 64 | boolean result = segmentRoutingService.createPolicy( |
Sangho Shin | 7c1182c | 2014-10-21 15:08:13 -0700 | [diff] [blame] | 65 | createParams.getPolicy_id(), null, null, null, |
Srikanth Vavilapalli | b563708 | 2014-10-24 13:01:58 -0700 | [diff] [blame] | 66 | src_ip, dst_ip, protoType, |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 67 | createParams.getSrc_tp_port(), |
| 68 | createParams.getDst_tp_port(), |
| 69 | createParams.getPriority(), |
| 70 | createParams.getTunnel_id()); |
| 71 | return (result == true) ? "success" : "fail"; |
| 72 | } |
| 73 | |
| 74 | private Byte getProtoTypeByte(String protoType) { |
Sangho Shin | 7c1182c | 2014-10-21 15:08:13 -0700 | [diff] [blame] | 75 | Byte protoTypeByte = null; |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 76 | switch (protoType) { |
| 77 | case "tcp": |
| 78 | protoTypeByte = IPv4.PROTOCOL_TCP; |
| 79 | break; |
| 80 | case "udp": |
| 81 | protoTypeByte = IPv4.PROTOCOL_UDP; |
| 82 | break; |
| 83 | } |
| 84 | return protoTypeByte; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Delete("json") |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 88 | public String deletePolicy(String policyParams) { |
| 89 | ISegmentRoutingService segmentRoutingService = |
| 90 | (ISegmentRoutingService) getContext().getAttributes(). |
| 91 | get(ISegmentRoutingService.class.getCanonicalName()); |
| 92 | ObjectMapper mapper = new ObjectMapper(); |
| 93 | SegmentRouterPolicyRESTParams createParams = null; |
| 94 | try { |
| 95 | if (policyParams != null) { |
| 96 | createParams = mapper.readValue(policyParams, |
| 97 | SegmentRouterPolicyRESTParams.class); |
| 98 | } |
| 99 | } catch (IOException ex) { |
| 100 | log.error("Exception occurred parsing inbound JSON", ex); |
| 101 | return "fail"; |
| 102 | } |
| 103 | |
| 104 | log.debug("deletePolicy with Id {}", createParams.getPolicy_id()); |
| 105 | boolean result = segmentRoutingService.removePolicy( |
| 106 | createParams.getPolicy_id()); |
| 107 | return (result == true) ? "deleted" : "fail"; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Get("json") |
Fahad Naeem Khan | 788895c | 2014-10-21 19:00:24 -0700 | [diff] [blame] | 111 | public Object getPolicy() { |
| 112 | ISegmentRoutingService segmentRoutingService = |
| 113 | (ISegmentRoutingService) getContext().getAttributes(). |
| 114 | get(ISegmentRoutingService.class.getCanonicalName()); |
| 115 | List<SegmentRouterPolicyInfo> policyList = new ArrayList<SegmentRouterPolicyInfo>(); |
Sangho Shin | f65b4da | 2014-10-28 16:58:13 -0700 | [diff] [blame^] | 116 | Collection<SegmentRoutingPolicy> policies = segmentRoutingService.getPoclicyTable(); |
| 117 | Iterator<SegmentRoutingPolicy> piI = policies.iterator(); |
Fahad Naeem Khan | 788895c | 2014-10-21 19:00:24 -0700 | [diff] [blame] | 118 | while(piI.hasNext()){ |
Sangho Shin | f65b4da | 2014-10-28 16:58:13 -0700 | [diff] [blame^] | 119 | SegmentRoutingPolicy policy = piI.next(); |
Fahad Naeem Khan | 788895c | 2014-10-21 19:00:24 -0700 | [diff] [blame] | 120 | String policyId = policy.getPolicyId(); |
Sangho Shin | f65b4da | 2014-10-28 16:58:13 -0700 | [diff] [blame^] | 121 | String tunnelId = null; |
| 122 | if (policy.getType() == PolicyType.TUNNEL_FLOW) { |
| 123 | tunnelId = ((SegmentRoutingPolicyTunnel)policy).getTunnelId(); |
| 124 | } |
Fahad Naeem Khan | 788895c | 2014-10-21 19:00:24 -0700 | [diff] [blame] | 125 | int priority = policy.getPriority(); |
Fahad Naeem Khan | 1e712ad | 2014-10-27 16:48:07 -0700 | [diff] [blame] | 126 | String policyType = policy.getType().name(); |
Fahad Naeem Khan | 788895c | 2014-10-21 19:00:24 -0700 | [diff] [blame] | 127 | PacketMatch flowEntries = policy.getMatch(); |
Fahad Naeem Khan | 6beb40c | 2014-10-27 12:01:40 -0700 | [diff] [blame] | 128 | SegmentRouterPolicyInfo pInfo = new SegmentRouterPolicyInfo(policyId, policyType, tunnelId, |
| 129 | priority, flowEntries); |
Fahad Naeem Khan | 788895c | 2014-10-21 19:00:24 -0700 | [diff] [blame] | 130 | policyList.add(pInfo); |
| 131 | } |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 132 | log.debug("getPolicy with params"); |
Fahad Naeem Khan | 788895c | 2014-10-21 19:00:24 -0700 | [diff] [blame] | 133 | return policyList; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 134 | } |
| 135 | } |