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; |
| 4 | |
| 5 | import net.onrc.onos.apps.segmentrouting.ISegmentRoutingService; |
| 6 | import net.onrc.onos.core.packet.IPv4; |
| 7 | import net.onrc.onos.core.util.IPv4Net; |
| 8 | |
| 9 | import org.codehaus.jackson.map.ObjectMapper; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 10 | import org.restlet.resource.Delete; |
| 11 | import org.restlet.resource.Get; |
| 12 | import org.restlet.resource.Post; |
| 13 | import org.restlet.resource.ServerResource; |
| 14 | import org.slf4j.Logger; |
| 15 | import org.slf4j.LoggerFactory; |
| 16 | |
| 17 | /** |
| 18 | * Base class for return router statistics |
| 19 | * |
| 20 | */ |
| 21 | public class SegmentRouterPolicyResource extends ServerResource { |
| 22 | protected final static Logger log = |
| 23 | LoggerFactory.getLogger(SegmentRouterPolicyResource.class); |
| 24 | |
| 25 | @Post("json") |
| 26 | public String createPolicy(String policyParams) { |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 27 | ISegmentRoutingService segmentRoutingService = |
| 28 | (ISegmentRoutingService) getContext().getAttributes(). |
| 29 | get(ISegmentRoutingService.class.getCanonicalName()); |
| 30 | ObjectMapper mapper = new ObjectMapper(); |
| 31 | SegmentRouterPolicyRESTParams createParams = null; |
| 32 | try { |
| 33 | if (policyParams != null) { |
| 34 | createParams = mapper.readValue(policyParams, |
| 35 | SegmentRouterPolicyRESTParams.class); |
| 36 | } |
| 37 | } catch (IOException ex) { |
| 38 | log.error("Exception occurred parsing inbound JSON", ex); |
| 39 | return "fail"; |
| 40 | } |
| 41 | |
| 42 | log.debug("createPolicy with params id {} src_ip {} dst_ip {}" |
| 43 | + "proto {} src_port {} dst_port {} priority {} tunnel_id {}", |
| 44 | createParams.getPolicy_id(), createParams.getSrc_ip(), |
| 45 | createParams.getDst_ip(), createParams.getProto_type(), |
| 46 | createParams.getSrc_tp_port(), createParams.getDst_tp_port(), |
| 47 | createParams.getPriority(), createParams.getTunnel_id()); |
| 48 | |
| 49 | boolean result = segmentRoutingService.createPolicy( |
Sangho Shin | 7c1182c | 2014-10-21 15:08:13 -0700 | [diff] [blame] | 50 | createParams.getPolicy_id(), null, null, null, |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 51 | new IPv4Net(createParams.getSrc_ip()), |
| 52 | new IPv4Net(createParams.getDst_ip()), |
| 53 | getProtoTypeByte(createParams.getProto_type()), |
| 54 | createParams.getSrc_tp_port(), |
| 55 | createParams.getDst_tp_port(), |
| 56 | createParams.getPriority(), |
| 57 | createParams.getTunnel_id()); |
| 58 | return (result == true) ? "success" : "fail"; |
| 59 | } |
| 60 | |
| 61 | private Byte getProtoTypeByte(String protoType) { |
Sangho Shin | 7c1182c | 2014-10-21 15:08:13 -0700 | [diff] [blame] | 62 | Byte protoTypeByte = null; |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 63 | switch (protoType) { |
| 64 | case "tcp": |
| 65 | protoTypeByte = IPv4.PROTOCOL_TCP; |
| 66 | break; |
| 67 | case "udp": |
| 68 | protoTypeByte = IPv4.PROTOCOL_UDP; |
| 69 | break; |
| 70 | } |
| 71 | return protoTypeByte; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Delete("json") |
Srikanth Vavilapalli | e7dabdd | 2014-10-20 21:54:39 -0700 | [diff] [blame] | 75 | public String deletePolicy(String policyParams) { |
| 76 | ISegmentRoutingService segmentRoutingService = |
| 77 | (ISegmentRoutingService) getContext().getAttributes(). |
| 78 | get(ISegmentRoutingService.class.getCanonicalName()); |
| 79 | ObjectMapper mapper = new ObjectMapper(); |
| 80 | SegmentRouterPolicyRESTParams createParams = null; |
| 81 | try { |
| 82 | if (policyParams != null) { |
| 83 | createParams = mapper.readValue(policyParams, |
| 84 | SegmentRouterPolicyRESTParams.class); |
| 85 | } |
| 86 | } catch (IOException ex) { |
| 87 | log.error("Exception occurred parsing inbound JSON", ex); |
| 88 | return "fail"; |
| 89 | } |
| 90 | |
| 91 | log.debug("deletePolicy with Id {}", createParams.getPolicy_id()); |
| 92 | boolean result = segmentRoutingService.removePolicy( |
| 93 | createParams.getPolicy_id()); |
| 94 | return (result == true) ? "deleted" : "fail"; |
Srikanth Vavilapalli | b1f168d | 2014-10-19 21:31:52 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Get("json") |
| 98 | public String getPolicy() { |
| 99 | String reply = "success"; |
| 100 | log.debug("getPolicy with params"); |
| 101 | return reply; |
| 102 | } |
| 103 | } |