blob: fc38eaa700506e90eb09eecf00ba1aaadaa9ebac [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;
Fahad Naeem Khan788895c2014-10-21 19:00:24 -070010import net.onrc.onos.apps.segmentrouting.SegmentRoutingManager.PolicyInfo;
11import net.onrc.onos.core.matchaction.match.PacketMatch;
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070012import net.onrc.onos.core.packet.IPv4;
13import net.onrc.onos.core.util.IPv4Net;
14
15import org.codehaus.jackson.map.ObjectMapper;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -070016import org.restlet.resource.Delete;
17import org.restlet.resource.Get;
18import org.restlet.resource.Post;
19import org.restlet.resource.ServerResource;
20import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
22
23/**
24 * Base class for return router statistics
25 *
26 */
27public 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 Vavilapallie7dabdd2014-10-20 21:54:39 -070033 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 Vavilapalli9cde3812014-10-21 21:49:10 -070048 log.debug("createPolicy of type {} with params id {} src_ip {} dst_ip {}"
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070049 + "proto {} src_port {} dst_port {} priority {} tunnel_id {}",
Srikanth Vavilapalli9cde3812014-10-21 21:49:10 -070050 createParams.getPolicy_type(),
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070051 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 Vavilapalli204d2982014-10-23 14:58:49 -070056 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 Vavilapallib5637082014-10-24 13:01:58 -070060 Byte protoType = (createParams.getProto_type() != null) ?
61 getProtoTypeByte(createParams.getProto_type()) : null;
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070062 boolean result = segmentRoutingService.createPolicy(
Sangho Shin7c1182c2014-10-21 15:08:13 -070063 createParams.getPolicy_id(), null, null, null,
Srikanth Vavilapallib5637082014-10-24 13:01:58 -070064 src_ip, dst_ip, protoType,
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070065 createParams.getSrc_tp_port(),
66 createParams.getDst_tp_port(),
67 createParams.getPriority(),
68 createParams.getTunnel_id());
69 return (result == true) ? "success" : "fail";
70 }
71
72 private Byte getProtoTypeByte(String protoType) {
Sangho Shin7c1182c2014-10-21 15:08:13 -070073 Byte protoTypeByte = null;
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070074 switch (protoType) {
75 case "tcp":
76 protoTypeByte = IPv4.PROTOCOL_TCP;
77 break;
78 case "udp":
79 protoTypeByte = IPv4.PROTOCOL_UDP;
80 break;
81 }
82 return protoTypeByte;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -070083 }
84
85 @Delete("json")
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070086 public String deletePolicy(String policyParams) {
87 ISegmentRoutingService segmentRoutingService =
88 (ISegmentRoutingService) getContext().getAttributes().
89 get(ISegmentRoutingService.class.getCanonicalName());
90 ObjectMapper mapper = new ObjectMapper();
91 SegmentRouterPolicyRESTParams createParams = null;
92 try {
93 if (policyParams != null) {
94 createParams = mapper.readValue(policyParams,
95 SegmentRouterPolicyRESTParams.class);
96 }
97 } catch (IOException ex) {
98 log.error("Exception occurred parsing inbound JSON", ex);
99 return "fail";
100 }
101
102 log.debug("deletePolicy with Id {}", createParams.getPolicy_id());
103 boolean result = segmentRoutingService.removePolicy(
104 createParams.getPolicy_id());
105 return (result == true) ? "deleted" : "fail";
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700106 }
107
108 @Get("json")
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700109 public Object getPolicy() {
110 ISegmentRoutingService segmentRoutingService =
111 (ISegmentRoutingService) getContext().getAttributes().
112 get(ISegmentRoutingService.class.getCanonicalName());
113 List<SegmentRouterPolicyInfo> policyList = new ArrayList<SegmentRouterPolicyInfo>();
114 Collection<PolicyInfo> policies = segmentRoutingService.getPoclicyTable();
115 Iterator<PolicyInfo> piI = policies.iterator();
116 while(piI.hasNext()){
117 PolicyInfo policy = piI.next();
118 String policyId = policy.getPolicyId();
119 int priority = policy.getPriority();
120 int policyType = policy.getType();
121 PacketMatch flowEntries = policy.getMatch();
122 SegmentRouterPolicyInfo pInfo = new SegmentRouterPolicyInfo(policyId, policyType, priority, flowEntries);
123 policyList.add(pInfo);
124 }
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700125 log.debug("getPolicy with params");
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700126 return policyList;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700127 }
128}