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