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