blob: 0df169e8488187ac7514ee20d96d281d9292d9cd [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;
Fahad Naeem Khan788895c2014-10-21 19:00:24 -07004import java.util.ArrayList;
5import java.util.Collection;
6import java.util.Iterator;
7import java.util.List;
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -07008
9import net.onrc.onos.apps.segmentrouting.ISegmentRoutingService;
Sangho Shinf65b4da2014-10-28 16:58:13 -070010import net.onrc.onos.apps.segmentrouting.SegmentRoutingPolicy;
11import net.onrc.onos.apps.segmentrouting.SegmentRoutingPolicy.PolicyType;
12import net.onrc.onos.apps.segmentrouting.SegmentRoutingPolicyTunnel;
Fahad Naeem Khan788895c2014-10-21 19:00:24 -070013import net.onrc.onos.core.matchaction.match.PacketMatch;
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070014import net.onrc.onos.core.packet.IPv4;
15import net.onrc.onos.core.util.IPv4Net;
16
17import org.codehaus.jackson.map.ObjectMapper;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -070018import org.restlet.resource.Delete;
19import org.restlet.resource.Get;
20import org.restlet.resource.Post;
21import org.restlet.resource.ServerResource;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25/**
26 * Base class for return router statistics
27 *
28 */
29public class SegmentRouterPolicyResource extends ServerResource {
30 protected final static Logger log =
31 LoggerFactory.getLogger(SegmentRouterPolicyResource.class);
32
33 @Post("json")
34 public String createPolicy(String policyParams) {
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070035 ISegmentRoutingService segmentRoutingService =
36 (ISegmentRoutingService) getContext().getAttributes().
37 get(ISegmentRoutingService.class.getCanonicalName());
38 ObjectMapper mapper = new ObjectMapper();
39 SegmentRouterPolicyRESTParams createParams = null;
40 try {
41 if (policyParams != null) {
42 createParams = mapper.readValue(policyParams,
43 SegmentRouterPolicyRESTParams.class);
44 }
45 } catch (IOException ex) {
46 log.error("Exception occurred parsing inbound JSON", ex);
47 return "fail";
48 }
49
Srikanth Vavilapalli9cde3812014-10-21 21:49:10 -070050 log.debug("createPolicy of type {} with params id {} src_ip {} dst_ip {}"
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070051 + "proto {} src_port {} dst_port {} priority {} tunnel_id {}",
Srikanth Vavilapalli9cde3812014-10-21 21:49:10 -070052 createParams.getPolicy_type(),
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070053 createParams.getPolicy_id(), createParams.getSrc_ip(),
54 createParams.getDst_ip(), createParams.getProto_type(),
55 createParams.getSrc_tp_port(), createParams.getDst_tp_port(),
56 createParams.getPriority(), createParams.getTunnel_id());
57
Srikanth Vavilapalli204d2982014-10-23 14:58:49 -070058 IPv4Net src_ip = (createParams.getSrc_ip() != null) ?
59 new IPv4Net(createParams.getSrc_ip()) : null;
60 IPv4Net dst_ip = (createParams.getDst_ip() != null) ?
61 new IPv4Net(createParams.getDst_ip()) : null;
Srikanth Vavilapallib5637082014-10-24 13:01:58 -070062 Byte protoType = (createParams.getProto_type() != null) ?
63 getProtoTypeByte(createParams.getProto_type()) : null;
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070064 boolean result = segmentRoutingService.createPolicy(
Sangho Shin7c1182c2014-10-21 15:08:13 -070065 createParams.getPolicy_id(), null, null, null,
Srikanth Vavilapallib5637082014-10-24 13:01:58 -070066 src_ip, dst_ip, protoType,
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070067 createParams.getSrc_tp_port(),
68 createParams.getDst_tp_port(),
69 createParams.getPriority(),
70 createParams.getTunnel_id());
71 return (result == true) ? "success" : "fail";
72 }
73
74 private Byte getProtoTypeByte(String protoType) {
Sangho Shin7c1182c2014-10-21 15:08:13 -070075 Byte protoTypeByte = null;
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070076 switch (protoType) {
77 case "tcp":
78 protoTypeByte = IPv4.PROTOCOL_TCP;
79 break;
80 case "udp":
81 protoTypeByte = IPv4.PROTOCOL_UDP;
82 break;
83 }
84 return protoTypeByte;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -070085 }
86
87 @Delete("json")
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070088 public String deletePolicy(String policyParams) {
89 ISegmentRoutingService segmentRoutingService =
90 (ISegmentRoutingService) getContext().getAttributes().
91 get(ISegmentRoutingService.class.getCanonicalName());
92 ObjectMapper mapper = new ObjectMapper();
93 SegmentRouterPolicyRESTParams createParams = null;
94 try {
95 if (policyParams != null) {
96 createParams = mapper.readValue(policyParams,
97 SegmentRouterPolicyRESTParams.class);
98 }
99 } catch (IOException ex) {
100 log.error("Exception occurred parsing inbound JSON", ex);
101 return "fail";
102 }
103
104 log.debug("deletePolicy with Id {}", createParams.getPolicy_id());
105 boolean result = segmentRoutingService.removePolicy(
106 createParams.getPolicy_id());
107 return (result == true) ? "deleted" : "fail";
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700108 }
109
110 @Get("json")
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700111 public Object getPolicy() {
112 ISegmentRoutingService segmentRoutingService =
113 (ISegmentRoutingService) getContext().getAttributes().
114 get(ISegmentRoutingService.class.getCanonicalName());
115 List<SegmentRouterPolicyInfo> policyList = new ArrayList<SegmentRouterPolicyInfo>();
Sangho Shinf65b4da2014-10-28 16:58:13 -0700116 Collection<SegmentRoutingPolicy> policies = segmentRoutingService.getPoclicyTable();
117 Iterator<SegmentRoutingPolicy> piI = policies.iterator();
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700118 while(piI.hasNext()){
Sangho Shinf65b4da2014-10-28 16:58:13 -0700119 SegmentRoutingPolicy policy = piI.next();
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700120 String policyId = policy.getPolicyId();
Sangho Shinf65b4da2014-10-28 16:58:13 -0700121 String tunnelId = null;
122 if (policy.getType() == PolicyType.TUNNEL_FLOW) {
123 tunnelId = ((SegmentRoutingPolicyTunnel)policy).getTunnelId();
124 }
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700125 int priority = policy.getPriority();
Fahad Naeem Khan1e712ad2014-10-27 16:48:07 -0700126 String policyType = policy.getType().name();
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700127 PacketMatch flowEntries = policy.getMatch();
Fahad Naeem Khan6beb40c2014-10-27 12:01:40 -0700128 SegmentRouterPolicyInfo pInfo = new SegmentRouterPolicyInfo(policyId, policyType, tunnelId,
129 priority, flowEntries);
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700130 policyList.add(pInfo);
131 }
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700132 log.debug("getPolicy with params");
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700133 return policyList;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700134 }
135}