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