blob: 0931dd3607bb0c569d385a00b1ea1060a8668966 [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
48 log.debug("createPolicy with params id {} src_ip {} dst_ip {}"
49 + "proto {} src_port {} dst_port {} priority {} tunnel_id {}",
50 createParams.getPolicy_id(), createParams.getSrc_ip(),
51 createParams.getDst_ip(), createParams.getProto_type(),
52 createParams.getSrc_tp_port(), createParams.getDst_tp_port(),
53 createParams.getPriority(), createParams.getTunnel_id());
54
55 boolean result = segmentRoutingService.createPolicy(
Sangho Shin7c1182c2014-10-21 15:08:13 -070056 createParams.getPolicy_id(), null, null, null,
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070057 new IPv4Net(createParams.getSrc_ip()),
58 new IPv4Net(createParams.getDst_ip()),
59 getProtoTypeByte(createParams.getProto_type()),
60 createParams.getSrc_tp_port(),
61 createParams.getDst_tp_port(),
62 createParams.getPriority(),
63 createParams.getTunnel_id());
64 return (result == true) ? "success" : "fail";
65 }
66
67 private Byte getProtoTypeByte(String protoType) {
Sangho Shin7c1182c2014-10-21 15:08:13 -070068 Byte protoTypeByte = null;
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070069 switch (protoType) {
70 case "tcp":
71 protoTypeByte = IPv4.PROTOCOL_TCP;
72 break;
73 case "udp":
74 protoTypeByte = IPv4.PROTOCOL_UDP;
75 break;
76 }
77 return protoTypeByte;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -070078 }
79
80 @Delete("json")
Srikanth Vavilapallie7dabdd2014-10-20 21:54:39 -070081 public String deletePolicy(String policyParams) {
82 ISegmentRoutingService segmentRoutingService =
83 (ISegmentRoutingService) getContext().getAttributes().
84 get(ISegmentRoutingService.class.getCanonicalName());
85 ObjectMapper mapper = new ObjectMapper();
86 SegmentRouterPolicyRESTParams createParams = null;
87 try {
88 if (policyParams != null) {
89 createParams = mapper.readValue(policyParams,
90 SegmentRouterPolicyRESTParams.class);
91 }
92 } catch (IOException ex) {
93 log.error("Exception occurred parsing inbound JSON", ex);
94 return "fail";
95 }
96
97 log.debug("deletePolicy with Id {}", createParams.getPolicy_id());
98 boolean result = segmentRoutingService.removePolicy(
99 createParams.getPolicy_id());
100 return (result == true) ? "deleted" : "fail";
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700101 }
102
103 @Get("json")
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700104 public Object getPolicy() {
105 ISegmentRoutingService segmentRoutingService =
106 (ISegmentRoutingService) getContext().getAttributes().
107 get(ISegmentRoutingService.class.getCanonicalName());
108 List<SegmentRouterPolicyInfo> policyList = new ArrayList<SegmentRouterPolicyInfo>();
109 Collection<PolicyInfo> policies = segmentRoutingService.getPoclicyTable();
110 Iterator<PolicyInfo> piI = policies.iterator();
111 while(piI.hasNext()){
112 PolicyInfo policy = piI.next();
113 String policyId = policy.getPolicyId();
114 int priority = policy.getPriority();
115 int policyType = policy.getType();
116 PacketMatch flowEntries = policy.getMatch();
117 SegmentRouterPolicyInfo pInfo = new SegmentRouterPolicyInfo(policyId, policyType, priority, flowEntries);
118 policyList.add(pInfo);
119 }
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700120 log.debug("getPolicy with params");
Fahad Naeem Khan788895c2014-10-21 19:00:24 -0700121 return policyList;
Srikanth Vavilapallib1f168d2014-10-19 21:31:52 -0700122 }
123}