blob: a95544060b3cc2fe949efca31ee21a2e42244858 [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 Vavilapalliad223492014-10-29 10:23:16 -070035 log.debug("createPolicy with policyParams {}", policyParams);
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070036 ISegmentRoutingService segmentRoutingService =
37 (ISegmentRoutingService) getContext().getAttributes().
38 get(ISegmentRoutingService.class.getCanonicalName());
39 ObjectMapper mapper = new ObjectMapper();
40 SegmentRouterPolicyRESTParams createParams = null;
41 try {
42 if (policyParams != null) {
43 createParams = mapper.readValue(policyParams,
44 SegmentRouterPolicyRESTParams.class);
45 }
46 } catch (IOException ex) {
47 log.error("Exception occurred parsing inbound JSON", ex);
48 return "fail";
49 }
50
Srikanth Vavilapalli9cde3812014-10-21 21:49:10 -070051 log.debug("createPolicy of type {} with params id {} src_ip {} dst_ip {}"
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070052 + "proto {} src_port {} dst_port {} priority {} tunnel_id {}",
Srikanth Vavilapalli9cde3812014-10-21 21:49:10 -070053 createParams.getPolicy_type(),
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070054 createParams.getPolicy_id(), createParams.getSrc_ip(),
55 createParams.getDst_ip(), createParams.getProto_type(),
56 createParams.getSrc_tp_port(), createParams.getDst_tp_port(),
57 createParams.getPriority(), createParams.getTunnel_id());
58
Srikanth Vavilapalli204d2982014-10-23 14:58:49 -070059 IPv4Net src_ip = (createParams.getSrc_ip() != null) ?
60 new IPv4Net(createParams.getSrc_ip()) : null;
61 IPv4Net dst_ip = (createParams.getDst_ip() != null) ?
62 new IPv4Net(createParams.getDst_ip()) : null;
Srikanth Vavilapallib5637082014-10-24 13:01:58 -070063 Byte protoType = (createParams.getProto_type() != null) ?
64 getProtoTypeByte(createParams.getProto_type()) : null;
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070065 boolean result = segmentRoutingService.createPolicy(
Sangho Shin7c1182c2014-10-21 15:08:13 -070066 createParams.getPolicy_id(), null, null, null,
Srikanth Vavilapallib5637082014-10-24 13:01:58 -070067 src_ip, dst_ip, protoType,
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070068 createParams.getSrc_tp_port(),
69 createParams.getDst_tp_port(),
70 createParams.getPriority(),
71 createParams.getTunnel_id());
72 return (result == true) ? "success" : "fail";
73 }
74
75 private Byte getProtoTypeByte(String protoType) {
Sangho Shin7c1182c2014-10-21 15:08:13 -070076 Byte protoTypeByte = null;
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070077 switch (protoType) {
78 case "tcp":
79 protoTypeByte = IPv4.PROTOCOL_TCP;
80 break;
81 case "udp":
82 protoTypeByte = IPv4.PROTOCOL_UDP;
83 break;
84 }
85 return protoTypeByte;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -070086 }
87
88 @Delete("json")
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070089 public String deletePolicy(String policyParams) {
90 ISegmentRoutingService segmentRoutingService =
91 (ISegmentRoutingService) getContext().getAttributes().
92 get(ISegmentRoutingService.class.getCanonicalName());
93 ObjectMapper mapper = new ObjectMapper();
94 SegmentRouterPolicyRESTParams createParams = null;
95 try {
96 if (policyParams != null) {
97 createParams = mapper.readValue(policyParams,
98 SegmentRouterPolicyRESTParams.class);
99 }
100 } catch (IOException ex) {
101 log.error("Exception occurred parsing inbound JSON", ex);
102 return "fail";
103 }
104
105 log.debug("deletePolicy with Id {}", createParams.getPolicy_id());
106 boolean result = segmentRoutingService.removePolicy(
107 createParams.getPolicy_id());
108 return (result == true) ? "deleted" : "fail";
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700109 }
110
111 @Get("json")
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700112 public Object getPolicy() {
113 ISegmentRoutingService segmentRoutingService =
114 (ISegmentRoutingService) getContext().getAttributes().
115 get(ISegmentRoutingService.class.getCanonicalName());
116 List<SegmentRouterPolicyInfo> policyList = new ArrayList<SegmentRouterPolicyInfo>();
Sangho Shinf65b4da2014-10-28 16:58:13 -0700117 Collection<SegmentRoutingPolicy> policies = segmentRoutingService.getPoclicyTable();
118 Iterator<SegmentRoutingPolicy> piI = policies.iterator();
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700119 while(piI.hasNext()){
Sangho Shinf65b4da2014-10-28 16:58:13 -0700120 SegmentRoutingPolicy policy = piI.next();
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700121 String policyId = policy.getPolicyId();
Sangho Shinf65b4da2014-10-28 16:58:13 -0700122 String tunnelId = null;
123 if (policy.getType() == PolicyType.TUNNEL_FLOW) {
124 tunnelId = ((SegmentRoutingPolicyTunnel)policy).getTunnelId();
125 }
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700126 int priority = policy.getPriority();
Fahad Naeem Khan1e712ad2014-10-27 16:48:07 -0700127 String policyType = policy.getType().name();
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700128 PacketMatch flowEntries = policy.getMatch();
Fahad Naeem Khan6beb40c2014-10-27 12:01:40 -0700129 SegmentRouterPolicyInfo pInfo = new SegmentRouterPolicyInfo(policyId, policyType, tunnelId,
130 priority, flowEntries);
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700131 policyList.add(pInfo);
132 }
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700133 log.debug("getPolicy with params");
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700134 return policyList;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700135 }
136}