blob: 91f3d29dc302b81638e2af1715558f0348a22f0e [file] [log] [blame]
Sangho Shin177e64f2014-10-28 17:49:23 -07001package net.onrc.onos.apps.segmentrouting;
2
3import java.io.IOException;
4import java.util.ArrayList;
5import java.util.List;
6
7import net.floodlightcontroller.core.IOF13Switch;
8import net.onrc.onos.apps.segmentrouting.SegmentRoutingTunnel.TunnelRouteInfo;
9import net.onrc.onos.core.matchaction.MatchAction;
10import net.onrc.onos.core.matchaction.MatchActionId;
11import net.onrc.onos.core.matchaction.MatchActionOperationEntry;
12import net.onrc.onos.core.matchaction.MatchActionOperations.Operator;
13import net.onrc.onos.core.matchaction.action.Action;
14import net.onrc.onos.core.matchaction.action.DecNwTtlAction;
15import net.onrc.onos.core.matchaction.action.GroupAction;
16import net.onrc.onos.core.matchaction.match.PacketMatch;
17import net.onrc.onos.core.util.SwitchPort;
18
19import org.slf4j.Logger;
20import org.slf4j.LoggerFactory;
21
22import com.esotericsoftware.minlog.Log;
23
24public class SegmentRoutingPolicyTunnel extends SegmentRoutingPolicy {
25
26 private static final Logger log = LoggerFactory
27 .getLogger(SegmentRoutingPolicyTunnel.class);
28
29 private String tunnelId;
30
31 public SegmentRoutingPolicyTunnel(SegmentRoutingManager srm, String pid,
32 PolicyType type, PacketMatch match, int priority, String tid) {
33 super(srm, pid, type, match, priority);
34 this.tunnelId = tid;
35 // TODO Auto-generated constructor stub
36 }
37
38 @Override
39 public boolean createPolicy() {
40
41 SegmentRoutingTunnel tunnelInfo = srManager.getTunnelInfo(tunnelId);
42
43 List<TunnelRouteInfo> routes = tunnelInfo.getRoutes();
44
45 for (TunnelRouteInfo route : routes) {
46 List<Action> actions = new ArrayList<>();
47
48 // Check PHP was done by stitching
49 // If no MPLS label is added, then NW TTL needs to be decremented
50 if (route.getRoute().isEmpty()) {
51 DecNwTtlAction decNwTtlAction = new DecNwTtlAction(1);
52 actions.add(decNwTtlAction);
53 }
54
55 GroupAction groupAction = new GroupAction();
56 groupAction.setGroupId(route.getGroupId());
57 actions.add(groupAction);
58
59 MatchAction matchAction = new MatchAction(new MatchActionId(
60 srManager.getNextMatchActionID()),
61 new SwitchPort((long) 0, (short) 0), match, priority,
62 actions);
63 MatchActionOperationEntry maEntry =
64 new MatchActionOperationEntry(Operator.ADD, matchAction);
65
66 IOF13Switch sw13 = srManager.getIOF13Switch(route.getSrcSwDpid());
67
68 if (sw13 != null) {
69 srManager.printMatchActionOperationEntry(sw13, maEntry);
70 try {
71 sw13.pushFlow(maEntry);
72 } catch (IOException e) {
73 e.printStackTrace();
74 return false;
75 }
76 }
77 else {
78 Log.warn("Cannot find the target switch {}", route.getSrcSwDpid());
79 return false;
80 }
81 }
82
83 return true;
84 }
85
86 @Override
87 public boolean removePolicy() {
88
89 List<Action> actions = new ArrayList<>();
90 int gropuId = 0; // dummy group ID
91 GroupAction groupAction = new GroupAction();
92 groupAction.setGroupId(gropuId);
93 actions.add(groupAction);
94
95 MatchAction matchAction = new MatchAction(new MatchActionId(
96 srManager.getNextMatchActionID()),
97 new SwitchPort((long) 0, (short) 0), match, priority,
98 actions);
99 MatchActionOperationEntry maEntry =
100 new MatchActionOperationEntry(Operator.REMOVE, matchAction);
101
102 SegmentRoutingTunnel tunnel = srManager.getTunnelInfo(tunnelId);
103 if (tunnel == null) {
104 log.warn("Cannot find the tunnel {} for the policy {}", tunnelId,
105 policyId);
106 return false;
107 }
108 List<TunnelRouteInfo> routes = tunnel.getRoutes();
109
110 for (TunnelRouteInfo route : routes) {
111 IOF13Switch sw13 = srManager.getIOF13Switch(route.getSrcSwDpid());
112 if (sw13 == null) {
113 log.warn("Cannt find the switch {}", route.getSrcSwDpid());
114 return false;
115 }
116 else {
117 srManager.printMatchActionOperationEntry(sw13, maEntry);
118 try {
119 sw13.pushFlow(maEntry);
120 } catch (IOException e) {
121 e.printStackTrace();
122 log.debug("policy remove failed due to pushFlow() exception");
123 return false;
124 }
125 }
126 }
127
128 return true;
129 }
130
131
132 public String getTunnelId(){
133 return this.tunnelId;
134 }
135
136}