blob: cbea58db7cbba8f244edbea39db372857a1a8ab2 [file] [log] [blame]
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -07001package net.onrc.onos.apps.segmentrouting.web;
2
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -07003import java.io.IOException;
4
5import net.onrc.onos.apps.segmentrouting.ISegmentRoutingService;
6import net.onrc.onos.core.packet.IPv4;
7import net.onrc.onos.core.util.IPv4Net;
8
9import org.codehaus.jackson.map.ObjectMapper;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -070010import org.restlet.resource.Delete;
11import org.restlet.resource.Get;
12import org.restlet.resource.Post;
13import org.restlet.resource.ServerResource;
14import org.slf4j.Logger;
15import org.slf4j.LoggerFactory;
16
17/**
18 * Base class for return router statistics
19 *
20 */
21public 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 Vavilapallie7dabdd2014-10-20 21:54:39 -070027 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 Shin7c1182c2014-10-21 15:08:13 -070050 createParams.getPolicy_id(), null, null, null,
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070051 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 Shin7c1182c2014-10-21 15:08:13 -070062 Byte protoTypeByte = null;
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070063 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 Vavilapallib1f168d2014-10-19 21:31:52 -070072 }
73
74 @Delete("json")
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070075 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 Vavilapallib1f168d2014-10-19 21:31:52 -070095 }
96
97 @Get("json")
98 public String getPolicy() {
99 String reply = "success";
100 log.debug("getPolicy with params");
101 return reply;
102 }
103}