blob: 5ea9b13daf2e57610702b6312c5017f711683eeb [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;
Sangho Shin177e64f2014-10-28 17:49:23 -070035 }
36
37 @Override
38 public boolean createPolicy() {
39
40 SegmentRoutingTunnel tunnelInfo = srManager.getTunnelInfo(tunnelId);
41
42 List<TunnelRouteInfo> routes = tunnelInfo.getRoutes();
43
44 for (TunnelRouteInfo route : routes) {
45 List<Action> actions = new ArrayList<>();
46
47 // Check PHP was done by stitching
48 // If no MPLS label is added, then NW TTL needs to be decremented
49 if (route.getRoute().isEmpty()) {
50 DecNwTtlAction decNwTtlAction = new DecNwTtlAction(1);
51 actions.add(decNwTtlAction);
52 }
53
54 GroupAction groupAction = new GroupAction();
55 groupAction.setGroupId(route.getGroupId());
56 actions.add(groupAction);
57
58 MatchAction matchAction = new MatchAction(new MatchActionId(
59 srManager.getNextMatchActionID()),
60 new SwitchPort((long) 0, (short) 0), match, priority,
61 actions);
62 MatchActionOperationEntry maEntry =
63 new MatchActionOperationEntry(Operator.ADD, matchAction);
64
65 IOF13Switch sw13 = srManager.getIOF13Switch(route.getSrcSwDpid());
66
67 if (sw13 != null) {
68 srManager.printMatchActionOperationEntry(sw13, maEntry);
69 try {
70 sw13.pushFlow(maEntry);
71 } catch (IOException e) {
72 e.printStackTrace();
73 return false;
74 }
75 }
76 else {
77 Log.warn("Cannot find the target switch {}", route.getSrcSwDpid());
78 return false;
79 }
80 }
81
82 return true;
83 }
84
85 @Override
86 public boolean removePolicy() {
87
88 List<Action> actions = new ArrayList<>();
89 int gropuId = 0; // dummy group ID
90 GroupAction groupAction = new GroupAction();
91 groupAction.setGroupId(gropuId);
92 actions.add(groupAction);
93
94 MatchAction matchAction = new MatchAction(new MatchActionId(
95 srManager.getNextMatchActionID()),
96 new SwitchPort((long) 0, (short) 0), match, priority,
97 actions);
98 MatchActionOperationEntry maEntry =
99 new MatchActionOperationEntry(Operator.REMOVE, matchAction);
100
101 SegmentRoutingTunnel tunnel = srManager.getTunnelInfo(tunnelId);
102 if (tunnel == null) {
103 log.warn("Cannot find the tunnel {} for the policy {}", tunnelId,
104 policyId);
105 return false;
106 }
107 List<TunnelRouteInfo> routes = tunnel.getRoutes();
108
109 for (TunnelRouteInfo route : routes) {
110 IOF13Switch sw13 = srManager.getIOF13Switch(route.getSrcSwDpid());
111 if (sw13 == null) {
112 log.warn("Cannt find the switch {}", route.getSrcSwDpid());
113 return false;
114 }
115 else {
116 srManager.printMatchActionOperationEntry(sw13, maEntry);
117 try {
118 sw13.pushFlow(maEntry);
119 } catch (IOException e) {
120 e.printStackTrace();
121 log.debug("policy remove failed due to pushFlow() exception");
122 return false;
123 }
124 }
125 }
126
127 return true;
128 }
129
Sangho Shinbe3c3612014-10-30 14:20:36 -0700130 /**
131 * Get the Tunnel ID
132 * @return tunnel ID
133 */
Sangho Shin177e64f2014-10-28 17:49:23 -0700134 public String getTunnelId(){
135 return this.tunnelId;
136 }
137
138}