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 | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 60 | boolean result = segmentRoutingService.createPolicy( |
Sangho Shin | 7c1182c | 2014-10-21 15:08:13 -0700 | [diff] [blame] | 61 | createParams.getPolicy_id(), null, null, null, |
Srikanth Vavilapalli | 204d298 | 2014-10-23 14:58:49 -0700 | [diff] [blame] | 62 | src_ip, dst_ip, |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 63 | getProtoTypeByte(createParams.getProto_type()), |
| 64 | createParams.getSrc_tp_port(), |
| 65 | createParams.getDst_tp_port(), |
| 66 | createParams.getPriority(), |
| 67 | createParams.getTunnel_id()); |
| 68 | return (result == true) ? "success" : "fail"; |
| 69 | } |
| 70 | |
| 71 | private Byte getProtoTypeByte(String protoType) { |
Sangho Shin | 7c1182c | 2014-10-21 15:08:13 -0700 | [diff] [blame] | 72 | Byte protoTypeByte = null; |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 73 | switch (protoType) { |
| 74 | case "tcp": |
| 75 | protoTypeByte = IPv4.PROTOCOL_TCP; |
| 76 | break; |
| 77 | case "udp": |
| 78 | protoTypeByte = IPv4.PROTOCOL_UDP; |
| 79 | break; |
| 80 | } |
| 81 | return protoTypeByte; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Delete("json") |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 85 | public String deletePolicy(String policyParams) { |
| 86 | ISegmentRoutingService segmentRoutingService = |
| 87 | (ISegmentRoutingService) getContext().getAttributes(). |
| 88 | get(ISegmentRoutingService.class.getCanonicalName()); |
| 89 | ObjectMapper mapper = new ObjectMapper(); |
| 90 | SegmentRouterPolicyRESTParams createParams = null; |
| 91 | try { |
| 92 | if (policyParams != null) { |
| 93 | createParams = mapper.readValue(policyParams, |
| 94 | SegmentRouterPolicyRESTParams.class); |
| 95 | } |
| 96 | } catch (IOException ex) { |
| 97 | log.error("Exception occurred parsing inbound JSON", ex); |
| 98 | return "fail"; |
| 99 | } |
| 100 | |
| 101 | log.debug("deletePolicy with Id {}", createParams.getPolicy_id()); |
| 102 | boolean result = segmentRoutingService.removePolicy( |
| 103 | createParams.getPolicy_id()); |
| 104 | return (result == true) ? "deleted" : "fail"; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Get("json") |
Fahad Naeem Khan | 788895c | 2014-10-21 19:00:24 -0700 | [diff] [blame] | 108 | public Object getPolicy() { |
| 109 | ISegmentRoutingService segmentRoutingService = |
| 110 | (ISegmentRoutingService) getContext().getAttributes(). |
| 111 | get(ISegmentRoutingService.class.getCanonicalName()); |
| 112 | List<SegmentRouterPolicyInfo> policyList = new ArrayList<SegmentRouterPolicyInfo>(); |
| 113 | Collection<PolicyInfo> policies = segmentRoutingService.getPoclicyTable(); |
| 114 | Iterator<PolicyInfo> piI = policies.iterator(); |
| 115 | while(piI.hasNext()){ |
| 116 | PolicyInfo policy = piI.next(); |
| 117 | String policyId = policy.getPolicyId(); |
| 118 | int priority = policy.getPriority(); |
| 119 | int policyType = policy.getType(); |
| 120 | PacketMatch flowEntries = policy.getMatch(); |
| 121 | SegmentRouterPolicyInfo pInfo = new SegmentRouterPolicyInfo(policyId, policyType, priority, flowEntries); |
| 122 | policyList.add(pInfo); |
| 123 | } |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 124 | log.debug("getPolicy with params"); |
Fahad Naeem Khan | 788895c | 2014-10-21 19:00:24 -0700 | [diff] [blame] | 125 | return policyList; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 126 | } |
| 127 | } |